<<
path:
root/public/blog.git/html/modules/uniform/walk.jai
blob: 6198b8ea89dc24b345bc86736f311f1fbe95e8b4
[raw]
[clear marker]
2Walk_State :: struct (T: Type) {
10walk_state :: (re: *Regexp_Node, parent_arg: $T) -> Walk_State(T) {
13 s.parent_arg = parent_arg;
14 s.child_args.allocator = temp;
18Walk :: struct (C: Type, T: Type) {
20 post_visit := no_post;
21 short_visit := no_short;
23 max_visits := 1000000;
25 //ToDo: Use [] T as child_args
26 no_post :: (ctx: C, re: *Regexp_Node, parent_arg: T, pre_arg: T, child_args: [..] T) -> T {
30 no_pre :: (ctx: C, re: *Regexp_Node, parent_arg: T) -> T, bool {
31 return parent_arg, false;
34 no_short :: (ctx: C, re: *Regexp_Node, parent_arg: T) -> T {
38 no_copy :: (ctx: C, arg: T) -> T {
43walk :: (ctx: C, re: *Regexp_Node, initial_value: T, w: Walk($C, $T)) -> T, stopped_early: bool {
44 do_visit :: (ctx: C, stack: *[..] Walk_State(T), w: *Walk(C, T)) -> T, stopped_early: bool, done: bool {
45 s := *(stack.*)[stack.count - 1];
48 if (w.max_visits < 0) {
49 return w.short_visit(ctx, s.re, s.parent_arg), true, true;
53 s.pre_arg, stop = w.pre_visit(ctx, s.re, s.parent_arg);
55 return s.pre_arg, false, true;
58 array_resize(*s.child_args, s.re.subs.count, false);
62 if (s.re.subs.count > 0) {
63 if (s.n < s.re.subs.count) {
64 if (w.copy && s.n > 0 && s.re.subs[s.n - 1] == s.re.subs[s.n]) {
65 s.child_args[s.n] = w.copy(ctx, s.child_args[s.n - 1]);
68 array_add(stack, walk_state(s.re.subs[s.n], s.pre_arg));
71 return t, false, false;
75 return w.post_visit(ctx, s.re, s.parent_arg, s.pre_arg, s.child_args), false, true;
77 assert(re != null, "Missing regexp");
79 stack: [..] Walk_State(T);
80 stack.allocator = temp;
81 array_add(*stack, walk_state(re, initial_value));
82 stopped_early := false;
85 t, early, done := do_visit(ctx, *stack, *w);
91 // We've finished stack.top().
92 // Update next guy down.
94 if !stack.count return t, stopped_early;
95 s := *stack[stack.count - 1];
96 if s.child_args.count {
97 s.child_args[s.n] = t;
103 // This will never be reached, but Jai complains otherwise
104 // @Cleanup: remove this once Jai is fixed