-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.js
175 lines (167 loc) · 5.81 KB
/
eval.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
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
const binaryOperation = (lambda) => {
return (args, context) => {
const [a, errorA] = eval(args[0], context) // should be a number
const [b, errorB] = eval(args[1], context) // should be a number
if (hasValue(a) && hasValue(b)) {
const x = parseFloat(a)
const y = parseFloat(b)
return [lambda(x, y), null]
} else {
const errorMessage = [errorA?.message, errorB?.message].filter(m => m != undefined).join(", ")
return [null, new Error(errorMessage)]
}
}
}
const getCellPositionFromCoord = (coord) => {
const column = [...coord].filter(isAlpha).join("")
const row = parseInt([...coord].filter(isDigit).join(""))
return [row, column]
}
const BUILT_IN_FUNCTIONS_MAP = {
"+": binaryOperation((a,b) => a + b),
"-": binaryOperation((a,b) => a - b),
"*": binaryOperation((a,b) => a * b),
"/": binaryOperation((a,b) => a / b),
"^": binaryOperation((a,b) => a ** b),
"=": binaryOperation((a,b) => a === b), // TODO: interesting
">": binaryOperation((a,b) => a > b),
">=": binaryOperation((a,b) => a >= b),
"<": binaryOperation((a,b) => a < b),
"<=": binaryOperation((a,b) => a <= b),
"<>": binaryOperation((a,b) => a !== b),
"sum": function(args, context) {
let result = 0
// assumption is that the first arg is a multi-array range
const [array, error] = eval(args[0], context)
if (error) {
return [null, error]
}
for (let arr of array) {
result += arr.reduce((acc, n) => acc + parseFloat(n), 0) // TODO: parseFloat can fail too
}
return [result, null]
},
"mean": function(args, context) {
let result = 0
let count = 0
// assumption is that the first arg is a multi-array range
const [array, error] = eval(args[0], context)
if (error) {
return [null, error]
}
for (let arr of array) {
result += arr.reduce((acc, n) => acc + parseFloat(n), 0)
count += arr.length
}
return [result / count, null]
},
"mode": function(args, context) {
return [0, null]
},
"count": function(args, context) {
let count = 0
// assumption is that the first arg is a multi-array range
const [array, error] = eval(args[0], context)
if (error) {
return [null, error]
}
for (let arr of array) {
count += arr.length
}
return [count, null]
},
"if": function(args, context) {
// if <condition> <true> <false>
const condition = args[0]
const success = args[1]
const fail = args[2]
const [conditionResult, error] = eval(condition, context)
if (error) {
return [null, error]
}
if (conditionResult == true) {
return eval(success, context)
}
return eval(fail, context)
},
"min": function(args, context) {
let globalMin = Infinity
// assumption is that the first arg is a multi-array range
const [array, error] = eval(args[0], context)
if (error) {
return [null, error]
}
for (let arr of array) {
const localMin = arr.reduce((min, n) => Math.min(min, n), Infinity)
globalMin = Math.min(globalMin, localMin)
}
return [globalMin, null]
},
"max": function(args, context) {
let globalMax = -Infinity
// assumption is that the first arg is a multi-array range
const [array, error] = eval(args[0], context)
if (error) {
return [null, error]
}
for (let arr of array) {
const localMax = arr.reduce((max, n) => Math.max(max, n), -Infinity)
globalMax = Math.max(globalMax, localMax)
}
return [globalMax, null]
},
"__range__": function(args, context) {
const [sr, sc] = getCellPositionFromCoord(args[0].value)
const [er, ec] = getCellPositionFromCoord(args[1].value)
const result = []
let currentCol = sc
do {
result.push(context[currentCol].slice(sr, er+1).map(cell => cell.computedValue))
if (currentCol === ec) {
break
}
currentCol = getColumnNumberFromName(currentCol)
} while(true);
return [result, null]
},
}
function eval(expression, context) {
const {value, type} = expression;
if (type === DataType.Expr) {
const [func, ...args] = value
if (func.type && func.type === DataType.Expr) {
return eval(func, context)
}
const [executable, error] = eval(func)
if (error) {
return [null, error]
}
return executable(args, context)
} else if (type === DataType.Function) {
const func = BUILT_IN_FUNCTIONS_MAP[value]
if (func) {
return [func, null]
}
return [null, new Error(`${value} function does not exist`)]
} else if (type === DataType.Text) {
return [value, null]
} else if (type === DataType.Number) {
return [value, null]
} else if (type === DataType.Boolean) {
return [value, null]
} else if (type === DataType.Reference) {
// resolve it using context
const [row, column] = getCellPositionFromCoord(value)
const response = context[column][row].value();
if (response instanceof Error) {
return [null, response]
}
if (response == undefined) {
return [null, new Error(`${value} is undefined`)]
}
return [response, null]
} else {
console.error(`unrecognized expression type: ${type} (${value}) (${expression})`)
return [null, new Error("formula parsing error")]
}
}