-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboolean_module.zig
More file actions
127 lines (110 loc) · 4.32 KB
/
Copy pathboolean_module.zig
File metadata and controls
127 lines (110 loc) · 4.32 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
const std = @import("std");
const Allocator = std.mem.Allocator;
const Module = @import("../../module.zig").Module;
const Interpreter = @import("../../interpreter.zig").Interpreter;
const helpers = @import("helpers.zig");
pub const BooleanModule = struct {
module: Module,
allocator: Allocator,
pub fn init(allocator: Allocator) !*BooleanModule {
const self = try allocator.create(BooleanModule);
self.* = .{
.module = try Module.init(allocator, "boolean", ""),
.allocator = allocator,
};
try self.registerWords();
return self;
}
pub fn deinit(self: *BooleanModule) void {
self.module.deinit();
self.allocator.destroy(self);
}
fn registerWords(self: *BooleanModule) !void {
try self.module.addWord("==", equal);
try self.module.addWord("!=", notEqual);
try self.module.addWord("<", lessThan);
try self.module.addWord(">", greaterThan);
try self.module.addWord("<=", lessThanOrEqual);
try self.module.addWord(">=", greaterThanOrEqual);
try self.module.addWord("AND", andOp);
try self.module.addWord("OR", orOp);
try self.module.addWord("NOT", notOp);
try self.module.addWord("TRUE", trueOp);
try self.module.addWord("FALSE", falseOp);
try self.module.addWord("NULL", nullOp);
try self.module.addWord("IN", inOp);
}
fn equal(interp: *Interpreter) !void {
const b = try interp.stackPop();
const a = try interp.stackPop();
try interp.stackPush(helpers.Helpers.areEqual(a, b));
}
fn notEqual(interp: *Interpreter) !void {
const b = try interp.stackPop();
const a = try interp.stackPop();
try interp.stackPush(!helpers.Helpers.areEqual(a, b));
}
fn lessThan(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);
const result = if (numA != null and numB != null) numA.? < numB.? else false;
try interp.stackPush(result);
}
fn greaterThan(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);
const result = if (numA != null and numB != null) numA.? > numB.? else false;
try interp.stackPush(result);
}
fn lessThanOrEqual(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);
const result = if (numA != null and numB != null) numA.? <= numB.? else false;
try interp.stackPush(result);
}
fn greaterThanOrEqual(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);
const result = if (numA != null and numB != null) numA.? >= numB.? else false;
try interp.stackPush(result);
}
fn andOp(interp: *Interpreter) !void {
const b = try interp.stackPop();
const a = try interp.stackPop();
try interp.stackPush(helpers.Helpers.isTruthy(a) and helpers.Helpers.isTruthy(b));
}
fn orOp(interp: *Interpreter) !void {
const b = try interp.stackPop();
const a = try interp.stackPop();
try interp.stackPush(helpers.Helpers.isTruthy(a) or helpers.Helpers.isTruthy(b));
}
fn notOp(interp: *Interpreter) !void {
const val = try interp.stackPop();
try interp.stackPush(!helpers.Helpers.isTruthy(val));
}
fn trueOp(interp: *Interpreter) !void {
try interp.stackPush(true);
}
fn falseOp(interp: *Interpreter) !void {
try interp.stackPush(false);
}
fn nullOp(interp: *Interpreter) !void {
try interp.stackPush(null);
}
fn inOp(interp: *Interpreter) !void {
const container = try interp.stackPop();
const item = try interp.stackPop();
_ = container;
_ = item;
// TODO: Check if item in container (array or map)
try interp.stackPush(false);
}
};