-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3-extended.js
More file actions
144 lines (124 loc) · 3.29 KB
/
3-extended.js
File metadata and controls
144 lines (124 loc) · 3.29 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
'use strict';
const listp = (list) => Array.isArray(list) && list.length > 0;
const head = (list) => list[0];
const tail = (list) => list.slice(1);
const OPERATORS = {
'+': (a, b) => a + b,
'-': (a, b) => a - b,
'*': (a, b) => a * b,
'/': (a, b) => a / b,
eq: (a, b) => a === b,
list: (...list) => (listp(list) ? list : null),
car: (list) => (listp(list) ? head(list) : null),
cdr: (list) => (listp(list) ? tail(list) : null),
};
class NumberExpression {
constructor(value) {
this.value = parseFloat(value);
}
interpret() {
return this.value;
}
}
class VariableExpression {
constructor(name) {
this.name = name;
}
interpret(context) {
if (!(this.name in context)) {
throw new Error(`Variable "${this.name}" is not defined`);
}
return context[this.name];
}
}
class OperationExpression {
constructor(operator, operands) {
this.operator = operator;
this.operands = operands;
}
interpret(context) {
const toValues = (operand) => operand.interpret(context);
const args = this.operands.map((x) => toValues(x));
const operator = OPERATORS[this.operator];
if (!operator) throw new Error(`Unknown operator: ${operator}`);
if (this.operator.length > 1) return operator(...args);
return args.reduce(operator);
}
}
const tokenize = (source) => {
const stack = [];
const parentStack = [];
let current = stack;
const tokens = source
.replaceAll('(', ' ( ')
.replaceAll(')', ' ) ')
.trim()
.split(/\s+/);
for (const token of tokens) {
if (token === '(') {
const newStack = [];
current.push(newStack);
parentStack.push(current);
current = newStack;
} else if (token === ')') {
current = parentStack.pop();
} else {
current.push(token);
}
}
return stack[0];
};
const parse = (tokens) => {
if (!Array.isArray(tokens)) {
const isVar = isNaN(tokens);
const Expression = isVar ? VariableExpression : NumberExpression;
return new Expression(tokens);
}
const operator = tokens[0];
const operands = tokens.slice(1);
const operandExpressions = operands.map((x) => parse(x));
return new OperationExpression(operator, operandExpressions);
};
const evaluate = (input, context = {}) => {
const tokens = tokenize(input);
const expression = parse(tokens);
return expression.interpret(context);
};
// Usage
{
const program = '(+ 2 (* x 5) (- y 2))';
const context = { x: 3, y: 7 };
const result = evaluate(program, context);
const expected = 2 + 3 * 5 + (7 - 2);
console.log({ program, expected, result });
}
{
const program = '(eq 3 3)';
const result = evaluate(program, {});
const expected = true;
console.log({ program, expected, result });
}
{
const program = '(eq 3 4)';
const result = evaluate(program, {});
const expected = false;
console.log({ program, expected, result });
}
{
const program = '(list 7 3 1)';
const result = evaluate(program, {});
const expected = [7, 3, 1];
console.log({ program, expected, result });
}
{
const program = '(car (list 7 3 1)';
const result = evaluate(program, {});
const expected = 7;
console.log({ program, expected, result });
}
{
const program = '(cdr (list 7 3 1)';
const result = evaluate(program, {});
const expected = [3, 1];
console.log({ program, expected, result });
}