Compare commits
3 Commits
76a50653d7
...
3c343b4df6
| Author | SHA1 | Date | |
|---|---|---|---|
| 3c343b4df6 | |||
| 7377808802 | |||
| fb2ffbeca7 |
@@ -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)))))
|
||||||
@@ -40,5 +40,8 @@ class KissInterp2TestCase extends Test {
|
|||||||
function testLambda() {
|
function testLambda() {
|
||||||
_testLambda();
|
_testLambda();
|
||||||
}
|
}
|
||||||
|
function testWhile() {
|
||||||
|
_testWhile();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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)")))
|
||||||
|
|||||||
Reference in New Issue
Block a user