aboutsummaryrefslogtreecommitdiff
path: root/ps/step2_eval.ps
blob: b353f8eab2c9dc64f438e74a449424369fa1e2c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
(types.ps) run
(reader.ps) run

% read
/_readline { print flush (%stdin) (r) file 99 string readline } def

/READ {
    /str exch def
    str read_str
} def


% eval
/eval_ast { 2 dict begin
    /env exch def
    /ast exch def
    %(eval_ast: ) print ast ==
    ast _symbol? { %if symbol
        env ast known {
            env ast get
        }{
            (') ast pr_str (' not found)
            concatenate concatenate throw
        } ifelse
    }{ ast _list? { %elseif list
        [
            ast {
                env EVAL
            } forall
        ]
    }{ % else
        ast
    } ifelse } ifelse
end } def

/EVAL { 3 dict begin
    /env exch def
    /ast exch def
    %(EVAL: ) print ast ==
    ast _list? not { %if not a list
        ast env eval_ast
    }{ %else apply the list
        /el ast env eval_ast def
        el _rest el _first % stack: ast function
        exec % apply function to args
    } ifelse
end } def


% print
/PRINT {
    true _pr_str
} def


% repl
/repl_env <<
    (+) { dup 0 get exch 1 get add }
    (-) { dup 0 get exch 1 get sub }
    (*) { dup 0 get exch 1 get mul }
    (/) { dup 0 get exch 1 get idiv }
>> def

/REP { READ repl_env EVAL PRINT } def

{ % loop
    (user> ) _readline
    not { exit } if  % exit if EOF

    { %try
        REP print (\n) print
    } stopped {
        (Error: ) print
        get_error_data false _pr_str print (\n) print
        $error /newerror false put
        $error /errorinfo null put
        clear
        cleardictstack
    } if
} bind loop

(\n) print  % final newline before exit for cleanliness
quit