static List <CompletionItem> DoStatement(Statement s) { List <CompletionItem> ret = new List <CompletionItem>(); if (s is AssignmentStatement && !(s is AugmentedAssignmentStatement)) { AssignmentStatement a = s as AssignmentStatement; foreach (Expression e in a.Lhs) { ret.AddRange(DoExpr(e)); } foreach (Expression e in a.Rhs) { ret.AddRange(DoExpr(e)); } } else if (s is AugmentedAssignmentStatement) { AugmentedAssignmentStatement a = s as AugmentedAssignmentStatement; foreach (Expression e in a.Lhs) { ret.AddRange(DoExpr(e)); } foreach (Expression e in a.Rhs) { ret.AddRange(DoExpr(e)); } } else if (s is BreakStatement) { } else if (s is CallStatement) { // Also incredibly simple... CallStatement c = s as CallStatement; return(DoExpr(c.Expression)); } else if (s is DoStatement) { DoStatement d = s as DoStatement; return(DoChunk(d.Body)); } else if (s is GenericForStatement) { GenericForStatement g = s as GenericForStatement; foreach (Variable v in g.VariableList) { ret.Add(new CompletionItem(v.Name)); } ret.AddRange(DoChunk(g.Body)); } else if (s is NumericForStatement) { NumericForStatement n = s as NumericForStatement; ret.Add(new CompletionItem(n.Variable.Name)); ret.AddRange(DoChunk(n.Body)); } else if (s is FunctionStatement) { FunctionStatement f = s as FunctionStatement; ret.AddRange(DoExpr(f.Name)); foreach (Variable v in f.Arguments) { ret.Add(new CompletionItem(v.Name)); } ret.AddRange(DoChunk(f.Body)); } else if (s is GotoStatement) { } else if (s is IfStmt) { IfStmt i = s as IfStmt; for (int x = 0; x < i.Clauses.Count; x++) { ret.AddRange(DoStatement(i.Clauses[x])); } } else if (s is LabelStatement) { } else if (s is RepeatStatement) { RepeatStatement r = s as RepeatStatement; ret.AddRange(DoChunk(r.Body)); ret.AddRange(DoExpr(r.Condition)); } else if (s is ReturnStatement) { // no variable defined here. hopefully. } else if (s is UsingStatement) { UsingStatement u = s as UsingStatement; ret.AddRange(DoStatement(u.Vars)); ret.AddRange(DoChunk(u.Body)); } else if (s is WhileStatement) { WhileStatement w = s as WhileStatement; ret.AddRange(DoExpr(w.Condition)); ret.AddRange(DoChunk(w.Body)); } else if (s is ElseIfStmt) { ElseIfStmt e = s as ElseIfStmt; return(DoChunk(e.Body)); } else if (s is ElseStmt) { return(DoChunk(((ElseStmt)s).Body)); } return(ret); }
internal string DoStatement(Statement s) { if (s is AssignmentStatement && !(s is AugmentedAssignmentStatement)) { AssignmentStatement a = s as AssignmentStatement; StringBuilder sb = new StringBuilder(); if (a.IsLocal) { sb.Append("local "); } for (int i = 0; i < a.Lhs.Count; i++) { sb.Append(DoExpr(a.Lhs[i])); if (i != a.Lhs.Count - 1) { sb.Append(","); } } if (a.Rhs.Count > 0) { sb.Append("="); for (int i = 0; i < a.Rhs.Count; i++) { sb.Append(DoExpr(a.Rhs[i])); if (i != a.Rhs.Count - 1) { sb.Append(","); } } } return(sb.ToString()); } else if (s is AugmentedAssignmentStatement) { AugmentedAssignmentStatement a = s as AugmentedAssignmentStatement; StringBuilder sb = new StringBuilder(); //sb.Append(DoExpr(a.Lhs[0])); if (a.IsLocal) { sb.Append("local "); } Expression assignment = ((BinOpExpr)a.Rhs[0]).Rhs; sb.Append(DoExpr((((BinOpExpr)a.Rhs[0]).Lhs))); sb.Append("" + ((BinOpExpr)a.Rhs[0]).Op + "="); sb.Append(DoExpr(assignment)); return(sb.ToString()); } else if (s is BreakStatement) { // HAHAHA this is incredibly simple... return("break"); } else if (s is CallStatement) { // Also incredibly simple... CallStatement c = s as CallStatement; return(DoExpr(c.Expression)); } else if (s is DoStatement) { DoStatement d = s as DoStatement; StringBuilder sb = new StringBuilder(); sb.Append("do "); sb.Append(DoChunk(d.Body)); sb.Append(" end"); return(sb.ToString()); } else if (s is GenericForStatement) { GenericForStatement g = s as GenericForStatement; StringBuilder sb = new StringBuilder(); sb.Append("for "); for (int i = 0; i < g.VariableList.Count; i++) { sb.Append(g.VariableList[i].Name); if (i != g.VariableList.Count - 1) { sb.Append(","); } } sb.Append(" in "); for (int i = 0; i < g.Generators.Count; i++) { sb.Append(DoExpr(g.Generators[i])); if (i != g.Generators.Count - 1) { sb.Append(","); } } sb.Append(" do "); sb.Append(DoChunk(g.Body)); sb.Append(" end"); return(sb.ToString()); } else if (s is NumericForStatement) { NumericForStatement n = s as NumericForStatement; StringBuilder sb = new StringBuilder(); sb.Append("for "); sb.Append(n.Variable.Name); sb.Append("="); sb.Append(DoExpr(n.Start)); sb.Append(","); sb.Append(DoExpr(n.End)); if (n.Step != null) { sb.Append(","); sb.Append(DoExpr(n.Step)); } sb.Append(" do "); sb.Append(DoChunk(n.Body)); sb.Append(" end"); return(sb.ToString()); } else if (s is FunctionStatement) { FunctionStatement f = s as FunctionStatement; StringBuilder sb = new StringBuilder(); sb.Append("function " + DoExpr(f.Name) + "("); for (int i = 0; i < f.Arguments.Count; i++) { sb.Append(f.Arguments[i].Name); if (i != f.Arguments.Count - 1 || f.IsVararg) { sb.Append(","); } } if (f.IsVararg) { sb.Append("..."); } sb.Append(")"); sb.Append(" " + DoStatement(f.Body[0])); sb.Append(" end"); return(sb.ToString()); } else if (s is GotoStatement) { GotoStatement g = s as GotoStatement; return("goto " + g.Label); } else if (s is IfStmt) { IfStmt i = s as IfStmt; StringBuilder sb = new StringBuilder(); for (int x = 0; x < i.Clauses.Count; x++) { string ss = DoStatement(i.Clauses[x]); if (x == 0) { sb.Append("if "); sb.Append(ss); } else if (i.Clauses[x] is ElseStmt) { sb.Append(" else "); sb.Append(ss); } else { sb.Append("elseif " + ss); } } sb.Append(" end"); return(sb.ToString()); } else if (s is LabelStatement) { LabelStatement l = s as LabelStatement; return("::" + l.Label + "::"); } else if (s is RepeatStatement) { RepeatStatement r = s as RepeatStatement; StringBuilder sb = new StringBuilder(); sb.Append("repeat "); sb.Append(DoChunk(r.Body)); sb.Append(" until " + DoExpr(r.Condition)); return(sb.ToString()); } else if (s is ReturnStatement) { ReturnStatement r = s as ReturnStatement; StringBuilder sb = new StringBuilder(); sb.Append("return "); for (int i = 0; i < r.Arguments.Count; i++) { sb.Append(DoExpr(r.Arguments[i])); if (i != r.Arguments.Count - 1) { sb.Append(","); } } return(sb.ToString()); } else if (s is UsingStatement) { UsingStatement u = s as UsingStatement; StringBuilder sb = new StringBuilder(); sb.Append("using "); sb.Append(DoStatement(u.Vars)); sb.Append(" do "); sb.Append(DoChunk(u.Body)); sb.Append(" end"); return(sb.ToString()); } else if (s is WhileStatement) { WhileStatement w = s as WhileStatement; StringBuilder sb = new StringBuilder(); sb.Append("while "); sb.Append(DoExpr(w.Condition)); sb.Append(" do "); sb.Append(DoChunk(w.Body)); sb.Append(" end"); return(sb.ToString()); } else if (s is ElseIfStmt) { ElseIfStmt e = s as ElseIfStmt; string s2 = DoExpr(e.Condition) + " then "; s2 += DoChunk(e.Body); return(s2); } else if (s is ElseStmt) { return(DoChunk(((ElseStmt)s).Body)); } throw new NotImplementedException(s.GetType().Name + " is not implemented"); }
internal string DoStatement(Statement s, ref int i) { // If the statement contains a body, we cant just fromTokens it, as it's body might not be // fully tokenized input. Therefore, we run DoChunk on Body's if (s.ScannedTokens != null && s.ScannedTokens.Count > 0) { if (s is AssignmentStatement && !(s is AugmentedAssignmentStatement)) { AssignmentStatement a = s as AssignmentStatement; StringBuilder sb = new StringBuilder(); if (a.IsLocal) { sb.Append(fromToken(a.ScannedTokens[i++], a.Scope)); sb.Append(" "); } for (int i2 = 0; i2 < a.Lhs.Count; i2++) { sb.Append(DoExpr(a.Lhs[i2], s.ScannedTokens, ref i, s.Scope)); if (i2 != a.Lhs.Count - 1) { sb.Append(fromToken(a.ScannedTokens[i++], a.Scope)); sb.Append(" "); } } if (a.Rhs.Count > 0) { sb.Append(" "); sb.Append(fromToken(a.ScannedTokens[i++], a.Scope)); sb.Append(" "); for (int i2 = 0; i2 < a.Rhs.Count; i2++) { sb.Append(DoExpr(a.Rhs[i2], s.ScannedTokens, ref i, s.Scope)); if (i2 != a.Rhs.Count - 1) { sb.Append(fromToken(a.ScannedTokens[i++], s.Scope)); sb.Append(" "); } } } return(sb.ToString()); } else if (s is AugmentedAssignmentStatement) { AugmentedAssignmentStatement a = s as AugmentedAssignmentStatement; StringBuilder sb = new StringBuilder(); //sb.Append(DoExpr(a.Lhs[0])); if (a.IsLocal) { sb.Append(fromToken(a.ScannedTokens[i++], a.Scope)); sb.Append(" "); } Expression assignment = ((BinOpExpr)a.Rhs[0]).Rhs; Expression tmp = ((BinOpExpr)a.Rhs[0]).Lhs; sb.Append(DoExpr(tmp, s.ScannedTokens, ref i, s.Scope)); sb.Append(" "); sb.Append(fromToken(s.ScannedTokens[i++], s.Scope)); sb.Append(" "); sb.Append(DoExpr(assignment, s.ScannedTokens, ref i, s.Scope)); return(sb.ToString()); } else if (s is BreakStatement) { // HAHAHA this is incredibly simple... return(fromTokens(s.ScannedTokens, s.Scope)); } else if (s is ContinueStatement) { return(fromTokens(s.ScannedTokens, s.Scope)); } else if (s is CallStatement) { // Also incredibly simple... return(DoExpr(((CallStatement)s).Expression, s.ScannedTokens, ref i, s.Scope)); } else if (s is DoStatement) { DoStatement d = s as DoStatement; StringBuilder sb = new StringBuilder(); sb.Append(fromToken(d.ScannedTokens[0], s.Scope)); // 'do' sb.Append(options.EOL); indent++; sb.Append(DoChunk(d.Body, ref i)); sb.Append(nldedent()); sb.Append(fromToken(d.ScannedTokens[d.ScannedTokens.Count - 1], s.Scope)); // end return(sb.ToString()); } else if (s is GenericForStatement) { GenericForStatement g = s as GenericForStatement; StringBuilder sb = new StringBuilder(); sb.Append(fromToken(g.ScannedTokens[i++], s.Scope)); // 'for' sb.Append(" "); for (int x = 0; x < g.VariableList.Count; x++) { sb.Append(fromToken(s.ScannedTokens[i++], s.Scope)); if (x != g.VariableList.Count - 1) { sb.Append(fromToken(s.ScannedTokens[i++], s.Scope)); // ',' sb.Append(" "); } } sb.Append(" "); sb.Append(fromToken(g.ScannedTokens[i++], s.Scope)); // 'in' sb.Append(" "); for (int x = 0; x < g.Generators.Count; x++) { //sb.Append(fromToken(s.ScannedTokens[i++], s.Scope)); DoExpr(g.Generators[x], g.ScannedTokens, ref i, s.Scope); if (x != g.VariableList.Count - 1) { sb.Append(fromToken(s.ScannedTokens[i++], s.Scope)); // ',' sb.Append(" "); } } sb.Append(" "); sb.Append(fromToken(g.ScannedTokens[i++], s.Scope)); // 'do' sb.Append(options.EOL); indent++; sb.Append(DoChunk(g.Body)); sb.Append(nldedent()); sb.Append(fromToken(s.ScannedTokens[s.ScannedTokens.Count - 1], s.Scope)); // <end> return(sb.ToString()); } else if (s is NumericForStatement) { NumericForStatement n = s as NumericForStatement; StringBuilder sb = new StringBuilder(); sb.Append(fromToken(n.ScannedTokens[i++], s.Scope)); // 'for' sb.Append(" "); sb.Append(fromToken(n.ScannedTokens[i++], s.Scope)); // <var> sb.Append(" "); sb.Append(fromToken(n.ScannedTokens[i++], s.Scope)); // '=' sb.Append(" "); sb.Append(DoExpr(n.Start, n.ScannedTokens, ref i, s.Scope)); // <start> //sb.Append(" "); sb.Append(fromToken(n.ScannedTokens[i++], s.Scope)); // ',' sb.Append(" "); sb.Append(DoExpr(n.End, n.ScannedTokens, ref i, s.Scope)); // <end> if (n.Step != null) { sb.Append(fromToken(n.ScannedTokens[i++], s.Scope)); // ',' sb.Append(" "); sb.Append(DoExpr(n.Step, n.ScannedTokens, ref i, s.Scope)); // <step> sb.Append(" "); } else { sb.Append(" "); } sb.Append(fromToken(n.ScannedTokens[i++], s.Scope)); // 'do' sb.Append(options.EOL); indent++; sb.Append(DoChunk(n.Body)); sb.Append(nldedent()); sb.Append(fromToken(s.ScannedTokens[s.ScannedTokens.Count - 1], s.Scope)); // <end> return(sb.ToString()); } else if (s is FunctionStatement) { FunctionStatement f = s as FunctionStatement; StringBuilder sb = new StringBuilder(); sb.Append(fromToken(s.ScannedTokens[i++], s.Scope)); // 'function' sb.Append(" "); sb.Append(DoExpr(f.Name, s.ScannedTokens, ref i, s.Scope)); sb.Append(fromToken(s.ScannedTokens[i++], s.Scope)); // '(' for (int i2 = 0; i2 < f.Arguments.Count; i2++) { sb.Append(fromToken(s.ScannedTokens[i++], s.Scope)); if (i2 != f.Arguments.Count - 1)// || f.IsVararg) { sb.Append(fromToken(s.ScannedTokens[i++], s.Scope)); sb.Append(" "); } } if (f.IsVararg) { sb.Append(fromToken(s.ScannedTokens[i++], s.Scope)); sb.Append(" "); sb.Append(fromToken(s.ScannedTokens[i++], s.Scope)); } sb.Append(fromToken(s.ScannedTokens[i++], s.Scope)); // ')' sb.Append(options.EOL); indent++; sb.Append(DoChunk(f.Body)); indent--; sb.Append(options.EOL); sb.Append(writeIndent()); sb.Append(fromToken(s.ScannedTokens[s.ScannedTokens.Count - 1], s.Scope)); // <end> sb.Append(options.EOL); return(sb.ToString()); } else if (s is GotoStatement) { // goto <string label>, so no expr //GotoStatement g = s as GotoStatement; return(fromTokens(s.ScannedTokens, s.Scope)); } else if (s is IfStmt) { IfStmt ifs = s as IfStmt; StringBuilder sb = new StringBuilder(); foreach (SubIfStmt clause in ifs.Clauses) { int i3 = 0; if (clause is ElseIfStmt) { ElseIfStmt c = clause as ElseIfStmt; sb.Append(fromToken(c.ScannedTokens[i3++], s.Scope)); // if/elseif sb.Append(" "); sb.Append(DoExpr(c.Condition, c.ScannedTokens, ref i3, c.Scope)); sb.Append(" "); sb.Append(fromToken(c.ScannedTokens[i3++], s.Scope)); // 'then' sb.Append(options.EOL); indent++; sb.Append(DoChunk(clause.Body)); sb.Append(nldedent()); } else if (clause is ElseStmt) { sb.Append(fromToken(clause.ScannedTokens[i3++], s.Scope)); // if/elseif sb.Append(options.EOL); indent++; sb.Append(DoChunk(clause.Body)); sb.Append(nldedent()); } else { throw new NotImplementedException(clause.GetType().Name); } } sb.Append(fromToken(s.ScannedTokens[s.ScannedTokens.Count - 1], s.Scope)); // 'end' return(sb.ToString()); } else if (s is LabelStatement) { // ::<string label>::, so no expr return(fromTokens(s.ScannedTokens, s.Scope)); } else if (s is RepeatStatement) { RepeatStatement r = s as RepeatStatement; StringBuilder sb = new StringBuilder(); sb.Append(fromToken(r.ScannedTokens[0], s.Scope)); i = 1; sb.Append(options.EOL); indent++; sb.Append(DoChunk(r.Body, ref i)); sb.Append(nldedent()); sb.Append(fromToken(r.ScannedTokens[i++], r.Scope)); sb.Append(" "); sb.Append(DoExpr(r.Condition, r.ScannedTokens, ref i, r.Scope)); return(sb.ToString()); } else if (s is ReturnStatement) { ReturnStatement rs = s as ReturnStatement; StringBuilder sb = new StringBuilder(); sb.Append(fromToken(s.ScannedTokens[i++], s.Scope)); // 'return' sb.Append(" "); for (int x = 0; x < rs.Arguments.Count; x++) { sb.Append(DoExpr(rs.Arguments[x], rs.ScannedTokens, ref i, s.Scope)); if (x != rs.Arguments.Count - 1) { sb.Append(fromToken(s.ScannedTokens[i++], s.Scope)); // ',' sb.Append(" "); } } return(sb.ToString()); } else if (s is UsingStatement) { UsingStatement u = s as UsingStatement; StringBuilder sb = new StringBuilder(); sb.Append(fromToken(s.ScannedTokens[i++], s.Scope)); // 'using' sb.Append(" "); AssignmentStatement a = u.Vars; for (int i2 = 0; i2 < a.Lhs.Count; i2++) { sb.Append(DoExpr(a.Lhs[i2], u.ScannedTokens, ref i, s.Scope)); if (i2 != a.Lhs.Count - 1) { sb.Append(fromToken(u.ScannedTokens[i++], a.Scope)); sb.Append(" "); } } if (a.Rhs.Count > 0) { sb.Append(" "); sb.Append(fromToken(u.ScannedTokens[i++], a.Scope)); // '=' sb.Append(" "); for (int i2 = 0; i2 < a.Rhs.Count; i2++) { sb.Append(DoExpr(a.Rhs[i2], u.ScannedTokens, ref i, s.Scope)); if (i2 != a.Rhs.Count - 1) { sb.Append(fromToken(u.ScannedTokens[i++], s.Scope)); sb.Append(" "); } } } sb.Append(" "); sb.Append(fromToken(s.ScannedTokens[i++], s.Scope)); // 'do' sb.Append(options.EOL); indent++; sb.Append(DoChunk(u.Body)); sb.Append(nldedent()); sb.Append(fromToken(s.ScannedTokens[s.ScannedTokens.Count - 1], s.Scope)); // 'end' return(sb.ToString()); } else if (s is WhileStatement) { WhileStatement w = s as WhileStatement; StringBuilder sb = new StringBuilder(); sb.Append(fromToken(s.ScannedTokens[i++], s.Scope)); // 'while' sb.Append(" "); sb.Append(DoExpr(w.Condition, w.ScannedTokens, ref i, s.Scope)); sb.Append(" "); sb.Append(fromToken(s.ScannedTokens[i++], s.Scope)); // 'do' sb.Append(options.EOL); indent++; sb.Append(DoChunk(w.Body)); sb.Append(nldedent()); sb.Append(fromToken(s.ScannedTokens[s.ScannedTokens.Count - 1], s.Scope)); return(sb.ToString()); } } else // No token stream, beautify { return(beautifier.DoStatement(s)); } throw new NotImplementedException(s.GetType().Name + " is not implemented"); }
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); }
void DoStatement(Statement s) { line = s.LineNumber; if (s is AssignmentStatement && !(s is AugmentedAssignmentStatement)) { AssignmentStatement a = s as AssignmentStatement; if (a.IsLocal == false) { for (int i = a.Rhs.Count; i > 0; i--) { DoExpr(a.Rhs[i - 1], false, a.Lhs.Count); } for (int i = a.Lhs.Count; i > 0; i--) { DoExpr(a.Lhs[i - 1], true); } return; } else { for (int i = a.Rhs.Count; i > 0; i--) { DoExpr(a.Rhs[i - 1], false, a.Lhs.Count); } for (int i = a.Lhs.Count; i > 0; i--) { DoExpr(a.Lhs[i - 1], true); } return; } } else if (s is AugmentedAssignmentStatement) { AugmentedAssignmentStatement aas = s as AugmentedAssignmentStatement; StringBuilder sb = new StringBuilder(); //sb.Append(DoExpr(a.Lhs[0])); if (aas.IsLocal) { throw new LuaSourceException(s.LineNumber, 0, "Local assignment cannot have augmented operators"); } DoExpr(aas.Rhs[0], true, 1); DoExpr(aas.Lhs[0], true, 1); return; } else if (s is BreakStatement) { bool hadLoop = false; while (block != null) { if (block.IsLoop) { hadLoop = true; break; } block = block.PreviousBlock; } if (!hadLoop) { throw new LuaSourceException(s.LineNumber, 0, "No loop to break"); } Instruction i = new Instruction("JMP"); i.A = 0; i.sBx = -1; // Infinite loop ;) emit(i); patchSbx.Push(i); return; } else if (s is ContinueStatement) { } else if (s is CallStatement) { CallStatement cs = s as CallStatement; DoExpr(cs.Expression); return; } else if (s is DoStatement) { } else if (s is GenericForStatement) { } else if (s is NumericForStatement) { } else if (s is FunctionStatement) { } else if (s is GotoStatement) { ; } else if (s is IfStmt) { } else if (s is LabelStatement) { ; } else if (s is RepeatStatement) { } else if (s is ReturnStatement) { } else if (s is UsingStatement) { } else if (s is WhileStatement) { WhileStatement ws = s as WhileStatement; int start = block.Chunk.Instructions.Count; DoExpr(ws.Condition); Instruction t = new Instruction("TEST"); t.A = block.regnum; t.C = 0; emit(t); DoChunk(ws.Body, true); Instruction i = new Instruction("JMP"); i.A = 0; i.sBx = -(block.Chunk.Instructions.Count - start) - 1; emit(i); while (patchSbx.Count > 0) { patchSbx.Pop().sBx = Math.Abs(block.Chunk.Instructions.Count + i.sBx - 1); } //return; } throw new NotImplementedException(s.GetType().Name + " is not implemented"); }
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; }