Exemplo n.º 1
0
        void ParseStream(TokenStream reader)
        {
            while (true)
            {
                var exp = reader.MoveNextToken();
                switch (exp.Type)
                {
                    case TokenType.EndOfStream:
                    case TokenType.ArgumentSeparator:
                    case TokenType.ArgumentEnd:
                        return;

                    case TokenType.Operator:
                        PushOperator(exp.ToOperator());
                        break;

                    case TokenType.Identifier:
                        //Get variable/unit/constant
                        var id = exp.ToIdentifier();
                        var def = block.GetDefinition(id);
                        if (def == null)
                            throw new SemanticError(id, "Undefined variable: " + id.Name);

                        if (def is FuncDefinition)
                        {
                            FunctionCall func = ParseFunctionCall(reader);
                            PushImplicitMultiplication(func);
                        }
                        else if (def is VarDefinition)
                            PushImplicitMultiplication(id);
                        else if (def is ConstDefinition)
                        {
                            //Preserve position of identifier
                            var c = ((ConstDefinition)def).Value;
                            var val = c.WithRangeOf(id);
                            PushImplicitMultiplication(val);
                        }
                        else if (def is UnitDefinition)
                            PushImplicitMultiplication(((UnitDefinition)def).GetLiteral(exp.CodeRange));
                        else
                            throw new NotImplementedException();
                        break;

                    case TokenType.Int:
                    case TokenType.Double:
                    case TokenType.Boolean:
                    case TokenType.String:
                        PushLiteral(exp.ToLiteral());
                        break;

                    default:
                        throw new NotImplementedException();
                }
            }
        }