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 {...@@ -28,7 +28,7 @@ pub const Attr = struct {
2828
29pub const Block = struct {29pub const Block = struct {
30 name: Type,30 name: Type,
31 args: []const string,31 args: []const []const string,
32 body: []const Value,32 body: []const Value,
3333
34 pub const Type = enum {34 pub const Type = enum {
...@@ -42,7 +42,7 @@ pub const Block = struct {...@@ -42,7 +42,7 @@ pub const Block = struct {
4242
43pub const Fn = struct {43pub const Fn = struct {
44 name: string,44 name: string,
45 args: []const string,45 args: []const []const string,
46};46};
4747
48//48//
...@@ -131,7 +131,7 @@ const Parser = struct {...@@ -131,7 +131,7 @@ const Parser = struct {
131 if (self.tryEatSymbol("#")) {131 if (self.tryEatSymbol("#")) {
132 const w = self.eat(.word);132 const w = self.eat(.word);
133 if (std.meta.stringToEnum(Block.Type, w)) |name| {133 if (std.meta.stringToEnum(Block.Type, w)) |name| {
134 const args = self.doReplacement();134 const args = self.doArgs();
135 var children: []const Value = &.{};135 var children: []const Value = &.{};
136 while (!self.tryEatSymbol("/")) {136 while (!self.tryEatSymbol("/")) {
137 children = children ++ &[_]Value{self.doValue()};137 children = children ++ &[_]Value{self.doValue()};
...@@ -146,7 +146,7 @@ const Parser = struct {...@@ -146,7 +146,7 @@ const Parser = struct {
146 }146 }
147 return Value{ .function = .{147 return Value{ .function = .{
148 .name = w,148 .name = w,
149 .args = self.doReplacement(),149 .args = self.doArgs(),
150 } };150 } };
151 }151 }
152 return Value{ .replacement = self.doReplacement() };152 return Value{ .replacement = self.doReplacement() };
...@@ -154,6 +154,21 @@ const Parser = struct {...@@ -154,6 +154,21 @@ const Parser = struct {
154 return Value{ .element = self.doElement() };154 return Value{ .element = self.doElement() };
155 }155 }
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
157 pub fn doReplacement(comptime self: *Parser) []const string {172 pub fn doReplacement(comptime self: *Parser) []const string {
158 var ret: []const string = &.{};173 var ret: []const string = &.{};
159 ret = ret ++ &[_]string{self.eat(.word)};174 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...@@ -89,10 +89,10 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype
89 @compileError("pek: compile: unsupported type: " ++ @typeName(TO));89 @compileError("pek: compile: unsupported type: " ++ @typeName(TO));
90 },90 },
91 .block => |v| {91 .block => |v| {
92 const x = comptime search(data, v.args);
93
94 switch (v.name) {92 switch (v.name) {
95 .each => {93 .each => {
94 comptime assertEqual(v.args.len, 1);
95 const x = comptime search(data, v.args[0]);
96 inline for (x) |item| {96 inline for (x) |item| {
97 inline for (v.body) |val| {97 inline for (v.body) |val| {
98 try do(writer, val, item, ctx, indent, flag1);98 try do(writer, val, item, ctx, indent, flag1);
...@@ -121,3 +121,7 @@ fn entityLookupBefore(in: []const u8) ?htmlentities.Entity {...@@ -121,3 +121,7 @@ fn entityLookupBefore(in: []const u8) ?htmlentities.Entity {
121 }121 }
122 return null;122 return null;
123}123}
124
125fn assertEqual(comptime a: usize, comptime b: usize) void {
126 if (a != b) @compileError(std.fmt.comptimePrint("{d} != {d}", .{ a, b }));
127}