Пример #1
0
 void mapLocal(string name, Variable v)
 {
     if (oldLocalNamesMap.ContainsKey(name))
         oldLocalNamesMap[name] = v;
     else
         oldLocalNamesMap.Add(name, v);
 }
Пример #2
0
        AnonymousFunctionExpr ParseExprFunctionArgsAndBody(Scope scope)
        {
            AnonymousFunctionExpr func = new AnonymousFunctionExpr();
            func.Scope = new Scope(scope);

            if (reader.ConsumeSymbol('(') == false)
                error("'(' expected");

            // arg list
            List<Variable> arglist = new List<Variable>();
            bool isVarArg = false;
            while (reader.ConsumeSymbol(')') == false)
            {
                if (reader.Is(TokenType.Ident))
                {
                    Variable arg = new Variable();
                    arg.Name = reader.Get().Data;
                    func.Scope.AddLocal(arg);
                    arglist.Add(arg);
                    if (!reader.ConsumeSymbol(','))
                    {
                        if (reader.ConsumeSymbol(')'))
                            break;
                        else
                            error("')' expected");
                        break;
                    }
                }
                else if (reader.ConsumeSymbol("..."))
                {
                    isVarArg = true;
                    if (!reader.ConsumeSymbol(')'))
                        error("'...' must be the last argument of a function");
                    break;
                }
                else
                {
                    error("Argument name or '...' expected");
                    break;
                }
            }

            // body
            List<Statement> body = ParseStatementList(func.Scope);
            // end
            if (!reader.ConsumeKeyword("end"))
                error("'end' expected after function body");

            //nodeFunc.AstType = AstType.Function;
            func.Arguments = arglist;
            func.Body = body;
            func.IsVararg = isVarArg;

            return func;
        }
Пример #3
0
 public static List<Location> FindReferences(Chunk c, Variable v)
 {
     throw new NotImplementedException();
 }
Пример #4
0
        // TODO: Check FunctionStatement Name's also

        public static Location FindImplementation(Chunk c, Variable v)
        {
            Location ret = null;
            foreach (Statement s in c.Body)
            {
                if (s is AssignmentStatement && !(s is AugmentedAssignmentStatement)) // couldn't be defined in AugmentedAssignment
                {
                    AssignmentStatement a = s as AssignmentStatement;
                    int tokPos = 0;
                    if (a.IsLocal)
                        tokPos++;
                    foreach (Expression e in a.Lhs)
                    {
                        int t = tokPos;
                        tokPos++;
                        if (e is VariableExpression)
                        {
                            VariableExpression var = e as VariableExpression;
                            if (var.Var == v)
                            {
                                if (a.ScannedTokens.Count >= t && a.ScannedTokens[t].Type == TokenType.Ident)
                                {
                                    Token tok = a.ScannedTokens[t];
                                    ret = tok.Location;
                                    break;
                                }
                            }
                        }
                        tokPos++;
                    }
                }
                else if (s is FunctionStatement)
                {
                    FunctionStatement fs = s as FunctionStatement;
                    Variable var = null;
                    int p = 1;
                    if (fs.IsLocal)
                        p++;
                    Expression e = fs.Name;
                    while (e is IndexExpr)
                    {
                        e = ((IndexExpr)e).Index;
                        p += 2; // <varname> '.'
                    }
                    if (e is VariableExpression)
                        var = ((VariableExpression)e).Var;

                    if (var != null)
                    {
                        if (var == v)
                        {
                            if (fs.ScannedTokens.Count >= p && fs.ScannedTokens[p].Type == TokenType.Ident)
                            {
                                Token tok = fs.ScannedTokens[p];
                                ret = tok.Location;
                                break;
                            }
                        }
                    }
                }

                if (s is Chunk && ret == null)
                {
                    ret = FindImplementation(s as Chunk, v);
                }
            }
            return ret == null ? new Location() { Line = -1, Column = -1 } : ret;
        }
Пример #5
0
 public void AddLocal(Variable v)
 {
     Locals.Add(v);
 }
Пример #6
0
 public Variable CreateGlobal(string name)
 {
     Variable v = new Variable();
     v.Name = name;
     v.IsGlobal = true;
     Globals.Add(v);
     v.References = 1;
     return v;
 }
Пример #7
0
 public void RenameVariable(Variable oldName, string newName)
 {
     RenameVariable(oldName.Name, newName);
 }
Пример #8
0
 public void RenameGlobal(Variable oldName, string newName)
 {
     RenameGlobal(oldName.Name, newName);
 }
