diff options
author | Matthias Andreas Benkard <matthias@benkard.de> | 2008-08-04 18:18:41 +0200 |
---|---|---|
committer | Matthias Andreas Benkard <matthias@benkard.de> | 2008-08-04 18:18:41 +0200 |
commit | 90e6023292e2bfe927bd633bac42fc355bb9f4d1 (patch) | |
tree | 14664b9e9d98f43c83b78547ea7c554fef8de9d1 /functions.m | |
parent | 383e3e833a7fbb8b1560ba861b76e8be96542c6f (diff) |
Add support for fixnums.
Diffstat (limited to 'functions.m')
-rw-r--r-- | functions.m | 65 |
1 files changed, 61 insertions, 4 deletions
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); +} |