-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.ts
235 lines (209 loc) · 5.85 KB
/
ast.ts
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import { Variable } from './api';
import SourceLocation from './parser-lib/sourceLocation';
import { Type } from './types';
import debug from './util/debug';
import join from './util/join';
export type Leaf = Number | Identifier | BooleanLiteral | StringLiteral;
export type Number = {
kind: 'number';
sourceLocation: SourceLocation;
value: number;
};
export type Identifier = {
kind: 'identifier';
sourceLocation: SourceLocation;
value: string;
};
export type BooleanLiteral = {
kind: 'booleanLiteral';
sourceLocation: SourceLocation;
value: boolean;
};
export type StringLiteral = {
kind: 'stringLiteral';
sourceLocation: SourceLocation;
value: string;
};
export type ReturnStatement = {
kind: 'returnStatement';
sourceLocation: SourceLocation;
expression: Ast;
};
export type ForLoop = {
kind: 'forLoop';
sourceLocation: SourceLocation;
var: Variable;
list: Ast;
body: Statement[];
};
export type Ternary = {
kind: 'ternary';
sourceLocation: SourceLocation;
condition: Ast;
ifTrue: Ast;
ifFalse: Ast;
};
export type Equality = {
kind: 'equality';
sourceLocation: SourceLocation;
lhs: Ast;
rhs: Ast;
type: Type;
};
export type Declaration = {
kind: 'declaration';
sourceLocation: SourceLocation;
type: Type;
destination: string;
expression: Ast;
};
export type Reassignment = {
kind: 'reassignment';
sourceLocation: SourceLocation;
destination: string;
expression: Ast;
};
export type FunctionCall = {
kind: 'callExpression';
sourceLocation: SourceLocation;
name: string;
arguments: Ast[];
};
// In an Ast, each function is stored in a map from name to contents. The reference refers to the function name in that map.
export type FunctionReference = {
kind: 'functionReference';
sourceLocation: SourceLocation;
name: string;
};
export type Statement = Declaration | Reassignment | ReturnStatement | ForLoop;
export type Addition = {
kind: 'addition';
sourceLocation: SourceLocation;
lhs: Ast;
rhs: Ast;
};
export type Subtraction = {
kind: 'subtraction';
sourceLocation: SourceLocation;
lhs: Ast;
rhs: Ast;
};
export type Product = {
kind: 'product';
sourceLocation: SourceLocation;
lhs: Ast;
rhs: Ast;
};
export type Concatenation = {
kind: 'concatenation';
sourceLocation: SourceLocation;
lhs: Ast;
rhs: Ast;
};
export type TypeDeclaration = {
kind: 'typeDeclaration';
sourceLocation: SourceLocation;
};
export type ObjectMember = {
name: string;
expression: Ast;
};
export type ObjectLiteral = {
kind: 'objectLiteral';
sourceLocation: SourceLocation;
type: Type;
members: ObjectMember[];
};
export type ListLiteral = {
kind: 'listLiteral';
sourceLocation: SourceLocation;
type: Type;
items: Ast[];
};
export type MemberAccess = {
kind: 'memberAccess';
sourceLocation: SourceLocation;
lhs: Ast;
lhsType: Type;
rhs: string;
};
export type IndexAccess = {
kind: 'indexAccess';
sourceLocation: SourceLocation;
accessed: Ast;
index: Ast;
// TODO: add list item type here
};
export type Ast =
| Leaf
| Ternary
| Equality
| FunctionCall
| FunctionReference
| Statement
| Subtraction
| Addition
| Product
| Concatenation
| TypeDeclaration
| ObjectLiteral
| ListLiteral
| IndexAccess
| MemberAccess;
export const astToString = (ast: Ast) => {
if (!ast) debug('Null ast in astToString');
switch (ast.kind) {
case 'returnStatement':
return `return ${astToString(ast.expression)}`;
case 'forLoop':
return `for (${ast.var} : ${astToString(ast.list)}) {
${join(ast.body.map(astToString), '\n')}
};`;
case 'ternary':
return `${astToString(ast.condition)} ? ${astToString(ast.ifTrue)} : ${astToString(
ast.ifFalse
)}`;
case 'equality':
return `${astToString(ast.lhs)} == ${astToString(ast.rhs)}`;
case 'identifier':
return ast.value;
case 'number':
return ast.value.toString();
case 'callExpression':
const args = join(ast.arguments.map(astToString), ', ');
return `${ast.name}(${args})`;
case 'functionReference':
return ast.name;
case 'product':
return `${astToString(ast.lhs)} * ${astToString(ast.rhs)}`;
case 'addition':
return `${astToString(ast.lhs)} + ${astToString(ast.rhs)}`;
case 'subtraction':
return `${astToString(ast.lhs)} - ${astToString(ast.rhs)}`;
case 'stringLiteral':
return `"${ast.value}"`;
case 'booleanLiteral':
return ast.value ? 'True' : 'False';
case 'concatenation':
return `${ast.lhs} ++ ${ast.rhs}`;
case 'declaration':
return `${ast.destination}: ${ast.type.type.kind} = ${astToString(ast.expression)};`;
case 'typeDeclaration':
return `(${ast.kind})`; // TODO: Figure out what parts of type declaration should go in AST vs uninferred AST.
case 'reassignment':
return `${ast.destination} = ${astToString(ast.expression)};`;
case 'objectLiteral':
const members = ast.members.map(
({ name, expression }) => `${name}: ${astToString(expression)}`
);
return `{ ${join(members, ', ')} }`;
case 'memberAccess':
return `(${astToString(ast.lhs)}).${ast.rhs}`;
case 'listLiteral':
return `[${join(ast.items.map(astToString), ', ')}]`;
case 'indexAccess':
return `(${astToString(ast.accessed)})[${astToString(ast.index)}]`;
default:
throw debug(`${(ast as any).kind} unhandled in astToString`);
}
};