Пример #9
0
        Statement ParseStatement(Scope scope)
        {
            int startP = reader.p;
            int startLine = reader.Peek().Line;
            Statement stat = null;
            // print(tok.Peek().Print())
            if (reader.ConsumeKeyword("if"))
            {
                //setup
                IfStmt _if = new IfStmt();

                //clauses
                do
                {
                    int sP = reader.p;
                    Expression nodeCond = ParseExpr(scope);

                    if (!reader.ConsumeKeyword("then"))
                        error("'then' expected");

                    List<Statement> nodeBody = ParseStatementList(scope);

                    List<Token> range = new List<Token>();
                    range.Add(reader.tokens[sP - 1]);
                    range.AddRange(reader.Range(sP, reader.p));

                    _if.Clauses.Add(new ElseIfStmt(scope)
                    {
                        Condition = nodeCond,
                        Body = nodeBody,
                        ScannedTokens = range
                    });
                }
                while (reader.ConsumeKeyword("elseif"));

                // else clause
                if (reader.ConsumeKeyword("else"))
                {
                    int sP = reader.p;
                    List<Statement> nodeBody = ParseStatementList(scope);
                    List<Token> range = new List<Token>();
                    range.Add(reader.tokens[sP - 1]);
                    range.AddRange(reader.Range(sP, reader.p));

                    _if.Clauses.Add(new ElseStmt(scope)
                    {
                        Body = nodeBody,
                        ScannedTokens = range
                    });
                }

                // end
                if (!reader.ConsumeKeyword("end"))
                    error("'end' expected");

                stat = _if;
            }
            else if (reader.ConsumeKeyword("while"))
            {
                WhileStatement w = new WhileStatement(scope);

                // condition
                Expression nodeCond = ParseExpr(scope);

                // do
                if (!reader.ConsumeKeyword("do"))
                    error("'do' expected");

                // body
                List<Statement> body = ParseStatementList(scope);

                //end
                if (!reader.ConsumeKeyword("end"))
                    error("'end' expected");


                // return
                w.Condition = nodeCond;
                w.Body = body;
                stat = w;
            }
            else if (reader.ConsumeKeyword("do"))
            {
                // do block
                List<Statement> b = ParseStatementList(scope);

                if (!reader.ConsumeKeyword("end"))
                    error("'end' expected");

                stat = new DoStatement(scope) { Body = b };
            }
            else if (reader.ConsumeKeyword("for"))
            {
                //for block
                if (!reader.Is(TokenType.Ident))
                    error("<ident> expected");

                Token baseVarName = reader.Get();
                if (reader.ConsumeSymbol('='))
                {
                    //numeric for
                    NumericForStatement forL = new NumericForStatement(scope);
                    Variable forVar = new Variable() { Name = baseVarName.Data };
                    forL.Scope.AddLocal(forVar);

                    Expression startEx = ParseExpr(scope);

                    if (!reader.ConsumeSymbol(','))
                        error("',' expected");

                    Expression endEx = ParseExpr(scope);

                    Expression stepEx = null;
                    if (reader.ConsumeSymbol(','))
                    {
                        stepEx = ParseExpr(scope);
                    }
                    if (!reader.ConsumeKeyword("do"))
                        error("'do' expected");


                    List<Statement> body = ParseStatementList(forL.Scope);

                    if (!reader.ConsumeKeyword("end"))
                        error("'end' expected");


                    forL.Variable = forVar;
                    forL.Start = startEx;
                    forL.End = endEx;
                    forL.Step = stepEx;
                    forL.Body = body;
                    stat = forL;
                }
                else
                {
                    // generic for
                    GenericForStatement forL = new GenericForStatement(scope);

                    List<Variable> varList = new List<Variable> { forL.Scope.CreateLocal(baseVarName.Data) };
                    while (reader.ConsumeSymbol(','))
                    {
                        if (!reader.Is(TokenType.Ident))
                            error("for variable expected");

                        varList.Add(forL.Scope.CreateLocal(reader.Get().Data));
                    }
                    if (!reader.ConsumeKeyword("in"))
                        error("'in' expected");

                    List<Expression> generators = new List<Expression>();
                    Expression first = ParseExpr(scope);

                    generators.Add(first);
                    while (reader.ConsumeSymbol(','))
                    {
                        Expression gen = ParseExpr(scope);
                        generators.Add(gen);
                    }
                    if (!reader.ConsumeKeyword("do"))
                        error("'do' expected");

                    List<Statement> body = ParseStatementList(forL.Scope);

                    if (!reader.ConsumeKeyword("end"))
                        error("'end' expected");

                    forL.VariableList = varList;
                    forL.Generators = generators;
                    forL.Body = body;
                    stat = forL;
                }
            }
            else if (reader.ConsumeKeyword("repeat"))
            {
                List<Statement> body = ParseStatementList(scope);

                if (!reader.ConsumeKeyword("until"))
                    error("'until' expected");

                Expression cond = ParseExpr(scope);

                RepeatStatement r = new RepeatStatement(scope);
                r.Condition = cond;
                r.Body = body;
                stat = r;
            }
            else if (reader.ConsumeKeyword("function"))
            {
                if (!reader.Is(TokenType.Ident))
                    error("function name expected");

                Expression name = ParseSuffixedExpr(scope, true);
                // true: only dots and colons

                FunctionStatement func = ParseFunctionArgsAndBody(scope);

                func.IsLocal = false;
                func.Name = name;
                stat = func;
            }
            else if (reader.ConsumeKeyword("local"))
            {
                if (reader.Is(TokenType.Ident))
                {
                    List<string> varList = new List<string> { reader.Get().Data };
                    while (reader.ConsumeSymbol(','))
                    {
                        if (!reader.Is(TokenType.Ident))
                            error("local variable name expected");
                        varList.Add(reader.Get().Data);
                    }

                    List<Expression> initList = new List<Expression>();
                    if (reader.ConsumeSymbol('='))
                    {
                        do
                        {
                            Expression ex = ParseExpr(scope);
                            initList.Add(ex);
                        } while (reader.ConsumeSymbol(','));
                    }

                    //now patch var list
                    //we can't do this before getting the init list, because the init list does not
                    //have the locals themselves in scope.
                    List<Expression> newVarList = new List<Expression>();
                    for (int i = 0; i < varList.Count; i++)
                    {
                        Variable x = scope.CreateLocal(varList[i]);
                        newVarList.Add(new VariableExpression { Var = x });
                    }

                    AssignmentStatement l = new AssignmentStatement();
                    l.Lhs = newVarList;
                    l.Rhs = initList;
                    l.IsLocal = true;
                    stat = l;
                }
                else if (reader.ConsumeKeyword("function"))
                {
                    if (!reader.Is(TokenType.Ident))
                        error("Function name expected");
                    string name = reader.Get().Data;
                    Variable localVar = scope.CreateLocal(name);

                    FunctionStatement func = ParseFunctionArgsAndBody(scope);

                    func.Name = new VariableExpression { Var = localVar };
                    func.IsLocal = true;
                    stat = func;
                }
                else
                    error("local variable or function definition expected");
            }
#if !VANILLA_LUA
            else if (reader.ConsumeSymbol("::"))
            {
                if (!reader.Is(TokenType.Ident))
                    error("label name expected");

                string label = reader.Get().Data;
                if (!reader.ConsumeSymbol("::"))
                    error("'::' expected");

                LabelStatement l = new LabelStatement();
                l.Label = label;
                stat = l;
            }
#endif
            else if (reader.ConsumeKeyword("return"))
            {
                List<Expression> exprList = new List<Expression>();
                if (!reader.IsKeyword("end") && !reader.IsEof())
                {
                    Expression firstEx = ParseExpr(scope);
                    exprList.Add(firstEx);
                    while (reader.ConsumeSymbol(','))
                    {
                        Expression ex = ParseExpr(scope);
                        exprList.Add(ex);
                    }
                }
                ReturnStatement r = new ReturnStatement();
                r.Arguments = exprList;
                stat = r;
            }
            else if (reader.ConsumeKeyword("break"))
            {
                stat = new BreakStatement();
            }
            else if (reader.ConsumeKeyword("continue"))
            {
                stat = new ContinueStatement();
            }
#if !VANILLA_LUA
            else if (reader.ConsumeKeyword("goto"))
            {
                if (!reader.Is(TokenType.Ident))
                    error("label expected");

                string label = reader.Get().Data;
                GotoStatement g = new GotoStatement();
                g.Label = label;
                stat = g;
            }
            else if (reader.ConsumeKeyword("using"))
            {
                // using <a, b = 1, x()> do <statements> end
                UsingStatement us = new UsingStatement(scope);
                us.Scope = new Scope(scope);

                List<Expression> lhs = new List<Expression> { ParseExpr(us.Scope) };
                while (reader.ConsumeSymbol(','))
                {
                    lhs.Add(ParseSuffixedExpr(us.Scope, true));
                }

                // equals
                if (!reader.ConsumeSymbol('='))
                    error("'=' expected");

                //rhs
                List<Expression> rhs = new List<Expression>();
                rhs.Add(ParseExpr(us.Scope));
                while (reader.ConsumeSymbol(','))
                {
                    rhs.Add(ParseExpr(scope));
                }

                AssignmentStatement a = new AssignmentStatement();
                a.Lhs = lhs;
                a.Rhs = rhs;
                a.IsLocal = true;

                if (!reader.ConsumeKeyword("do"))
                    error("'do' expected");

                List<Statement> block = ParseStatementList(us.Scope);

                if (!reader.ConsumeKeyword("end"))
                    error("'end' expected");

                us.Vars = a;
                us.Body = block;
                stat = us;
            }
#endif
            else
            {
                // statementParseExpr
                Expression suffixed = ParseSuffixedExpr(scope);
                // assignment or call?
                if (reader.IsSymbol(',') || reader.IsSymbol('='))
                {
                    // check that it was not parenthesized, making it not an lvalue
                    if (suffixed.ParenCount > 0)
                        error("Can not assign to parenthesized expression, it is not an lvalue");

                    // more processing needed
                    List<Expression> lhs = new List<Expression> { suffixed };
                    while (reader.ConsumeSymbol(','))
                    {
                        lhs.Add(ParseSuffixedExpr(scope));
                    }

                    // equals
                    if (!reader.ConsumeSymbol('='))
                        error("'=' expected");

                    //rhs
                    List<Expression> rhs = new List<Expression>();
                    rhs.Add(ParseExpr(scope));
                    while (reader.ConsumeSymbol(','))
                    {
                        rhs.Add(ParseExpr(scope));
                    }

                    AssignmentStatement a = new AssignmentStatement();
                    a.Lhs = lhs;
                    a.Rhs = rhs;
                    stat = a;
                }
#if !VANILLA_LUA
                else if (isAugmentedAssignment(reader.Peek()))
                {
                    AugmentedAssignmentStatement aas = new AugmentedAssignmentStatement();
                    Expression left = suffixed;
                    Expression right = null;
                    string augmentedOp = reader.Get().Data;
                    right = ParseExpr(scope);
                    BinOpExpr nRight = new BinOpExpr();
                    nRight.Lhs = left;
                    nRight.Op = augmentedOp.Substring(0, augmentedOp.Length - 1); // strip the '='
                    nRight.Rhs = right;

                    aas.Lhs = new List<Expression> { left };
                    aas.Rhs = new List<Expression> { nRight };
                    stat = aas;
                }
#endif
                else if (suffixed is CallExpr ||
                       suffixed is TableCallExpr ||
                       suffixed is StringCallExpr)
                {
                    //it's a call statement
                    CallStatement c = new CallStatement();
                    c.Expression = suffixed;
                    stat = c;
                }
                else
                    error("assignment statement expected");
            }

            stat.ScannedTokens = reader.Range(startP, reader.p);
            if (reader.Peek().Data == ";" && reader.Peek().Type == TokenType.Symbol)
            {
                stat.HasSemicolon = true;
                stat.SemicolonToken = reader.Get();
            }
            if (stat.Scope == null)
                stat.Scope = scope;
            stat.LineNumber = startLine;
            return stat;
        }
