KissInterp2 while
This commit is contained in:
@@ -34,6 +34,10 @@
|
||||
(let [name (symbolNameValue (first args))]
|
||||
(evalCC (second args) ->val {(dictSet scope name val) (cc val)})))
|
||||
|
||||
(function callSymbol [:String symbolName :Array<ReaderExp> args]
|
||||
(ReaderExpDef.CallExp
|
||||
(object pos null def (ReaderExpDef.Symbol symbolName)) args))
|
||||
|
||||
(method makeFunction [:Array<ReaderExp> argExps :Array<ReaderExp> bodyExps]
|
||||
(let [capture (localScopes.copy)
|
||||
argNames []
|
||||
@@ -151,6 +155,22 @@
|
||||
func (makeFunction argExps bodyExps)]
|
||||
(dictSet globals name func)
|
||||
(cc func))
|
||||
/*
|
||||
=>"for"
|
||||
(forLoop true)
|
||||
=>"doFor"
|
||||
(forLoop false)
|
||||
*/
|
||||
=>"while"
|
||||
->[args cc]
|
||||
(let [condition (first args) body (rest args)]
|
||||
(localFunction run []
|
||||
(evalCC condition
|
||||
->v
|
||||
(if v
|
||||
(evalCC (callSymbol "begin" body) ->_ (run))
|
||||
(cc null))))
|
||||
(run))
|
||||
=>"set"
|
||||
->[args cc]
|
||||
(evalCC (second args)
|
||||
|
||||
@@ -40,5 +40,8 @@ class KissInterp2TestCase extends Test {
|
||||
function testLambda() {
|
||||
_testLambda();
|
||||
}
|
||||
|
||||
function testWhile() {
|
||||
_testWhile();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -48,3 +48,10 @@
|
||||
(function _testLambda []
|
||||
(let [interp (new Interp)]
|
||||
(assertEval 6 "(let [add ->[a b] (+ a b)] (add 2 4))")))
|
||||
|
||||
(function _testWhile []
|
||||
(let [interp (new Interp)]
|
||||
(assertEval 9 "(let [c 0]
|
||||
(while (< c 9)
|
||||
(set c (+ c 1)))
|
||||
c)")))
|
||||
|
||||
Reference in New Issue
Block a user