KissInterp2 while

This commit is contained in:
2025-11-12 14:38:08 -06:00
parent 76a50653d7
commit fb2ffbeca7
3 changed files with 31 additions and 1 deletions

View File

@@ -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)

View File

@@ -40,5 +40,8 @@ class KissInterp2TestCase extends Test {
function testLambda() {
_testLambda();
}
function testWhile() {
_testWhile();
}
}

View File

@@ -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)")))