-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.js
150 lines (127 loc) · 3.86 KB
/
compiler.js
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
const tokenizePattern = (type, pattern, input, current) => {
let char = input[current];
let consumedChars = 0;
if (pattern.test(char)) {
let value = '';
while (char && pattern.test(char)) {
value += char;
consumedChars ++;
char = input[current + consumedChars];
}
return [consumedChars , { type, value }];
}
return [0, null]
}
const tokenizeString = (input, current) => {
if (input[current] === '"') {
let value = '';
let consumedChars = 0;
consumedChars ++;
char = input[current + consumedChars];
while (char !== '"') {
if(char === undefined) {
throw new TypeError("unterminated string ");
}
value += char;
consumedChars ++;
char = input[current + consumedChars];
}
return [consumedChars + 1, { type: 'string', value }];
}
return [0, null]
}
const tokenizeCharacter = (type, value, input, current) => (value === input[current]) ? [1, { type, value }] : [0, null]
const tokenizeParOpen = (input, current) => tokenizeCharacter('paren', '(', input, current);
const tokenizeParClose = (input, current) => tokenizeCharacter('paren', ')', input, current);
const tokenizeName = (input, current) => tokenizePattern("name", /[a-z]/i, input, current)
const tokenizeNumber = (input, current) => tokenizePattern('number', /[0-9]/, input, current);
const skipWhiteSpace = (input, current) => (/\s/.test(input[current])) ? [1, null] : [0, null];
const tokenizers = [tokenizeParOpen, tokenizeParClose, tokenizeString, tokenizeNumber, tokenizeName, skipWhiteSpace];
const tokenizer = (input) => {
let current = 0;
let tokens = [];
while (current < input.length) {
let tokenized = false;
tokenizers.forEach((tokenizer) => {
if (tokenized) return;
let [consumedChars, token] = tokenizer(input, current);
if (consumedChars !== 0) {
tokenized = true;
current += consumedChars;
}
if (token) {
tokens.push(token);
}
});
if (!tokenized) {
throw new TypeError('I dont know what this character is: ' + input[current]);
}
}
return tokens;
}
const parser = (tokens) => {
let current = 0;
const walk = () => {
let token = tokens[current];
if (token.type === 'number') {
current++;
return {
type: 'NumberLiteral',
value: token.value,
};
}
if (token.type === 'string') {
current++;
return {
type: 'StringLiteral',
value: token.value,
};
}
if (token.type === 'paren' && token.value === '(') {
token = tokens[++current]; // Ignore parentesis
const node = {
type: 'CallExpression',
name: token.value, // Token "name"
params: [],
};
token = tokens[++current]; // Next token
while (!(token.type === 'paren' && token.value ===')')) {
node.params.push(walk());
token = tokens[current]; // Because token is inside the closure and we have to take it outside
}
current++;
return node;
}
throw new TypeError(token.type);
}
const ast = {
type: 'Program',
body: [],
};
while (current < tokens.length) {
ast.body.push(walk());
}
return ast;
}
emitNumber = node => node.value;
emitString = node => `"${node.value}"`;
emitProgram = node => node.body.map(exp => emitter(exp) + ";").join('\n');
emitExpression = node => `${node.name}(${node.params.map(emitter).join(', ')})`
emitter = node => {
switch (node.type) {
case 'Program': return emitProgram(node);
case 'CallExpression': return emitExpression(node);
case 'StringLiteral': return emitString(node);
case 'NumberLiteral': return emitNumber(node);
default: throw new TypeError(node.type);
}
}
compiler = input => {
const tokens = tokenizer(input);
const ast = parser(tokens);
const output = emitter(ast);
return output;
}
module.exports = {
compiler
};