Mercurial > projects > dang
annotate ast/Exp.d @ 7:2ce5209f1954
Starting to work on bool support, for now == works
author | Anders Halager <halager@gmail.com> |
---|---|
date | Fri, 18 Apr 2008 12:50:54 +0200 |
parents | 2168f4cb73f1 |
children | 2f493057cf17 |
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, | |
14 Identifier, | |
15 AssignExp, | |
16 CallExp, | |
17 } | |
18 | |
19 class Exp | |
20 { | |
21 this(ExpType expType) | |
22 { | |
23 this.expType = expType; | |
24 } | |
25 | |
26 ExpType expType; | |
27 Scope env; | |
28 } | |
29 | |
30 class CallExp : Exp | |
31 { | |
32 this(Exp exp, Exp[] args) | |
33 { | |
34 super(ExpType.CallExp); | |
35 this.exp = exp; | |
36 this.args = args; | |
37 } | |
38 | |
39 Exp exp; | |
40 Exp[] args; | |
41 } | |
42 | |
43 class AssignExp : Exp | |
44 { | |
45 this(Identifier identifier, Exp exp) | |
46 { | |
47 super(ExpType.AssignExp); | |
48 this.identifier = identifier; | |
49 this.exp = exp; | |
50 } | |
51 | |
52 Identifier identifier; | |
53 Exp exp; | |
54 } | |
55 | |
56 class BinaryExp : Exp | |
57 { | |
7
2ce5209f1954
Starting to work on bool support, for now == works
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
58 public enum Operator |
1 | 59 { |
7
2ce5209f1954
Starting to work on bool support, for now == works
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
60 Eq, Ne, |
2ce5209f1954
Starting to work on bool support, for now == works
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
61 Mul, Div, |
2ce5209f1954
Starting to work on bool support, for now == works
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
62 Add, Sub, |
1 | 63 } |
64 | |
65 this(Operator op, Exp left, Exp right) | |
66 { | |
67 super(ExpType.Binary); | |
68 this.op = op; | |
69 this.left = left; | |
70 this.right = right; | |
71 } | |
72 | |
7
2ce5209f1954
Starting to work on bool support, for now == works
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
73 char[] resultType() |
2ce5209f1954
Starting to work on bool support, for now == works
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
74 { |
2ce5209f1954
Starting to work on bool support, for now == works
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
75 if (op >= Operator.Eq && op <= Operator.Ne) |
2ce5209f1954
Starting to work on bool support, for now == works
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
76 return "bool"; |
2ce5209f1954
Starting to work on bool support, for now == works
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
77 return null; |
2ce5209f1954
Starting to work on bool support, for now == works
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
78 } |
2ce5209f1954
Starting to work on bool support, for now == works
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
79 |
1 | 80 Operator op; |
81 Exp left, right; | |
82 } | |
83 | |
84 class NegateExp : Exp | |
85 { | |
86 this(Exp exp) | |
87 { | |
88 super(ExpType.Negate); | |
89 this.exp = exp; | |
90 } | |
91 | |
92 public Exp exp; | |
93 } | |
94 | |
95 class IntegerLit : Exp | |
96 { | |
97 this(Token t) | |
98 { | |
99 super(ExpType.IntegerLit); | |
100 this.token = t; | |
101 } | |
102 | |
103 Token token; | |
104 } | |
105 | |
106 class Identifier : Exp | |
107 { | |
108 this(Token t) | |
109 { | |
110 super(ExpType.Identifier); | |
111 this.token = t; | |
112 name = t.get; | |
113 } | |
114 | |
115 char[] get() | |
116 { | |
117 return name; | |
118 } | |
119 | |
120 hash_t toHash() | |
121 { | |
122 return jhash(name); | |
123 } | |
124 | |
125 int opCmp(Object o) | |
126 { | |
127 if (auto id = cast(Identifier)o) | |
128 return typeid(char[]).compare(&name, &id.name); | |
129 return 0; | |
130 } | |
131 | |
132 int opEquals(Object o) | |
133 { | |
134 if (auto id = cast(Identifier)o) | |
135 return typeid(char[]).equals(&name, &id.name); | |
136 return 0; | |
137 } | |
138 | |
139 Token token; | |
140 char[] name; | |
141 } | |
142 | |
143 |