Ejemplo n.º 1
0
        public Comparison Parse(ILexer lexer, IExpressionParser expressionParser, IWasmicSyntaxTreeExpression lhs)
        {
            ComparisonOperator op;

            switch (lexer.Next.TokenType)
            {
            case TokenType.EqualComparer: op = ComparisonOperator.Equals; break;

            case TokenType.GrThanComparer: op = ComparisonOperator.GreaterThan; break;

            case TokenType.GrThanOrEqComparer: op = ComparisonOperator.GreaterThanOrEqual; break;

            case TokenType.LsThanComparer: op = ComparisonOperator.LessThan; break;

            case TokenType.LsThanOrEqComparer: op = ComparisonOperator.LessThanOrEqual; break;

            default:
                throw new WasmicCompilerException(
                          $"expected comparison operator, found {lexer.Next.TokenType}"
                          );
            }
            lexer.Advance(); // eat comparison
            var rhs = expressionParser.GetExpression();

            return(new Comparison(lhs, rhs, op));
        }
Ejemplo n.º 2
0
        public BinopExpresison Parse(ILexer lexer, IExpressionParser expressionParser, IWasmicSyntaxTreeExpression lhs)
        {
            Operation operation;

            switch (lexer.Next.TokenType)
            {
            case TokenType.Plus:
                operation = Operation.Add;
                break;

            case TokenType.Minus:
                operation = Operation.Subtract;
                break;

            case TokenType.Star:
                operation = Operation.Multiply;
                break;

            case TokenType.Slash:
                operation = Operation.Divide;
                break;

            default:
                throw new WasmicCompilerException($"{lexer.Next.TokenType} is not binop");
            }
            lexer.Advance(); // eat operand
            var rhs = expressionParser.GetExpression();

            return(new BinopExpresison(lhs, rhs, operation));
        }
Ejemplo n.º 3
0
        public FunctionCall Parse(
            ILexer lexer,
            IModuleFunctionMap functionMap,
            IExpressionParser expressionParser,
            string name)
        {
            if (functionMap.Contains(name) == false)
            {
                throw new WasmicCompilerException($"function {name} is not declared");
            }

            var callFunc = functionMap.Get(name);

            var parameters = new List <IWasmicSyntaxTreeExpression>();

            lexer.AssertNext(TokenType.L_Paren);
            lexer.Advance();
            if (lexer.Next.TokenType != TokenType.R_Paren)
            {
                do
                {
                    var parameterLoad = expressionParser.GetExpression();
                    parameters.Add(parameterLoad);
                } while(lexer.Next.TokenType == TokenType.Comma);
            }
            lexer.AssertNext(TokenType.R_Paren);
            lexer.Advance();

            var expectedParameters = callFunc.Parameters;

            if (expectedParameters.Count != parameters.Count)
            {
                throw new WasmicCompilerException($"unmatched argument count");
            }
            for (int i = 0; i < expectedParameters.Count; i++)
            {
                if (expectedParameters[i].Type != parameters[i].Type)
                {
                    throw new WasmicCompilerException($"expected argument to be {expectedParameters[i].Type} but found {parameters[i].Type}");
                }
            }

            return(new FunctionCall(name, callFunc.ReturnType?.Type, parameters));
        }