Пример #1
0
        protected StatementNode GenerateEntryPoint(CompilerNode owner, IEnumerable <MethodNode> methods)
        {
            var result = new BlockNode(owner);

            var opDecl = new DeclarationNode(result);

            opDecl.identifier = "operation";
            opDecl.type       = new TypeNode(opDecl, TypeKind.String);
            result.declarations.Add(opDecl);

            var assSt = new StackAssignmentNode(result);

            assSt.declaration = opDecl;
            result.statements.Add(assSt);

            foreach (var method in methods)
            {
                if (method.visibility != Visibility.Public)
                {
                    continue;
                }

                var ifExpr   = new IfNode(result);
                var cmpExpr  = new BinaryExpressionNode(ifExpr);
                var leftExpr = new VariableExpressionNode(cmpExpr);
                leftExpr.declaration = opDecl;

                var rightExpr = new LiteralExpressionNode(cmpExpr);
                rightExpr.kind  = LiteralKind.String;
                rightExpr.value = method.name;

                cmpExpr.left      = leftExpr;
                cmpExpr.right     = rightExpr;
                cmpExpr.@operator = OperatorKind.Equals;

                var callSt = new CallNode(ifExpr);
                callSt.method = method;

                ifExpr.expr       = cmpExpr;
                ifExpr.trueBranch = callSt;

                result.statements.Add(ifExpr);
            }

            result.statements.Add(new ExitNode(result));
            return(result);
        }
Пример #2
0
 private void WriteLiteralExpression(LiteralExpressionNode literalExpression)
 {
     _builder.Append(literalExpression.Token);
 }
 protected override AssignmentState handleLiteral(LiteralExpressionNode literal, HashSet <StatementNode> visited, bool nested)
 {
     return(AssignmentState.NotAssigned);
 }
Пример #4
0
 public override PapyrusType VisitLiteralExpression(LiteralExpressionNode node)
 {
     // TODO: intrinsics
     return(base.VisitLiteralExpression(node));
 }
Пример #5
0
        protected StatementNode ParseStatement(List <Token> tokens, ref int index, CompilerNode owner)
        {
            BlockNode block = null;

            do
            {
                if (index >= tokens.Count)
                {
                    throw new ParserException(tokens.Last(), ParserException.Kind.EndOfStream);
                }

                var token = tokens[index];

                StatementNode statement = null;

                var type = GetTypeFromToken(owner, token.text);
                if (type != null)
                {
                    var decl = new DeclarationNode(block);
                    decl.type = type;
                    index++;
                    decl.identifier = ExpectIdentifier(tokens, ref index, false);

                    if (ExpectOptional(tokens, ref index, "="))
                    {
                        var node = new AssignmentNode(owner);
                        node.identifier = decl.identifier;
                        node.expr       = ParseExpression(tokens, ref index, block);
                        statement       = node;
                    }

                    ExpectDelimiter(tokens, ref index, ";");
                }
                else
                {
                    switch (token.text)
                    {
                    case "{":
                    {
                        index++;
                        block = new BlockNode(owner);
                        owner = block;
                        break;
                    }

                    case "return":
                    {
                        index++;

                        var node = new ReturnNode(owner);
                        node.expr = ParseExpression(tokens, ref index, node);

                        ExpectDelimiter(tokens, ref index, ";");

                        statement = node;
                        break;
                    }

                    case "if":
                    {
                        index++;

                        ExpectDelimiter(tokens, ref index, "(");

                        BlockNode parent = (block != null) ? block : owner as BlockNode;
                        if (parent == null)
                        {
                            throw new ParserException(token, ParserException.Kind.UnexpectedToken);
                        }

                        var node = new IfNode(parent);
                        node.expr = ParseExpression(tokens, ref index, parent);

                        ExpectDelimiter(tokens, ref index, ")");

                        node.trueBranch = ParseStatement(tokens, ref index, parent);

                        if (ExpectOptional(tokens, ref index, "else"))
                        {
                            node.falseBranch = ParseStatement(tokens, ref index, parent);
                        }

                        statement = node;
                        break;
                    }

                    case "switch":
                    {
                        index++;

                        var node = new SwitchNode(block);
                        statement = node;

                        ExpectDelimiter(tokens, ref index, "(");
                        node.expr = ParseExpression(tokens, ref index, node);
                        ExpectDelimiter(tokens, ref index, ")");

                        ExpectDelimiter(tokens, ref index, "{");

                        var keys = new HashSet <string>();
                        do
                        {
                            if (tokens[index].text == "}")
                            {
                                break;
                            }


                            if (ExpectOptional(tokens, ref index, "default"))
                            {
                                ExpectDelimiter(tokens, ref index, ":");
                                var st = ParseStatement(tokens, ref index, node);
                                node.defaultBranch = st;
                            }
                            else
                            {
                                ExpectKeyword(tokens, ref index, "case");

                                LiteralKind litKind;
                                var         val = ExpectLiteral(tokens, ref index, out litKind);

                                var key = val.ToString();
                                if (keys.Contains(key))
                                {
                                    throw new ParserException(tokens[index], ParserException.Kind.DuplicatedLabel);
                                }

                                var lit = new LiteralExpressionNode(node);
                                lit.kind  = litKind;
                                lit.value = val;

                                ExpectDelimiter(tokens, ref index, ":");
                                var st = ParseStatement(tokens, ref index, node);
                                node.cases[lit] = st;
                                keys.Add(key);
                            }
                        }while (true);

                        ExpectDelimiter(tokens, ref index, "}");
                        break;
                    }

                    default:
                    {
                        throw new ParserException(tokens[index], ParserException.Kind.UnexpectedToken);
                    }
                    }
                }

                if (block == null)
                {
                    return(statement);
                }
                else
                if (statement != null)
                {
                    block.statements.Add(statement);
                }
            } while (tokens[index].text != "}");

            index++;

            return(block);
        }
