From 90e6023292e2bfe927bd633bac42fc355bb9f4d1 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Mon, 4 Aug 2008 18:18:41 +0200 Subject: Add support for fixnums. --- functions.m | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 4 deletions(-) (limited to 'functions.m') diff --git a/functions.m b/functions.m index 136c2da..068d217 100644 --- a/functions.m +++ b/functions.m @@ -27,7 +27,7 @@ NSString *MLKPrintToString (id object) return [object descriptionForLisp]; else if (MLKFixnumP (object)) return MLKPrintToString ([MLKInteger - integerWithInt:(MLKIntWithFixnum (object))]); + integerWithIntptr_t:(MLKIntWithFixnum (object))]); else { NSLog (@"MLKPrintToString: Encountered a really weird object at address %p", @@ -48,13 +48,13 @@ id MLKFixnumWithInt (intptr_t value) id MLKIntegerWithInt (intptr_t value) { - intptr_t maybeFixnum = ((intptr_t)value << 1) | 1; + intptr_t maybeFixnum = (value << 1) | 1; if (value == (maybeFixnum >> 1)) return (id)maybeFixnum; else - return [MLKInteger integerWithInt:value]; + return [MLKInteger integerWithIntptr_t:value]; } - + BOOL MLKFixnumP (id thing) { return ((intptr_t)thing & 1); @@ -64,3 +64,60 @@ BOOL MLKInstanceP (id thing) { return !((intptr_t)thing & 1); } + +id MLKCanoniseInteger (MLKInteger *x) +{ + if (MLKFixnumP (x)) + { + return x; + } + else if (MLKInstanceP (x)) + { + if ([x fitsIntoFixnum]) + return [x fixnumValue]; + else + return x; + } + else + { + NSLog (@"MLKCanoniseInteger: Encountered a really weird object at address %p", + x); + return 0; + } +} + +id MLKAddFixnums (id x, id y) +{ + intptr_t ix = MLKIntWithFixnum (x); + intptr_t iy = MLKIntWithFixnum (y); + intptr_t result = ix + iy; + + return MLKIntegerWithInt (result); +} + +id MLKSubtractFixnums (id x, id y) +{ + intptr_t ix = MLKIntWithFixnum (x); + intptr_t iy = MLKIntWithFixnum (y); + intptr_t result = ix - iy; + + return MLKIntegerWithInt (result); +} + +id MLKIDivideFixnums (id x, id y) +{ + intptr_t ix = MLKIntWithFixnum (x); + intptr_t iy = MLKIntWithFixnum (y); + intptr_t result = ix / iy; + + return MLKIntegerWithInt (result); +} + +id MLKMultiplyFixnums (id x, id y) +{ + id ix = [MLKInteger integerWithFixnum:x]; + id iy = [MLKInteger integerWithFixnum:y]; + id result = [ix multiplyWith:iy]; + + return MLKCanoniseInteger (result); +} -- cgit v1.2.3