diff options
author | Peter Mikkelsen <peter@pmikkelsen.com> | 2021-07-05 16:59:06 +0000 |
---|---|---|
committer | Peter Mikkelsen <peter@pmikkelsen.com> | 2021-07-05 16:59:06 +0000 |
commit | bdcc02a5ea2d165c638d667978e8e2cf7462558a (patch) | |
tree | d2fffbe7ee5f0c09c070f93204587d055d088b61 /builtins.c | |
parent | 44ab8a339c78bcc3460d44b2f435116f21faa60a (diff) |
Turn integers and floats into seperate term types
Diffstat (limited to 'builtins.c')
-rw-r--r-- | builtins.c | 55 |
1 files changed, 26 insertions, 29 deletions
@@ -186,7 +186,7 @@ builtininteger(Term *goal, Binding **bindings) { USED(bindings); Term *arg = goal->children; - return (arg->tag == NumberTerm && arg->numbertype == NumberInt); + return (arg->tag == IntegerTerm); } int @@ -194,7 +194,7 @@ builtinfloat(Term *goal, Binding **bindings) { USED(bindings); Term *arg = goal->children; - return (arg->tag == NumberTerm && arg->numbertype == NumberFloat); + return (arg->tag == FloatTerm); } int @@ -202,7 +202,7 @@ builtinatomic(Term *goal, Binding **bindings) { USED(bindings); Term *arg = goal->children; - return (arg->tag == AtomTerm || arg->tag == NumberTerm); + return (arg->tag == AtomTerm || arg->tag == FloatTerm || arg->tag == IntegerTerm); } int @@ -226,7 +226,7 @@ builtinnumber(Term *goal, Binding **bindings) { USED(bindings); Term *arg = goal->children; - return (arg->tag == NumberTerm); + return (arg->tag == FloatTerm || arg->tag == IntegerTerm); } #define Compare(A, B) ((A < B) ? -1 : ((A > B) ? 1 : 0)) @@ -247,14 +247,11 @@ compareterms(Term *t1, Term *t2) else result = Compare(t1->clausenr, t2->clausenr); break; - case NumberTerm: - if(t1->numbertype == t2->numbertype){ - if(t1->numbertype == NumberInt) - result = Compare(t1->ival, t2->ival); - else - result = Compare(t1->dval, t2->dval); - }else - result = Compare(t1->numbertype, t2->numbertype); + case FloatTerm: + result = Compare(t1->dval, t2->dval); + break; + case IntegerTerm: + result = Compare(t1->ival, t2->ival); break; case AtomTerm: result = runestrcmp(t1->text, t2->text); @@ -314,11 +311,11 @@ builtinfunctor(Term *goal, Binding **bindings) if(term->tag == CompoundTerm){ Term *realname = mkatom(term->text); - Term *realarity = mknumber(NumberInt, term->arity, 0); + Term *realarity = mkinteger(term->arity); if(unify(name, realname, bindings) && unify(arity, realarity, bindings)) return 1; - }else if(arity->tag == NumberTerm && arity->numbertype == NumberInt && - (name->tag == AtomTerm || name->tag == NumberTerm)){ + }else if(arity->tag == IntegerTerm && + (name->tag == AtomTerm || name->tag == IntegerTerm || name->tag == FloatTerm)){ if(arity->ival == 0) return unify(term, name, bindings); else{ @@ -348,7 +345,7 @@ builtinarg(Term *goal, Binding **bindings) Term *term = n->next; Term *arg = term->next; - if(n->tag != NumberTerm || n->numbertype != NumberInt) + if(n->tag != IntegerTerm) return 0; if(n->ival < 0) Throw(domainerror(L"not_less_than_zero", n)); @@ -416,28 +413,28 @@ builtinuniv(Term *goal, Binding **bindings) } } -#define ToFloat(t) (t->numbertype == NumberInt ? (double)t->ival : t->dval) +#define ToFloat(t) (t->tag == IntegerTerm ? (double)t->ival : t->dval) Term * aritheval(Term *expr) { /* Not every arithmetic operation is defined right now. */ - if(expr->tag == NumberTerm) + if(expr->tag == FloatTerm || expr->tag == IntegerTerm) return expr; else if(expr->tag == CompoundTerm && expr->arity == 2){ Term *A = aritheval(expr->children); Term *B = aritheval(expr->children->next); - Term *result = mknumber(NumberInt, 0, 0); + Term *result = mkinteger(0); if(A == nil || B == nil) return nil; if(runestrcmp(expr->text, L"+") == 0){ - if(A->numbertype == NumberInt && B->numbertype == NumberInt){ - result->numbertype = NumberInt; + if(A->tag == IntegerTerm && B->tag == IntegerTerm){ + result->tag = IntegerTerm; result->ival = A->ival + B->ival; }else{ - result->numbertype = NumberFloat; + result->tag = FloatTerm; result->dval = ToFloat(A) + ToFloat(B); } }else @@ -600,7 +597,7 @@ builtinclose(Term *goal, Binding **bindings) if(options->tag != AtomTerm || runestrcmp(options->text, L"[]") != 0) Throw(typeerror(L"empty_list", options)); - if((stream->tag != NumberTerm || stream->numbertype != NumberInt) && stream->tag != AtomTerm) + if(stream->tag != IntegerTerm && stream->tag != AtomTerm) Throw(domainerror(L"stream_or_alias", stream)); if(!isopenstream(stream)) @@ -617,7 +614,7 @@ builtincurrentinput(Term *goal, Binding **bindings) USED(bindings); Term *stream = goal->children; - if(stream->tag != VariableTerm && (stream->tag != NumberTerm || stream->numbertype != NumberInt)) + if(stream->tag != VariableTerm && stream->tag != IntegerTerm) Throw(domainerror(L"stream", stream)); Term *current = currentinputstream(); @@ -630,7 +627,7 @@ builtincurrentoutput(Term *goal, Binding **bindings) USED(bindings); Term *stream = goal->children; - if(stream->tag != VariableTerm && (stream->tag != NumberTerm || stream->numbertype != NumberInt)) + if(stream->tag != VariableTerm && stream->tag != IntegerTerm) Throw(domainerror(L"stream", stream)); Term *current = currentoutputstream(); @@ -646,7 +643,7 @@ builtinsetinput(Term *goal, Binding **bindings) if(stream->tag == VariableTerm) Throw(instantiationerror()); - if((stream->tag != NumberTerm || stream->numbertype != NumberInt) && stream->tag != AtomTerm) + if(stream->tag != IntegerTerm && stream->tag != AtomTerm) Throw(domainerror(L"stream_or_alias", stream)); if(!isopenstream(stream)) @@ -668,7 +665,7 @@ builtinsetoutput(Term *goal, Binding **bindings) if(stream->tag == VariableTerm) Throw(instantiationerror()); - if((stream->tag != NumberTerm || stream->numbertype != NumberInt) && stream->tag != AtomTerm) + if(stream->tag != IntegerTerm && stream->tag != AtomTerm) Throw(domainerror(L"stream_or_alias", stream)); if(!isopenstream(stream)) @@ -694,7 +691,7 @@ builtinreadterm(Term *goal, Binding **bindings) Throw(instantiationerror()); if(options->tag != AtomTerm || runestrcmp(options->text, L"[]") != 0) Throw(typeerror(L"empty_list", options)); - if((stream->tag != NumberTerm || stream->numbertype != NumberInt) && stream->tag != AtomTerm) + if(stream->tag != IntegerTerm && stream->tag != AtomTerm) Throw(domainerror(L"stream_or_alias", stream)); if(!isopenstream(stream)) Throw(existenceerror(L"stream", stream)); @@ -724,7 +721,7 @@ builtinwriteterm(Term *goal, Binding **bindings) Throw(instantiationerror()); if(options->tag != AtomTerm || runestrcmp(options->text, L"[]") != 0) Throw(typeerror(L"empty_list", options)); - if((stream->tag != NumberTerm || stream->numbertype != NumberInt) && stream->tag != AtomTerm) + if(stream->tag != IntegerTerm && stream->tag != AtomTerm) Throw(domainerror(L"stream_or_alias", stream)); if(!isopenstream(stream)) Throw(existenceerror(L"stream", stream)); |