-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0736-parse-lisp-expression.js
More file actions
96 lines (87 loc) · 3.02 KB
/
0736-parse-lisp-expression.js
File metadata and controls
96 lines (87 loc) · 3.02 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
/**
* 736. Parse Lisp Expression
* Time Complexity: O(N^2)
* Space Complexity: O(N^2)
*/
var evaluate = function (expression) {
const initialScopeMap = new Map();
return parseExpression(expression, initialScopeMap);
function parseExpression(currentExpressionString, currentVariableScope) {
const integerValue = parseInt(currentExpressionString);
if (!isNaN(integerValue)) {
return integerValue;
}
if (!currentExpressionString.startsWith("(")) {
const foundVariableValue = currentVariableScope.get(
currentExpressionString,
);
return foundVariableValue;
}
const innerContentString = currentExpressionString.slice(1, -1);
const expressionParts = tokenizeString(innerContentString);
const operationType = expressionParts[0];
if (operationType === "add") {
const firstOperandExpression = expressionParts[1];
const secondOperandExpression = expressionParts[2];
const firstEvaluatedValue = parseExpression(
firstOperandExpression,
currentVariableScope,
);
const secondEvaluatedValue = parseExpression(
secondOperandExpression,
currentVariableScope,
);
return firstEvaluatedValue + secondEvaluatedValue;
}
if (operationType === "mult") {
const multiplicandExpression = expressionParts[1];
const multiplierExpression = expressionParts[2];
const multiplicandValue = parseExpression(
multiplicandExpression,
currentVariableScope,
);
const multiplierValue = parseExpression(
multiplierExpression,
currentVariableScope,
);
return multiplicandValue * multiplierValue;
}
if (operationType === "let") {
const letBlockScope = new Map(currentVariableScope);
let partIndex = 1;
while (partIndex < expressionParts.length - 1) {
const variableName = expressionParts[partIndex];
const variableValueDefinition = expressionParts[partIndex + 1];
const evaluatedVariableValue = parseExpression(
variableValueDefinition,
letBlockScope,
);
letBlockScope.set(variableName, evaluatedVariableValue);
partIndex += 2;
}
const finalEvaluationExpression =
expressionParts[expressionParts.length - 1];
return parseExpression(finalEvaluationExpression, letBlockScope);
}
}
function tokenizeString(inputContent) {
const tokenResultArray = [];
let currentBuildString = "";
let currentParenthesesDepth = 0;
for (const characterInInput of inputContent) {
if (characterInInput === " " && currentParenthesesDepth === 0) {
tokenResultArray.push(currentBuildString);
currentBuildString = "";
} else {
currentBuildString += characterInInput;
if (characterInInput === "(") {
currentParenthesesDepth++;
} else if (characterInInput === ")") {
currentParenthesesDepth--;
}
}
}
tokenResultArray.push(currentBuildString);
return tokenResultArray;
}
};