internal override ExitType Eval(Tokenizer tokenizer = null, WordListBuilder _ = null) { _iLoop = LoopIndices.Count; ExitType et; var iStart = Stack.Pop(); var iEnd = Stack.Pop(); if (_isQuestDo && iStart == iEnd) { return(ExitType.Okay); } LoopIndices.Push(iStart); // Break into two nearly identical cases for speed if (iEnd > iStart) { do { et = _wlDo.Eval(); if (et != ExitType.Okay) { return(et == ExitType.Leave ? ExitType.Okay : et); } if (_plusLoop) { LoopIndices[_iLoop] += Stack.Pop(); } else { ++LoopIndices[_iLoop]; } } while (LoopIndices[_iLoop] < iEnd); } else { do { et = _wlDo.Eval(); if (et != ExitType.Okay) { return(et == ExitType.Leave ? ExitType.Okay : et); } if (_plusLoop) { LoopIndices[_iLoop] += Stack.Pop(); } else { --LoopIndices[_iLoop]; } } while (LoopIndices[_iLoop] > iEnd); } LoopIndices.Pop(); return(ExitType.Okay); }
internal override ExitType Eval(Tokenizer _ = null, WordListBuilder wlb = null) { while (true) { ExitType et; if (_evalCond != null) { et = _evalCond.Eval(); if (et != ExitType.Okay) { return(et); } if (Stack.Pop() == 0) { return(ExitType.Okay); } } et = _wlBegin.Eval(); if (et != ExitType.Okay) { return(et); } if (_isUntil) { if (Stack.Pop() != 0) { return(ExitType.Okay); } } } }
internal override ExitType Eval(Tokenizer _ = null, WordListBuilder wlb = null) { if (Stack.Pop() != 0) { var retThen = _wlThen.Eval(); if (retThen != ExitType.Okay) { return(retThen); } } else { if (_wlElse != null) { var retElse = _wlElse.Eval(); if (retElse != ExitType.Okay) { return(retElse); } } } return(ExitType.Okay); }
public void TestWordListCreation() { var val1 = 0; var val2 = 0; var test1 = new Primitive(() => val1 = 5, ""); var test2 = new Primitive(() => val2 = 10, ""); var wordList = new WordList(new List <Evaluable>() { test1, test2 }); wordList.Eval(); val1.Should().Be(5); val2.Should().Be(10); }
public void TestDefinition() { using (var nf = new Session()) { var intPrim10 = new IntPrim(10); var intPrim20 = new IntPrim(20); var plus = Vocabulary.Lookup("+"); var dup = Vocabulary.Lookup("Dup"); plus.Should().NotBeNull(); dup.Should().NotBeNull(); // Should be 20 + 2 * 10 = 40. var def = new WordList("", intPrim20, intPrim10, dup, plus, plus); def.Eval(); var val = Session.Stack.Pop(); Session.Stack.Should().BeEmpty(); val.Should().Be(40, "because 20 + 2 * 10 = 40"); } }