Пример #6
0
        protected ExpressionNode ParseExpression(List <Token> tokens, ref int index, CompilerNode owner, int precedence = -1)
        {
            if (index >= tokens.Count)
            {
                throw new ParserException(tokens.Last(), ParserException.Kind.EndOfStream);
            }

            ExpressionNode term;

            if (tokens[index].text == "(")
            {
                index++;
                term = ParseExpression(tokens, ref index, owner);
                ExpectDelimiter(tokens, ref index, ")");
            }
            else
            if (Lexer.IsLiteral(tokens[index].kind))
            {
                LiteralKind litKind;

                var node = new LiteralExpressionNode(owner);
                node.value = ExpectLiteral(tokens, ref index, out litKind);
                node.kind  = litKind;
                term       = node;
            }
            else
            if (tokens[index].kind == Token.Kind.Operator)
            {
                var node = new UnaryExpressionNode(owner);
                node.op = tokens[index].text;
                index++;

                node.term = ParseExpression(tokens, ref index, node);
                term      = node;
            }
            else
            {
                var node = new VariableExpressionNode(owner);
                node.identifier = ExpectIdentifier(tokens, ref index, false);
                term            = node;
            }

            while (tokens[index].kind == Token.Kind.Operator)
            {
                var p = GetOperatorPrecedence(tokens[index].text);

                if (precedence < 0 || p > precedence)
                {
                    var expr = new BinaryExpressionNode(owner);

                    expr.left = term;

                    expr.@operator = ParseOperator(ExpectOperator(tokens, ref index));

                    expr.right = ParseExpression(tokens, ref index, expr, p);

                    term = expr;
                }
                else
                {
                    break;
                }
            }

            return(term);
        }
Пример #7
0
 private Expression BindLiteralExpression(LiteralExpressionNode node) => new LiteralExpression(node.Type, node.Value);
