1 /runlibfile where
{ pop }{ /runlibfile
{ run
} def
} ifelse %
4 (printer.ps
) runlibfile
9 /_readline
{ print flush
(%stdin) (r) file 1024 string readline } def
18 % is_pair?: ast -> is_pair? -> bool
19 % return true if non-empty list, otherwise false
21 dup _sequential?
{ _count
0 gt
}{ pop false } ifelse
24 % ast -> quasiquote -> new_ast
25 /quasiquote
{ 3 dict begin
27 ast is_pair? not
{ %if not is_pair?
31 a0
/unquote eq
{ %if a0 unquote symbol
33 }{ a0 is_pair?
{ %elseif a0 is_pair?
35 a00
/splice
-unquote eq
{ %if splice-unquote
36 /concat a0
1 _nth ast _rest quasiquote
3 _list
37 }{ %else not splice-unquote
38 /cons a0 quasiquote ast _rest quasiquote
3 _list
40 }{ % else not a0 is_pair?
41 /cons a0 quasiquote ast _rest quasiquote
3 _list
46 /is_macro_call?
{ 3 dict begin
51 a0 _symbol?
{ %if a0 is symbol
52 env a0 env_find null ne
{ %if a0 is in env
53 env a0 env_get _mal_function?
{ %if user defined function
54 env a0 env_get
/macro? get
true eq
%if marked as macro
61 /macroexpand
{ 3 dict begin
65 ast env is_macro_call?
{
66 /mac env ast
0 _nth env_get def
67 /ast ast _rest mac fload EVAL def
75 /eval_ast
{ 2 dict begin
78 %(eval_ast: ) print ast ==
79 ast _symbol?
{ %if symbol
81 }{ ast _sequential?
{ %elseif list or vector
83 ast
/data get
{ %forall items
86 ] ast _list?
{ _list_from_array
}{ _vector_from_array
} ifelse
87 }{ ast _hash_map?
{ %elseif list or vector
89 ast
/data get
{ %forall entries
92 >> _hash_map_from_dict
95 } ifelse } ifelse } ifelse
105 %(EVAL: ) print ast true _pr_str print (\n) print
106 ast _list? not
{ %if not a list
108 }{ %else apply the list
109 /ast ast env macroexpand def
110 ast _list? not
{ %if no longer a list
112 }{ %else still a list
116 }{ /def
! a0 eq
{ %if def!
119 env a1 a2 env EVAL env_set
120 }{ /let
* a0 eq
{ %if let*
123 /let_env env null null env_new def
124 0 2 a1 _count
1 sub { %for each pair
128 a1 idx
1 add _nth let_env EVAL
130 pop % discard the return value
134 /loop?
true def
% loop
135 }{ /quote a0 eq
{ %if quote
137 }{ /quasiquote a0 eq
{ %if quasiquote
138 ast
1 _nth quasiquote
140 /loop?
true def
% loop
141 }{ /defmacro
! a0 eq
{ %if defmacro!
145 dup /macro?
true put
% set macro flag
146 env
exch a1
exch env_set
% def! it
147 }{ /macroexpand a0 eq
{ %if defmacro!
148 ast
1 _nth env macroexpand
149 }{ /do a0 eq
{ %if do
150 ast _count
2 gt
{ %if ast has more than 2 elements
151 ast
1 ast _count
2 sub _slice env eval_ast
pop
153 ast ast _count
1 sub _nth
% last ast becomes new ast
155 /loop?
true def
% loop
156 }{ /try
* a0 eq
{ %if try*
158 countdictstack
/dictcnt
exch def
159 count /stackcnt
exch def
162 % clean up the dictionary stack
163 1 1 countdictstack dictcnt
sub { %foreach added dict
164 %(popping dict\n) print
165 pop end
% pop idx and pop dict
166 %(new ast: ) print ast true _pr_str print (\n) print
168 % clean up the operand stack
169 count 1 exch 1 exch stackcnt
sub { %foreach added operand
170 %(op stack: ) print pstack
171 pop pop % pop idx and operand
172 %(popped op stack\n) print pstack
174 % get error data and reset $error dict
175 /errdata get_error_data def
176 $error
/newerror
false put
177 $error
/errorinfo null put
179 ast _count
3 lt
{ %if no third (catch*) form
182 ast
2 _nth
0 _nth
(catch
*) eq not
{ %if third form not catch*
183 (No catch
* in throw form
) _throw
187 ast
2 _nth
1 _nth
1 _list
192 }{ /if a0 eq
{ %if if
194 /cond a1 env EVAL def
195 cond null eq cond
false eq or
{ % if cond is nil or false
196 ast _count
3 gt
{ %if false branch with a3
199 }{ % else false branch with no a3
206 }{ /fn
* a0 eq
{ %if fn*
209 a2 env a1 _mal_function
211 /el ast env eval_ast def
212 el _rest el _first
% stack: ast function
213 dup _mal_function?
{ %if user defined function
214 fload
% stack: ast new_env
216 }{ dup _function?
{ %else if builtin function
218 }{ %else (regular procedure/function)
219 (cannot apply native proc
!\n) print
quit
221 } ifelse } ifelse } ifelse } ifelse } ifelse } ifelse } ifelse } ifelse } ifelse } ifelse } ifelse
225 loop? not
{ exit } if
237 /repl_env null null null env_new def
239 /RE
{ READ repl_env EVAL
} def
240 /REP
{ READ repl_env EVAL PRINT
} def
242 % core.ps: defined using postscript
243 /_ref
{ repl_env
3 1 roll env_set
pop } def
244 core_ns
{ _function _ref
} forall
245 (eval
) { 0 _nth repl_env EVAL
} _function _ref
246 (*ARGV
*) [ ] _list_from_array _ref
248 % core.mal: defined using the language itself
249 (\
(def
! not \
(fn
* \
(a\
) \
(if a
false true\
)\
)\
)) RE
pop
250 (\
(def
! load
-file \
(fn
* \
(f\
) \
(eval \
(read
-string \
(str
"\(do " \
(slurp f\
) "\)"\
)\
)\
)\
)\
)) RE
pop
251 (\
(defmacro
! cond \
(fn
* \
(& xs\
) \
(if \
(> \
(count xs\
) 0\
) \
(list
'if \(first xs\) \(if \(> \(count xs\) 1\) \(nth xs 1\) \(throw "odd number of forms to cond"\)\) \(cons 'cond \
(rest \
(rest xs\
)\
)\
)\
)\
)\
)\
)) RE
pop
252 (\
(defmacro
! or \
(fn
* \
(& xs\
) \
(if \
(empty? xs\
) nil \
(if \
(= 1 \
(count xs\
)\
) \
(first xs\
) `\
(let
* \
(or_FIXME ~\
(first xs\
)\
) \
(if or_FIXME or_FIXME \
(or ~@\
(rest xs\
)\
)\
)\
)\
)\
)\
)\
)) RE
pop
254 userdict
/ARGUMENTS known
{ %if command line arguments
255 ARGUMENTS length
0 gt
{ %if more than 0 arguments
256 (*ARGV
*) ARGUMENTS
1 ARGUMENTS length
1 sub getinterval
257 _list_from_array _ref
259 (\
(load
-file
") exch ("\
)) concatenate concatenate RE
pop
267 not
{ exit } if % exit if EOF
273 get_error_data
false _pr_str print
(\n) print
274 $error
/newerror
false put
275 $error
/errorinfo null put
281 (\n) print
% final newline before exit for cleanliness