'defined?' and 'defdynamic-once'

This commit is contained in:
Erik Svedäng 2019-12-18 12:10:47 +01:00
parent c258296623
commit 7c15ec299b
2 changed files with 15 additions and 2 deletions

View File

@ -148,8 +148,8 @@
(list
'let (array variable from)
(list
'while (list (if (< step (- step step)) '> '<) variable to)
(list
'while (list (if (< step (- step step)) '> '<) variable to)
(list
'do
(if (= (length body) 0)
()
@ -366,3 +366,9 @@ The expression must be evaluable at compile time.")
(defmacro until [cnd body]
(list 'while (list 'not cnd)
body))
(doc defdynamic-once "creates a dynamic variable and sets its value if it's not already defined.")
(defmacro defdynamic-once [var expr]
(list 'if (list 'defined? var)
()
(list 'defdynamic var expr)))

View File

@ -289,6 +289,13 @@ eval env xobj =
XObj (Sym (SymPath [] "use") _) _ _ : _ ->
return (makeEvalError ctx Nothing ("Invalid args to `use`: " ++ pretty xobj) (info xobj))
[XObj (Sym (SymPath [] "defined?") _) _ _, xobj@(XObj (Sym path _) _ _)] ->
case lookupInEnv path env of
Just found -> return (Right trueXObj)
Nothing -> return (Right falseXObj)
XObj (Sym (SymPath [] "defined?") _) _ _ : _ ->
return (makeEvalError ctx Nothing ("Invalid args to `defined?`: " ++ pretty xobj) (info xobj))
XObj With _ _ : xobj@(XObj (Sym path _) _ _) : forms ->
specialCommandWith xobj path forms
XObj With _ _ : _ ->