Пример #8
0
        private List <ExpressionNode> PrimaryExpression()
        {
            printDebug("Primary Expression");
            if (Match(TokenType.NEW_KEYWORD))
            {
                ConsumeToken();
                var left  = InstanceExpression();
                var right = PrimaryExpressionPrime();

                var ret = new List <ExpressionNode>();

                ret.Add(left);
                ret.AddRange(right);

                return(ret);
            }
            else if (MatchAny(this.literals))
            {
                var token = ConsumeToken();
                var left  = new LiteralExpressionNode(token) as ExpressionNode;
                var right = PrimaryExpressionPrime();

                var ret = new List <ExpressionNode>();

                ret.Add(left);
                ret.AddRange(right);
                return(ret);
            }
            else if (Match(TokenType.ID))
            {
                var token = ConsumeToken();
                var left  = new IdentifierExpressionNode(token) as ExpressionNode;
                var right = PrimaryExpressionPrime();

                var ret = new List <ExpressionNode>();
                if (right.Count > 0 &&
                    (
                        (right[0] is FunctionCallExpressionNode && ((FunctionCallExpressionNode)right[0]).identifier == null) ||
                        (right[0] is ArrayAccessExpressionNode && ((ArrayAccessExpressionNode)right[0]).identifier == null) ||
                        (right[0] is PostAdditiveExpressionNode && ((PostAdditiveExpressionNode)right[0]).indentifier == null)
                    )
                    )
                {
                    if (right[0] is FunctionCallExpressionNode)
                    {
                        ((FunctionCallExpressionNode)right[0]).identifier = left;
                    }
                    else if (right[0] is ArrayAccessExpressionNode)
                    {
                        ((ArrayAccessExpressionNode)right[0]).identifier = left;
                    }
                    else
                    {
                        ((PostAdditiveExpressionNode)right[0]).indentifier = left;
                    }
                }
                else
                {
                    ret.Add(left);
                }
                ret.AddRange(right);

                return(ret);
            }
            else if (Match(TokenType.PAREN_OPEN))
            {
                ConsumeToken();
                var expr = Expression();
                MatchExactly(TokenType.PAREN_CLOSE);

                var right = PrimaryExpressionPrime();
                //REVISAR EL PRIMARY EXPRESSION PRIME
                if (right.Count > 0 && (right[0] is FunctionCallExpressionNode || right[0] is ArrayAccessExpressionNode || (right[0] is PostAdditiveExpressionNode)))
                {
                    if (right[0] is FunctionCallExpressionNode)
                    {
                        ((FunctionCallExpressionNode)right[0]).identifier = expr;
                    }
                    else if (right[0] is ArrayAccessExpressionNode)
                    {
                        ((ArrayAccessExpressionNode)right[0]).identifier = expr;
                    }
                    else
                    {
                        ((PostAdditiveExpressionNode)right[0]).indentifier = expr;
                    }
                    return(right);
                }
                else
                {
                    var internal_expression = new ParenthesizedExpressionNode(expr);

                    var ret = new List <ExpressionNode>();
                    ret.Add(internal_expression);
                    ret.AddRange(right);

                    return(ret);
                }
            }
            else if (Match(TokenType.THIS_KEYWORD))
            {
                var token = MatchExactly(TokenType.THIS_KEYWORD);
                var left  = new ReferenceAccessNode(token) as ExpressionNode;
                var right = PrimaryExpressionPrime();

                var ret = new List <ExpressionNode>();
                ret.Add(left);
                ret.AddRange(right);

                return(ret);
            }
            else if (Match(TokenType.BASE_KEYWORD))
            {
                var token = MatchExactly(TokenType.BASE_KEYWORD);
                var left  = new ReferenceAccessNode(token) as ExpressionNode;
                var right = PrimaryExpressionPrime();

                var ret = new List <ExpressionNode>();
                ret.Add(left);
                ret.AddRange(right);

                return(ret);
            }
            else if (MatchAny(this.buildInTypes))
            {
                var token = ConsumeToken();
                var left  = new BuiltInTypeExpressionNode(token) as ExpressionNode;
                var right = PrimaryExpressionPrime();
                var ret   = new List <ExpressionNode>();
                ret.Add(left);
                ret.AddRange(right);

                return(ret);
            }
            else
            {
                ThrowSyntaxException("new, literal, identifier, '(' or 'this' expected");
                return(null);
            }
        }