Skip to content

Commit 58b687e

Browse files
committed
Zig: support yyfn:throw configuration.
This is useful for --recursive-functions code model, as it's necessary to use different syntax for function calls with an additional `try` keyword before the call expression (which would be impossible to do before this change). The test/codegen/zig/recursive_functions/basic.re test has been updated to demonstrate the new functionality.
1 parent 7f624d2 commit 58b687e

File tree

4 files changed

+36
-23
lines changed

4 files changed

+36
-23
lines changed

bootstrap/src/default_syntax_zig.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -235,15 +235,17 @@ static constexpr const char* DEFAULT_SYNTAX_ZIG =
235235
" topindent \"fn \" name \"(\"\n"
236236
" [arg{0:-2}: argname \": \" argtype \", \"]\n"
237237
" [arg{-1}: argname \": \" argtype]\n"
238-
" \") \" type \" {\" nl\n"
238+
" \") \" (.yyfn.throw ? throw \"!\") type \" {\" nl\n"
239239
" indent [stmt: stmt] dedent\n"
240240
" \"}\" nl;\n"
241241
"\n"
242242
"code:fncall =\n"
243-
" topindent (.retval ? retval \" = \" name) \"(\" [arg{0:-2}: arg \", \"] [arg{-1}: arg] \");\" nl;\n"
243+
" topindent (.retval ? retval \" = \") (.yyfn.throw ? \"try \")\n"
244+
" name \"(\" [arg{0:-2}: arg \", \"] [arg{-1}: arg] \");\" nl;\n"
244245
"\n"
245246
"code:tailcall =\n"
246-
" topindent (.retval ? \"return \" name) \"(\" [arg{0:-2}: arg \", \"] [arg{-1}: arg] \");\" nl;\n"
247+
" topindent (.retval ? \"return \") (.yyfn.throw ? \"try \")\n"
248+
" name \"(\" [arg{0:-2}: arg \", \"] [arg{-1}: arg] \");\" nl;\n"
247249
"\n"
248250
"code:recursive_functions = [fn: fndef nl];\n"
249251
"\n"

include/syntax/zig

+5-3
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,17 @@ code:fndef =
234234
topindent "fn " name "("
235235
[arg{0:-2}: argname ": " argtype ", "]
236236
[arg{-1}: argname ": " argtype]
237-
") " type " {" nl
237+
") " (.yyfn.throw ? throw "!") type " {" nl
238238
indent [stmt: stmt] dedent
239239
"}" nl;
240240

241241
code:fncall =
242-
topindent (.retval ? retval " = " name) "(" [arg{0:-2}: arg ", "] [arg{-1}: arg] ");" nl;
242+
topindent (.retval ? retval " = ") (.yyfn.throw ? "try ")
243+
name "(" [arg{0:-2}: arg ", "] [arg{-1}: arg] ");" nl;
243244

244245
code:tailcall =
245-
topindent (.retval ? "return " name) "(" [arg{0:-2}: arg ", "] [arg{-1}: arg] ");" nl;
246+
topindent (.retval ? "return ") (.yyfn.throw ? "try ")
247+
name "(" [arg{0:-2}: arg ", "] [arg{-1}: arg] ");" nl;
246248

247249
code:recursive_functions = [fn: fndef nl];
248250

test/codegen/zig/recursive_functions/basic.re

+9-4
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,25 @@ const State = struct {
77
cur: u32,
88
};
99

10+
const SyntaxError = error {
11+
UnexpectedCharacter
12+
};
13+
1014
/*!re2c
1115
re2c:api = custom;
12-
re2c:define:YYFN = ["lex;bool", "st;*State"];
16+
re2c:define:YYFN = ["lex;void", "st;*State"];
1317
re2c:define:YYPEEK = "st.str[st.cur]";
1418
re2c:define:YYSKIP = "st.cur += 1;";
1519
re2c:yyfill:enable = 0;
20+
re2c:yyfn:throw = "SyntaxError";
1621
1722
number = [1-9][0-9]*;
1823
19-
number { _ = st; return true; }
20-
* { _ = st; return false; }
24+
number { _ = st; return; }
25+
* { _ = st; return SyntaxError.UnexpectedCharacter; }
2126
*/
2227

2328
test {
2429
var st = State{.str = "1234", .cur = 0};
25-
try std.testing.expect(lex(&st));
30+
try lex(&st);
2631
}

test/codegen/zig/recursive_functions/basic.zig

+17-13
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,46 @@ const State = struct {
88
cur: u32,
99
};
1010

11+
const SyntaxError = error {
12+
UnexpectedCharacter
13+
};
14+
1115

12-
fn yy0(st: *State) bool {
16+
fn yy0(st: *State) SyntaxError!void {
1317
const yych = st.str[st.cur];
1418
st.cur += 1;
1519
switch (yych) {
16-
0x31...0x39 => { return yy2(st); },
17-
else => { return yy1(st); },
20+
0x31...0x39 => { return try yy2(st); },
21+
else => { return try yy1(st); },
1822
}
1923
}
2024

21-
fn yy1(st: *State) bool {
22-
_ = st; return false;
25+
fn yy1(st: *State) SyntaxError!void {
26+
_ = st; return SyntaxError.UnexpectedCharacter;
2327
}
2428

25-
fn yy2(st: *State) bool {
29+
fn yy2(st: *State) SyntaxError!void {
2630
const yych = st.str[st.cur];
2731
switch (yych) {
2832
0x30...0x39 => {
2933
st.cur += 1;
30-
return yy2(st);
34+
return try yy2(st);
3135
},
32-
else => { return yy3(st); },
36+
else => { return try yy3(st); },
3337
}
3438
}
3539

36-
fn yy3(st: *State) bool {
37-
_ = st; return true;
40+
fn yy3(st: *State) SyntaxError!void {
41+
_ = st; return;
3842
}
3943

40-
fn lex(st: *State) bool {
41-
return yy0(st);
44+
fn lex(st: *State) SyntaxError!void {
45+
return try yy0(st);
4246
}
4347

4448

4549

4650
test {
4751
var st = State{.str = "1234", .cur = 0};
48-
try std.testing.expect(lex(&st));
52+
try lex(&st);
4953
}

0 commit comments

Comments
 (0)