Exemplo n.º 1
0
        static LuaArguments EvalWhile(WhileStat stat, LuaContext Context, out LuaReturnStatus returned)
        {
            returned.returned = false;
            returned.broke    = false;
            LuaObject  cond = EvalExpression(stat.Condition, Context)[0];
            LuaContext ctx  = new LuaContext(Context);

            while (cond.AsBool())
            {
                LuaArguments obj = EvalBlock(stat.Block, ctx, out returned);
                if (returned.broke)
                {
                    break;
                }
                if (returned.returned)
                {
                    return(obj);
                }
                else
                {
                    cond = EvalExpression(stat.Condition, Context)[0];
                }
            }
            return(Lua.Return());
        }
Exemplo n.º 2
0
        public async Task ExecuteWhileStat(WhileStat whileStat, LuaState state, CancellationToken token)
        {
            var whileState = state.WithNewContext();

            while ((await _engine.EvaluateExpression(whileStat.Condition, state, token).FirstAsync()).AsBool())
            {
                await _engine.ExecuteStatement(whileStat.Block, whileState, token);
            }
        }
Exemplo n.º 3
0
 public bool Match(XMLLuaSearchWhileStatement req, WhileStat real)
 {
     Logger.Debug($"while_statement");
     if (Match(req.Block, real.Block) && Match(req.Condition, real.Condition))
     {
         SetSelectionIfSelected(real.Span, req);
         return(true);
     }
     return(false);
 }
Exemplo n.º 4
0
        private static void CgWhileStat(FuncInfo fi, WhileStat node)
        {
            var pcBeforeExp = fi.PC();

            var r = fi.AllocReg();

            CgExp(fi, node.Exp, r, 1);
            fi.FreeReg();

            fi.EmitTest(r, 0);
            var pcJmpToEnd = fi.EmitJmp(0, 0);

            fi.EnterScope(true);
            CgBlock(fi, node.Block);
            fi.CloseOpenUpvals();
            fi.EmitJmp(0, pcBeforeExp - fi.PC() - 1);
            fi.ExitScope();

            fi.FixsBx(pcJmpToEnd, fi.PC() - pcJmpToEnd);
        }
    }         // NT_IfStat

    private static void NT_WhileStat(out Stat s)
    {
        Expr   e    = null;
        Stat   body = null;
        SrcPos sp   = null;

        s = null;

        for (;;)
        {
            switch (Syn.Interpret())
            {
            case 0:
                return;

            case 1: // SEM
                sp = new SrcPos();

                break;

            case 2:
                NT_Expr(out e);
                break;

            case 3: // SEM
                loopLevel++;

                break;

            case 4:
                NT_Stat(out body);
                break;

            case 5: // SEM
                s = new WhileStat(sp, e, body);
                loopLevel--;

                break;
            } // switch
        }     // for
    }         // NT_WhileStat
Exemplo n.º 6
0
    } // DumpSymTab

    // === generate source text from symbol table and AST ===

    public static void WriteStat(Stat stat)
    {
        switch (stat.kind)
        {
        case Stat.Kind.emptyStatKind:
            genMcpp.WriteLine(Indent() + ";");
            break;

        case Stat.Kind.blockStatKind:
            BlockStat b_s = (BlockStat)stat;
            genMcpp.WriteLine(Indent() + "{");
            IncIndent();
            WriteStatList(b_s.statList);
            DecIndent();
            genMcpp.WriteLine(Indent() + "}");
            break;

        case Stat.Kind.incStatKind:
            IncStat i_s = (IncStat)stat;
            genMcpp.WriteLine(Indent() + i_s.vo.sy + "++;");
            break;

        case Stat.Kind.decStatKind:
            DecStat d_s = (DecStat)stat;
            genMcpp.WriteLine(Indent() + d_s.vo.sy + "--;");
            break;

        case Stat.Kind.assignStatKind:
            AssignStat a_s = (AssignStat)stat;
            genMcpp.WriteLine(Indent() + a_s.lhs + " = " + a_s.rhs + ";");
            break;

        case Stat.Kind.callStatKind:
            CallStat c_s = (CallStat)stat;
            genMcpp.WriteLine(Indent() + c_s.func + "(" + c_s.apl + ");");
            break;

        case Stat.Kind.ifStatKind:
            IfStat if_s = (IfStat)stat;
            genMcpp.WriteLine(Indent() + "if (" + if_s.cond + ")");
            IncIndent();
            WriteStatList(if_s.thenStat);
            DecIndent();
            if (if_s.elseStat != null)
            {
                genMcpp.WriteLine(Indent() + "else ");
                IncIndent();
                WriteStatList(if_s.elseStat);
                DecIndent();
            } // if
            break;

        case Stat.Kind.whileStatKind:
            WhileStat w_s = (WhileStat)stat;
            genMcpp.WriteLine(Indent() + "while (" + w_s.cond + ")");
            IncIndent();
            WriteStatList(w_s.body);
            DecIndent();
            break;

        case Stat.Kind.breakStatKind:
            genMcpp.WriteLine(Indent() + "break;");
            break;

        case Stat.Kind.inputStatKind:
            InputStat in_s = (InputStat)stat;
            genMcpp.WriteLine(Indent() + "cin >> " + in_s.vo.sy + ";");
            break;

        case Stat.Kind.outputStatKind:
            OutputStat out_s = (OutputStat)stat;
            genMcpp.Write(Indent() + "cout");
            foreach (Object o in out_s.values)
            {
                genMcpp.Write(" << ");
                if (o is Expr)
                {
                    genMcpp.Write(o);
                }
                else if (o is String)
                {
                    String s = o as String;
                    if (s == "\n")
                    {
                        genMcpp.Write("endl");
                    }
                    else
                    {
                        genMcpp.Write('"' + s + '"');
                    }
                }
                else
                {
                    throw new Exception("invalid value");
                }
            } // foreach
            genMcpp.WriteLine(";");
            break;

        case Stat.Kind.deleteStatKind:
            DeleteStat del_s = (DeleteStat)stat;
            genMcpp.WriteLine(Indent() + "delete[] " +
                              NameList.NameOf(del_s.vo.sy.spix) + ";");
            break;

        case Stat.Kind.returnStatKind:
            ReturnStat r_s = (ReturnStat)stat;
            genMcpp.Write(Indent() + "return");
            if (r_s.e != null)
            {
                genMcpp.Write(" " + r_s.e);
            }
            genMcpp.WriteLine(";");
            break;

        default:
            throw new Exception("invalid statement kind");
        } // switch
    }     // WriteStatList