authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-08-14 13:58:08 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-08-14 13:58:08 -07:00
log48b67051c2548d668e1bde1ed93a01eb2f21d385
tree5f391f514e4de41403c0e6931e7fb75288cf4c4e
parentce0478831d26f7bde9c49be4d377b02b5955499a

make `compile` accept an allocator


1 files changed, 23 insertions(+), 25 deletions(-)

src/lib.zig+23-25
......@@ -17,14 +17,14 @@ pub fn parse(comptime input: []const u8) astgen.Value {
1717 return astgen.Value{ .element = astgen.do(tokenize.do(input, &.{ '[', '=', ']', '(', ')', '{', '}', '#', '/', '.', '<', '>' })) };
1818}
1919
20pub fn compile(writer: anytype, comptime value: astgen.Value, data: anytype) !void {
20pub fn compile(alloc: *std.mem.Allocator, writer: anytype, comptime value: astgen.Value, data: anytype) !void {
2121 try writer.writeAll("<!DOCTYPE html>\n");
22 try do(writer, value, data, data, 0, false);
22 try do(alloc, writer, value, data, data, 0, false);
2323 try writer.writeAll("\n");
2424}
2525
26fn do(writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype, indent: usize, flag1: bool) anyerror!void {
27 switch (value) {
26fn do(alloc: *std.mem.Allocator, writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype, indent: usize, flag1: bool) anyerror!void {
27 switch (comptime value) {
2828 .element => |v| {
2929 const hastext = for (v.children) |x| {
3030 if (x == .string or x == .replacement) break true;
......@@ -56,7 +56,7 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype
5656
5757 if (!hastext) try writer.writeAll("\n");
5858 inline for (v.children) |it| {
59 try do(writer, it, data, ctx, indent + 1, !hastext);
59 try do(alloc, writer, it, data, ctx, indent + 1, !hastext);
6060 }
6161 if (!hastext) for (range(indent)) |_| try writer.writeAll(" ");
6262 try writer.print("</{s}>", .{v.name});
......@@ -97,39 +97,37 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype
9797 switch (v.name) {
9898 .each => {
9999 comptime assertEqual(v.args.len, 1);
100 for (x) |item| try do(writer, body, item, ctx, indent, flag1);
100 for (x) |item| try do(alloc, writer, body, item, ctx, indent, flag1);
101101 },
102102 .@"if" => {
103103 comptime assertEqual(v.args.len, 1);
104 switch (TI) {
105 .Bool => try doif(writer, body, bottom, data, ctx, indent, flag1, x, true),
106 .Optional => try docap(writer, body, bottom, data, ctx, indent, flag1, x, true),
107 else => unreachable,
104 switch (comptime TI) {
105 .Bool => try doif(alloc, writer, body, bottom, data, ctx, indent, flag1, x),
106 .Optional => try docap(alloc, writer, body, bottom, data, ctx, indent, flag1, x),
108107 }
109108 },
110109 .ifnot => {
111110 comptime assertEqual(v.args.len, 1);
112 switch (TI) {
113 .Bool => try doif(writer, body, bottom, data, ctx, indent, flag1, x, false),
114 .Optional => try docap(writer, body, bottom, data, ctx, indent, flag1, x, false),
115 else => unreachable,
111 switch (comptime TI) {
112 .Bool => try doif(alloc, writer, body, bottom, data, ctx, indent, flag1, !x),
113 .Optional => try docap(alloc, writer, body, bottom, data, ctx, indent, flag1, !x),
116114 }
117115 },
118116 .ifequal => {
119117 comptime assertEqual(v.args.len, 2);
120118 const y = if (comptime std.mem.eql(u8, v.args[1][0], "this")) search(v.args[1][1..], data) else search(v.args[1], ctx);
121 try doif(writer, body, bottom, data, ctx, indent, flag1, x == y, true);
119 try doif(alloc, writer, body, bottom, data, ctx, indent, flag1, x == y);
122120 },
123121 .ifnotequal => {
124122 comptime assertEqual(v.args.len, 2);
125123 const y = if (comptime std.mem.eql(u8, v.args[1][0], "this")) search(v.args[1][1..], data) else search(v.args[1], ctx);
126 try doif(writer, body, bottom, data, ctx, indent, flag1, x != y, true);
124 try doif(alloc, writer, body, bottom, data, ctx, indent, flag1, x != y);
127125 },
128126 }
129127 },
130128 .body => |v| {
131129 inline for (v) |val| {
132 try do(writer, val, data, ctx, indent, flag1);
130 try do(alloc, writer, val, data, ctx, indent, flag1);
133131 }
134132 },
135133 else => unreachable,
......@@ -199,18 +197,18 @@ fn contains(haystack: []const []const u8, needle: []const u8) bool {
199197 return false;
200198}
201199
202fn doif(writer: anytype, comptime top: astgen.Value, comptime bottom: astgen.Value, data: anytype, ctx: anytype, indent: usize, flag1: bool, flag2: bool, flag3: bool) anyerror!void {
203 if (flag2 == flag3) {
204 try do(writer, top, data, ctx, indent, flag1);
200fn doif(alloc: *std.mem.Allocator, writer: anytype, comptime top: astgen.Value, comptime bottom: astgen.Value, data: anytype, ctx: anytype, indent: usize, flag1: bool, flag2: bool) anyerror!void {
201 if (flag2) {
202 try do(alloc, writer, top, data, ctx, indent, flag1);
205203 } else {
206 try do(writer, bottom, data, ctx, indent, flag1);
204 try do(alloc, writer, bottom, data, ctx, indent, flag1);
207205 }
208206}
209207
210fn docap(writer: anytype, comptime top: astgen.Value, comptime bottom: astgen.Value, data: anytype, ctx: anytype, indent: usize, flag1: bool, flag2: bool, flag3: bool) anyerror!void {
211 if (flag2 == flag3) {
212 try do(writer, top, data, ctx, indent, flag1);
208fn docap(alloc: *std.mem.Allocator, writer: anytype, comptime top: astgen.Value, comptime bottom: astgen.Value, data: anytype, ctx: anytype, indent: usize, flag1: bool, flag2: bool) anyerror!void {
209 if (flag2) |_| {
210 try do(alloc, writer, top, data, ctx, indent, flag1);
213211 } else {
214 try do(writer, bottom, data, ctx, indent, flag1);
212 try do(alloc, writer, bottom, data, ctx, indent, flag1);
215213 }
216214}