comparison ast/Exp.d @ 1:2168f4cb73f1

First push
author johnsen@johnsen-desktop
date Fri, 18 Apr 2008 02:01:38 +0200
parents
children 2ce5209f1954
comparison
equal deleted inserted replaced
-1:000000000000 1:2168f4cb73f1
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 {
58 public enum Operator : char
59 {
60 Mul = '*', Div = '/',
61 Add = '+', Sub = '-'
62 }
63
64 this(Operator op, Exp left, Exp right)
65 {
66 super(ExpType.Binary);
67 this.op = op;
68 this.left = left;
69 this.right = right;
70 }
71
72 Operator op;
73 Exp left, right;
74 }
75
76 class NegateExp : Exp
77 {
78 this(Exp exp)
79 {
80 super(ExpType.Negate);
81 this.exp = exp;
82 }
83
84 public Exp exp;
85 }
86
87 class IntegerLit : Exp
88 {
89 this(Token t)
90 {
91 super(ExpType.IntegerLit);
92 this.token = t;
93 }
94
95 Token token;
96 }
97
98 class Identifier : Exp
99 {
100 this(Token t)
101 {
102 super(ExpType.Identifier);
103 this.token = t;
104 name = t.get;
105 }
106
107 char[] get()
108 {
109 return name;
110 }
111
112 hash_t toHash()
113 {
114 return jhash(name);
115 }
116
117 int opCmp(Object o)
118 {
119 if (auto id = cast(Identifier)o)
120 return typeid(char[]).compare(&name, &id.name);
121 return 0;
122 }
123
124 int opEquals(Object o)
125 {
126 if (auto id = cast(Identifier)o)
127 return typeid(char[]).equals(&name, &id.name);
128 return 0;
129 }
130
131 Token token;
132 char[] name;
133 }
134
135