Пример #10
0
        Expression ParseSimpleExpr(Scope scope)
        {
            if (reader.Is(TokenType.Number))
                return new NumberExpr { Value = reader.Get().Data };
            else if (reader.Is(TokenType.DoubleQuoteString) || reader.Is(TokenType.SingleQuoteString) || reader.Is(TokenType.LongString))
            {
                StringExpr s = new StringExpr
                {
                    Value = reader.Peek().Data,
                    StringType = reader.Peek().Type
                };
                reader.Get();
                return s;
            }
            else if (reader.ConsumeKeyword("nil"))
                return new NilExpr();
            else if (reader.IsKeyword("false") || reader.IsKeyword("true"))
                return new BoolExpr { Value = reader.Get().Data == "true" };
            else if (reader.ConsumeSymbol("..."))
                return new VarargExpr();
            else if (reader.ConsumeSymbol('{'))
            {
                TableConstructorExpr v = new TableConstructorExpr();
                while (true)
                {
                    if (reader.IsSymbol('['))
                    {
                        // key
                        reader.Get(); // eat '['
                        Expression key = ParseExpr(scope);

                        if (!reader.ConsumeSymbol(']'))
                        {
                            error("']' expected");
                            break;
                        }

                        if (!reader.ConsumeSymbol('='))
                        {
                            error("'=' Expected");
                            break;
                        }

                        Expression value = ParseExpr(scope);

                        v.EntryList.Add(new TableConstructorKeyExpr
                        {
                            Key = key,
                            Value = value,
                        });
                    }
                    else if (reader.Is(TokenType.Ident))
                    {
                        // value or key
                        Token lookahead = reader.Peek(1);
                        if (lookahead.Type == TokenType.Symbol && lookahead.Data == "=")
                        {
                            // we are a key
                            Token key = reader.Get();
                            if (!reader.ConsumeSymbol('='))
                                error("'=' Expected");

                            Expression value = ParseExpr(scope);

                            v.EntryList.Add(new TableConstructorStringKeyExpr
                            {
                                Key = key.Data,
                                Value = value,
                            });
                        }
                        else
                        {
                            // we are a value
                            Expression val = ParseExpr(scope);

                            v.EntryList.Add(new TableConstructorValueExpr
                            {
                                Value = val
                            });

                        }
                    }
#if !VANILLA_LUA
                    else if (reader.ConsumeKeyword("function"))
                    {
                        if (reader.Peek().Type != TokenType.Ident)
                            error("function name expected");
                        string name = reader.Get().Data;
                        FunctionStatement fs = ParseFunctionArgsAndBody(scope);
                        fs.IsLocal = false;
                        fs.Name = new StringExpr(name);
                        v.EntryList.Add(new TableConstructorNamedFunctionExpr
                        {
                            Value = fs
                        });
                    }
#endif
                    else if (reader.ConsumeSymbol('}'))
                        break;
                    else
                    {
                        //value
                        Expression value = ParseExpr(scope);
                        v.EntryList.Add(new TableConstructorValueExpr
                        {
                            Value = value
                        });
                    }

                    if (reader.ConsumeSymbol(';') || reader.ConsumeSymbol(','))
                    {
                        // I could have used just an empty statement (';') here, instead of { }
                        // but that leaves a warning, which clutters up the output
                        // other than that, all is good
                    }
                    else if (reader.ConsumeSymbol('}'))
                        break;
                    else
                    {
                        error("'}' or table entry Expected");
                        break;
                    }
                }
                return v;
            }
            else if (reader.ConsumeKeyword("function"))
            {
                AnonymousFunctionExpr func = ParseExprFunctionArgsAndBody(scope);
                //func.IsLocal = true;
                return func;
            }
#if !VANILLA_LUA
            else if (reader.ConsumeSymbol('|'))
            {
                // inline function... |<arg list>| -> <expr>, <expr>
                InlineFunctionExpression func = new InlineFunctionExpression();
                func.Scope = new Scope(scope);
                // arg list
                List<Variable> arglist = new List<Variable>();
                bool isVarArg = false;
                while (reader.ConsumeSymbol('|') == false)
                {
                    if (reader.Is(TokenType.Ident))
                    {
                        Variable arg = new Variable();
                        arg.Name = reader.Get().Data;
                        func.Scope.AddLocal(arg);
                        arglist.Add(arg);
                        if (!reader.ConsumeSymbol(','))
                        {
                            if (reader.ConsumeSymbol('|'))
                            {
                                break;
                            }
                            else
                            {
                                error("'|' expected");
                                break;
                            }
                        }
                    }
                    else if (reader.ConsumeSymbol("..."))
                    {
                        isVarArg = true;
                        if (!reader.ConsumeSymbol('|'))
                            error("'...' must be the last argument of a function");
                        break;
                    }
                    else
                    {
                        error("Argument name or '...' expected");
                        break;
                    }
                }
                if (!reader.ConsumeSymbol("->"))
                    error("'->' expected");
                // body
                List<Expression> body = new List<Expression> { ParseExpr(func.Scope) };
                while (reader.ConsumeSymbol(','))
                    body.Add(ParseExpr(func.Scope));
                // end

                //nodeFunc.AstType = AstType.Function;
                func.Arguments = arglist;
                func.Expressions = body;
                func.IsVararg = isVarArg;

                return func;
            }
#endif
            else
                return ParseSuffixedExpr(scope);
        }