summaryrefslogtreecommitdiff
path: root/builtins.c
diff options
context:
space:
mode:
authorPeter Mikkelsen <peter@pmikkelsen.com>2021-07-16 15:25:01 +0000
committerPeter Mikkelsen <peter@pmikkelsen.com>2021-07-16 15:25:01 +0000
commit480de114963ecee700ece5b8793916726c04b9ab (patch)
tree9c1543307aef92c1c88289a4d07ad4dcbae6b38c /builtins.c
parentee65a81ee5b0112ba4480619ca672c569fb28b45 (diff)
Replace the C repl with one written in prolog :)
Diffstat (limited to 'builtins.c')
-rw-r--r--builtins.c69
1 files changed, 47 insertions, 22 deletions
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