Mercurial > projects > dang
annotate ast/Exp.d @ 44:495188f9078e new_gen
Big update - Moving towards a better, more seperated parser
The parser no loner creates the AST directly, but through
callbacks(actions). This means the parser can be run with a different set
of actions that do something else.
The parser is not back to full strength yet, the main thing missing is the
various statements and structs.
Also added a SmallArray that uses the stack only until a given size is
exceeded, after which the array is copied unto the heap.
author | Anders Halager <halager@gmail.com> |
---|---|
date | Wed, 23 Apr 2008 00:57:45 +0200 |
parents | 69464d465284 |
children | b6c1dc30ca4b |
rev | line source |
---|---|
1 | 1 module ast.Exp; |
2 | |
3 import tango.text.Util : jhash; | |
4 | |
5 import lexer.Token; | |
6 | |
7 import sema.SymbolTable; | |
8 | |
9 enum ExpType | |
10 { | |
11 Binary, | |
12 Negate, | |
13 IntegerLit, | |
28
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
14 MemberLookup, |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
15 ArrayLookup, |
1 | 16 Identifier, |
17 AssignExp, | |
18 CallExp, | |
19 } | |
20 | |
21 class Exp | |
22 { | |
23 this(ExpType expType) | |
24 { | |
25 this.expType = expType; | |
26 } | |
27 | |
28 ExpType expType; | |
29 Scope env; | |
30 } | |
31 | |
32 class CallExp : Exp | |
33 { | |
34 this(Exp exp, Exp[] args) | |
35 { | |
36 super(ExpType.CallExp); | |
37 this.exp = exp; | |
38 this.args = args; | |
39 } | |
40 | |
41 Exp exp; | |
42 Exp[] args; | |
43 } | |
44 | |
45 class AssignExp : Exp | |
46 { | |
28
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
47 this(Exp identifier, Exp exp) |
1 | 48 { |
49 super(ExpType.AssignExp); | |
50 this.identifier = identifier; | |
51 this.exp = exp; | |
52 } | |
53 | |
28
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
54 Exp identifier; |
1 | 55 Exp exp; |
56 } | |
57 | |
58 class BinaryExp : Exp | |
59 { | |
7
2ce5209f1954
Starting to work on bool support, for now == works
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
60 public enum Operator |
1 | 61 { |
7
2ce5209f1954
Starting to work on bool support, for now == works
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
62 Eq, Ne, |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
63 |
10
2f493057cf17
Some support for the rest of the boolean operators
Anders Halager <halager@gmail.com>
parents:
7
diff
changeset
|
64 Lt, Le, |
2f493057cf17
Some support for the rest of the boolean operators
Anders Halager <halager@gmail.com>
parents:
7
diff
changeset
|
65 Gt, Ge, |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
66 |
7
2ce5209f1954
Starting to work on bool support, for now == works
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
67 Add, Sub, |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
68 Mul, Div, Mod, |
1 | 69 } |
70 | |
71 this(Operator op, Exp left, Exp right) | |
72 { | |
73 super(ExpType.Binary); | |
74 this.op = op; | |
75 this.left = left; | |
76 this.right = right; | |
77 } | |
78 | |
7
2ce5209f1954
Starting to work on bool support, for now == works
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
79 char[] resultType() |
2ce5209f1954
Starting to work on bool support, for now == works
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
80 { |
10
2f493057cf17
Some support for the rest of the boolean operators
Anders Halager <halager@gmail.com>
parents:
7
diff
changeset
|
81 if (op >= Operator.Eq && op <= Operator.Ge) |
7
2ce5209f1954
Starting to work on bool support, for now == works
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
82 return "bool"; |
2ce5209f1954
Starting to work on bool support, for now == works
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
83 return null; |
2ce5209f1954
Starting to work on bool support, for now == works
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
84 } |
2ce5209f1954
Starting to work on bool support, for now == works
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
85 |
1 | 86 Operator op; |
87 Exp left, right; | |
88 } | |
89 | |
90 class NegateExp : Exp | |
91 { | |
92 this(Exp exp) | |
93 { | |
94 super(ExpType.Negate); | |
95 this.exp = exp; | |
96 } | |
97 | |
98 public Exp exp; | |
99 } | |
100 | |
101 class IntegerLit : Exp | |
102 { | |
103 this(Token t) | |
104 { | |
105 super(ExpType.IntegerLit); | |
106 this.token = t; | |
107 } | |
108 | |
109 Token token; | |
110 } | |
111 | |
28
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
112 class MemberLookup : Exp |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
113 { |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
114 this(Exp target, Identifier child) |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
115 { |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
116 super(ExpType.MemberLookup); |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
117 this.target = target; |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
118 this.child = child; |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
119 } |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
120 |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
121 Identifier child; |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
122 Exp target; |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
123 } |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
124 |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
125 class ArrayLookup : Exp |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
126 { |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
127 this(Exp target, IntegerLit pos) |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
128 { |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
129 super(ExpType.ArrayLookup); |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
130 this.target = target; |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
131 this.pos = pos; |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
132 } |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
133 |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
134 Exp target; |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
135 IntegerLit pos; |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
136 } |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
10
diff
changeset
|
137 |
1 | 138 class Identifier : Exp |
139 { | |
140 this(Token t) | |
141 { | |
142 super(ExpType.Identifier); | |
143 this.token = t; | |
144 name = t.get; | |
145 } | |
146 | |
147 char[] get() | |
148 { | |
149 return name; | |
150 } | |
151 | |
152 hash_t toHash() | |
153 { | |
154 return jhash(name); | |
155 } | |
156 | |
157 int opCmp(Object o) | |
158 { | |
159 if (auto id = cast(Identifier)o) | |
160 return typeid(char[]).compare(&name, &id.name); | |
161 return 0; | |
162 } | |
163 | |
164 int opEquals(Object o) | |
165 { | |
166 if (auto id = cast(Identifier)o) | |
167 return typeid(char[]).equals(&name, &id.name); | |
168 return 0; | |
169 } | |
170 | |
171 Token token; | |
172 char[] name; | |
173 } | |
174 | |
175 |