1 /runlibfile where
{ pop }{ /runlibfile
{ run
} def
} ifelse %
4 (printer.ps
) runlibfile
9 /_readline
{ print flush
(%stdin) (r) file 1024 string readline } def
18 /eval_ast
{ 2 dict begin
21 %(eval_ast: ) print ast ==
22 ast _symbol?
{ %if symbol
24 }{ ast _sequential?
{ %elseif list or vector
26 ast
/data get
{ %forall items
29 ] ast _list?
{ _list_from_array
}{ _vector_from_array
} ifelse
30 }{ ast _hash_map?
{ %elseif list or vector
32 ast
/data get
{ %forall entries
35 >> _hash_map_from_dict
38 } ifelse } ifelse } ifelse
48 %(EVAL: ) print ast true _pr_str print (\n) print
49 ast _list? not
{ %if not a list
51 }{ %else apply the list
53 /def
! a0 eq
{ %if def!
56 env a1 a2 env EVAL env_set
57 }{ /let
* a0 eq
{ %if let*
60 /let_env env null null env_new def
61 0 2 a1 _count
1 sub { %for each pair
65 a1 idx
1 add _nth let_env EVAL
67 pop % discard the return value
71 /loop?
true def
% loop
73 ast _count
2 gt
{ %if ast has more than 2 elements
74 ast
1 ast _count
2 sub _slice env eval_ast
pop
76 ast ast _count
1 sub _nth
% last ast becomes new ast
78 /loop?
true def
% loop
82 cond null eq cond
false eq or
{ % if cond is nil or false
83 ast _count
3 gt
{ %if false branch with a3
86 }{ % else false branch with no a3
93 }{ /fn
* a0 eq
{ %if fn*
96 a2 env a1 _mal_function
98 /el ast env eval_ast def
99 el _rest el _first
% stack: ast function
100 dup _mal_function?
{ %if user defined function
101 fload
% stack: ast new_env
103 }{ dup _function?
{ %else if builtin function
105 }{ %else (regular procedure/function)
106 (cannot apply native proc
!\n) print
quit
108 } ifelse } ifelse } ifelse } ifelse } ifelse
111 loop? not
{ exit } if
123 /repl_env null null null env_new def
125 /RE
{ READ repl_env EVAL
} def
126 /REP
{ READ repl_env EVAL PRINT
} def
128 % core.ps: defined using postscript
129 /_ref
{ repl_env
3 1 roll env_set
pop } def
130 core_ns
{ _function _ref
} forall
131 (eval
) { 0 _nth repl_env EVAL
} _function _ref
132 (*ARGV
*) [ ] _list_from_array _ref
134 % core.mal: defined using the language itself
135 (\
(def
! not \
(fn
* \
(a\
) \
(if a
false true\
)\
)\
)) RE
pop
136 (\
(def
! load
-file \
(fn
* \
(f\
) \
(eval \
(read
-string \
(str
"\(do " \
(slurp f\
) "\)"\
)\
)\
)\
)\
)) RE
pop
138 userdict
/ARGUMENTS known
{ %if command line arguments
139 ARGUMENTS length
0 gt
{ %if more than 0 arguments
140 (*ARGV
*) ARGUMENTS
1 ARGUMENTS length
1 sub getinterval
141 _list_from_array _ref
143 (\
(load
-file
") exch ("\
)) concatenate concatenate RE
pop
151 not
{ exit } if % exit if EOF
157 get_error_data
false _pr_str print
(\n) print
158 $error
/newerror
false put
159 $error
/errorinfo null put
165 (\n) print
% final newline before exit for cleanliness