| author | |
| committer | |
| log | 8e154277a37b31d312596f6108f123eb597846da |
| tree | 291559c76af1f87fe8ce59d8ff8a412a0f108b02 |
| parent | bcb022592b5751ff9acd5b9ac18d0dfea8fe447f |
2 files changed, 34 insertions(+), 11 deletions(-)
src/astgen.zig+9-4| ... | ... | @@ -84,7 +84,7 @@ const Parser = struct { |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | fn tryEatSymbol(comptime self: *Parser, comptime needle: string) bool { |
| 87 | switch (self.tokens[self.index]) { | |
| 87 | switch (self.tokens[self.index].data) { | |
| 88 | 88 | .symbol => |sym| { |
| 89 | 89 | if (std.mem.eql(u8, sym, needle)) { |
| 90 | 90 | self.index += 1; |
| ... | ... | @@ -126,7 +126,7 @@ const Parser = struct { |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | pub fn doValue(comptime self: *Parser) Value { |
| 129 | if (self.tokens[self.index] == .string) { | |
| 129 | if (self.tokens[self.index].data == .string) { | |
| 130 | 130 | return Value{ .string = self.eat(.string) }; |
| 131 | 131 | } |
| 132 | 132 | if (self.tryEatSymbol("{")) { |
| ... | ... | @@ -193,8 +193,13 @@ const Parser = struct { |
| 193 | 193 | return ret; |
| 194 | 194 | } |
| 195 | 195 | |
| 196 | fn eat(comptime self: *Parser, comptime typ: std.meta.Tag(Token)) string { | |
| 196 | fn eat(comptime self: *Parser, comptime typ: std.meta.Tag(Token.Data)) string { | |
| 197 | 197 | defer self.index += 1; |
| 198 | return @field(self.tokens[self.index], @tagName(typ)); | |
| 198 | const tok = self.tokens[self.index]; | |
| 199 | const tag = std.meta.activeTag(tok.data); | |
| 200 | if (tag != typ) { | |
| 201 | @compileError(std.fmt.comptimePrint("pek: file:{d}:{d}: expected {s}, found {s}", .{ tok.line, tok.pos, @tagName(tag), @tagName(typ) })); | |
| 202 | } | |
| 203 | return @field(tok.data, @tagName(typ)); | |
| 199 | 204 | } |
| 200 | 205 | }; |
src/tokenize.zig+25-7| ... | ... | @@ -4,12 +4,18 @@ const string = []const u8; |
| 4 | 4 | // |
| 5 | 5 | // |
| 6 | 6 | |
| 7 | pub const Token = union(enum) { | |
| 8 | word: string, | |
| 9 | symbol: string, | |
| 10 | string: string, | |
| 7 | pub const Token = struct { | |
| 8 | data: Data, | |
| 9 | line: usize, | |
| 10 | pos: usize, | |
| 11 | 11 | |
| 12 | 12 | pub const skippedChars = &[_]u8{ ' ', '\n', '\t', '\r' }; |
| 13 | ||
| 14 | pub const Data = union(enum) { | |
| 15 | word: string, | |
| 16 | symbol: string, | |
| 17 | string: string, | |
| 18 | }; | |
| 13 | 19 | }; |
| 14 | 20 | |
| 15 | 21 | // |
| ... | ... | @@ -58,7 +64,11 @@ pub fn do(comptime input: string, comptime symbols: []const u8) []const Token { |
| 58 | 64 | } |
| 59 | 65 | if (mode == 2) { |
| 60 | 66 | if (c == input[start]) { |
| 61 | ret = ret ++ &[_]Token{.{ .string = input[start .. i + 1] }}; | |
| 67 | ret = ret ++ &[_]Token{.{ | |
| 68 | .data = .{ .string = input[start .. i + 1] }, | |
| 69 | .line = line, | |
| 70 | .pos = pos, | |
| 71 | }}; | |
| 62 | 72 | start = i + 1; |
| 63 | 73 | end = i; |
| 64 | 74 | mode = 0; |
| ... | ... | @@ -84,7 +94,11 @@ pub fn do(comptime input: string, comptime symbols: []const u8) []const Token { |
| 84 | 94 | if (shouldFlush) { |
| 85 | 95 | if (mode == 0) { |
| 86 | 96 | if (end - start > 0) { |
| 87 | ret = ret ++ &[_]Token{.{ .word = input[start..end] }}; | |
| 97 | ret = ret ++ &[_]Token{.{ | |
| 98 | .data = .{ .word = input[start..end] }, | |
| 99 | .line = line, | |
| 100 | .pos = pos, | |
| 101 | }}; | |
| 88 | 102 | start = i; |
| 89 | 103 | end = i; |
| 90 | 104 | } |
| ... | ... | @@ -93,7 +107,11 @@ pub fn do(comptime input: string, comptime symbols: []const u8) []const Token { |
| 93 | 107 | end += 1; |
| 94 | 108 | } |
| 95 | 109 | if (std.mem.indexOfScalar(u8, symbols, c)) |_| { |
| 96 | ret = ret ++ &[_]Token{.{ .symbol = s }}; | |
| 110 | ret = ret ++ &[_]Token{.{ | |
| 111 | .data = .{ .symbol = s }, | |
| 112 | .line = line, | |
| 113 | .pos = pos, | |
| 114 | }}; | |
| 97 | 115 | start += 1; |
| 98 | 116 | end += 1; |
| 99 | 117 | } |