Exemplo n.º 1
0
        private Statement expressionStatement()
        {
            Expr expr = expression();

            if (expr is Expr.AssignExpr || expr is Expr.Call || expr is Expr.Set || expr is Expr.postfix || expr is Expr.prefix || expr is Expr.Variable || expr is Expr.Literal || expr is Expr.Super || expr is Expr.This) //Take out Literal
            {
                consume(TokenType.SEMICOLON, "Expect ';' after expression.");
                return(new Statement.Expression(expr));
            }
            if ((expr is Expr.TernaryExpr && ((Expr.TernaryExpr)expr).isNullAccessor)) //Optional semicolon, but doesn't print after evaluation.
            {
                match(TokenType.SEMICOLON);
                return(new Statement.Expression(expr));
            }
            if (!inBlock)
            {
                match(TokenType.SEMICOLON);
                return(new Statement.Print(expr));
            }
            throw error(previous(), "Cannot use REPL inside a body." + expr.GetType());
        }
Exemplo n.º 2
0
        private Expr assignment()
        {
            Expr expr = ternaryExpression();

            if (match(TokenType.EQUALS))
            {
                Token equals = previous();
                Expr  value  = assignment();

                if (expr is Expr.Variable)
                {
                    Token name = ((Expr.Variable)expr).name;
                    return(new Expr.AssignExpr(name, value));
                }
                else if (expr is Expr.Get)
                {
                    Expr.Get get = (Expr.Get)expr;
                    return(new Expr.Set(get._object, get.name, value, get.isCascading));
                }
                throw error(equals, "Invalid assignment target: " + expr.GetType());
            }
            return(expr);
        }