3 Commits

Author SHA1 Message Date
3c343b4df6 reuse callSymbol helper
Some checks failed
CI / test (push) Failing after 53s
CI / test-core (14, ubuntu-latest, 3.x, js) (push) Failing after 1m40s
CI / test-core (14, ubuntu-latest, 3.x, cpp) (push) Failing after 1m47s
CI / test-core (14, ubuntu-latest, 3.x, py) (push) Failing after 2m14s
CI / test-core (14, ubuntu-latest, 3.x, interp) (push) Failing after 2m36s
CI / test-core (14, ubuntu-latest, 3.x, nodejs) (push) Failing after 1m40s
2025-11-12 14:56:43 -06:00
7377808802 fix transformToFieldExp return behavior 2025-11-12 14:54:33 -06:00
fb2ffbeca7 KissInterp2 while 2025-11-12 14:38:08 -06:00
3 changed files with 38 additions and 7 deletions

View File

@@ -34,6 +34,10 @@
(let [name (symbolNameValue (first args))] (let [name (symbolNameValue (first args))]
(evalCC (second args) ->val {(dictSet scope name val) (cc val)}))) (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] (method makeFunction [:Array<ReaderExp> argExps :Array<ReaderExp> bodyExps]
(let [capture (localScopes.copy) (let [capture (localScopes.copy)
argNames [] argNames []
@@ -65,7 +69,7 @@
(if hasOpt (if hasOpt
null null
(throw "not enough arguments! need $argName"))))) (throw "not enough arguments! need $argName")))))
(evalCC (ReaderExpDef.CallExp (object pos null def (ReaderExpDef.Symbol "begin")) bodyExps) (evalCC (callSymbol "begin" bodyExps)
->v ->v
{ {
(set localScopes currentLocals) (set localScopes currentLocals)
@@ -110,7 +114,7 @@
(dictSet scope vName val) (dictSet scope vName val)
(nextVar) (nextVar)
})) }))
(evalCC (ReaderExpDef.CallExp (makeExp (Symbol "begin")) body) ->result { (evalCC (callSymbol "begin" body) ->result {
(localScopes.pop) (localScopes.pop)
(cc result) (cc result)
}))) })))
@@ -151,6 +155,22 @@
func (makeFunction argExps bodyExps)] func (makeFunction argExps bodyExps)]
(dictSet globals name func) (dictSet globals name func)
(cc 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" =>"set"
->[args cc] ->[args cc]
(evalCC (second args) (evalCC (second args)
@@ -272,7 +292,7 @@
(cc f) (cc f)
(return))) (return)))
// Check for field access // Check for field access
(whenLet [exp (transformToFieldExp exp)] (whenLet [exp (transformToFieldExp exp true)]
(evalCC exp cc) (evalCC exp cc)
(return)) (return))
// Check for ident aliases // Check for ident aliases
@@ -301,11 +321,12 @@
(cc outputs))) (cc outputs)))
// Check if an identifier has field access in it, return true if transform it to FieldExp // Check if an identifier has field access in it, return true if transform it to FieldExp
(method transformToFieldExp [:ReaderExp exp] (method transformToFieldExp [:ReaderExp exp &opt :Bool nullIfNot]
(case exp.def (case exp.def
((Symbol ident) ((Symbol ident)
(let [idx (ident.indexOf ".")] (let [idx (ident.indexOf ".")]
(unless (= -1 idx) (if (= -1 idx)
(return (if nullIfNot null exp))
(let [parts (ident.split ".") (let [parts (ident.split ".")
&mut newExp (makeExp (Symbol (first parts)))] &mut newExp (makeExp (Symbol (first parts)))]
(doFor part (parts.slice 1) (doFor part (parts.slice 1)
@@ -313,4 +334,4 @@
(when safe (set part (part.substr 1))) (when safe (set part (part.substr 1)))
(set newExp (makeExp (FieldExp part newExp safe))))) (set newExp (makeExp (FieldExp part newExp safe)))))
(return newExp))))) (return newExp)))))
(otherwise (return null)))) (otherwise (return (if nullIfNot null exp)))))

View File

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

View File

@@ -48,3 +48,10 @@
(function _testLambda [] (function _testLambda []
(let [interp (new Interp)] (let [interp (new Interp)]
(assertEval 6 "(let [add ->[a b] (+ a b)] (add 2 4))"))) (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)")))