public
Description: Arc Lisp to C compiler
Homepage:
Clone URL: git://github.com/sacado/arc2c.git
arc2c / structs.arc
100644 121 lines (89 sloc) 2.715 kb
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
; structs.arc
; by sacado
; comments by AmkG
; Contains definitions for the abstract syntax trees used
; in CPS- and closure-conversions.
 
; a variable
(def make-var (id uid)
  (listtab `((type var) (id ,id) (uid ,uid))))
 
(def avar (x)
  (and (isa x 'table) (is x!type 'var)))
 
; a special form (not really a macro, at least not
; an Arc-style macro, since the output of the macro
; should be an AST, not a list)
(def make-macro (id expander)
  (listtab `((type macro) (id ,id) (expander ,expander))))
 
(def amacro (x)
  (and (isa x 'table) (is x!type 'macro)))
 
;----------------------------------------AST's
 
; a literal: number or t/nil
; (might be extended to chars someday)
(def make-lit (subx val)
  (listtab `((type lit) (subx ,subx) (val ,val))))
 
(def alit (x)
  (and (isa x 'table) (is x!type 'lit)))
 
; a variable reference
(def make-ref (subx var)
  (listtab `((type ref) (subx ,subx) (var ,var))))
 
(def aref (x)
  (and (isa x 'table) (is x!type 'ref)))
 
; a quoted form
; (currently only literals)
(def make-quote (subx val)
  (listtab `((type quote) (subx ,subx) (val ,val))))
 
(def aquote (x)
  (and (isa x 'table) (is x!type 'quote)))
 
; a (set var val) form
(def make-set (subx var)
  (listtab `((type set) (subx ,subx) (var ,var))))
 
(def aset (x)
  (and (isa x 'table) (is x!type 'set)))
 
; an if form
; (supports only 2 or 3-arg form but code
; is preprocessed to that form anyway)
(def make-cnd (subx)
  (listtab `((type cnd) (subx ,subx))))
 
(def acnd (x)
  (and (isa x 'table) (is x!type 'cnd)))
 
; a primitive
; (possibly make these *optimized* primitives,
; since it's technically possible, in Arc, to
; redefine most primitives; basically if a
; primitive is redefined, it's no longer
; represented using this)
(def make-prim (subx op)
  (listtab `((type prim) (subx ,subx) (op ,op))))
 
(def aprim (x)
  (and (isa x 'table) (is x!type 'prim)))
 
; a function call (a function *app*lication)
(def make-app (subx)
  (listtab `((type app) (subx ,subx))))
 
(def anapp (x)
  (and (isa x 'table) (is x!type 'app)))
 
; a function form (a *lam*bda)
(def make-lam (subx params)
  (listtab `((type lam) (subx ,subx) (params ,params))))
 
(def alam (x)
  (and (isa x 'table) (is x!type 'lam)))
 
; a do form
(def make-seq (subx)
  (listtab `((type seq) (subx ,subx))))
 
(def aseq (x)
  (and (isa x 'table) (is x!type 'seq)))
 
 
(def extend (bindings env)
  (+ bindings env))
 
(def lookup (id env)
  (if (no env)
    nil
    (let head (car env)
      (if (is head!id id)
        head
        (lookup id (cdr env))))))
 
(= seq-num* 0)
 
(def new-var (id)
  (++ seq-num*)
  (make-var id (sym (string id "@" seq-num*))))
 
(def new-global (id)
  (make-var id id))
 
(def aglobal (var)
  (is var!id var!uid))