Goose  Artifact [a56180b746]

Artifact a56180b746f1125a55382fac13e5de69bfcebd3ae6bc89960363c6530494c15a:

  • File bs/builtins/statements/using.cpp — part of check-in [2efa23555d] at 2019-08-11 01:26:17 on branch trunk —
    • ir: created a new type for LocationId which is handled in a specific way so that two LocationIds are always considered equal by pattern matching. This prevent the values' locationIds stored in ir expressions from fucking up everything.
    • propagate value locations in a few places: in the parser, when resolving invocations and when doing eager evaluation. There are probably a lot of other places still missing.
    • converted all the builtin statements to use the DiagnosticsManager.
    (user: achavasse size: 5823)

#include "builtins/builtins.h"
#include "parse/parse.h"
#include "precedence.h"

using namespace empathy;
using namespace empathy::ir;
using namespace empathy::parse;

namespace empathy::builtins
{
    void SetupUsingStmt( Env& e )
    {
        auto handleUsing = []( Parser& p, uint32_t locationId, uint32_t prec )
        {
            auto& dm = DiagnosticsManager::GetInstance();

            auto nameTerm = p.resolver()->consume();
            if( !nameTerm )
            {
                dm.emitErrorMessage( p.resolver()->getCurrentLocation(), "expected an identifier.", 0 );
                return false;
            }

            const auto* name = get_if< StringId >( &nameTerm->first );
            if( !name )
            {
                dm.emitErrorMessage( nameTerm->second, "expected an identifier.", 0 );
                return false;
            }

            auto eqTerm = p.resolver()->consumeUnresolved();
            if( !eqTerm )
            {
                dm.emitErrorMessage( p.resolver()->getCurrentLocation(), "expected '='.", 0 );
                return false;
            }

            const auto* eq = get_if< StringId >( &eqTerm->first );
            if( !eq || *eq != "="_sid )
            {
                dm.emitErrorMessage( eqTerm->second, "expected '='.", 0 );
                return false;
            }

            p.resolver()->consumeNewLines();

            // Store all the units following the equal sign until the next semicolon or newline.
            vector< TermLoc > toks;
            while( !p.resolver()->eos() )
            {
                const auto& tok = p.resolver()->lookAheadRaw();
                if( !tok )
                    break;

                const auto* delim = get_if< Delimiter >( &tok->first );
                if( delim && *delim == Delimiter::Newline )
                {
                    p.resolver()->consumeRaw();
                    break;
                }

                const auto* id = get_if< StringId >( &tok->first );
                if( id && *id == ";"_sid )
                    break;

                auto g = p.resolver()->consumeUnit();
                move( g.begin(), g.end(), back_inserter( toks ) );
            }

            if( toks.empty() )
            {
                // TODO maybe use the current location after consuming the equal sign as the location here
                dm.emitErrorMessage( eqTerm->second, "expected an expression.", 0 );
                return false;
            }

            const auto& context = p.resolver()->context();

            // Create a local identity for the constant, from which the current identity will be visible.
            // This is to avoid anything declared from within the using expression to leak outside,
            // and also to make sure that whenever it will be parsed, it will have access to the current
            // context in which it has been declared.
            auto localIdentity = AppendToVectorTerm( context.identity(), nameTerm->first );
            context.env()->addVisibilityRule( context.identity(), localIdentity );

            variant< vector< TermLoc >, Term > content = move( toks );
            auto nameSid = *name;
            auto nameLoc = nameTerm->second;

            bool bInUse = false;

            auto UsingValProvider = [content, localIdentity, bInUse, nameSid, nameLoc]( Env& e, const Term& identity, const Term& contextId, Term& result ) mutable
            {
                auto& dm = DiagnosticsManager::GetInstance();

                if( holds_alternative< vector< TermLoc > >( content ) )
                {
                    if( bInUse )
                    {
                        dm.emitErrorMessage( nameLoc, "recursive using expression." );
                        return Env::Status::NoMatch;
                    }

                    bInUse = true;

                    Context localContext( e.shared_from_this(), localIdentity );
                    auto tokProvider = lex::MakeVectorAdapter( get< vector< TermLoc > >( content ) );
                    auto r = make_shared< parse::Resolver >( tokProvider, localContext );
                    Parser p( r );

                    if( !p.parseExpression() )
                    {
                        // TODO: Perhaps return a poison value instead of nothing?
                        dm.emitErrorMessage( nameLoc, "invalid expression." );
                        return Env::Status::NoMatch;
                    }

                    auto result = p.peekLastValue();
                    if( !result )
                    {
                        // TODO: Perhaps return a poison value instead of nothing?
                        dm.emitErrorMessage( nameLoc, "invalid expression." );
                        return Env::Status::NoMatch;
                    }

                    if( !result->isConstant() )
                    {
                        // TODO: Perhaps return a poison value instead of nothing?
                        dm.emitErrorMessage( nameLoc, "using expression doesn't evaluate to a constant." );
                        return Env::Status::NoMatch;
                    }

                    content = ValueToIRExpr( *result );
                    bInUse = false;
                }

                result = get< Term >( content );
                return Env::Status::Success;
            };

            context.env()->storeValue( localIdentity, ANYTERM( _ ),
                make_shared< Env::ValueProvider >( move( UsingValProvider ) ) );

            return true;
        };

        Rule r( handleUsing );
        auto ruleVal = ToValue( move( r ) );
        auto ruleTerm = ValueToIRExpr( ruleVal );
        e.storeValue( AppendToVectorTerm( RootIdentity(), TSID( using ) ), ANYTERM( _ ), ruleTerm );
    }
}