diff options
Diffstat (limited to 'builtins.c')
-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 |