ref: bdcc02a5ea2d165c638d667978e8e2cf7462558a
parent: 44ab8a339c78bcc3460d44b2f435116f21faa60a
author: Peter Mikkelsen <peter@pmikkelsen.com>
date: Mon Jul 5 12:59:06 EDT 2021
Turn integers and floats into seperate term types
--- a/builtins.c
+++ b/builtins.c
@@ -186,7 +186,7 @@
{
USED(bindings);
Term *arg = goal->children;
- return (arg->tag == NumberTerm && arg->numbertype == NumberInt);
+ return (arg->tag == IntegerTerm);
}
int
@@ -194,7 +194,7 @@
{
USED(bindings);
Term *arg = goal->children;
- return (arg->tag == NumberTerm && arg->numbertype == NumberFloat);
+ return (arg->tag == FloatTerm);
}
int
@@ -202,7 +202,7 @@
{
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 @@
{
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,15 +247,12 @@
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);
break;
@@ -314,11 +311,11 @@
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 @@
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,7 +413,7 @@
}
}
-#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)
@@ -423,21 +420,21 @@
{
/* 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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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));
--- a/dat.h
+++ b/dat.h
@@ -14,7 +14,6 @@
int arity;
Term *next;
Term *children;
- int numbertype;
vlong ival;
double dval;
uvlong clausenr;
@@ -64,14 +63,10 @@
/* Sorted so that a lower value means it comes earlier in the standard ordering */
enum {
VariableTerm,
- NumberTerm,
+ FloatTerm,
+ IntegerTerm,
AtomTerm,
CompoundTerm,
-};
-
-enum {
- NumberFloat,
- NumberInt,
};
int debug;
--- a/eval.c
+++ b/eval.c
@@ -246,14 +246,10 @@
return runestrcmp(a->text, b->text) == 0;
case VariableTerm:
return (runestrcmp(a->text, b->text) == 0 && a->clausenr == b->clausenr);
- case NumberTerm:
- if(a->numbertype != b->numbertype)
- return 0;
- if(a->numbertype == NumberInt && a->ival == b->ival)
- return 1;
- if(a->numbertype == NumberFloat && a->dval == b->dval)
- return 1;
- return 0;
+ case FloatTerm:
+ return a->dval == b->dval;
+ case IntegerTerm:
+ return a->ival == b->ival;
default:
return 0;
}
--- a/fns.h
+++ b/fns.h
@@ -11,7 +11,8 @@
Term *mkatom(Rune *);
Term *mkvariable(Rune *);
Term *mkcompound(Rune *, int, Term *);
-Term *mknumber(int, vlong, double);
+Term *mkfloat(double);
+Term *mkinteger(vlong);
Term *mkstring(Rune *);
Term *mklist(Term *);
Clause *copyclause(Clause *, uvlong *);
--- a/misc.c
+++ b/misc.c
@@ -83,16 +83,22 @@
}
Term *
-mknumber(int type, vlong ival, double dval)
+mkfloat(double dval)
{
- Term *t = mkterm(NumberTerm);
- t->numbertype = type;
- t->ival = ival;
+ Term *t = mkterm(FloatTerm);
t->dval = dval;
return t;
}
Term *
+mkinteger(vlong ival)
+{
+ Term *t = mkterm(IntegerTerm);
+ t->ival = ival;
+ return t;
+}
+
+Term *
mkstring(Rune *text)
{
Term *t = nil;
@@ -109,7 +115,7 @@
break;
case DoubleQuotesCodes:
for(r = text; *r != '\0'; r++){
- Term *code = mknumber(NumberInt, *r, 0);
+ Term *code = mkinteger(*r);
t = appendterm(t, code);
}
t = mklist(t);
--- a/parser.c
+++ b/parser.c
@@ -169,11 +169,11 @@
match(VarTok);
break;
case IntTok:
- result = mknumber(NumberInt, lookahead.ival, 0);
+ result = mkinteger(lookahead.ival);
match(IntTok);
break;
case FloatTok:
- result = mknumber(NumberFloat, 0, lookahead.dval);
+ result = mkfloat(lookahead.dval);
match(FloatTok);
break;
case CommaTok:
--- a/prettyprint.c
+++ b/prettyprint.c
@@ -31,11 +31,11 @@
case VariableTerm:
result = runesmprint("%S(%ulld)", t->text, t->clausenr);
break;
- case NumberTerm:
- if(t->numbertype == NumberInt)
- result = runesmprint("%lld", t->ival);
- else
- result = runesmprint("%f", t->dval);
+ case FloatTerm:
+ result = runesmprint("%f", t->dval);
+ break;
+ case IntegerTerm:
+ result = runesmprint("%lld", t->ival);
break;
default:
result = runesmprint("cant print term with tag %d", t->tag);
--- a/streams.c
+++ b/streams.c
@@ -84,7 +84,7 @@
}
Stream *s = openstreamfd(fd, bio, TextStream, smode);
- *stream = mknumber(NumberInt, s->fd, 0);
+ *stream = mkinteger(s->fd);
return 0;
}
@@ -116,13 +116,13 @@
Term *
currentinputstream(void)
{
- return mknumber(NumberInt, currentinput->fd, 0);
+ return mkinteger(currentinput->fd);
}
Term *
currentoutputstream(void)
{
- return mknumber(NumberInt, currentoutput->fd, 0);
+ return mkinteger(currentoutput->fd);
}
void
@@ -268,7 +268,7 @@
getstream(Term *t)
{
Stream *s = nil;
- if(t->tag == NumberTerm && t->numbertype == NumberInt)
+ if(t->tag == IntegerTerm)
s = getstreambyfd(t->ival);
else if(t->tag == AtomTerm)
s = getstreambyalias(t->text);