3 import mal_types
as types
4 from mal_types
import (MalSym
, MalInt
, MalStr
,
5 nil
, true
, false
, _symbol
, _keywordu
,
6 MalList
, _list
, MalVector
, MalHashMap
, MalFunc
)
13 return reader
.read_str(str)
16 def eval_ast(ast
, env
):
17 if types
._symbol
_Q
(ast
):
18 assert isinstance(ast
, MalSym
)
20 elif types
._list
_Q
(ast
):
23 res
.append(EVAL(a
, env
))
25 elif types
._vector
_Q
(ast
):
28 res
.append(EVAL(a
, env
))
30 elif types
._hash
_map
_Q
(ast
):
32 for k
in ast
.dct
.keys():
33 new_dct
[k
] = EVAL(ast
.dct
[k
], env
)
34 return MalHashMap(new_dct
)
36 return ast
# primitive value, return unchanged
39 #print("EVAL %s" % printer._pr_str(ast))
40 if not types
._list
_Q
(ast
):
41 return eval_ast(ast
, env
)
44 if len(ast
) == 0: return ast
46 if isinstance(a0
, MalSym
):
52 a1
, a2
= ast
[1], ast
[2]
54 return env
.set(a1
, res
)
55 elif u
"let*" == a0sym
:
56 a1
, a2
= ast
[1], ast
[2]
58 for i
in range(0, len(a1
), 2):
59 let_env
.set(a1
[i
], EVAL(a1
[i
+1], let_env
))
60 return EVAL(a2
, let_env
)
62 el
= eval_ast(ast
.rest(), env
)
65 a1
, a2
= ast
[1], ast
[2]
67 if cond
is nil
or cond
is false
:
68 if len(ast
) > 3: return EVAL(ast
[3], env
)
73 a1
, a2
= ast
[1], ast
[2]
74 return MalFunc(None, a2
, env
, a1
, EVAL
)
76 el
= eval_ast(ast
, env
)
78 if isinstance(f
, MalFunc
):
79 return f
.apply(el
.rest())
81 raise Exception("%s is not callable" % f
)
85 return printer
._pr
_str
(exp
)
88 def entry_point(argv
):
91 return PRINT(EVAL(READ(str), env
))
93 # core.py: defined using python
94 for k
, v
in core
.ns
.items():
95 repl_env
.set(_symbol(unicode(k
)), MalFunc(v
))
97 # core.mal: defined using the language itself
98 REP("(def! not (fn* (a) (if a false true)))", repl_env
)
102 line
= mal_readline
.readline("user> ")
103 if line
== "": continue
104 print(REP(line
, repl_env
))
105 except EOFError as e
:
109 except types
.MalException
as e
:
110 print(u
"Error: %s" % printer
._pr
_str
(e
.object, False))
111 except Exception as e
:
112 print("Error: %s" % e
)
113 #print("".join(traceback.format_exception(*sys.exc_info())))
116 # _____ Define and setup target ___
120 # Just run entry_point if not RPython compilation
122 if not sys
.argv
[0].endswith('rpython'):
123 entry_point(sys
.argv
)