summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apl9.h6
-rw-r--r--functions.c4
-rw-r--r--lexer.c1
-rw-r--r--symbol.c5
4 files changed, 11 insertions, 5 deletions
diff --git a/apl9.h b/apl9.h
index bbba10f..2cf0069 100644
--- a/apl9.h
+++ b/apl9.h
@@ -153,6 +153,7 @@ struct Function
QuadnameDef *quad;
FunctionTrain train;
};
+ DfnFrame *scope;
Array *left;
};
@@ -219,7 +220,8 @@ struct DfnFrame
Array *right;
Datum *lefto;
Datum *righto;
- DfnFrame *prev;
+ DfnFrame *prev; /* prev in the call stack */
+ DfnFrame *chain; /* prev in the lexical scope */
};
struct ThreadData
@@ -279,7 +281,7 @@ Datum *eval(Statement *, int);
Symbol *getsym(Rune *, int);
void initsymtab(void);
DfnFrame *getcurrentdfn(void);
-DfnFrame *pushdfnframe(Rune *, Datum *, Datum *, Array *, Array *);
+DfnFrame *pushdfnframe(Rune *, DfnFrame *, Datum *, Datum *, Array *, Array *);
void popdfnframe(void);
vlong globalIO(void);
void globalIOset(vlong);
diff --git a/functions.c b/functions.c
index 46cbe7a..7e462c3 100644
--- a/functions.c
+++ b/functions.c
@@ -141,7 +141,7 @@ runfunc(Function f, Array *left, Array *right)
code = f.operator.dop;
}
- pushdfnframe(code, lefto, righto, left, right);
+ pushdfnframe(code, f.scope, lefto, righto, left, right);
Datum *dfnres = evalline(code, nil, 0);
popdfnframe();
@@ -228,6 +228,7 @@ rundfn(Rune *code, Datum *lefto, Datum *righto, Array *left, Array *right)
Function dfn;
dfn.type = FunctypeDfn;
dfn.dfn = code;
+ dfn.scope = getcurrentdfn();
return runfunc(dfn, left, right);
}else if(lefto != nil){
Function dop;
@@ -237,6 +238,7 @@ rundfn(Rune *code, Datum *lefto, Datum *righto, Array *left, Array *right)
dop.operator.right = righto;
dop.operator.dyadic = righto != nil;
dop.operator.dop = code;
+ dop.scope = getcurrentdfn();
return runfunc(dop, left, right);
}else{
throwerror(L"Malformed call to rundfn", ENotImplemented);
diff --git a/lexer.c b/lexer.c
index ed2c2e4..93fdb62 100644
--- a/lexer.c
+++ b/lexer.c
@@ -118,6 +118,7 @@ lexline(InputStream *input, int toplevel)
stmt->toks[stmt->ntoks] = allocdatum(FunctionTag, 0);
stmt->toks[stmt->ntoks]->func.type = FunctypeDfn;
stmt->toks[stmt->ntoks]->func.dfn = runestrdup(buf);
+ stmt->toks[stmt->ntoks]->func.scope = getcurrentdfn();
}else{
stmt->toks[stmt->ntoks] = allocdatum(oplevel == 1 ? MonadicOpTag : DyadicOpTag, 0);
stmt->toks[stmt->ntoks]->operator.type = OperatortypeDop;
diff --git a/symbol.c b/symbol.c
index 8f4797a..6e9aa4a 100644
--- a/symbol.c
+++ b/symbol.c
@@ -34,7 +34,7 @@ getsym(Rune *name, int fresh)
return tab->syms[i];
}
if(dfn)
- dfn = dfn->prev;
+ dfn = dfn->chain;
else
done = 1;
}while(!done && !fresh);
@@ -108,7 +108,7 @@ getcurrentdfn(void)
}
DfnFrame *
-pushdfnframe(Rune *code, Datum *lefto, Datum *righto, Array *left, Array *right)
+pushdfnframe(Rune *code, DfnFrame *scope, Datum *lefto, Datum *righto, Array *left, Array *right)
{
ThreadData *td = getthreaddata();
DfnFrame *new = emalloc(sizeof(DfnFrame));
@@ -129,6 +129,7 @@ pushdfnframe(Rune *code, Datum *lefto, Datum *righto, Array *left, Array *right)
new->right = right;
incarrayref(right);
new->prev = td->currentdfn;
+ new->chain = scope;
td->currentdfn = new;
return new;
}