Expression ParseSubExpr(Scope scope, int level) { // base item, possibly with unop prefix Expression exp = null; if (isUnOp(reader.Peek().Data) && (reader.Peek().Type == TokenType.Symbol || reader.Peek().Type == TokenType.Keyword)) { string op = reader.Get().Data; exp = ParseSubExpr(scope, unopprio); exp = new UnOpExpr { Rhs = exp, Op = op }; } else { exp = ParseSimpleExpr(scope); } if (exp is InlineFunctionExpression) { return(exp); // inline functions cannot have any extra parts } // next items in chain while (true) { priority_ prio = getpriority(reader.Peek().Data); if (prio != null && prio.l > level) { string op = reader.Get().Data; Expression rhs = ParseSubExpr(scope, prio.r); BinOpExpr binOpExpr = new BinOpExpr(); binOpExpr.Lhs = exp; binOpExpr.Op = op; binOpExpr.Rhs = rhs; exp = binOpExpr; } else { break; } } return(exp); }
string DoExpr(Expression e) { string ret = ""; if (e is AnonymousFunctionExpr) // function() ... end { AnonymousFunctionExpr f = e as AnonymousFunctionExpr; StringBuilder sb = new StringBuilder(); sb.Append("function("); 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(")"); if (f.Body.Count > 1) { sb.Append(EOL); indent++; sb.Append(DoChunk(f.Body)); sb.Append(nldedent()); sb.Append("end"); } else if (f.Body.Count == 0) { sb.Append(" end"); } else { sb.Append(" " + DoStatement(f.Body[0])); sb.Append(" end"); } ret = sb.ToString(); } else if (e is BinOpExpr) { BinOpExpr b = e as BinOpExpr; string left = DoExpr(b.Lhs); string op = b.Op; string right = DoExpr(b.Rhs); if (op == "!=") { op = "~="; } if (op == ">>") { ret = string.Format("bit.rshift({0}, {1})", left, right); } else if (op == "<<") { ret = string.Format("bit.lshift({0}, {1})", left, right); } else if (op == "&") { ret = string.Format("bit.band({0}, {1})", left, right); } else if (op == "|") { ret = string.Format("bit.bor({0}, {1})", left, right); } else if (op == "^^") { ret = string.Format("bit.bxor({0}, {1})", left, right); } else { ret = string.Format("{0} {1} {2}", left, op, right); } } else if (e is BoolExpr) { bool val = (e as BoolExpr).Value; ret = val ? "true" : "false"; } else if (e is CallExpr && (!(e is StringCallExpr) && !(e is TableCallExpr))) { CallExpr c = e as CallExpr; StringBuilder sb = new StringBuilder(); sb.Append(DoExpr(c.Base) + "("); for (int i = 0; i < c.Arguments.Count; i++) { sb.Append(DoExpr(c.Arguments[i])); if (i != c.Arguments.Count - 1) { sb.Append(", "); } } sb.Append(")"); ret = sb.ToString(); } else if (e is StringCallExpr) { StringCallExpr s = e as StringCallExpr; ret = string.Format("{0} {1}", DoExpr(s.Base), DoExpr(s.Arguments[0])); } else if (e is TableCallExpr) { TableCallExpr s = e as TableCallExpr; ret = string.Format("{0} {1}", DoExpr(s.Base), DoExpr(s.Arguments[0])); } else if (e is IndexExpr) { IndexExpr i = e as IndexExpr; ret = string.Format("{0}[{1}]", DoExpr(i.Base), DoExpr(i.Index)); } else if (e is InlineFunctionExpression) // |<args>| -> <exprs> { InlineFunctionExpression ife = e as InlineFunctionExpression; StringBuilder sb = new StringBuilder(); sb.Append("function("); for (int i = 0; i < ife.Arguments.Count; i++) { sb.Append(ife.Arguments[i].Name); if (i != ife.Arguments.Count - 1 || ife.IsVararg) { sb.Append(", "); } } if (ife.IsVararg) { sb.Append("..."); } sb.Append(") return "); for (int i2 = 0; i2 < ife.Expressions.Count; i2++) { sb.Append(DoExpr(ife.Expressions[i2])); if (i2 != ife.Expressions.Count - 1) { sb.Append(", "); } } sb.Append(" end"); ret = sb.ToString(); } else if (e is TableConstructorKeyExpr) { TableConstructorKeyExpr t = e as TableConstructorKeyExpr; ret = "[" + DoExpr(t.Key) + "] = " + DoExpr(t.Value); } else if (e is MemberExpr) { MemberExpr m = e as MemberExpr; ret = DoExpr(m.Base) + m.Indexer + m.Ident; } else if (e is NilExpr) { ret = "nil"; } else if (e is NumberExpr) { ret = (e as NumberExpr).Value; ret = ret.Replace("_", ""); } else if (e is StringExpr) { StringExpr se = e as StringExpr; string delim = se.StringType == TokenType.SingleQuoteString ? "'" : se.StringType == TokenType.DoubleQuoteString ? "\"" : "["; if (delim == "[") { // Long strings keep their [=*[ ret = se.Value; } else { ret = delim + se.Value + delim; } } else if (e is TableConstructorStringKeyExpr) { TableConstructorStringKeyExpr tcske = e as TableConstructorStringKeyExpr; ret = tcske.Key + " = " + DoExpr(tcske.Value); } else if (e is TableConstructorExpr) { TableConstructorExpr t = e as TableConstructorExpr; StringBuilder sb = new StringBuilder(); sb.Append("{ "); for (int i = 0; i < t.EntryList.Count; i++) { sb.Append(DoExpr(t.EntryList[i])); if (i != t.EntryList.Count - 1) { sb.Append(", "); } } sb.Append("} "); ret = sb.ToString(); } else if (e is UnOpExpr) { string op = (e as UnOpExpr).Op; if (op == "!") { op = "not"; } string s = op; if (s == "~") { ret = "bit.bnot(" + DoExpr((e as UnOpExpr).Rhs) + ")"; } else if (s == "+") { ret = "math.abs(" + DoExpr((e as UnOpExpr).Rhs) + ")"; } else { if (s.Length != 1) { s += " "; } ret = s + DoExpr((e as UnOpExpr).Rhs); } } else if (e is TableConstructorValueExpr) { ret = DoExpr((e as TableConstructorValueExpr).Value); } else if (e is VarargExpr) { ret = "..."; } else if (e is VariableExpression) { ret = (e as VariableExpression).Var.Name; } else if (e is TableConstructorNamedFunctionExpr) { TableConstructorNamedFunctionExpr fs = e as TableConstructorNamedFunctionExpr; AnonymousFunctionExpr a = new AnonymousFunctionExpr(); a.Arguments = fs.Value.Arguments; a.Arguments.Insert(0, new Variable() { Name = "self", IsGlobal = false, References = -1 }); a.Body = fs.Value.Body; a.IsVararg = fs.Value.IsVararg; ret = DoExpr(fs.Value.Name) + " = " + DoExpr(a); } return(string.Format("{0}{1}{2}", oparens(e.ParenCount), ret, cparens(e.ParenCount))); throw new NotImplementedException(e.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 DoExpr(Expression e, bool setVar = false, int setVarLhsCount = -1, bool onlyCheckConsts = false) { if (e is AnonymousFunctionExpr) // function() ... end { } else if (e is BinOpExpr) { BinOpExpr boe = e as BinOpExpr; switch (boe.GetOperator()) { case BinaryOperator.Add: binOp("ADD", boe.Lhs, boe.Rhs); return; case BinaryOperator.Subtract: binOp("SUB", boe.Lhs, boe.Rhs); return; case BinaryOperator.Multiply: binOp("MUL", boe.Lhs, boe.Rhs); return; case BinaryOperator.Divide: binOp("DIV", boe.Lhs, boe.Rhs); return; case BinaryOperator.Power: binOp("POW", boe.Lhs, boe.Rhs); return; case BinaryOperator.Modulus: binOp("MOD", boe.Lhs, boe.Rhs); return; case BinaryOperator.Concat: binOp("CONCAT", boe.Lhs, boe.Rhs); return; case BinaryOperator.And: break; case BinaryOperator.Or: break; case BinaryOperator.LessThan: break; case BinaryOperator.LessThanOrEqualTo: break; case BinaryOperator.GreaterThan: break; case BinaryOperator.GreaterThanOrEqualTo: break; case BinaryOperator.NotEqual: break; case BinaryOperator.ShiftRight: CallExpr ce = new CallExpr(); ce.Arguments.Add(boe.Lhs); ce.Arguments.Add(boe.Rhs); ce.Base = new MemberExpr() { Base = new VariableExpression() { Var = new Variable() { Name = "bit", IsGlobal = true } }, Ident = "rshift", Indexer = ".", }; DoExpr(ce); return; case BinaryOperator.ShiftLeft: ce = new CallExpr(); ce.Arguments.Add(boe.Lhs); ce.Arguments.Add(boe.Rhs); ce.Base = new MemberExpr() { Base = new VariableExpression() { Var = new Variable() { Name = "bit", IsGlobal = true } }, Ident = "lshift", Indexer = ".", }; DoExpr(ce); return; case BinaryOperator.Xor: ce = new CallExpr(); ce.Arguments.Add(boe.Lhs); ce.Arguments.Add(boe.Rhs); ce.Base = new MemberExpr() { Base = new VariableExpression() { Var = new Variable() { Name = "bit", IsGlobal = true } }, Ident = "bxor", Indexer = ".", }; DoExpr(ce); return; case BinaryOperator.BitAnd: ce = new CallExpr(); ce.Arguments.Add(boe.Lhs); ce.Arguments.Add(boe.Rhs); ce.Base = new MemberExpr() { Base = new VariableExpression() { Var = new Variable() { Name = "bit", IsGlobal = true } }, Ident = "band", Indexer = ".", }; DoExpr(ce); return; case BinaryOperator.BitOr: ce = new CallExpr(); ce.Arguments.Add(boe.Lhs); ce.Arguments.Add(boe.Rhs); ce.Base = new MemberExpr() { Base = new VariableExpression() { Var = new Variable() { Name = "bit", IsGlobal = true } }, Ident = "bor", Indexer = ".", }; DoExpr(ce); return; case BinaryOperator.BitNot: ce = new CallExpr(); ce.Arguments.Add(boe.Lhs); ce.Arguments.Add(boe.Rhs); ce.Base = new MemberExpr() { Base = new VariableExpression() { Var = new Variable() { Name = "bit", IsGlobal = true } }, Ident = "bnot", Indexer = ".", }; DoExpr(ce); return; case BinaryOperator.NONE: default: throw new Exception("Unknown binary operator '" + boe.Op + "'"); } } else if (e is BoolExpr) { bool v = ((BoolExpr)e).Value; Instruction i = new Instruction("LOADBOOL"); i.A = block.getreg(); i.B = v ? 1 : 0; i.C = 0; emit(i); return; } else if (e is CallExpr)//&& (!(e is StringCallExpr) && !(e is TableCallExpr))) { CallExpr ce = e as CallExpr; int breg = ++block.regnum; DoExpr(ce.Base); bool isZero = false; bool isMethod = false; Expression ex = ce.Base; while (ex != null) { if (ex is IndexExpr) { ex = ((IndexExpr)ex).Index; } else if (ex is MemberExpr) { MemberExpr me = ex as MemberExpr; if (me.Indexer == ":") { isMethod = true; break; } else { break; } //ex = me.Ident; } else { break; } } foreach (Expression e2 in ce.Arguments) { DoExpr(e2); if (e2 is CallExpr || block.Chunk.Instructions[block.Chunk.Instructions.Count - 1].Opcode == Instruction.LuaOpcode.CALL) { isZero = true; Instruction i_ = block.Chunk.Instructions[block.Chunk.Instructions.Count - 1]; Debug.Assert(i_.Opcode == Instruction.LuaOpcode.CALL); i_.C = 0; } } Instruction i = new Instruction("CALL"); i.A = breg; if (isMethod) { //i.B++; i.B = isZero ? 2 : (ce.Arguments.Count > 0 ? 2 + ce.Arguments.Count : 2); } else { i.B = isZero ? 0 : (ce.Arguments.Count > 0 ? 1 + ce.Arguments.Count : 1); } i.C = setVarLhsCount == 0 || setVarLhsCount == -1 ? 1 : //(isZero ? 0 : 1) : 1 + setVarLhsCount; // (isZero ? 0 : 1 + setVarLhsCount); //i.C = setVarLhsCount == 0 || setVarLhsCount == -1 ? 1 : 1 + setVarLhsCount; emit(i); return; } else if (e is StringCallExpr) { throw new Exception(); } else if (e is TableCallExpr) { throw new Exception(); } else if (e is IndexExpr) { IndexExpr ie = e as IndexExpr; int regnum = block.regnum; DoExpr(ie.Base); DoExpr(ie.Index); Instruction i = new Instruction("GETTABLE"); i.A = regnum; i.B = regnum; i.C = block.regnum - 1;// block.getreg(); emit(i); block.regnum = regnum + 1; return; } else if (e is InlineFunctionExpression) // |<args>| -> <exprs> { InlineFunctionExpression i = e as InlineFunctionExpression; AnonymousFunctionExpr af = new AnonymousFunctionExpr(); af.Arguments = i.Arguments; af.IsVararg = i.IsVararg; af.Body = new List <Statement>() { new ReturnStatement { Arguments = i.Expressions } }; DoExpr(af); } else if (e is MemberExpr) { MemberExpr me = e as MemberExpr; if (me.Indexer == ".") { int regnum = block.regnum; DoExpr(me.Base); DoExpr(new StringExpr(me.Ident), false, -1, true); Instruction i = new Instruction("GETTABLE"); i.A = regnum; i.B = regnum; i.C = 256 + block.K[me.Ident]; //i.C = block.regnum - 1;// block.getreg(); emit(i); block.regnum = regnum + 1; return; } else if (me.Indexer == ":") { int regnum = block.regnum; DoExpr(me.Base); DoExpr(new StringExpr(me.Ident), false, -1, true); Instruction i = new Instruction("SELF"); i.A = regnum; i.B = regnum; i.C = 256 + block.K[me.Ident]; //i.C = block.regnum - 1;// block.getreg(); emit(i); block.regnum = regnum + 1; return; } else { throw new Exception("Unknown member indexer '" + me.Indexer + "'"); } } else if (e is NilExpr) { Instruction i = new Instruction("LOADNIL"); i.A = block.getreg(); i.B = setVarLhsCount == -1 ? i.A : setVarLhsCount - 1; i.C = 0; emit(i); return; } else if (e is NumberExpr) { NumberExpr ne = e as NumberExpr; // TODO: this can optimized into a Dictionary to avoid re-parsing numbers each time double r; int x = Lua.luaO_str2d(ne.Value.Replace("_", ""), out r); if (x == 0) { throw new LuaSourceException(line, 0, "Invalid number"); } if (onlyCheckConsts == false) { Instruction i = new Instruction("loadk"); i.A = block.getreg(); i.Bx = block.K[r]; emit(i); } else { block.K.Check(r); } return; } else if (e is StringExpr) { StringExpr se = e as StringExpr; string s = se.Value; if (se.StringType != TokenType.LongString) { s = Unescaper.Unescape(s); } else { int i = 1; while (s[i] != '[') { i++; } i++; s = s.Substring(i, s.Length - i - 2); } if (onlyCheckConsts == false) { Instruction i2 = new Instruction("loadk"); i2.A = block.getreg(); i2.Bx = block.K[s]; emit(i2); } else { block.K.Check(s); } return; } else if (e is TableConstructorExpr) { Instruction i = new Instruction("NEWTABLE"); int tblA = block.regnum; i.A = block.getreg(); i.B = 0; i.C = 0; emit(i); TableConstructorExpr tce = e as TableConstructorExpr; if (tce.EntryList.Count == 0) { return; } int b = 0; bool wasLastCall = false; foreach (Expression e2 in tce.EntryList) { if (e2 is TableConstructorKeyExpr) { TableConstructorKeyExpr tcke = e2 as TableConstructorKeyExpr; DoExpr(tcke.Key); DoExpr(tcke.Value); } else if (e2 is TableConstructorNamedFunctionExpr) { TableConstructorNamedFunctionExpr tcnfe = e2 as TableConstructorNamedFunctionExpr; } else if (e2 is TableConstructorStringKeyExpr) { TableConstructorStringKeyExpr tcske = e2 as TableConstructorStringKeyExpr; DoExpr(new StringExpr(tcske.Key)); DoExpr(tcske.Value); } else if (e2 is TableConstructorValueExpr) { TableConstructorValueExpr tcve = e2 as TableConstructorValueExpr; DoExpr(tcve.Value); if (tcve.Value is VarargExpr || tcve.Value is CallExpr) { wasLastCall = true; } else { wasLastCall = false; } } b++; } i.B = b; i = new Instruction("SETLIST"); i.A = tblA; if (wasLastCall) { i.B = 0; } else { i.B = block.regnum - 1; } i.C = tblA + 1; emit(i); block.regnum = tblA; return; } else if (e is UnOpExpr) { UnOpExpr uoe = e as UnOpExpr; switch (uoe.GetOperator()) { case UnaryOperator.Not: unOp("NOT", uoe.Rhs); return; case UnaryOperator.Length: unOp("LEN", uoe.Rhs); return; case UnaryOperator.BitNot: CallExpr ce = new CallExpr(); ce.Arguments.Add(uoe.Rhs); ce.Base = new MemberExpr() { Base = new VariableExpression() { Var = new Variable() { Name = "bit", IsGlobal = true } }, Ident = "bnot", Indexer = ".", }; DoExpr(ce); return; case UnaryOperator.Negate: unOp("UNM", uoe.Rhs); return; case UnaryOperator.UnNegate: ce = new CallExpr(); ce.Arguments.Add(uoe.Rhs); ce.Base = new MemberExpr() { Base = new VariableExpression() { Var = new Variable() { Name = "math", IsGlobal = true } }, Ident = "abs", Indexer = ".", }; DoExpr(ce); return; case UnaryOperator.NONE: default: throw new Exception("Unknown unary operator '" + uoe.Op + "'"); } } else if (e is VarargExpr) { if (block.Chunk.Vararg == 0) { throw new LuaSourceException(0, 0, "Cannot use varargs (...) outside of a vararg function"); } Instruction i = new Instruction("VARARG"); i.A = block.getreg(); if (setVar) { i.B = setVarLhsCount == -1 ? 0 : 1 + setVarLhsCount; } else { i.B = 0; } emit(i); return; } else if (e is VariableExpression) { VariableExpression ve = e as VariableExpression; if (ve.Var.IsGlobal == false) { // local if (setVar) { //Instruction i = new Instruction("move"); //i.B = block.V[ve.Var.Name]; // moved into here //i.A = block.getreg(); // from here //emit(i); int _TMP_001_ = block.V[ve.Var.Name]; // Should probably just add a Check method in Var2Reg block.CheckLocalName(ve.Var.Name); } else { Instruction i = new Instruction("move"); i.B = block.V[ve.Var.Name]; // moved into here i.A = block.getreg(); // from here emit(i); } } else { // global Instruction i = null; if (setVar) { i = new Instruction("setglobal"); i.A = block.regnum - 1; // ret } else { i = new Instruction("getglobal"); i.A = block.getreg(); // ret } i.Bx = block.K[ve.Var.Name]; // const emit(i); } return; } throw new NotImplementedException(e.GetType().Name + " is not implemented"); }