-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath_module.zig
More file actions
282 lines (228 loc) · 9.26 KB
/
Copy pathmath_module.zig
File metadata and controls
282 lines (228 loc) · 9.26 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
const std = @import("std");
const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;
const Module = @import("../../module.zig").Module;
const Interpreter = @import("../../interpreter.zig").Interpreter;
const Value = @import("../../value.zig").Value;
const helpers = @import("helpers.zig");
const word_mod = @import("../../word.zig");
const ModuleWord = word_mod.ModuleWord;
pub const MathModule = struct {
module: Module,
allocator: Allocator,
word_ptrs: ArrayList(*ModuleWord),
pub fn init(allocator: Allocator) !*MathModule {
const self = try allocator.create(MathModule);
self.* = .{
.module = Module.init(allocator, "math", ""),
.allocator = allocator,
.word_ptrs = ArrayList(*ModuleWord){},
};
try self.registerWords();
return self;
}
pub fn deinit(self: *MathModule) void {
// Free word pointers
for (self.word_ptrs.items) |word_ptr| {
word_ptr.asWord().deinit(self.allocator);
self.allocator.destroy(word_ptr);
}
self.word_ptrs.deinit(self.allocator);
self.module.deinit();
self.allocator.destroy(self);
}
fn addModuleWord(self: *MathModule, name: []const u8, handler: word_mod.HandlerFn) !void {
const word_ptr = try self.allocator.create(ModuleWord);
word_ptr.* = ModuleWord.init(self.allocator, name, handler);
try self.word_ptrs.append(self.allocator, word_ptr); // Track for cleanup
try self.module.addExportableWord(word_ptr.asWord());
}
fn registerWords(self: *MathModule) !void {
// Arithmetic
try self.addModuleWord("+", plus);
try self.addModuleWord("ADD", plus);
try self.addModuleWord("-", minus);
try self.addModuleWord("SUBTRACT", minus);
try self.addModuleWord("*", times);
try self.addModuleWord("MULTIPLY", times);
try self.addModuleWord("/", divide);
try self.addModuleWord("DIVIDE", divide);
try self.addModuleWord("MOD", mod);
// Aggregates
try self.addModuleWord("SUM", sum);
try self.addModuleWord("MEAN", mean);
try self.addModuleWord("MAX", max);
try self.addModuleWord("MIN", min);
// Conversions
try self.addModuleWord(">INT", toInt);
try self.addModuleWord(">FLOAT", toFloat);
try self.addModuleWord("ROUND", round);
try self.addModuleWord(">FIXED", toFixed);
// Functions
try self.addModuleWord("ABS", abs);
try self.addModuleWord("SQRT", sqrt);
try self.addModuleWord("FLOOR", floor);
try self.addModuleWord("CEIL", ceil);
try self.addModuleWord("CLAMP", clamp);
// Special
try self.addModuleWord("INFINITY", infinity);
try self.addModuleWord("UNIFORM-RANDOM", uniformRandom);
}
fn plus(interp: *Interpreter) !void {
const b = try interp.stackPop();
// TODO: Case 1: Array on stack - sum all elements
// Case 2: Two numbers
const a = try interp.stackPop();
// Preserve integer types when both are integers
if (a == .int_value and b == .int_value) {
const result = a.int_value + b.int_value;
try interp.stackPush(Value.initInt(result));
return;
}
const numA = helpers.Helpers.toNumber(a);
const numB = helpers.Helpers.toNumber(b);
if (numA == null or numB == null) {
try interp.stackPush(Value.initNull());
return;
}
const result = numA.? + numB.?;
try interp.stackPush(Value.initFloat(result));
}
fn minus(interp: *Interpreter) !void {
const b = try interp.stackPop();
const a = try interp.stackPop();
const numA = helpers.Helpers.toNumber(a);
const numB = helpers.Helpers.toNumber(b);
if (numA == null or numB == null) {
try interp.stackPush(Value.initNull());
return;
}
try interp.stackPush(Value.initFloat(numA.? - numB.?));
}
fn times(interp: *Interpreter) !void {
const b = try interp.stackPop();
const a = try interp.stackPop();
const numA = helpers.Helpers.toNumber(a);
const numB = helpers.Helpers.toNumber(b);
if (numA == null or numB == null) {
try interp.stackPush(Value.initNull());
return;
}
try interp.stackPush(Value.initFloat(numA.? * numB.?));
}
fn divide(interp: *Interpreter) !void {
const b = try interp.stackPop();
const a = try interp.stackPop();
const numA = helpers.Helpers.toNumber(a);
const numB = helpers.Helpers.toNumber(b);
if (numA == null or numB == null or numB.? == 0) {
try interp.stackPush(Value.initNull());
return;
}
try interp.stackPush(Value.initFloat(numA.? / numB.?));
}
fn mod(interp: *Interpreter) !void {
const n = try interp.stackPop();
const m = try interp.stackPop();
const numM = helpers.Helpers.toNumber(m);
const numN = helpers.Helpers.toNumber(n);
if (numM == null or numN == null) {
try interp.stackPush(Value.initNull());
return;
}
const result = @mod(@as(i64, @intFromFloat(numM.?)), @as(i64, @intFromFloat(numN.?)));
try interp.stackPush(Value.initInt(result));
}
fn sum(interp: *Interpreter) !void {
const items = try interp.stackPop();
_ = items; // TODO: Implement array sum
try interp.stackPush(Value.initFloat(0.0));
}
fn mean(interp: *Interpreter) !void {
const items = try interp.stackPop();
_ = items; // TODO: Implement array mean
try interp.stackPush(Value.initFloat(0.0));
}
fn max(interp: *Interpreter) !void {
const items = try interp.stackPop();
_ = items; // TODO: Implement array max
try interp.stackPush(Value.initNull());
}
fn min(interp: *Interpreter) !void {
const items = try interp.stackPop();
_ = items; // TODO: Implement array min
try interp.stackPush(Value.initNull());
}
fn toInt(interp: *Interpreter) !void {
const val = try interp.stackPop();
const num = helpers.Helpers.toNumber(val);
const result = if (num) |n| @as(i64, @intFromFloat(n)) else 0;
try interp.stackPush(Value.initInt(result));
}
fn toFloat(interp: *Interpreter) !void {
const val = try interp.stackPop();
const num = helpers.Helpers.toNumber(val);
try interp.stackPush(Value.initFloat(num orelse 0.0));
}
fn round(interp: *Interpreter) !void {
const val = try interp.stackPop();
const num = helpers.Helpers.toNumber(val);
try interp.stackPush(Value.initFloat(if (num) |n| @round(n) else 0.0));
}
fn toFixed(interp: *Interpreter) !void {
const decimals = try interp.stackPop();
const val = try interp.stackPop();
_ = decimals;
_ = val;
// TODO: Format number to fixed decimals
const empty_str = try interp.allocator.dupe(u8, "");
try interp.stackPush(Value.initString(empty_str));
}
fn abs(interp: *Interpreter) !void {
const val = try interp.stackPop();
const num = helpers.Helpers.toNumber(val);
try interp.stackPush(Value.initFloat(if (num) |n| @abs(n) else 0.0));
}
fn sqrt(interp: *Interpreter) !void {
const val = try interp.stackPop();
const num = helpers.Helpers.toNumber(val);
try interp.stackPush(Value.initFloat(if (num) |n| @sqrt(n) else 0.0));
}
fn floor(interp: *Interpreter) !void {
const val = try interp.stackPop();
const num = helpers.Helpers.toNumber(val);
try interp.stackPush(Value.initFloat(if (num) |n| @floor(n) else 0.0));
}
fn ceil(interp: *Interpreter) !void {
const val = try interp.stackPop();
const num = helpers.Helpers.toNumber(val);
try interp.stackPush(Value.initFloat(if (num) |n| @ceil(n) else 0.0));
}
fn clamp(interp: *Interpreter) !void {
const max_val = try interp.stackPop();
const min_val = try interp.stackPop();
const val = try interp.stackPop();
const num = helpers.Helpers.toNumber(val);
const min_num = helpers.Helpers.toNumber(min_val);
const max_num = helpers.Helpers.toNumber(max_val);
if (num == null or min_num == null or max_num == null) {
try interp.stackPush(Value.initNull());
return;
}
const result = @max(min_num.?, @min(max_num.?, num.?));
try interp.stackPush(Value.initFloat(result));
}
fn infinity(interp: *Interpreter) !void {
try interp.stackPush(Value.initFloat(std.math.inf(f64)));
}
fn uniformRandom(interp: *Interpreter) !void {
const max_val = try interp.stackPop();
const min_val = try interp.stackPop();
const min_num = helpers.Helpers.toNumber(min_val) orelse 0.0;
const max_num = helpers.Helpers.toNumber(max_val) orelse 1.0;
var prng = std.Random.DefaultPrng.init(@intCast(std.time.timestamp()));
const random = prng.random();
const result = min_num + (random.float(f64) * (max_num - min_num));
try interp.stackPush(Value.initFloat(result));
}
};