From 480de114963ecee700ece5b8793916726c04b9ab Mon Sep 17 00:00:00 2001 From: Peter Mikkelsen Date: Fri, 16 Jul 2021 15:25:01 +0000 Subject: Replace the C repl with one written in prolog :) --- builtins.c | 69 ++++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 22 deletions(-) (limited to 'builtins.c') diff --git a/builtins.c b/builtins.c index 369ac92..bde52fd 100644 --- a/builtins.c +++ b/builtins.c @@ -60,6 +60,8 @@ BuiltinProto(builtingetchar); BuiltinProto(builtinpeekchar); BuiltinProto(builtinputchar); BuiltinProto(builtincharcode); +BuiltinProto(builtinchoicestacksize); +BuiltinProto(builtincollectgarbage); int compareterms(Term *, Term *); @@ -169,6 +171,10 @@ findbuiltin(Term *goal) return builtinputchar; if(Match(L"char_code", 2)) return builtincharcode; + if(Match(L"$choicestack_size", 1)) + return builtinchoicestacksize; + if(Match(L"$collect_garbage", 0)) + return builtincollectgarbage; return nil; } @@ -569,37 +575,30 @@ int builtinthrow(Term *goal, Binding **bindings, Module *module) { USED(bindings); + USED(module); Term *ball = goal->children; - print("Throwing: %S\n", prettyprint(ball, 0, 0, 0, module)); Goal *g; for(g = goalstack; g != nil; g = g->next){ if(g->catcher == nil) continue; if(unify(g->catcher, ball, bindings)){ - if(g->goal == nil){ - /* As soon as we have print facilities as builtins, we can avoid this by having the protector frame have a unhandled exception handler*/ - print("Unhandled exception: %S\n", prettyprint(ball, 0, 0, 0, module)); - exits("exception"); - return 0; - }else{ - goalstack = g->next; - Goal *newgoal = gmalloc(sizeof(Goal)); - newgoal->goal = copyterm(g->goal, nil); - newgoal->module = module; - newgoal->catcher = nil; - newgoal->next = goalstack; - goalstack = newgoal; - applybinding(newgoal->goal, *bindings); - - Choicepoint *cp = choicestack; - while(cp != nil && cp->id >= goal->clausenr) - cp = cp->next; - choicestack = cp; - return 1; - } + goalstack = g->next; + Goal *newgoal = gmalloc(sizeof(Goal)); + newgoal->goal = copyterm(g->goal, nil); + newgoal->module = g->module; + newgoal->catcher = nil; + newgoal->next = goalstack; + goalstack = newgoal; + applybinding(newgoal->goal, *bindings); + + Choicepoint *cp = choicestack; + while(cp != nil && cp->id >= goal->clausenr) + cp = cp->next; + choicestack = cp; + return 1; } } return 0; @@ -1404,3 +1403,29 @@ builtincharcode(Term *goal, Binding **bindings, Module *module) } } +int +builtinchoicestacksize(Term *goal, Binding **bindings, Module *module) +{ + USED(bindings); + USED(module); + Term *size = goal->children; + + vlong i = 0; + Choicepoint *cp; + for(cp = choicestack; cp != nil; cp = cp->next) + i++; + Term *realsize = mkinteger(i); + return unify(size, realsize, bindings); +} + +int +builtincollectgarbage(Term *goal, Binding **bindings, Module *module) +{ + USED(goal); + USED(bindings); + USED(module); + vlong amount = collectgarbage(); + if(amount != 0 & debug) + print("Collected %lld bytes of garbage\n", amount); + return 1; +} \ No newline at end of file -- cgit v1.2.3