-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpreter.js
More file actions
222 lines (193 loc) · 6.99 KB
/
Copy pathinterpreter.js
File metadata and controls
222 lines (193 loc) · 6.99 KB
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
/**
* Interpreter - Executes the parsed AST for DevLang
*
* @version 1.0.0
* @license MIT
*/
const readline = require('readline');
const { TokenTypes } = require('./tokenTypes');
const { SymbolTable } = require('./symbolTable');
const { Tokenizer } = require('./tokenizer');
const { Parser } = require('./parser');
/**
* Interpreter - Executes the parsed AST
*/
class Interpreter {
constructor(code, isAsync = false) {
this.symbolTable = new SymbolTable();
this.isAsync = isAsync;
const tokenizer = new Tokenizer(code);
const parser = new Parser(tokenizer.getTokens());
this.ast = parser.parse();
if (isAsync) {
this.setupReadline();
}
}
setupReadline() {
this.rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
}
async execute() {
try {
await this.visit(this.ast);
} finally {
if (this.rl) {
this.rl.close();
}
}
}
// Synchronous execution for backwards compatibility
executeSync() {
this.visit(this.ast);
}
async visit(node) {
switch (node.type) {
case 'Program':
case 'block':
for (const child of node.children) {
await this.visit(child);
}
break;
case 'assignment':
await this.handleAssignment(node.children);
break;
case 'print':
this.handlePrint(node.children);
break;
case 'conditional':
await this.handleConditional(node.children);
break;
}
}
async handleAssignment(children) {
const name = children[0].value;
if (children[1].value === 'input') {
const expression = children[1].children[0];
const prompt = this.evaluateExpression(expression.children);
if (this.isAsync && this.rl) {
// Async input handling
const userInput = await this.getUserInput(prompt);
const value = this.parseInput(userInput);
if (this.symbolTable.lookup(name) === null) {
this.symbolTable.insert(name, typeof value, 'local', value);
} else {
this.symbolTable.modify(name, value);
}
} else {
// Synchronous fallback - simulate input
console.log(`Input prompt: ${prompt}`);
const simulatedInput = this.getSimulatedInput(prompt);
const value = this.parseInput(simulatedInput);
if (this.symbolTable.lookup(name) === null) {
this.symbolTable.insert(name, typeof value, 'local', value);
} else {
this.symbolTable.modify(name, value);
}
}
return;
}
const value = this.evaluateExpression(children[1].children);
if (this.symbolTable.lookup(name) === null) {
this.symbolTable.insert(name, typeof value, 'local', value);
} else {
this.symbolTable.modify(name, value);
}
}
async getUserInput(prompt) {
return new Promise((resolve) => {
this.rl.question(`${prompt}: `, (answer) => {
resolve(answer);
});
});
}
getSimulatedInput(prompt) {
// For demonstration purposes, provide simulated inputs
const simulatedInputs = {
'Apna naam batao': 'DevLang User',
'Apni age batao': '25',
'Enter your name': 'John Doe',
'Enter your age': '30'
};
return simulatedInputs[prompt] || '0';
}
parseInput(input) {
// Try to parse as number first
const num = parseFloat(input);
if (!isNaN(num) && isFinite(num)) {
return Number.isInteger(num) ? parseInt(input) : num;
}
// Return as string if not a number
return input;
}
handlePrint(children) {
for (const child of children) {
if (child.type === 'expression') {
const result = this.evaluateExpression(child.children);
console.log(result);
return result;
}
}
}
async handleConditional(children) {
const conditionNode = children[0];
const thenBlock = children[1];
const elseBlock = children.length > 2 ? children[2] : null;
const condition = this.evaluateExpression(conditionNode.children);
if (condition) {
await this.visit(thenBlock);
} else if (elseBlock) {
await this.visit(elseBlock.children[0]);
}
}
evaluateExpression(children) {
if (!children || children.length === 0) {
return null;
}
// Handle single string literals
if (children.length === 1 && children[0].type === TokenTypes.STRING_LITERAL) {
return children[0].value;
}
// Handle single identifiers
if (children.length === 1 && children[0].type === TokenTypes.IDENTIFIER) {
const symbol = this.symbolTable.lookup(children[0].value);
if (symbol === null) {
throw new Error(`Variable '${children[0].value}' not declared`);
}
return symbol.value;
}
// Handle single numbers
if (children.length === 1 && children[0].type === TokenTypes.INT_LITERAL) {
return parseInt(children[0].value);
}
// Build expression string for complex expressions
let expression = '';
for (const child of children) {
if (child.type === TokenTypes.STRING_LITERAL) {
expression += `"${child.value}"`;
} else if (child.type === TokenTypes.IDENTIFIER) {
const symbol = this.symbolTable.lookup(child.value);
if (symbol === null) {
throw new Error(`Variable '${child.value}' not declared`);
}
// Ensure numeric values are treated as numbers in expressions
if (typeof symbol.value === 'number') {
expression += symbol.value.toString();
} else if (typeof symbol.value === 'string' && !isNaN(parseFloat(symbol.value))) {
expression += parseFloat(symbol.value).toString();
} else {
expression += `"${symbol.value}"`;
}
} else {
expression += child.value;
}
}
try {
return eval(expression);
} catch (error) {
throw new Error(`Error evaluating expression: ${expression}`);
}
}
}
module.exports = { Interpreter };