From e4f9b993e97b6e6e790810b6d261dc5bf61b0513 Mon Sep 17 00:00:00 2001 From: Nicholas Date: Mon, 15 Nov 2021 19:48:42 -0800 Subject: Feat: added functions --- src/cmd/rc/exec.c | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) (limited to 'src/cmd/rc/exec.c') 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) @@ -1061,6 +1078,26 @@ Xunlocal(void) efree(v); } +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) { @@ -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++){ -- cgit v1.2.1