authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-08-14 13:58:57 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-08-14 13:58:57 -07:00
log4f7eb30e4050d1816fff5c39d3411c11a2088494
tree3f429fbfb9529b32ca0147d2575353c3b67bd5bd
parentd644cb06a389c433e1493ea19182fe04f0657edb

add support for custom functions


1 files changed, 22 insertions(+), 0 deletions(-)

src/lib.zig+22
...@@ -9,6 +9,7 @@...@@ -9,6 +9,7 @@
9const std = @import("std");9const std = @import("std");
10const range = @import("range").range;10const range = @import("range").range;
11const htmlentities = @import("htmlentities");11const htmlentities = @import("htmlentities");
12const root = @import("root");
1213
13const tokenize = @import("./tokenize.zig");14const tokenize = @import("./tokenize.zig");
14const astgen = @import("./astgen.zig");15const astgen = @import("./astgen.zig");
...@@ -134,6 +135,27 @@ fn do(alloc: *std.mem.Allocator, writer: anytype, comptime value: astgen.Value,...@@ -134,6 +135,27 @@ fn do(alloc: *std.mem.Allocator, writer: anytype, comptime value: astgen.Value,
134 try do(alloc, writer, val, data, ctx, indent, flag1);135 try do(alloc, writer, val, data, ctx, indent, flag1);
135 }136 }
136 },137 },
138 .function => |v| {
139 if (@hasDecl(root, "pek_" ++ v.name)) {
140 const func = @field(root, "pek_" ++ v.name);
141 var args: FnArgsTuple(func) = undefined;
142 args.@"0" = alloc;
143 inline for (v.args) |arg, i| {
144 const field_name = comptime std.fmt.comptimePrint("{d}", .{i + 1});
145 @field(args, field_name) = if (comptime std.mem.eql(u8, arg[0], "this")) search(arg[1..], data) else search(arg, ctx);
146 }
147 const s: []const u8 = try @call(.{}, func, args);
148 for (s) |c| {
149 if (entityLookupBefore(&[_]u8{c})) |ent| {
150 try writer.writeAll(ent.entity);
151 } else {
152 try writer.writeAll(&[_]u8{c});
153 }
154 }
155 return;
156 }
157 @compileError("pek: unknown custom function: " ++ v.name);
158 },
137 else => unreachable,159 else => unreachable,
138 }160 }
139}161}