From 834cb55baadb9dd7c3a4ad096d81259d0868eeb9 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sun, 20 Nov 2016 17:02:08 +0100 Subject: Fix reducing bigtime --- environment.cpp | 152 ++++++++++++++++++++++++++++++++++++-------------------- prelude.cpp | 16 +++++- 2 files changed, 112 insertions(+), 56 deletions(-) diff --git a/environment.cpp b/environment.cpp index e36031b..3ca55b1 100644 --- a/environment.cpp +++ b/environment.cpp @@ -153,16 +153,50 @@ static void singlify(AST &ast){ } } +bool Environment::resolve(AST &ast){ + if(ast.quoted){ + return false; + } + + bool ret=false; + + switch(ast.type){ + case AST::Type::number: + case AST::Type::string: + case AST::Type::index: + case AST::Type::native: + break; + + case AST::Type::name: + try { + ast=get(ast.nameval); + resolve(ast); + ret=true; + } catch(NameError){ + // just leave an unknown name as it is + } + break; + + case AST::Type::tuple: + for(AST &term : ast.terms){ + ret=resolve(term)||ret; + } + break; + + case AST::Type::lambda: + ret=resolve(*ast.lambdaval.body)||ret; + break; + } + + return ret; +} + AST Environment::run(const AST &astinput){ AST ast(astinput); indexify(ast); #ifdef DEBUG cerr<<"indexify gave "< &p : repl){ - *p.first=ast.terms[1]; - increaseFree(*p.first,p.second-1,1); - } - newterm=ast.terms[0]; - ast=newterm; - success=true; - } else if(ast.terms[0].type==AST::Type::native){ - reduce(ast.terms[1],depth+1); - ast=ast.terms[0].nativeval(ast.terms[1]); - success=true; - } else if(ast.terms[0].type==AST::Type::name&&ast.terms[0].nameval=="do"){ - reduce(ast.terms[1],depth+1); - ast=ast.terms[0]; - success=true; - } else { - success=false; + // cerr<<"=== β-REDUCE LAMBDA ==="< &p : repl){ + *p.first=ast.terms[1]; + increaseFree(*p.first,p.second-1,1); } + newterm=ast.terms[0]; + ast=newterm; + success=true; #ifdef DEBUG cerr< "<2){ + singlify(ast); + } + ret=reduce(ast.terms[0],depth+1); + if(ast.terms[0].type==AST::Type::lambda){ + bool brret=betareduce(ast,depth+1); + ret=ret||brret; + if(brret){ + ret=reduce(ast,depth+1)||ret; + } + } else if(ast.terms[0].type==AST::Type::native){ +#ifdef DEBUG + cerr<0); - if(ast.type==AST::Type::name){ - // TODO: this is really dodgy; why is this necessary? - reduce(ast,depth+1); } break; } + #ifdef DEBUG - cerr< "< "< AST { - throw logic_error("'do' stub called; this should not happen"); + intoEnv.define("do",doNative); + + intoEnv.define("unquote",AST::makeNative([](const AST &ast) -> AST { + AST res(ast); + res.quoted=false; + return res; })); intoEnv.define2("+",[](Environment&,const AST &arg1,const AST &arg2) -> AST { -- cgit v1.2.3-70-g09d2