authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-08-07 18:35:19 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-08-07 18:35:19 -07:00
log0c4b6bca9b5c601bba5cbbc25abd8d81c58bc1cb
tree7b1de91be452efaffb8873e0b88745a14ea20fa1
parentd306c30b670ab35f059fa98866607c2dd11d7559

allow functions to have multiple arguments


2 files changed, 25 insertions(+), 6 deletions(-)

src/astgen.zig+19-4
......@@ -28,7 +28,7 @@ pub const Attr = struct {
2828
2929pub const Block = struct {
3030 name: Type,
31 args: []const string,
31 args: []const []const string,
3232 body: []const Value,
3333
3434 pub const Type = enum {
......@@ -42,7 +42,7 @@ pub const Block = struct {
4242
4343pub const Fn = struct {
4444 name: string,
45 args: []const string,
45 args: []const []const string,
4646};
4747
4848//
......@@ -131,7 +131,7 @@ const Parser = struct {
131131 if (self.tryEatSymbol("#")) {
132132 const w = self.eat(.word);
133133 if (std.meta.stringToEnum(Block.Type, w)) |name| {
134 const args = self.doReplacement();
134 const args = self.doArgs();
135135 var children: []const Value = &.{};
136136 while (!self.tryEatSymbol("/")) {
137137 children = children ++ &[_]Value{self.doValue()};
......@@ -146,7 +146,7 @@ const Parser = struct {
146146 }
147147 return Value{ .function = .{
148148 .name = w,
149 .args = self.doReplacement(),
149 .args = self.doArgs(),
150150 } };
151151 }
152152 return Value{ .replacement = self.doReplacement() };
......@@ -154,6 +154,21 @@ const Parser = struct {
154154 return Value{ .element = self.doElement() };
155155 }
156156
157 pub fn doArgs(comptime self: *Parser) []const []const string {
158 var ret: []const []const string = &.{};
159 var temp: []const string = &.{self.eat(.word)};
160 while (!self.tryEatSymbol("}")) {
161 if (self.tryEatSymbol(".")) {
162 temp = temp ++ &[_]string{self.eat(.word)};
163 } else {
164 ret = ret ++ &[_][]const string{temp};
165 temp = &.{self.eat(.word)};
166 }
167 }
168 ret = ret ++ &[_][]const string{temp};
169 return ret;
170 }
171
157172 pub fn doReplacement(comptime self: *Parser) []const string {
158173 var ret: []const string = &.{};
159174 ret = ret ++ &[_]string{self.eat(.word)};
src/lib.zig+6-2
......@@ -89,10 +89,10 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype
8989 @compileError("pek: compile: unsupported type: " ++ @typeName(TO));
9090 },
9191 .block => |v| {
92 const x = comptime search(data, v.args);
93
9492 switch (v.name) {
9593 .each => {
94 comptime assertEqual(v.args.len, 1);
95 const x = comptime search(data, v.args[0]);
9696 inline for (x) |item| {
9797 inline for (v.body) |val| {
9898 try do(writer, val, item, ctx, indent, flag1);
......@@ -121,3 +121,7 @@ fn entityLookupBefore(in: []const u8) ?htmlentities.Entity {
121121 }
122122 return null;
123123}
124
125fn assertEqual(comptime a: usize, comptime b: usize) void {
126 if (a != b) @compileError(std.fmt.comptimePrint("{d} != {d}", .{ a, b }));
127}