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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
|
const std = @import("std");
const opcodes = @import("opcodes.zig");
/// A number's value can be pure or point to a register
const NumberType = enum {
pure,
register,
};
const NumberValue = union(NumberType) {
pure: u64,
register: u8,
};
/// An expression's result can be a NumberValue or a string
const ExpressionResultType = enum {
number,
string,
};
const ExpressionResult = union(ExpressionResultType) {
number: NumberValue,
string: []const u8,
};
/// A constant can be a number of a string
const ConstantType = enum {
number,
string,
};
const ConstantValue = union(ConstantType) {
number: u64,
string: []const u8,
};
/// The Parser reads a provided input and assembles it into MMIX object code
pub const Parser = struct {
allocator: std.mem.Allocator,
input: []const u8,
location: u64,
ch_pos: usize,
symbols: std.StringHashMap(NumberValue),
object: std.ArrayList(u8),
/// Test is a character is whitespace
/// Note that newlines are special and not included in this implementation.
fn isWhitespace(ch: u8) bool {
return ch == ' ' or ch == '\t' or ch == '\r';
}
/// Test if a character is a letter
/// Note that underscores are letters for the purposes of symbol recognition.
fn isLetter(ch: u8) bool {
return ch == '_' or (ch >= 'a' and ch <= 'z') or (ch >= 'A' and ch <= 'Z');
}
/// Test if a character is a decimal digit
fn isDecimal(ch: u8) bool {
return ch >= '0' and ch <= '9';
}
/// Test if a character is a hexadecimal digit
fn isHexadecimal(ch: u8) bool {
return isDecimal(ch) or (ch >= 'a' and ch <= 'f') or (ch >= 'A' and ch <= 'F');
}
/// Test if a character is a symbol character
/// Note that all valid unicode characters larger than 126 are also valid symbol characters.
fn isSymbolChar(ch: u8) bool {
return isLetter(ch) or isDecimal(ch) or ch > 126;
}
/// Get a byte from the input at a specified location
/// Return 0 if the requested byte is out of range
fn getByte(self: *Parser, pos: usize) u8 {
if (pos < self.input.len) {
return self.input[pos];
}
return 0;
}
/// Move the cursor forward until it does not point at whitespace
fn skipWhitespace(self: *Parser) void {
while (isWhitespace(self.getByte(self.ch_pos))) {
self.ch_pose += 1;
}
}
/// Determine whether the cursor points at a valid integer in base 10
/// Move the cursor past the integer and return it
fn identifyDecimal(self: *Parser) !u64 {
const start = self.ch_pos;
while (isDecimal(self.getByte(self.ch_pos))) {
self.ch_pos += 1;
}
const end = self.ch_pos;
return std.fmt.parseInt(u64, self.input[start..end], 10) catch return error.NoDecimal;
}
/// Determine whether the cursor points at a valid integer in base 16
/// Base 16 is identified by a number starting with #
/// Move the cursor past the integer and return it
fn identifyHexadecimal(self: *Parser) !u64 {
if (self.getByte(self.ch_pos) != '#') {
return error.NoHexadecimal;
}
self.ch_pos += 1;
const start = self.ch_pos;
while (isHexadecimal(self.getByte(self.ch_pos))) {
self.ch_pos += 1;
}
const end = self.ch_pos;
return std.fmt.parseInt(u64, self.input[start..end], 16) catch return error.NoHexadecimal;
}
/// Determine whether the cursor points at a valid unicode character wrapped in single quotes
/// Move the cursor past the closing quote and return the character
fn identifyChar(self: *Parser) !u21 {
if (self.getByte(self.ch_pos) != '\'') {
return error.NoChar;
}
self.ch_pos += 1;
const start = self.ch_pos;
while (self.getByte(self.ch_pos) != 0 and self.getByte(self.ch_pos) != '\'') {
self.ch_pos += 1;
if (self.ch_pos - start > 4) {
return error.NoChar;
}
}
const end = self.ch_pos;
self.ch_pos += 1;
if (end <= start) {
return error.NoChar;
}
const view = std.unicode.Utf8View.init(self.input[start..end]) catch return error.NoChar;
var iter = view.iterator();
var count: u8 = 0;
var character: u21 = undefined;
while (iter.nextCodepoint()) |u| {
character = u;
count += 1;
if (count > 1) {
return error.NoChar;
}
}
if (count != 1) {
return error.NoChar;
}
return character;
}
/// Determine whether the cursor points at a valid string wrapped in double quotes
/// Note that a string has at least one character in it and that it cannot have " or newlines in it
/// Move the cursor past the string and return the string
fn identifyString(self: *Parser) ![]const u8 {
if (self.getByte(self.ch_pos) != '"') {
return error.NoString;
}
self.ch_pos += 1;
const start = self.ch_pos;
while (self.getByte(self.ch_pos) != 0 and self.getByte(self.ch_pos) != '"') {
if (self.getByte(self.ch_pos) == '\n') {
return error.NoString;
}
self.ch_pos += 1;
}
const end = self.ch_pos;
if (self.getByte(self.ch_pos) == '"') {
self.ch_pos += 1;
}
if (end <= start) {
return error.NoString;
}
return self.input[start..end];
}
/// Determine whether the cursor points at a valid constant
/// The constant may be a string or a number
/// Move the cursor past the constant and return it
fn identifyConstant(self: *Parser) !ConstantValue {
switch (self.getByte(self.ch_pos)) {
'0'...'9' => {
const number = try identifyDecimal(self);
return ConstantValue{ .number = number };
},
'#' => {
const number = try identifyHexadecimal(self);
return ConstantValue{ .number = number };
},
'\'' => {
const char = try identifyChar(self);
return ConstantValue{ .number = char };
},
'"' => {
const string = try identifyString(self);
return ConstantValue{ .string = string };
},
else => return error.NoConstant,
}
}
/// Determine whether the cursor points at a symbol
/// A symbol starts with a letter and only has symbol characters after that point
/// There is an exception that there are 30 special symbols of the form xH, xF, and xB where x is a single decimal digit
/// Move the cursor past the symbol and return its name
fn identifySymbol(self: *Parser) ![]const u8 {
const start = self.ch_pos;
if ((isLetter(self.getByte(self.ch_pos)) or self.getByte(self.ch_pos) == '_')) {
self.ch_pos += 1;
while (isSymbolChar(self.getByte(self.ch_pos))) {
self.ch_pos += 1;
}
} else if (isDecimal(self.getByte(self.ch_pos)) and
(self.getByte(self.ch_pos + 1) == 'H' or self.getByte(self.ch_pos + 1) == 'F' or self.getByte(self.ch_pos + 1) == 'B'))
{
self.ch_pos += 2;
return self.input[self.ch_pos - 2 .. self.ch_pos];
}
const end = self.ch_pos;
if (end > start) {
return self.input[start..end];
}
return error.NoSymbol;
}
/// Get the number associated with a given symbol
fn handleSymbol(self: *Parser, symbol: []const u8) !NumberValue {
// TODO: handle xH, XF, xB
const n = self.symbols.get(symbol);
if (n == null) return error.UndefinedSymbol;
return n.?;
}
const WeakOp = enum {
add,
sub,
bit_or,
bit_xor,
none,
};
/// Determine whether the cursor points at a valid expression
/// Move the cursor past the expression, evaluate it, and return its value
fn identifyExpression(self: *Parser) anyerror!ExpressionResult {
var result: u64 = 0;
var last_op = WeakOp.none;
var done = false;
while (!done) {
const term = try self.identifyTerm();
switch (term) {
.string => {
if (last_op == WeakOp.none) return term;
return error.NoExpression;
},
.number => |nv| {
switch (nv) {
.pure => |n| {
result = switch (last_op) {
WeakOp.add => result +% n,
WeakOp.sub => result -% n,
WeakOp.bit_or => result | n,
WeakOp.bit_xor => result ^ n,
WeakOp.none => n,
};
},
.register => {
return error.NoExpression;
},
}
},
}
last_op = switch (self.getByte(self.ch_pos)) {
'+' => WeakOp.add,
'-' => WeakOp.sub,
'|' => WeakOp.bit_or,
'^' => WeakOp.bit_xor,
else => WeakOp.none,
};
if (last_op == WeakOp.none) {
done = true;
} else {
self.ch_pos += 1;
}
}
return ExpressionResult{ .number = NumberValue{ .pure = result } };
}
const StrongOp = enum {
mult,
div,
frac_div,
rem,
lshift,
rshift,
bit_and,
none,
};
/// Determine whether the cursor points at a valid term
/// Move the cursor past the term, evaluate it, and return its value
fn identifyTerm(self: *Parser) anyerror!ExpressionResult {
var result: u64 = 0;
var last_op = StrongOp.none;
var done = false;
while (!done) {
const primary = try self.identifyPrimary();
switch (primary) {
.string => {
if (last_op == StrongOp.none) return primary;
return error.NoTerm;
},
.number => |nv| {
switch (nv) {
.pure => |n| {
result = switch (last_op) {
StrongOp.mult => result *% n,
StrongOp.div => div: {
if (n == 0) return error.NoTerm;
break :div result / n;
},
StrongOp.frac_div => frac_div: {
if (n == 0 or result >= n) return error.NoTerm;
const shifted: u128 = (@as(u128, result)) << 8;
const divided: u128 = shifted / n;
if (divided >= 1 << 64) return error.NoTerm;
break :frac_div @as(u64, @intCast(divided));
},
StrongOp.rem => rem: {
if (n == 0) return error.NoTerm;
break :rem result % n;
},
StrongOp.lshift => if (n >= 64) 0 else result << @as(u6, @intCast(n)),
StrongOp.rshift => if (n >= 64) 0 else result >> @as(u6, @intCast(n)),
StrongOp.bit_and => result & n,
StrongOp.none => n,
};
},
else => {
if (last_op == StrongOp.none) return primary;
return error.NoTerm;
},
}
},
}
last_op = switch (self.getByte(self.ch_pos)) {
'*' => StrongOp.mult,
'/' => div: {
var op = StrongOp.div;
if (self.getByte(self.ch_pos + 1) == '/') {
op = StrongOp.frac_div;
self.ch_pos += 1;
}
break :div op;
},
'%' => StrongOp.rem,
'<' => lshift: {
var op = StrongOp.lshift;
if (self.getByte(self.ch_pos + 1) == '<') {
self.ch_pos += 1;
} else {
op = StrongOp.none;
}
break :lshift op;
},
'>' => rshift: {
var op = StrongOp.rshift;
if (self.getByte(self.ch_pos + 1) == '>') {
self.ch_pos += 1;
} else {
op = StrongOp.none;
}
break :rshift op;
},
'&' => StrongOp.bit_and,
else => StrongOp.none,
};
if (last_op == StrongOp.none) {
done = true;
} else {
self.ch_pos += 1;
}
}
return ExpressionResult{ .number = NumberValue{ .pure = result } };
}
/// Determine whether the cursor points at a valid primary
/// Move the cursor past the primary, evaluate it, and return its value
fn identifyPrimary(self: *Parser) anyerror!ExpressionResult {
if (isDecimal(self.getByte(self.ch_pos)) and
(self.getByte(self.ch_pos + 1) == 'H' or self.getByte(self.ch_pos + 1) == 'F' or self.getByte(self.ch_pos + 1) == 'B'))
{
const symbol = try self.identifySymbol();
if (symbol.len != 2) return error.UndefinedSymbol;
if (symbol[1] == 'H') return error.UndefinedSymbol;
const symbol_val = try self.handleSymbol(symbol);
return ExpressionResult{ .number = symbol_val };
}
switch (self.getByte(self.ch_pos)) {
'@' => return ExpressionResult{ .number = NumberValue{ .pure = self.location } },
'(' => {
self.ch_pos += 1;
const expr = try self.identifyExpression();
if (self.getByte(self.ch_pos) != ')') return error.NoPrimary;
self.ch_pos += 1;
return expr;
},
'0'...'9', '#', '\'', '"' => {
const constant = try self.identifyConstant();
switch (constant) {
.number => |n| return ExpressionResult{ .number = NumberValue{ .pure = n } },
.string => |s| return ExpressionResult{ .string = s },
}
},
'+' => {
self.ch_pos += 1;
const primary = try self.identifyPrimary();
switch (primary) {
.number => return primary,
else => return error.NoPimary,
}
},
'-' => {
self.ch_pos += 1;
const primary = try self.identifyPrimary();
switch (primary) {
.number => |nv| {
switch (nv) {
.register, .pure => |n| return ExpressionResult{ .number = NumberValue{ .pure = 0 -% n } },
}
},
else => return error.NoPrimary,
}
},
'~' => {
self.ch_pos += 1;
const primary = try self.identifyPrimary();
switch (primary) {
.number => |nv| {
switch (nv) {
.register, .pure => |n| return ExpressionResult{ .number = NumberValue{ .pure = ~n } },
}
},
else => return error.NoPrimary,
}
},
'$' => {
self.ch_pos += 1;
const primary = try self.identifyPrimary();
switch (primary) {
.number => |nv| {
switch (nv) {
.register, .pure => |n| {
if (n >= 256) return error.NoPrimary;
const n8 = @as(u8, @intCast(n));
return ExpressionResult{ .number = NumberValue{ .register = n8 } };
},
}
},
else => return error.NoPrimary,
}
},
else => {
const symbol = try self.identifySymbol();
const symbol_value = try self.handleSymbol(symbol);
return ExpressionResult{ .number = symbol_value };
},
}
}
/// Determine whether the cursor points at a valid opcode or pseudo operation
/// An opcode consists solely of symbol characters (letters and numbers in fact)
/// Move the cursor past the opcode and return it
fn identifyOperation(self: *Parser) !opcodes.Operation {
const start = self.ch_pos;
while (isSymbolChar(self.getByte(self.ch_pos))) {
self.ch_pos += 1;
}
const end = self.ch_pos;
return opcodes.parseOp(self.allocator, self.input[start..end]);
}
pub fn init(allocator: std.mem.Allocator, input: []const u8) Parser {
return Parser{
.allocator = allocator,
.input = input,
.location = 0,
.ch_pos = 0,
.symbols = std.StringHashMap(NumberValue).init(allocator),
.object = std.ArrayList(u8).init(allocator),
};
}
pub fn deinit(self: *Parser) void {
self.symbols.deinit();
self.object.deinit();
}
};
test "normal ascii characters are recognized as symbol chars" {
const chars = "qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM_";
for (chars) |c| {
try std.testing.expect(Parser.isSymbolChar(c));
}
}
test "large unicode characters are recognized as symbol chars" {
const cuneiform = "𒀀𒀁𒀂𒀃𒀄𒀅𒀆𒀇𒀈𒀉𒀊𒀋𒀌𒀍𒀎𒀏𒀐𒀑𒀒𒀓𒀔𒀕𒀖𒀗𒀘𒀙𒀚𒀛𒀜𒀝𒀞𒀟𒀠𒀡𒀢𒀣𒀤𒀥𒀦𒀧𒀨𒀩𒀪𒀫𒀬𒀭𒀮𒀯𒀰𒀱𒈷𒌄";
for (cuneiform) |c| {
try std.testing.expect(Parser.isSymbolChar(c));
}
}
test "non-symbol characters are detected" {
const chars = "\n\r \t!@#$%^&*()-=+[]{}\\|;:'\"/?,.<>`~";
for (chars) |c| {
try std.testing.expect(!Parser.isSymbolChar(c));
}
}
test "symbols are identified" {
const test_cases = [_][]const u8{
"_asdf$%@",
"ASFLKJ3332__q5 ;asdf;lk",
"asdf𒀤𒀥𒀦\nalsfkd",
"2H",
"5F",
"0B",
};
const expected = [_][]const u8{
"_asdf",
"ASFLKJ3332__q5",
"asdf𒀤𒀥𒀦",
"2H",
"5F",
"0B",
};
for (0..6) |i| {
var parser = Parser.init(std.testing.allocator, test_cases[i]);
const symbol = try parser.identifySymbol();
try std.testing.expect(std.mem.eql(u8, expected[i], symbol));
parser.deinit();
}
}
test "no symbols are found successfully" {
const test_cases = [_][]const u8{
" _asdf",
";ASFLKJ3332__q5",
"\nasdf𒀤𒀥𒀦",
};
for (test_cases) |case| {
var parser = Parser.init(std.testing.allocator, case);
const symbol = parser.identifySymbol();
try std.testing.expectEqual(error.NoSymbol, symbol);
parser.deinit();
}
}
test "opcodes are identified" {
const test_cases = [_][]const u8{
"2ADDU%aldfk",
"GO ",
"ADD\taksfdjas",
"GREG\n",
"IS",
};
const expected = [_]opcodes.Operation{
opcodes.Operation{ .opcode = opcodes.Opcode._2ADDU },
opcodes.Operation{ .opcode = opcodes.Opcode.GO },
opcodes.Operation{ .opcode = opcodes.Opcode.ADD },
opcodes.Operation{ .pseudo_op = opcodes.PseudoOp.GREG },
opcodes.Operation{ .pseudo_op = opcodes.PseudoOp.IS },
};
for (0..5) |i| {
var parser = Parser.init(std.testing.allocator, test_cases[i]);
const op = try parser.identifyOperation();
try std.testing.expectEqual(expected[i], op);
parser.deinit();
}
}
test "no opcodes are found successfully" {
const test_cases = [_][]const u8{
" _asdf",
";ASFLKJ3332__q5",
"\nasdf𒀤𒀥𒀦",
"asdfklajsdfl",
};
for (test_cases) |case| {
var parser = Parser.init(std.testing.allocator, case);
const symbol = parser.identifyOperation();
try std.testing.expectEqual(error.NoOpcode, symbol);
parser.deinit();
}
}
test "decimals are recognized" {
const test_cases = [_][]const u8{
"012314aslkfdj",
"1234567890 43",
"1234567891234567889\n123124",
};
const expected = [_]u64{
12314,
1234567890,
1234567891234567889,
};
for (0..3) |i| {
var parser = Parser.init(std.testing.allocator, test_cases[i]);
const symbol = try parser.identifyDecimal();
try std.testing.expectEqual(expected[i], symbol);
parser.deinit();
}
}
test "malformed decimals are not recognized" {
const test_cases = [_][]const u8{
"",
"asdf123",
" 123",
"12345678901234567890123456789012345678901234567890",
};
for (test_cases) |case| {
var parser = Parser.init(std.testing.allocator, case);
const symbol = parser.identifyDecimal();
try std.testing.expectEqual(error.NoDecimal, symbol);
parser.deinit();
}
}
test "hexadecimals are recognized" {
const test_cases = [_][]const u8{
"#012314saslkfdj",
"#1234567890abcdef 43",
"#1234567891\n123124",
};
const expected = [_]u64{
0x12314,
0x1234567890abcdef,
0x1234567891,
};
for (0..3) |i| {
var parser = Parser.init(std.testing.allocator, test_cases[i]);
const symbol = try parser.identifyHexadecimal();
try std.testing.expectEqual(expected[i], symbol);
parser.deinit();
}
}
test "malformed hexadecimals are not recognized" {
const test_cases = [_][]const u8{
"",
"sasdf123",
" 123",
"#12345678901234567890123456789012345678901234567890",
"#",
};
for (test_cases) |case| {
var parser = Parser.init(std.testing.allocator, case);
const symbol = parser.identifyHexadecimal();
try std.testing.expectEqual(error.NoHexadecimal, symbol);
parser.deinit();
}
}
test "characters are recognized" {
const test_cases = [_][]const u8{
"'a'",
"'1'",
"'𒀤'",
};
const expected = [_]u21{
'a',
'1',
'𒀤',
};
for (0..3) |i| {
var parser = Parser.init(std.testing.allocator, test_cases[i]);
const symbol = try parser.identifyChar();
try std.testing.expectEqual(expected[i], symbol);
parser.deinit();
}
}
test "invalid unicode sequences are not characters" {
const test_cases = [_][]const u8{
"'asdf'",
"'asdfg'",
"'as'",
"''",
"'",
};
for (test_cases) |case| {
var parser = Parser.init(std.testing.allocator, case);
const symbol = parser.identifyChar();
try std.testing.expectEqual(error.NoChar, symbol);
parser.deinit();
}
}
test "strings are recognized" {
const test_cases = [_][]const u8{
"\" \"",
"\"aslkdfjlaskdfj lkasjflkasjdflaksjfd''12309)($)(#$[[]𒀤\"",
};
const expected = [_][]const u8{
" ",
"aslkdfjlaskdfj lkasjflkasjdflaksjfd''12309)($)(#$[[]𒀤",
};
for (0..2) |i| {
var parser = Parser.init(std.testing.allocator, test_cases[i]);
const symbol = try parser.identifyString();
try std.testing.expect(std.mem.eql(u8, expected[i], symbol));
parser.deinit();
}
}
test "invalid strings are not recognized" {
const test_cases = [_][]const u8{
"\"\"",
"\"",
"\"\n\"",
};
for (test_cases) |case| {
var parser = Parser.init(std.testing.allocator, case);
const symbol = parser.identifyString();
try std.testing.expectEqual(error.NoString, symbol);
parser.deinit();
}
}
test "constants are recognized" {
const test_cases = [_][]const u8{
"1234567890 1234",
"#1234567890abcdef;%#*(",
"'a'uuuuuu",
"\"hello \"world",
};
const expected = [_]ConstantValue{
ConstantValue{ .number = 1234567890 },
ConstantValue{ .number = 0x1234567890abcdef },
ConstantValue{ .number = 'a' },
ConstantValue{ .string = "hello " },
};
for (0..4) |i| {
var parser = Parser.init(std.testing.allocator, test_cases[i]);
defer parser.deinit();
const symbol = try parser.identifyConstant();
switch (symbol) {
.number => try std.testing.expectEqual(expected[i].number, symbol.number),
.string => try std.testing.expect(std.mem.eql(u8, expected[i].string, symbol.string)),
}
}
}
test "basic primaries are identified" {
const test_cases = [_][]const u8{
"1234",
"@",
"'a'",
"\"hello world\"",
"+1234",
"-#1",
"~#0",
"$123",
};
const expected = [_]ExpressionResult{
ExpressionResult{ .number = NumberValue{ .pure = 1234 } },
ExpressionResult{ .number = NumberValue{ .pure = 0 } },
ExpressionResult{ .number = NumberValue{ .pure = 'a' } },
ExpressionResult{ .string = "hello world" },
ExpressionResult{ .number = NumberValue{ .pure = 1234 } },
ExpressionResult{ .number = NumberValue{ .pure = 0xFFFFFFFFFFFFFFFF } },
ExpressionResult{ .number = NumberValue{ .pure = 0xFFFFFFFFFFFFFFFF } },
ExpressionResult{ .number = NumberValue{ .register = 123 } },
};
for (0..test_cases.len) |i| {
var parser = Parser.init(std.testing.allocator, test_cases[i]);
defer parser.deinit();
const symbol = try parser.identifyPrimary();
switch (symbol) {
.number => try std.testing.expectEqual(expected[i].number, symbol.number),
.string => try std.testing.expect(std.mem.eql(u8, expected[i].string, symbol.string)),
}
}
}
test "invalid primaries are detected" {
const test_cases = [_][]const u8{
"$256",
"$~0",
};
for (test_cases) |case| {
var parser = Parser.init(std.testing.allocator, case);
defer parser.deinit();
const symbol = parser.identifyPrimary();
try std.testing.expectEqual(error.NoPrimary, symbol);
}
}
|