Skip to content
This repository has been archived by the owner on Jan 16, 2021. It is now read-only.

Commit

Permalink
1818 - 'or', 'all' and 'some' all working with splice!
Browse files Browse the repository at this point in the history
Why did I ever start looking for '' inside lookup?
Even if it's still a dead end, that makes no sense.
  • Loading branch information
akkartik committed Jun 13, 2012
1 parent f3e2b3c commit e3124e9
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion 007scope.cc
Expand Up @@ -118,7 +118,7 @@ Cell* scopeContainingBinding(Cell* sym, Cell* scope) {
bool skippedAlreadyEvald = false;
Cell* maybeStripAlreadyEvald(bool dontReallyStrip, Cell* x) {
if (dontReallyStrip) {
skippedAlreadyEvald = contains(x, newSym("''"));
skippedAlreadyEvald = isAlreadyEvald(x);
return x;
}
if (isAlreadyEvald(x))
Expand Down
6 changes: 5 additions & 1 deletion 008eval.cc
Expand Up @@ -65,7 +65,7 @@
Cell* forms = body(fn);
if (cdr(forms) != nil) return false;
Cell* form = car(forms);
if (car(form) != newSym("eval")) return false;
if (car(form) != newSym("mac-eval")) return false;
if (car(cdr(cdr(form))) != newSym("caller-scope")) return false;
if (cdr(cdr(cdr(form))) != nil) return false;
return true;
Expand Down Expand Up @@ -357,6 +357,9 @@ Cell* eval(Cell* expr, Cell* scope, bool dontStripAlreadyEval) {
if (isBackQuoted(expr))
return processUnquotes(cdr(expr), 1, scope); // already mkref'd

if (isAlreadyEvald(expr))
return mkref(dontStripAlreadyEval ? expr : stripAlreadyEvald(expr));

if (car(expr) == newSym("fn"))
return mkref(newFn("function", expr, scope));
else if (isFn(expr))
Expand All @@ -381,6 +384,7 @@ Cell* eval(Cell* expr, Cell* scope, bool dontStripAlreadyEval) {
// keyword args can change what we eval
Cell* orderedArgs = reorderKeywordArgs(sig(fn), splicedArgs);
Cell* evaldArgs = evalArgs(sig(fn), orderedArgs, scope, dontStripAlreadyEval);
dbg << car(expr) << "/" << dontStripAlreadyEval << ": " << evaldArgs << endl;

// swap in the function's lexical environment
if (!isCompiledFn(body(fn)))
Expand Down
4 changes: 2 additions & 2 deletions 008eval.test.cc
Expand Up @@ -603,7 +603,7 @@ void test_eval_handles_splice6() {
}

void test_eval_splice_on_macros_warns() {
Cell* expr = read(stream("(fn '(x y) (eval (cons 'cons (cons x (cons y nil))) caller-scope))"));
Cell* expr = read(stream("(fn '(x y) (mac-eval (cons 'cons (cons x (cons y nil))) caller-scope))"));
Cell* fn = eval(expr);
newDynamicScope("f", fn);
newDynamicScope("a", newNum(3));
Expand All @@ -625,7 +625,7 @@ void test_eval_splice_on_macros_warns() {
}

void test_eval_splice_on_macros_with_backquote() {
Cell* expr = read(stream("(fn '(x y) (eval `(cons ,x ,y) caller-scope))"));
Cell* expr = read(stream("(fn '(x y) (mac-eval `(cons ,x ,y) caller-scope))"));
Cell* fn = eval(expr);
newDynamicScope("f", fn);
newDynamicScope("a", newNum(3));
Expand Down
7 changes: 7 additions & 0 deletions 011.cc
Expand Up @@ -10,6 +10,13 @@ COMPILE_FN(eval, compiledFn_eval, "($x . $scope)",
return eval(lookup("$x"), scope);
)

COMPILE_FN(mac-eval, compiledFn_mac_eval, "('$x $scope)",
Cell* x = eval(lookup("$x"), currLexicalScopes.top(), true);
Cell* ans = eval(x, lookup("$scope"), true);
rmref(x);
return ans;
)

COMPILE_FN(mac?, compiledFn_isMacro, "($f)",
Cell* f = lookup("$f");
return isMacro(f) ? mkref(f) : nil;
Expand Down
10 changes: 5 additions & 5 deletions 030.wart
@@ -1,9 +1,9 @@
= mac! (fn '(name params . body)
(eval `(= ,name (fn ',params
; remember to update isMacro when changing this
(eval ((fn() ,@body))
caller-scope)))
caller-scope))
(mac-eval `(= ,name (fn ',params
; remember to update isMacro when changing this
(mac-eval ((fn() ,@body))
caller-scope)))
caller-scope))

mac! def!(name params . body)
`(= ,name (fn ,params ,@body))
Expand Down
7 changes: 2 additions & 5 deletions 038list.wart
Expand Up @@ -121,13 +121,10 @@ def rem(f seq)
alias skip rem

def some(f seq)
if seq
if (f car.seq)
1
(some f cdr.seq)
(or @(map f seq))

def all(f seq)
(~some ~f seq)
(and @(map f seq))

alias any some
alias none ~some
Expand Down

0 comments on commit e3124e9

Please sign in to comment.