Esempio n. 1
0
        void DoStatement(Statement s)
        {
            if (s is AssignmentStatement && !(s is AugmentedAssignmentStatement))
            {
                AssignmentStatement a = s as AssignmentStatement;
                if (a.IsLocal)
                {

                }
                else
                {

                }
            }
            else if (s is AugmentedAssignmentStatement)
                ;
            else if (s is BreakStatement)
                ;
            else if (s is CallStatement)
                ;
            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)
                ;

            else if (s is ElseIfStmt)
                ;
            else if (s is ElseStmt)
                ;

            throw new NotImplementedException(s.GetType().Name + " is not implemented");
        }
        internal string DoStatement(Statement s)
        {
            // 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 is AssignmentStatement && !(s is AugmentedAssignmentStatement))
            {
                AssignmentStatement a = s as AssignmentStatement;
                StringBuilder sb = new StringBuilder();
                if (a.IsLocal)
                {
                    sb.Append(fromToken(tok[index++], a.Scope));
                    sb.Append(" ");
                }
                for (int i2 = 0; i2 < a.Lhs.Count; i2++)
                {
                    sb.Append(DoExpr(a.Lhs[i2], s.Scope));
                    if (i2 != a.Lhs.Count - 1)
                    {
                        sb.Append(fromToken(tok[index++], a.Scope));
                        sb.Append(" ");
                    }
                }
                if (a.Rhs.Count > 0)
                {
                    sb.Append(" ");
                    sb.Append(fromToken(tok[index++], a.Scope));
                    sb.Append(" ");
                    for (int i2 = 0; i2 < a.Rhs.Count; i2++)
                    {
                        sb.Append(DoExpr(a.Rhs[i2], s.Scope));
                        if (i2 != a.Rhs.Count - 1)
                        {
                            sb.Append(fromToken(tok[index++], 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(tok[index++], a.Scope));
                    sb.Append(" ");
                }
                Expression assignment = ((BinOpExpr)a.Rhs[0]).Rhs;
                Expression tmp = ((BinOpExpr)a.Rhs[0]).Lhs;
                sb.Append(DoExpr(tmp, s.Scope));
                sb.Append(" ");
                sb.Append(fromToken(tok[index++], s.Scope));
                sb.Append(" ");
                sb.Append(DoExpr(assignment, s.Scope));
                return sb.ToString();
            }
            else if (s is BreakStatement)
            {
                // HAHAHA this is incredibly simple...
                return fromToken(tok[index++], s.Scope); // 'break'
            }
            else if (s is ContinueStatement)
            {
                return fromToken(tok[index++], s.Scope); // 'continue'
            }
            else if (s is CallStatement)
            {
                // Also incredibly simple...
                return DoExpr(((CallStatement)s).Expression, s.Scope);
            }
            else if (s is DoStatement)
            {
                DoStatement d = s as DoStatement;
                StringBuilder sb = new StringBuilder();
                sb.Append(fromToken(tok[index++], s.Scope)); // 'do'
                sb.Append(options.EOL);
                indent++;
                sb.Append(DoChunk(d.Body));
                sb.Append(nldedent());
                sb.Append(fromToken(tok[index++], s.Scope)); // end
                return sb.ToString();
            }
            else if (s is GenericForStatement)
            {
                GenericForStatement g = s as GenericForStatement;
                StringBuilder sb = new StringBuilder();
                sb.Append(fromToken(tok[index++], s.Scope)); // 'for'
                sb.Append(" ");
                for (int x = 0; x < g.VariableList.Count; x++)
                {
                    sb.Append(fromToken(tok[index++], s.Scope));
                    if (x != g.VariableList.Count - 1)
                    {
                        sb.Append(fromToken(tok[index++], s.Scope)); // ','
                        sb.Append(" ");
                    }
                }
                sb.Append(" ");
                sb.Append(fromToken(tok[index++], s.Scope)); // 'in'
                sb.Append(" ");
                for (int x = 0; x < g.Generators.Count; x++)
                {
                    //sb.Append(fromToken(tok[index++], s.Scope));
                    DoExpr(g.Generators[x], s.Scope);
                    if (x != g.VariableList.Count - 1)
                    {
                        sb.Append(fromToken(tok[index++], s.Scope)); // ','
                        sb.Append(" ");
                    }
                }
                sb.Append(" ");
                sb.Append(fromToken(tok[index++], s.Scope)); // 'do'
                sb.Append(options.EOL);
                indent++;
                sb.Append(DoChunk(g.Body));
                sb.Append(nldedent());
                sb.Append(fromToken(tok[index++], s.Scope)); // <end>
                return sb.ToString();
            }
            else if (s is NumericForStatement)
            {
                NumericForStatement n = s as NumericForStatement;
                StringBuilder sb = new StringBuilder();
                sb.Append(fromToken(tok[index++], s.Scope)); // 'for'
                sb.Append(" ");
                sb.Append(fromToken(tok[index++], s.Scope)); // <var>
                sb.Append(" ");
                sb.Append(fromToken(tok[index++], s.Scope)); // '='
                sb.Append(" ");
                sb.Append(DoExpr(n.Start, s.Scope)); // <start>
                //sb.Append(" ");
                sb.Append(fromToken(tok[index++], s.Scope)); // ','
                sb.Append(" ");
                sb.Append(DoExpr(n.End, s.Scope)); // <end>
                if (n.Step != null)
                {
                    sb.Append(fromToken(tok[index++], s.Scope)); // ','
                    sb.Append(" ");
                    sb.Append(DoExpr(n.Step, s.Scope)); // <step>
                    sb.Append(" ");
                }
                else
                    sb.Append(" ");
                sb.Append(fromToken(tok[index++], s.Scope)); // 'do'
                sb.Append(options.EOL);
                indent++;
                sb.Append(DoChunk(n.Body));
                sb.Append(nldedent());
                sb.Append(fromToken(tok[index++], s.Scope)); // <end>
                return sb.ToString();
            }
            else if (s is FunctionStatement)
            {
                FunctionStatement f = s as FunctionStatement;
                StringBuilder sb = new StringBuilder();
                
                if (f.IsLocal)
                {
                    Debug.Assert(tok[index].Data == "local");
                    sb.Append(fromToken(tok[index++], s.Scope)); // 'local'
                }

                sb.Append(fromToken(tok[index++], s.Scope)); // 'function'
                sb.Append(" ");
                sb.Append(DoExpr(f.Name, s.Scope));
                sb.Append(fromToken(tok[index++], s.Scope)); // '('
                for (int i2 = 0; i2 < f.Arguments.Count; i2++)
                {
                    sb.Append(fromToken(tok[index++], s.Scope));
                    if (i2 != f.Arguments.Count - 1)// || f.IsVararg)
                    {
                        sb.Append(fromToken(tok[index++], s.Scope));
                        sb.Append(" ");
                    }
                }
                if (f.IsVararg)
                {
                    sb.Append(fromToken(tok[index++], s.Scope));
                    sb.Append(" ");
                    sb.Append(fromToken(tok[index++], s.Scope));
                }
                sb.Append(fromToken(tok[index++], s.Scope)); // ')'
                sb.Append(options.EOL);
                indent++;
                sb.Append(DoChunk(f.Body));
                indent--;
                sb.Append(options.EOL);
                sb.Append(writeIndent());
                sb.Append(fromToken(tok[index++], 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 fromToken(tok[index++], s.Scope) + fromToken(tok[index++], s.Scope);
            }
            else if (s is IfStmt)
            {
                IfStmt ifs = s as IfStmt;
                StringBuilder sb = new StringBuilder();

                foreach (SubIfStmt clause in ifs.Clauses)
                {
                    if (clause is ElseIfStmt)
                    {
                        ElseIfStmt c = clause as ElseIfStmt;

                        sb.Append(fromToken(tok[index++], s.Scope)); // if/elseif
                        sb.Append(" ");
                        sb.Append(DoExpr(c.Condition, c.Scope));
                        sb.Append(" ");
                        sb.Append(fromToken(tok[index++], s.Scope)); // 'then'
                        sb.Append(options.EOL);
                        indent++;
                        sb.Append(DoChunk(clause.Body));
                        sb.Append(nldedent());
                    }
                    else if (clause is ElseStmt)
                    {
                        sb.Append(fromToken(tok[index++], s.Scope)); // else
                        sb.Append(options.EOL);
                        indent++;
                        sb.Append(DoChunk(clause.Body));
                        sb.Append(nldedent());
                    }
                    else
                        throw new NotImplementedException(clause.GetType().Name);
                }
                sb.Append(fromToken(tok[index++], s.Scope)); // 'end'
                return sb.ToString();
            }
            else if (s is LabelStatement)
            {
                // ::<string label>::, so no expr
                return fromToken(tok[index++], s.Scope) // '::'
                    + fromToken(tok[index++], s.Scope)  // label
                    + fromToken(tok[index++], s.Scope); //'::'
            }
            else if (s is RepeatStatement)
            {
                RepeatStatement r = s as RepeatStatement;
                StringBuilder sb = new StringBuilder();
                sb.Append(fromToken(tok[index++], s.Scope));
                sb.Append(options.EOL);
                indent++;
                sb.Append(DoChunk(r.Body));
                sb.Append(nldedent());
                sb.Append(fromToken(tok[index++], r.Scope));
                sb.Append(" ");
                sb.Append(DoExpr(r.Condition, r.Scope));
                return sb.ToString();
            }
            else if (s is ReturnStatement)
            {
                ReturnStatement rs = s as ReturnStatement;
                StringBuilder sb = new StringBuilder();
                sb.Append(fromToken(tok[index++], s.Scope)); // 'return'
                sb.Append(" ");
                for (int x = 0; x < rs.Arguments.Count; x++)
                {
                    sb.Append(DoExpr(rs.Arguments[x], s.Scope));
                    if (x != rs.Arguments.Count - 1)
                    {
                        sb.Append(fromToken(tok[index++], s.Scope)); // ','
                        sb.Append(" ");
                    }
                }
                return sb.ToString();
            }
            else if (s is UsingStatement)
            {
                UsingStatement u = s as UsingStatement;
                StringBuilder sb = new StringBuilder();
                sb.Append(fromToken(tok[index++], s.Scope)); // 'using'
                sb.Append(" ");

                AssignmentStatement a = u.Vars;
                for (int i2 = 0; i2 < a.Lhs.Count; i2++)
                {
                    sb.Append(DoExpr(a.Lhs[i2], s.Scope));
                    if (i2 != a.Lhs.Count - 1)
                    {
                        sb.Append(fromToken(tok[index++], a.Scope));
                        sb.Append(" ");
                    }
                }
                if (a.Rhs.Count > 0)
                {
                    sb.Append(" ");
                    sb.Append(fromToken(tok[index++], a.Scope)); // '='
                    sb.Append(" ");
                    for (int i2 = 0; i2 < a.Rhs.Count; i2++)
                    {
                        sb.Append(DoExpr(a.Rhs[i2], s.Scope));
                        if (i2 != a.Rhs.Count - 1)
                        {
                            sb.Append(fromToken(tok[index++], s.Scope));
                            sb.Append(" ");
                        }
                    }
                }
                sb.Append(" ");
                sb.Append(fromToken(tok[index++], s.Scope)); // 'do'
                sb.Append(options.EOL);
                indent++;
                sb.Append(DoChunk(u.Body));
                sb.Append(nldedent());
                sb.Append(fromToken(tok[index++], s.Scope)); // 'end'
                return sb.ToString();
            }
            else if (s is WhileStatement)
            {
                WhileStatement w = s as WhileStatement;
                StringBuilder sb = new StringBuilder();
                sb.Append(fromToken(tok[index++], s.Scope)); // 'while'
                sb.Append(" ");
                sb.Append(DoExpr(w.Condition, s.Scope));
                sb.Append(" ");
                sb.Append(fromToken(tok[index++], s.Scope)); // 'do'
                sb.Append(options.EOL);
                indent++;
                sb.Append(DoChunk(w.Body));
                sb.Append(nldedent());
                sb.Append(fromToken(tok[index++], s.Scope));
                return sb.ToString();
            }
            else
                throw new NotImplementedException(s.GetType().Name + " is not implemented");
        }
Esempio n. 3
0
        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;
        }
Esempio n. 4
0
        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");
        }
Esempio n. 5
0
        internal string DoStatement(Statement s)
        {
            // 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();
                    int p = 0;
                    if (a.IsLocal)
                        sb.Append(fromToken(a.ScannedTokens[p++], a.Scope));
                    for (int i = 0; i < a.Lhs.Count; i++)
                    {
                        sb.Append(DoExpr(a.Lhs[i], s.ScannedTokens, ref p, s.Scope));
                        if (i != a.Lhs.Count - 1)
                            sb.Append(fromToken(a.ScannedTokens[p++], a.Scope));
                    }
                    if (a.Rhs.Count > 0)
                    {
                        sb.Append(fromToken(a.ScannedTokens[p++], a.Scope));
                        for (int i = 0; i < a.Rhs.Count; i++)
                        {
                            sb.Append(DoExpr(a.Rhs[i], s.ScannedTokens, ref p, s.Scope));
                            if (i != a.Rhs.Count - 1)
                                sb.Append(fromToken(a.ScannedTokens[p++], s.Scope));
                        }
                    }

                    return sb.ToString();
                }
                else if (s is AugmentedAssignmentStatement)
                {
                    AugmentedAssignmentStatement a = s as AugmentedAssignmentStatement;
                    StringBuilder sb = new StringBuilder();
                    //sb.Append(DoExpr(a.Lhs[0]));
                    int p = 0;
                    if (a.IsLocal)
                        sb.Append(fromToken(a.ScannedTokens[p++], a.Scope));
                    Expression assignment = ((BinOpExpr)a.Rhs[0]).Rhs;
                    Expression tmp = ((BinOpExpr)a.Rhs[0]).Lhs;
                    sb.Append(DoExpr(tmp, s.ScannedTokens, ref p, s.Scope));
                    sb.Append(fromToken(s.ScannedTokens[p++], s.Scope));
                    sb.Append(DoExpr(assignment, s.ScannedTokens, ref p, 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...
                    int p = 0;
                    return DoExpr(((CallStatement)s).Expression, s.ScannedTokens, ref p, 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(DoChunk(d.Body));
                    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();
                    int i = 0;
                    sb.Append(fromToken(g.ScannedTokens[i++], s.Scope)); // 'for'
                    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(fromToken(g.ScannedTokens[i++], s.Scope)); // 'in'
                    for (int x = 0; x < g.Generators.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(fromToken(g.ScannedTokens[i++], s.Scope)); // 'do'
                    sb.Append(DoChunk(g.Body));
                    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();
                    int i = 0;
                    sb.Append(fromToken(n.ScannedTokens[i++], s.Scope)); // 'for'
                    sb.Append(fromToken(n.ScannedTokens[i++], s.Scope)); // <var>
                    sb.Append(fromToken(n.ScannedTokens[i++], s.Scope)); // '='
                    sb.Append(fromToken(n.ScannedTokens[i++], s.Scope)); // <start>
                    sb.Append(fromToken(n.ScannedTokens[i++], s.Scope)); // ','
                    sb.Append(fromToken(n.ScannedTokens[i++], s.Scope)); // <end>
                    if (n.Step != null)
                    {
                        sb.Append(fromToken(n.ScannedTokens[i++], s.Scope)); // ','
                        sb.Append(fromToken(n.ScannedTokens[i++], s.Scope)); // <step>
                    }
                    sb.Append(fromToken(n.ScannedTokens[i++], s.Scope)); // 'do'
                    sb.Append(DoChunk(n.Body));
                    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();

                    int i = 0;
                    if (f.IsLocal)
                        sb.Append(fromToken(s.ScannedTokens[i++], s.Scope)); // 'local'
                    sb.Append(fromToken(s.ScannedTokens[i++], s.Scope)); // 'function'
                    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));
                    }
                    if (f.IsVararg)
                        sb.Append(fromToken(s.ScannedTokens[i++], s.Scope));
                    sb.Append(fromToken(s.ScannedTokens[i++], s.Scope)); // ')'

                    sb.Append(DoChunk(f.Body));
                    sb.Append(fromToken(s.ScannedTokens[s.ScannedTokens.Count - 1], s.Scope)); // <end>

                    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 i = s as IfStmt;
                    StringBuilder sb = new StringBuilder();

                    foreach (SubIfStmt clause in i.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(DoExpr(c.Condition, c.ScannedTokens, ref i3, c.Scope));
                            sb.Append(fromToken(c.ScannedTokens[i3++], s.Scope)); // 'then'
                            sb.Append(DoChunk(clause.Body));
                        }
                        else if (clause is ElseStmt)
                        {
                            sb.Append(fromToken(clause.ScannedTokens[i3++], s.Scope)); // if/elseif
                            sb.Append(DoChunk(clause.Body));
                        }
                        else
                            throw new NotImplementedException(clause.GetType().Name);
                    }
                    sb.Append(fromToken(s.ScannedTokens[s.ScannedTokens.Count - 1], s.Scope));
                    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));
                    sb.Append(DoChunk(r.Body));
                    int i = -1;
                    for (int k = r.ScannedTokens.Count - 1; k > 0; k--)
                        if (r.ScannedTokens[k].Type == TokenType.Keyword && r.ScannedTokens[k].Data == "until")
                        {
                            i = k;
                            break;
                        }
                    sb.Append(fromToken(r.ScannedTokens[i++], r.Scope));
                    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();
                    int i = 0;
                    sb.Append(fromToken(s.ScannedTokens[i++], s.Scope)); // 'return'
                    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)); // ','
                    }
                    return sb.ToString();
                }
                else if (s is UsingStatement)
                {
                    UsingStatement u = s as UsingStatement;
                    StringBuilder sb = new StringBuilder();
                    int i = 0;
                    sb.Append(fromToken(s.ScannedTokens[i++], s.Scope)); // 'using'

                    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));
                    }
                    if (a.Rhs.Count > 0)
                    {
                        sb.Append(fromToken(u.ScannedTokens[i++], a.Scope));
                        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(fromToken(s.ScannedTokens[i++], s.Scope)); // 'do'
                    sb.Append(DoChunk(u.Body));
                    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();
                    int i = 0;
                    sb.Append(fromToken(s.ScannedTokens[i++], s.Scope)); // 'while'
                    sb.Append(DoExpr(w.Condition, w.ScannedTokens, ref i, s.Scope));
                    sb.Append(fromToken(s.ScannedTokens[i++], s.Scope)); // 'do'

                    sb.Append(DoChunk(w.Body));
                    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");
        }
Esempio n. 6
0
 internal string DoStatement(Statement s)
 {
     int x = 0;
     string str = DoStatement(s, ref x);
     return str;
 }
Esempio n. 7
0
 static void dump2(Statement s)
 {
     Console.WriteLine(s.GetType().Name);// + " " + s.HasSemicolon);
     foreach (Token x in s.ScannedTokens)
         Console.WriteLine("    " + x.Print());
 }
Esempio n. 8
0
        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 ContinueStatement)
                return "continue";
            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(DoChunk(f.Body));
                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");
        }