1
1
mirror of https://github.com/kanaka/mal.git synced 2024-11-10 12:47:45 +03:00
mal/ps/step4_if_fn_do.ps

142 lines
3.7 KiB
PostScript
Raw Normal View History

2014-05-11 01:29:07 +04:00
/runlibfile where { pop }{ /runlibfile { run } def } ifelse %
(types.ps) runlibfile
(reader.ps) runlibfile
(printer.ps) runlibfile
(env.ps) runlibfile
(core.ps) runlibfile
2014-03-30 03:20:07 +04:00
% read
/_readline { print flush (%stdin) (r) file 99 string readline } def
2014-03-30 03:20:07 +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:20:07 +04:00
env ast env_get
}{ ast _sequential? { %elseif list or vector
2014-03-30 03:20:07 +04:00
[
ast /data get { %forall items
2014-03-30 03:20:07 +04:00
env EVAL
} forall
] ast _list? { _list_from_array }{ _vector_from_array } ifelse
}{ ast _hash_map? { %elseif list or vector
<<
ast /data get { %forall entries
env EVAL
} forall
>> _hash_map_from_dict
2014-03-30 03:20:07 +04:00
}{ % else
ast
} ifelse } ifelse } ifelse
2014-03-30 03:20:07 +04:00
end } def
/EVAL { 9 dict begin
2014-03-30 03:20:07 +04:00
/env exch def
/ast exch def
%(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:20:07 +04:00
ast env eval_ast
}{ %else apply the list
/a0 ast 0 _nth def
2014-03-30 03:20:07 +04:00
/def! a0 eq { %if def!
/a1 ast 1 _nth def
/a2 ast 2 _nth def
2014-03-30 03:20:07 +04:00
env a1 a2 env EVAL env_set
}{ /let* a0 eq { %if let*
/a1 ast 1 _nth def
/a2 ast 2 _nth def
/let_env env null null env_new def
0 2 a1 _count 1 sub { %for each pair
2014-03-30 03:20:07 +04:00
/idx exch def
let_env
a1 idx _nth
a1 idx 1 add _nth let_env EVAL
2014-03-30 03:20:07 +04:00
env_set
pop % discard the return value
2014-03-30 03:20:07 +04:00
} for
a2 let_env EVAL
}{ /do a0 eq { %if do
/el ast _rest env eval_ast def
el el _count 1 sub _nth % return last value
2014-03-30 03:20:07 +04:00
}{ /if a0 eq { %if if
/a1 ast 1 _nth def
2014-03-30 03:20:07 +04:00
/cond a1 env EVAL def
cond null eq cond false eq or { % if cond is nil or false
ast _count 3 gt { %if false branch with a3
ast 3 _nth env
EVAL
}{ % else false branch with no a3
2014-03-30 03:20:07 +04:00
null
} ifelse
}{ % true branch
ast 2 _nth env
EVAL
2014-03-30 03:20:07 +04:00
} ifelse
}{ /fn* a0 eq { %if fn*
/a1 ast 1 _nth def
/a2 ast 2 _nth def
a2 env a1 _mal_function
2014-03-30 03:20:07 +04:00
}{
/el ast env eval_ast def
el _rest el _first % stack: ast function
dup _mal_function? { %if user defined function
fload % stack: ast new_env
EVAL
}{ dup _function? { %else if builtin function
/data get exec
}{ %else (regular procedure/function)
(cannot apply native proc!\n) print quit
} ifelse } ifelse
2014-03-30 03:20:07 +04:00
} ifelse } ifelse } ifelse } ifelse } ifelse
} ifelse
end } def
% print
/PRINT {
true _pr_str
} def
% repl
/repl_env null null null env_new def
2014-03-30 03:20:07 +04:00
/RE { READ repl_env EVAL } def
/REP { READ repl_env EVAL PRINT } def
% core.ps: defined using postscript
/_ref { repl_env 3 1 roll env_set pop } def
core_ns { _function _ref } forall
2014-03-30 03:20:07 +04:00
% core.mal: defined using the language itself
2014-03-30 03:20:07 +04:00
(\(def! not \(fn* \(a\) \(if a false true\)\)\)) RE pop
% repl loop
{ %loop
(user> ) _readline
2014-03-30 03:20:07 +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:20:07 +04:00
clear
cleardictstack
2014-03-30 03:20:07 +04:00
} if
} bind loop
(\n) print % final newline before exit for cleanliness
quit