2014-03-30 03:35:22 +04:00
|
|
|
(types.ps) run
|
|
|
|
(reader.ps) run
|
|
|
|
|
|
|
|
% read
|
2014-04-02 06:50:24 +04:00
|
|
|
/_readline { print flush (%stdin) (r) file 99 string readline } def
|
|
|
|
|
2014-03-30 03:35:22 +04:00
|
|
|
/READ {
|
|
|
|
/str exch def
|
|
|
|
str read_str
|
|
|
|
} def
|
|
|
|
|
|
|
|
|
|
|
|
% eval
|
|
|
|
/eval_ast { 2 dict begin
|
|
|
|
/env exch def
|
|
|
|
/ast exch def
|
|
|
|
%(eval_ast: ) print ast ==
|
2014-03-31 07:39:44 +04:00
|
|
|
ast _symbol? { %if symbol
|
2014-03-30 03:35:22 +04:00
|
|
|
env ast env_get
|
2014-03-31 07:39:44 +04:00
|
|
|
}{ ast _list? { %elseif list
|
2014-03-30 03:35:22 +04:00
|
|
|
[
|
|
|
|
ast {
|
|
|
|
env EVAL
|
|
|
|
} forall
|
|
|
|
]
|
|
|
|
}{ % else
|
|
|
|
ast
|
|
|
|
} ifelse } ifelse
|
|
|
|
end } def
|
|
|
|
|
|
|
|
/EVAL { 13 dict begin
|
|
|
|
{ %loop (TCO)
|
|
|
|
|
|
|
|
/env exch def
|
|
|
|
/ast exch def
|
|
|
|
/loop? false def
|
|
|
|
|
2014-04-01 08:05:41 +04:00
|
|
|
%(EVAL: ) print ast true _pr_str print (\n) print
|
2014-03-31 07:39:44 +04:00
|
|
|
ast _list? not { %if not a list
|
2014-03-30 03:35:22 +04:00
|
|
|
ast env eval_ast
|
|
|
|
}{ %else apply the list
|
|
|
|
/a0 ast 0 get def
|
|
|
|
/def! a0 eq { %if def!
|
|
|
|
/a1 ast 1 get def
|
|
|
|
/a2 ast 2 get def
|
|
|
|
env a1 a2 env EVAL env_set
|
|
|
|
}{ /let* a0 eq { %if let*
|
|
|
|
/a1 ast 1 get def
|
|
|
|
/a2 ast 2 get def
|
|
|
|
/let_env env [ ] [ ] env_new def
|
|
|
|
0 2 a1 length 1 sub { %for each pair
|
|
|
|
/idx exch def
|
|
|
|
let_env
|
|
|
|
a1 idx get
|
|
|
|
a1 idx 1 add get let_env EVAL
|
|
|
|
env_set
|
2014-04-01 08:05:41 +04:00
|
|
|
pop % discard the return value
|
2014-03-30 03:35:22 +04:00
|
|
|
} for
|
|
|
|
a2 let_env EVAL
|
|
|
|
}{ /do a0 eq { %if do
|
2014-04-01 08:05:41 +04:00
|
|
|
ast length 2 gt { %if ast has more than 2 elements
|
|
|
|
ast 1 ast length 2 sub getinterval env eval_ast pop
|
2014-03-30 03:35:22 +04:00
|
|
|
} if
|
|
|
|
ast ast length 1 sub get % last ast becomes new ast
|
|
|
|
env
|
|
|
|
/loop? true def % loop
|
|
|
|
}{ /if a0 eq { %if if
|
|
|
|
/a1 ast 1 get def
|
|
|
|
/cond a1 env EVAL def
|
|
|
|
cond null eq cond false eq or { % if cond is nil or false
|
2014-04-01 08:05:41 +04:00
|
|
|
ast length 3 gt { %if false branch with a3
|
|
|
|
ast 3 get env
|
2014-03-30 03:35:22 +04:00
|
|
|
/loop? true def
|
2014-04-01 08:05:41 +04:00
|
|
|
}{ % else false branch with no a3
|
2014-03-30 03:35:22 +04:00
|
|
|
null
|
|
|
|
} ifelse
|
2014-04-01 08:05:41 +04:00
|
|
|
}{ % true branch
|
|
|
|
ast 2 get env
|
2014-03-30 03:35:22 +04:00
|
|
|
/loop? true def
|
|
|
|
} ifelse
|
|
|
|
}{ /fn* a0 eq { %if fn*
|
|
|
|
/a1 ast 1 get def
|
|
|
|
/a2 ast 2 get def
|
2014-04-01 08:05:41 +04:00
|
|
|
<<
|
|
|
|
/type /_maltype_function % user defined function
|
|
|
|
/params null % close over parameters
|
|
|
|
/ast null % close over ast
|
|
|
|
/env null % close over environment
|
2014-04-02 06:50:24 +04:00
|
|
|
/data { __self__ fload EVAL }
|
2014-04-01 08:05:41 +04:00
|
|
|
>>
|
|
|
|
dup length dict copy % make an actual copy/new instance
|
|
|
|
dup /params a1 put % insert closed over a1 into position 2
|
|
|
|
dup /ast a2 put % insert closed over a2 into position 3
|
|
|
|
dup /env env put % insert closed over env into position 4
|
2014-04-02 06:50:24 +04:00
|
|
|
dup dup /data get exch 0 exch put % insert self reference
|
2014-03-30 03:35:22 +04:00
|
|
|
}{
|
|
|
|
/el ast env eval_ast def
|
2014-04-02 06:50:24 +04:00
|
|
|
el _rest el _first % stack: ast function
|
|
|
|
dup _mal_function? { % if user defined function
|
|
|
|
fload % stack: ast new_env
|
2014-03-30 03:35:22 +04:00
|
|
|
/loop? true def
|
|
|
|
}{ %else (regular procedure/function)
|
2014-04-02 06:50:24 +04:00
|
|
|
exec % apply function to args
|
2014-03-30 03:35:22 +04:00
|
|
|
} ifelse
|
|
|
|
} ifelse } ifelse } ifelse } ifelse } ifelse
|
|
|
|
} ifelse
|
|
|
|
|
|
|
|
loop? not { exit } if
|
|
|
|
} loop % TCO
|
|
|
|
end } def
|
|
|
|
|
|
|
|
|
|
|
|
% print
|
|
|
|
/PRINT {
|
|
|
|
true _pr_str
|
|
|
|
} def
|
|
|
|
|
|
|
|
|
|
|
|
% repl
|
|
|
|
/repl_env null [ ] [ ] env_new def
|
|
|
|
|
|
|
|
/RE { READ repl_env EVAL } def
|
|
|
|
/REP { READ repl_env EVAL PRINT } def
|
|
|
|
/_ref { repl_env 3 1 roll env_set pop } def
|
|
|
|
|
|
|
|
types_ns { _ref } forall
|
|
|
|
|
|
|
|
(\(def! not \(fn* \(a\) \(if a false true\)\)\)) RE pop
|
|
|
|
|
|
|
|
{ % loop
|
2014-04-02 06:50:24 +04:00
|
|
|
(user> ) _readline
|
2014-03-30 03:35:22 +04:00
|
|
|
not { exit } if % exit if EOF
|
|
|
|
|
|
|
|
{ %try
|
|
|
|
REP print (\n) print
|
|
|
|
} stopped {
|
|
|
|
(Error: ) print
|
|
|
|
get_error_data false _pr_str print (\n) print
|
2014-03-31 07:39:44 +04:00
|
|
|
$error /newerror false put
|
|
|
|
$error /errorinfo null put
|
2014-03-30 03:35:22 +04:00
|
|
|
clear
|
2014-04-02 06:50:24 +04:00
|
|
|
cleardictstack
|
2014-03-30 03:35:22 +04:00
|
|
|
} if
|
|
|
|
} bind loop
|
|
|
|
|
|
|
|
(\n) print % final newline before exit for cleanliness
|
|
|
|
quit
|