コード例 #1
0
    public T2 Assign()
    {
        T2    statement = default;
        Token token     = LookAheadToken;

        MatchThenLookAhead(TokenTag.ID);
        IdExpression idExpression = TopSymbol.Get(token);

        if (idExpression is null)
        {
            Error($"{token} undeclared");
        }
        if (LookAheadToken?.Tag == '=')
        {
            LookAhead();
            statement = (T2)Activator.CreateInstance(typeof(SetStatement), new object[] { idExpression, Boolean(), _emitterNode });
        }
        else
        {
            AccessingOperationExpression accessingOperationExpression = Offset(idExpression);
            MatchThenLookAhead('=');
            statement = (T2)Activator.CreateInstance(typeof(SetElementStatement), new object[] { accessingOperationExpression, Boolean(), _emitterNode });
        }
        MatchThenLookAhead(';');
        return(statement);
    }
コード例 #2
0
 public virtual void Declare()
 {
     while (LookAheadToken?.Tag == TokenTag.BASIC)
     {
         TypeToken typeToken = Type();
         var       token     = LookAheadToken;
         MatchThenLookAhead(TokenTag.ID);
         MatchThenLookAhead(';');
         IdExpression idExpression = new((WordToken)token, typeToken, Used, _emitterNode);
         TopSymbol.Put(token, idExpression);
         Used += typeToken.Width;
     }
 }
コード例 #3
0
    public T3 Factor()
    {
        T3 expression = default;

        switch (LookAheadToken?.Tag)
        {
        case '(':
            LookAhead();
            expression = Boolean();
            MatchThenLookAhead(')');
            return(expression);

        case TokenTag.NUMBER:
            expression = (T3)Activator.CreateInstance(typeof(ConstantExpression), new object[] { LookAheadToken, TypeToken.INT, _emitterNode });
            LookAhead();
            return(expression);

        case TokenTag.REAL:
            expression = (T3)Activator.CreateInstance(typeof(ConstantExpression), new object[] { LookAheadToken, TypeToken.FLOAT, _emitterNode });
            LookAhead();
            return(expression);

        case TokenTag.TRUE:
            expression = (T3)Activator.CreateInstance(typeof(ConstantExpression), new object[] { WordToken.TRUE, TypeToken.BOOL, _emitterNode });
            LookAhead();
            return(expression);

        case TokenTag.FALSE:
            expression = (T3)Activator.CreateInstance(typeof(ConstantExpression), new object[] { WordToken.FALSE, TypeToken.BOOL, _emitterNode });
            LookAhead();
            return(expression);

        case TokenTag.ID:
            var idExpression = TopSymbol.Get(LookAheadToken);
            if (idExpression == null)
            {
                Error($"{LookAheadToken} undeclared");
            }
            LookAhead();
            if (LookAheadToken?.Tag != '[')
            {
                return((T3)Activator.CreateInstance(typeof(IdExpression), new object[] {
                    idExpression.Token,
                    idExpression.TypeToken,
                    idExpression.Offset,
                    idExpression.Node
                }));
            }
            else
            {
                var accessingOperationExpression = Offset(idExpression);
                return((T3)Activator.CreateInstance(typeof(AccessingOperationExpression), new object[] {
                    accessingOperationExpression.ArrayExpression,
                    accessingOperationExpression.IndexExpression,
                    accessingOperationExpression.TypeToken,
                    accessingOperationExpression.Node
                }));
            }

        default:
            Error("Syntax error");
            return(expression);
        }
    }