-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.scanner.zig
More file actions
125 lines (100 loc) · 6.25 KB
/
test.scanner.zig
File metadata and controls
125 lines (100 loc) · 6.25 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
const std = @import("std");
const Scanner = @import("./scanner.zig").Scanner;
const TokenType = @import("./scanner.zig").TokenType;
test "scanner should correctly tokenize only keywords" {
const src = "var if else class fun return while for and or print super this true false nil";
var scanner = Scanner.init(src);
var token = scanner.scanToken();
std.debug.print("\nExpected: {}, Got: {}, lexeme: {s}\n", .{ TokenType.VAR, token.token_type, token.lexeme });
try std.testing.expect(token.token_type == TokenType.VAR);
try std.testing.expectEqualStrings("var", token.lexeme);
token = scanner.scanToken();
std.debug.print("\nExpected: {}, Got: {}, lexeme: {s}\n", .{ TokenType.IF, token.token_type, token.lexeme });
try std.testing.expect(token.token_type == TokenType.IF);
try std.testing.expectEqualStrings("if", token.lexeme);
token = scanner.scanToken();
std.debug.print("\nExpected: {}, Got: {}, lexeme: {s}\n", .{ TokenType.ELSE, token.token_type, token.lexeme });
try std.testing.expect(token.token_type == TokenType.ELSE);
try std.testing.expectEqualStrings("else", token.lexeme);
token = scanner.scanToken();
std.debug.print("\nExpected: {}, Got: {}, lexeme: {s}\n", .{ TokenType.CLASS, token.token_type, token.lexeme });
try std.testing.expect(token.token_type == TokenType.CLASS);
try std.testing.expectEqualStrings("class", token.lexeme);
token = scanner.scanToken();
std.debug.print("\nExpected: {}, Got: {}, lexeme: {s}\n", .{ TokenType.FUN, token.token_type, token.lexeme });
try std.testing.expect(token.token_type == TokenType.FUN);
try std.testing.expectEqualStrings("fun", token.lexeme);
token = scanner.scanToken();
std.debug.print("\nExpected: {}, Got: {}, lexeme: {s}\n", .{ TokenType.RETURN, token.token_type, token.lexeme });
try std.testing.expect(token.token_type == TokenType.RETURN);
try std.testing.expectEqualStrings("return", token.lexeme);
token = scanner.scanToken();
std.debug.print("\nExpected: {}, Got: {}, lexeme: {s}\n", .{ TokenType.WHILE, token.token_type, token.lexeme });
try std.testing.expect(token.token_type == TokenType.WHILE);
try std.testing.expectEqualStrings("while", token.lexeme);
token = scanner.scanToken();
std.debug.print("\nExpected: {}, Got: {}, lexeme: {s}\n", .{ TokenType.FOR, token.token_type, token.lexeme });
try std.testing.expect(token.token_type == TokenType.FOR);
try std.testing.expectEqualStrings("for", token.lexeme);
token = scanner.scanToken();
std.debug.print("\nExpected: {}, Got: {}, lexeme: {s}\n", .{ TokenType.AND, token.token_type, token.lexeme });
try std.testing.expect(token.token_type == TokenType.AND);
try std.testing.expectEqualStrings("and", token.lexeme);
token = scanner.scanToken();
std.debug.print("\nExpected: {}, Got: {}, lexeme: {s}\n", .{ TokenType.OR, token.token_type, token.lexeme });
try std.testing.expect(token.token_type == TokenType.OR);
try std.testing.expectEqualStrings("or", token.lexeme);
token = scanner.scanToken();
std.debug.print("\nExpected: {}, Got: {}, lexeme: {s}\n", .{ TokenType.PRINT, token.token_type, token.lexeme });
try std.testing.expect(token.token_type == TokenType.PRINT);
try std.testing.expectEqualStrings("print", token.lexeme);
token = scanner.scanToken();
std.debug.print("\nExpected: {}, Got: {}, lexeme: {s}\n", .{ TokenType.SUPER, token.token_type, token.lexeme });
try std.testing.expect(token.token_type == TokenType.SUPER);
try std.testing.expectEqualStrings("super", token.lexeme);
token = scanner.scanToken();
std.debug.print("\nExpected: {}, Got: {}, lexeme: {s}\n", .{ TokenType.THIS, token.token_type, token.lexeme });
try std.testing.expect(token.token_type == TokenType.THIS);
try std.testing.expectEqualStrings("this", token.lexeme);
token = scanner.scanToken();
std.debug.print("\nExpected: {}, Got: {}, lexeme: {s}\n", .{ TokenType.TRUE, token.token_type, token.lexeme });
try std.testing.expect(token.token_type == TokenType.TRUE);
try std.testing.expectEqualStrings("true", token.lexeme);
token = scanner.scanToken();
std.debug.print("\nExpected: {}, Got: {}, lexeme: {s}\n", .{ TokenType.FALSE, token.token_type, token.lexeme });
try std.testing.expect(token.token_type == TokenType.FALSE);
try std.testing.expectEqualStrings("false", token.lexeme);
token = scanner.scanToken();
std.debug.print("\nExpected: {}, Got: {}, lexeme: {s}\n", .{ TokenType.NIL, token.token_type, token.lexeme });
try std.testing.expect(token.token_type == TokenType.NIL);
try std.testing.expectEqualStrings("nil", token.lexeme);
token = scanner.scanToken();
try std.testing.expect(token.token_type == TokenType.EOF);
}
test "scanner should correctly tokenize source code" {
const src = "var x = 10;";
var scanner = Scanner.init(src);
var token = scanner.scanToken();
std.debug.print("Expected: {}, Got: {}\n", .{ TokenType.VAR, token.token_type });
try std.testing.expect(token.token_type == TokenType.VAR);
try std.testing.expectEqualStrings("var", token.lexeme);
token = scanner.scanToken();
std.debug.print("Expected: {}, Got: {}\n", .{ TokenType.IDENTIFIER, token.token_type });
try std.testing.expect(token.token_type == TokenType.IDENTIFIER);
try std.testing.expectEqualStrings("x", token.lexeme);
token = scanner.scanToken();
std.debug.print("Expected: {}, Got: {}\n", .{ TokenType.EQUAL, token.token_type });
try std.testing.expect(token.token_type == TokenType.EQUAL);
try std.testing.expectEqualStrings("=", token.lexeme);
token = scanner.scanToken();
std.debug.print("Expected: {}, Got: {}\n", .{ TokenType.NUMBER, token.token_type });
try std.testing.expect(token.token_type == TokenType.NUMBER);
try std.testing.expectEqualStrings("10", token.lexeme);
token = scanner.scanToken();
std.debug.print("Expected: {}, Got: {}\n", .{ TokenType.SEMICOLON, token.token_type });
try std.testing.expect(token.token_type == TokenType.SEMICOLON);
try std.testing.expectEqualStrings(";", token.lexeme);
token = scanner.scanToken();
std.debug.print("Expected: {}, Got: {}\n", .{ TokenType.EOF, token.token_type });
try std.testing.expect(token.token_type == TokenType.EOF);
}