Example #1
0
File: VM.cs Project: takuto-h/rhea
 public void SetCont(IValue returnValue, ValueCont cont, SourceInfo info)
 {
     var winders = cont.Winders;
     if (winders == Winders)
     {
         Insns = cont.Insns;
         Stack = cont.Stack;
         Env = cont.Env;
         Push(returnValue);
     }
     else if (winders.ContainsList(Winders))
     {
         var newWinders = winders.GetPreviousList(Winders);
         IValue before = newWinders.Head.Key;
         Stack<IInsn> insnStack = new Stack<IInsn>();
         insnStack.Push(new InsnPush(before));
         insnStack.Push(new InsnCall(0, info));
         insnStack.Push(InsnPop.Instance);
         insnStack.Push(new InsnSetWinders(newWinders));
         insnStack.Push(new InsnPush(cont));
         insnStack.Push(new InsnPush(returnValue));
         insnStack.Push(new InsnCall(1, info));
         Insns = insnStack.ToSList();
         Stack = SList.Nil<IValue>();
     }
     else
     {
         IValue after = Winders.Head.Value;
         Winders = Winders.Tail;
         Stack<IInsn> insnStack = new Stack<IInsn>();
         insnStack.Push(new InsnPush(after));
         insnStack.Push(new InsnCall(0, info));
         insnStack.Push(InsnPop.Instance);
         insnStack.Push(new InsnPush(cont));
         insnStack.Push(new InsnPush(returnValue));
         insnStack.Push(new InsnCall(1, info));
         Insns = insnStack.ToSList();
         Stack = SList.Nil<IValue>();
     }
 }
Example #2
0
 private IValue Run(ISList<IInsn> insns)
 {
     VM vm = new VM(
         insns,
         SList.Nil<IValue>(),
         mEnv,
         SList.Nil<KeyValuePair<IValue, IValue>>()
     );
     while (true)
     {
         try
         {
             return vm.Run();
         }
         catch (RheaException e)
         {
             Console.WriteLine("{0}: {1}", e.Info, e.Message);
             ValueCont cont = new ValueCont(
                 SList.Nil<IInsn>(),
                 SList.Nil<IValue>(),
                 vm.Env,
                 SList.Nil<KeyValuePair<IValue, IValue>>()
             );
             vm.SetCont(null, cont, e.Info);
         }
     }
 }