From 6d3d4a2dbba8c3092b39bbb51d155b1df653ca5f Mon Sep 17 00:00:00 2001 From: Peter Mikkelsen Date: Tue, 13 Jul 2021 18:51:22 +0000 Subject: Add atom_length/2 --- builtins.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/builtins.c b/builtins.c index cf238eb..6c2c16a 100644 --- a/builtins.c +++ b/builtins.c @@ -54,6 +54,7 @@ BuiltinProto(builtinasserta); BuiltinProto(builtinassertz); BuiltinProto(builtinretractone); BuiltinProto(builtinabolish); +BuiltinProto(builtinatomlength); int compareterms(Term *, Term *); @@ -151,6 +152,8 @@ findbuiltin(Term *goal) return builtinretractone; if(Match(L"abolish", 1)) return builtinabolish; + if(Match(L"atom_length", 2)) + return builtinatomlength; return nil; } @@ -1107,4 +1110,25 @@ builtinabolish(Term *goal, Binding **bindings, Module *module) } } return 1; +} + +int +builtinatomlength(Term *goal, Binding **bindings, Module *module) +{ + USED(module); + Term *atom = goal->children; + Term *length = atom->next; + + if(atom->tag == VariableTerm) + Throw(instantiationerror()); + if(atom->tag != AtomTerm) + Throw(typeerror(L"atom", atom)); + if(length->tag != VariableTerm && length->tag != IntegerTerm) + Throw(typeerror(L"integer", length)); + if(length->tag == IntegerTerm && length->ival < 0) + Throw(domainerror(L"not_less_than_zero", length)); + + int len = runestrlen(atom->text); + Term *reallength = mkinteger(len); + return unify(length, reallength, bindings); } \ No newline at end of file -- cgit v1.2.3