diff options
author | Nicholas <nbnoll@eml.cc> | 2021-11-15 19:48:42 -0800 |
---|---|---|
committer | Nicholas <nbnoll@eml.cc> | 2021-11-15 19:48:42 -0800 |
commit | e4f9b993e97b6e6e790810b6d261dc5bf61b0513 (patch) | |
tree | db386a2dd2b58e207deb1e3f44a84e76827c2621 /src/cmd/rc/exec.c | |
parent | a93cc453bd8072e7d79ebe8f00614e1b99eb5852 (diff) |
Feat: added functions
Diffstat (limited to 'src/cmd/rc/exec.c')
-rw-r--r-- | src/cmd/rc/exec.c | 42 |
1 files changed, 39 insertions, 3 deletions
diff --git a/src/cmd/rc/exec.c b/src/cmd/rc/exec.c index 2bdaebd..71f5359 100644 --- a/src/cmd/rc/exec.c +++ b/src/cmd/rc/exec.c @@ -336,9 +336,26 @@ run(Code *c, int pc, Var *local, int inherit) runner = new; } + // ----------------------------------------------------------------------- // exported builtins +void +xfunc(Var *v) +{ + Word *star; + popword(); // "func" + star = runner->args->word; + runner->args->word = nil; + poplist(); // args + + run(v->func.code, v->func.start, runner->local, 1); + + runner->local = makevar(strdup("*"), runner->local); + runner->local->val = star; + runner->local->new = 1; +} + // XXX: find a better place for these Word* makeword(char *str, Word *link) @@ -1062,6 +1079,26 @@ Xunlocal(void) } void +Xfunc(void) +{ + Var *v; + Word *arg; + int end; + + end = runner->code.exe[runner->code.i].i; + for(arg=runner->args->word; arg; arg=arg->link){ + v = var(arg->str); + if(v->func.code) + freecode(v->func.code); + v->func.code = copycode(runner->code.exe); + v->func.start = runner->code.i+2; + v->newfunc = 1; + } + runner->code.i = end; + poplist(); +} + +void Xasync(void) { int pid; @@ -1193,9 +1230,8 @@ Xbasic(void) } v = var(arg->str); - if(v->func){ - return; - } + if(v->func.code) + return xfunc(v); // see if it matches a builtin for(b = builtin; b->name; b++){ |