diff options
author | Peter Mikkelsen <peter@pmikkelsen.com> | 2021-07-13 18:51:22 +0000 |
---|---|---|
committer | Peter Mikkelsen <peter@pmikkelsen.com> | 2021-07-13 18:51:22 +0000 |
commit | 6d3d4a2dbba8c3092b39bbb51d155b1df653ca5f (patch) | |
tree | 1bb5b7742afbe228a3da020644de662f3eea7c29 | |
parent | 9d5c9d3fe5d8951fac0902abf125c68a6e720e52 (diff) |
Add atom_length/2
-rw-r--r-- | builtins.c | 24 |
1 files changed, 24 insertions, 0 deletions
@@ -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 |