nob 实现最后一步编译框架 移动测试用例 准备for的新语法

This commit is contained in:
zzy committed 2026-11-02 21:41:19 +08:00
1 parent c4d1bbae92
commit bb8761d392
92 files changed
+803 -27

No files matched your search

+2
View File
@@ -96,6 +96,7 @@ type Body = enum {
label: usize,
iter_exprs: Vec,
pattern: usize,
pattern_type: usize,
body: usize,
else_branch: usize,
},
@@ -546,6 +547,7 @@ fn dump_node(ast: *Ast, buf: *ByteBuffer, stack: *Vec, ref: usize, last: bool):
ok = dump_opt(ast, buf, stack, f.label, false) &&
dump_list(ast, buf, stack, f.iter_exprs) &&
dump_opt(ast, buf, stack, f.pattern, false) &&
dump_opt(ast, buf, stack, f.pattern_type, true) &&
dump_opt(ast, buf, stack, f.body, false) &&
dump_opt(ast, buf, stack, f.else_branch, true);
},
+12 -1
View File
@@ -2122,7 +2122,17 @@ fn parse_for_expr(self: *Parser): usize {
advance(self);
var pattern = parse_pattern(self);
if pattern == 0 { ret 0; }
if !expect_punct(self, .Colon, "':' after for pattern") { ret 0; }
var pattern_type: usize = 0;
if at_punct(self, .Colon) {
advance(self);
pattern_type = parse_type_expr(self);
if pattern_type == 0 { ret 0; }
}
if !at_keyword(self, .In) {
parse_error(self, peek(self), "'in' after for pattern");
ret 0;
}
advance(self);
var iter_exprs = ref_vec();
self.stop_agg = self.stop_agg + 1;
var e0 = parse_expr(self);
@@ -2145,6 +2155,7 @@ fn parse_for_expr(self: *Parser): usize {
.label = 0,
.iter_exprs = iter_exprs,
.pattern = pattern,
.pattern_type = pattern_type,
.body = body,
.else_branch = else_branch,
}), kw);
+5 -1
View File
@@ -7,7 +7,7 @@ type .{ .ByteView } = @import("utils/byte_view.spl");
type Keyword = enum {
Any, As, Bool, Break, Catch, Comptime, Const, Continue, Defer, Else, Enum,
Errdefer, False, Fn, For, If, Loop, Match, Null, Ret, Shape, Struct, True,
Errdefer, False, Fn, For, If, In, Loop, Match, Null, Ret, Shape, Struct, True,
Try, Type, Undefined, Union, Var, While, Underscore,
}
@@ -75,6 +75,7 @@ fn keyword_of(text: ByteView): KeywordResult {
if c0 == 'a' && is(text, "as") { ret .Yes(.As); }
if c0 == 'f' && is(text, "fn") { ret .Yes(.Fn); }
if c0 == 'i' && is(text, "if") { ret .Yes(.If); }
if c0 == 'i' && is(text, "in") { ret .Yes(.In); }
ret .No;
}
if n == 3 {
@@ -147,6 +148,7 @@ fn keyword_name(kw: Keyword): []u8 {
.Fn => { ret "KW_FN"; },
.For => { ret "KW_FOR"; },
.If => { ret "KW_IF"; },
.In => { ret "KW_IN"; },
.Loop => { ret "KW_LOOP"; },
.Match => { ret "KW_MATCH"; },
.Null => { ret "KW_NULL"; },
@@ -259,6 +261,7 @@ fn expect_keyword(result: KeywordResult, expected: Keyword) {
fn test_keywords() {
expect_keyword(keyword_of(view("fn")), .Fn);
expect_keyword(keyword_of(view("in")), .In);
expect_keyword(keyword_of(view("while")), .While);
expect_keyword(keyword_of(view("_")), .Underscore);
match keyword_of(view("foo")) {
@@ -337,6 +340,7 @@ fn test_kind_names() {
expect_kind_name(.Keyword(.Fn), "KW_FN");
expect_kind_name(.Keyword(.For), "KW_FOR");
expect_kind_name(.Keyword(.If), "KW_IF");
expect_kind_name(.Keyword(.In), "KW_IN");
expect_kind_name(.Keyword(.Loop), "KW_LOOP");
expect_kind_name(.Keyword(.Match), "KW_MATCH");
expect_kind_name(.Keyword(.Null), "KW_NULL");