1 //  Toy interpreter AST
2 //
3 // Copyright (C) 2014-2015 Iain Buclaw.
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 
14 // You should have received a copy of the GNU General Public License
15 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 
17 // Written by Iain  Buclaw <ibuclaw@gdcproject.org>
18 
19 module toy.ast;
20 
21 import toy.backend;
22 import toy.diag;
23 
24 import std.conv;
25 
26 ///// AST /////
27 
28 class TokenClass
29 {
30     string value;
31 
32     this(string value)
33     {
34         this.value = value;
35     }
36 }
37 
38 class Keyword
39 {
40     string value;
41 
42     this(string value)
43     {
44         this.value = value;
45     }
46 }
47 
48 /// Statements ///
49 
50 class Statement
51 {
52     void compile(Backend)
53     {
54         throw new InternalError("compile() not implemented for Statement class: " ~ this.toString());
55     }
56 }
57 
58 class AssignStatement : Statement
59 {
60     Expression name, value;
61 
62     this(Expression name, Expression value)
63     {
64         this.name = name;
65         this.value = value;
66     }
67 
68     override void compile(Backend b)
69     {
70         b.compile(this);
71     }
72 }
73 
74 class CompoundStatement : Statement
75 {
76     Statement s1, s2;
77 
78     this(Statement s1, Statement s2)
79     {
80         this.s1 = s1;
81         this.s2 = s2;
82     }
83 
84     override void compile(Backend b)
85     {
86         b.compile(this);
87     }
88 }
89 
90 class IfStatement : Statement
91 {
92     Expression condition;
93     Statement ifbody, elsebody;
94 
95     this(Expression condition, Statement ifbody, Statement elsebody)
96     {
97         this.condition = condition;
98         this.ifbody = ifbody;
99         this.elsebody = elsebody;
100     }
101 
102     override void compile(Backend b)
103     {
104         b.compile(this);
105     }
106 }
107 
108 class WhileStatement : Statement
109 {
110     Expression condition;
111     Statement whilebody;
112 
113     this(Expression condition, Statement whilebody)
114     {
115         this.condition = condition;
116         this.whilebody = whilebody;
117     }
118 
119     override void compile(Backend b)
120     {
121         b.compile(this);
122     }
123 }
124 
125 class PrintStatement : Statement
126 {
127     Expression value;
128 
129     this(Expression value)
130     {
131         this.value = value;
132     }
133 
134     override void compile(Backend b)
135     {
136         b.compile(this);
137     }
138 }
139 
140 /// Expressions ///
141 
142 class Expression
143 {
144     BEValue compile(Backend b)
145     {
146         throw new InternalError("compile() not implemented for Expression class: " ~ this.toString());
147     }
148 }
149 
150 class IntegerExp : Expression
151 {
152     int e1;
153     BEValue bevalue;
154 
155     this(string e1)
156     {
157         this.e1 = to!int(e1);
158     }
159 
160     override BEValue compile(Backend b)
161     {
162         return b.compile(this);
163     }
164 }
165 
166 class VarExp : Expression
167 {
168     string e1;
169     BEValue bevalue;
170 
171     this(string e1)
172     {
173         this.e1 = e1;
174     }
175 
176     override BEValue compile(Backend b)
177     {
178         return b.compile(this);
179     }
180 }
181 
182 class BinExp : Expression
183 {
184     string op;
185     Expression e1, e2;
186 
187     this(string op, Expression e1, Expression e2)
188     {
189         this.op = op;
190         this.e1 = e1;
191         this.e2 = e2;
192     }
193 
194     override BEValue compile(Backend b)
195     {
196         return b.compile(this);
197     }
198 }
199 
200 class CmpExp : Expression
201 {
202     string op;
203     Expression e1, e2;
204 
205     this(string op, Expression e1, Expression e2)
206     {
207         this.op = op;
208         this.e1 = e1;
209         this.e2 = e2;
210     }
211 
212     override BEValue compile(Backend b)
213     {
214         return b.compile(this);
215     }
216 }
217 
218 class AndExp : Expression
219 {
220     Expression e1, e2;
221 
222     this(Expression e1, Expression e2)
223     {
224         this.e1 = e1;
225         this.e2 = e2;
226     }
227 
228     override BEValue compile(Backend b)
229     {
230         return b.compile(this);
231     }
232 }
233 
234 class OrExp : Expression
235 {
236     Expression e1, e2;
237 
238     this(Expression e1, Expression e2)
239     {
240         this.e1 = e1;
241         this.e2 = e2;
242     }
243 
244     override BEValue compile(Backend b)
245     {
246         return b.compile(this);
247     }
248 }
249 
250 class NotExp : Expression
251 {
252     Expression e1;
253 
254     this(Expression e1)
255     {
256         this.e1 = e1;
257     }
258 
259     override BEValue compile(Backend b)
260     {
261         return b.compile(this);
262     }
263 }
264