about summary refs log tree commit diff stats
path: root/mu.arc
blob: b73afa5c55c739a5e1827ca78ff35ba380cfb5d9 (plain) (blame)
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
; things that a future assembler will need separate memory for:
;   code; types; args channel
(def clear ()
  (= types* (obj
              integer (obj size 1)
              address (obj size 1)))
  (= memory* (table))
  (= function* (table)))
(clear)

(def add-fns (fns)
  (each (name . body) fns
    (= function*.name body)))

(def run (instrs (o fn-args) (o returned))
  (each instr instrs
    (unless returned
;?       (prn instr)
;?       (prn memory*)
      (let delim (or (pos '<- instr) -1)
        (with (oarg  (if (>= delim 0)
                       (cut instr 0 delim))
               op  (instr (+ delim 1))
               arg  (cut instr (+ delim 2)))
;?           (prn op " " oarg)
          (case op
            loadi
              (= (memory* oarg.0.1) arg.0)
            add
              (= (memory* oarg.0.1)
                 (+ (memory* arg.0.1) (memory* arg.1.1)))
            sub
              (= (memory* oarg.0.1)
                 (- (memory* arg.0.1) (memory* arg.1.1)))
            mul
              (= (memory* oarg.0.1)
                 (* (memory* arg.0.1) (memory* arg.1.1)))
            div
              (= (memory* oarg.0.1)
                 (/ (real (memory* arg.0.1)) (memory* arg.1.1)))
            idiv
              (= (memory* oarg.0.1)
                 (trunc:/ (memory* arg.0.1) (memory* arg.1.1))
                 (memory* oarg.1.1)
                 (mod (memory* arg.0.1) (memory* arg.1.1)))
            read
              (= (memory* oarg.0.1)
                 ; hardcoded channel for now
                 (memory* pop.fn-args.1))
            reply
              (= returned (annotate 'result arg))
            ; else user-defined function
              (let results (run function*.op arg)
;?                 (prn "== " memory*)
                (each o oarg
;?                   (prn o)
                  (= (memory* o.1) (memory* pop.results.1))))
            )))))
;?   (prn "return")
    rep.returned)

(awhen cdr.argv
  (each file it
;?     (prn file)
    (add-fns readfile.file))
;?   (prn function*)
  (run function*!main)
  (prn memory*))