From 26a11662d6d371a1d0c44de6bb028a1523ba4dc5 Mon Sep 17 00:00:00 2001 From: Matthias Benkard Date: Sun, 6 Jul 2008 23:53:04 +0200 Subject: Add class MLKNumber, unify arithmetic interface. --- MLKDoubleFloat.h | 8 ++++---- MLKDoubleFloat.m | 16 ++++++++-------- MLKFloat.h | 12 ++++++------ MLKFloat.m | 10 +++++----- MLKInteger.h | 12 ++++++------ MLKInteger.m | 11 ++++++----- MLKNumber.h | 27 +++++++++++++++++++++++++++ MLKNumber.m | 35 +++++++++++++++++++++++++++++++++++ MLKRatio.h | 12 ++++++------ MLKRatio.m | 11 ++++++----- MLKRoot.m | 9 +++++---- MLKSingleFloat.h | 8 ++++---- MLKSingleFloat.m | 24 ++++++++++++------------ util.h | 6 +++--- 14 files changed, 133 insertions(+), 68 deletions(-) create mode 100644 MLKNumber.h create mode 100644 MLKNumber.m diff --git a/MLKDoubleFloat.h b/MLKDoubleFloat.h index ce27ebd..deba097 100644 --- a/MLKDoubleFloat.h +++ b/MLKDoubleFloat.h @@ -44,10 +44,10 @@ -(float) floatValue; -(double) doubleValue; --(MLKFloat *) add:(MLKFloat *)arg; --(MLKFloat *) subtract:(MLKFloat *)arg; --(MLKFloat *) multiplyWith:(MLKFloat *)arg; --(MLKFloat *) divideBy:(MLKFloat *)arg; +-(MLKNumber *) add:(MLKNumber *)arg; +-(MLKNumber *) subtract:(MLKNumber *)arg; +-(MLKNumber *) multiplyWith:(MLKNumber *)arg; +-(MLKNumber *) divideBy:(MLKNumber *)arg; -(NSString *) description; -(NSString *) descriptionForLisp; diff --git a/MLKDoubleFloat.m b/MLKDoubleFloat.m index e066a1a..3a598fd 100644 --- a/MLKDoubleFloat.m +++ b/MLKDoubleFloat.m @@ -94,24 +94,24 @@ return value; } --(MLKFloat *) add:(MLKFloat *)arg +-(MLKNumber *) add:(MLKNumber *)arg { - return [MLKDoubleFloat doubleFloatWithDouble:(value + [arg doubleValue])]; + return [MLKDoubleFloat doubleFloatWithDouble:(value + [(MLKFloat*)arg doubleValue])]; } --(MLKFloat *) subtract:(MLKFloat *)arg +-(MLKNumber *) subtract:(MLKNumber *)arg { - return [MLKDoubleFloat doubleFloatWithDouble:(value - [arg doubleValue])]; + return [MLKDoubleFloat doubleFloatWithDouble:(value - [(MLKFloat*)arg doubleValue])]; } --(MLKFloat *) multiplyWith:(MLKFloat *)arg +-(MLKNumber *) multiplyWith:(MLKNumber *)arg { - return [MLKDoubleFloat doubleFloatWithDouble:(value * [arg doubleValue])]; + return [MLKDoubleFloat doubleFloatWithDouble:(value * [(MLKFloat*)arg doubleValue])]; } --(MLKFloat *) divideBy:(MLKFloat *)arg +-(MLKNumber *) divideBy:(MLKNumber *)arg { - return [MLKDoubleFloat doubleFloatWithDouble:(value / [arg doubleValue])]; + return [MLKDoubleFloat doubleFloatWithDouble:(value / [(MLKFloat*)arg doubleValue])]; } -(NSString *) description diff --git a/MLKFloat.h b/MLKFloat.h index bd8c3e4..6cb1db3 100644 --- a/MLKFloat.h +++ b/MLKFloat.h @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -#import "MLKLispValue.h" +#import "MLKNumber.h" #import @@ -26,7 +26,7 @@ // instances are of one of its subclasses (currently MLKSingleFloat und // MLKDoubleFloat; an arbitrary-precision, GMP-backed type is planned // for the future). -@interface MLKFloat : MLKLispValue +@interface MLKFloat : MLKNumber +(MLKFloat *) floatWithExponentMarker:(unichar)exponentMarker integerPart:(NSString *)intPart negative:(BOOL)negative @@ -38,10 +38,10 @@ -(float) floatValue; -(double) doubleValue; --(MLKFloat *) add:(MLKFloat *)arg; --(MLKFloat *) subtract:(MLKFloat *)arg; --(MLKFloat *) multiplyWith:(MLKFloat *)arg; --(MLKFloat *) divideBy:(MLKFloat *)arg; +-(MLKNumber *) add:(MLKNumber *)arg; +-(MLKNumber *) subtract:(MLKNumber *)arg; +-(MLKNumber *) multiplyWith:(MLKNumber *)arg; +-(MLKNumber *) divideBy:(MLKNumber *)arg; -(NSString *) description; @end diff --git a/MLKFloat.m b/MLKFloat.m index 869726b..e73c433 100644 --- a/MLKFloat.m +++ b/MLKFloat.m @@ -26,7 +26,7 @@ #import -@implementation MLKFloat : MLKLispValue +@implementation MLKFloat +(MLKFloat *) floatWithExponentMarker:(unichar)exponentMarker integerPart:(NSString *)intPart negative:(BOOL)negative @@ -70,10 +70,10 @@ DECLARE_ABSTRACT (-(float) floatValue, 0.0) DECLARE_ABSTRACT (-(double) doubleValue, 0.0) -DECLARE_ABSTRACT (-(MLKFloat *) add:(MLKFloat *)arg, nil) -DECLARE_ABSTRACT (-(MLKFloat *) subtract:(MLKFloat *)arg, nil) -DECLARE_ABSTRACT (-(MLKFloat *) multiplyWith:(MLKFloat *)arg, nil) -DECLARE_ABSTRACT (-(MLKFloat *) divideBy:(MLKFloat *)arg, nil) +DECLARE_ABSTRACT (-(MLKNumber *) add:(MLKNumber *)arg, nil) +DECLARE_ABSTRACT (-(MLKNumber *) subtract:(MLKNumber *)arg, nil) +DECLARE_ABSTRACT (-(MLKNumber *) multiplyWith:(MLKNumber *)arg, nil) +DECLARE_ABSTRACT (-(MLKNumber *) divideBy:(MLKNumber *)arg, nil) -(NSString *) description { diff --git a/MLKInteger.h b/MLKInteger.h index f3c8d4f..d0decb6 100644 --- a/MLKInteger.h +++ b/MLKInteger.h @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -#import "MLKLispValue.h" +#import "MLKNumber.h" #include #include @@ -25,7 +25,7 @@ @class NSString; -@interface MLKInteger : MLKLispValue +@interface MLKInteger : MLKNumber { mpz_t value; } @@ -44,10 +44,10 @@ -(int) intValue; --(MLKInteger *) add:(MLKInteger *)arg; --(MLKInteger *) subtract:(MLKInteger *)arg; --(MLKInteger *) multiplyWith:(MLKInteger *)arg; --(MLKInteger *) divideBy:(MLKInteger *)arg; +-(MLKNumber *) add:(MLKNumber *)arg; +-(MLKNumber *) subtract:(MLKNumber *)arg; +-(MLKNumber *) multiplyWith:(MLKNumber *)arg; +-(MLKNumber *) divideBy:(MLKNumber *)arg; -(NSString *) description; -(NSString *) descriptionWithBase:(int)base; diff --git a/MLKInteger.m b/MLKInteger.m index 8823aa6..345235a 100644 --- a/MLKInteger.m +++ b/MLKInteger.m @@ -69,11 +69,12 @@ } -#define DEFINE_MPZ_TWOARG_OPERATION(SELECTOR, GMPFUN) \ - DEFINE_GMP_OPERATION (SELECTOR (MLKInteger *)arg, \ - mpz, \ - GMPFUN (mpval, self->value, arg->value), \ - MLKInteger, \ +#define DEFINE_MPZ_TWOARG_OPERATION(SELECTOR, GMPFUN) \ + DEFINE_GMP_OPERATION (SELECTOR (MLKNumber *)arg, \ + mpz, \ + GMPFUN (mpval, self->value, ((MLKInteger*)arg)->value), \ + MLKNumber, \ + MLKInteger, \ integerWithMPZ:) DEFINE_MPZ_TWOARG_OPERATION (add:, mpz_add) diff --git a/MLKNumber.h b/MLKNumber.h new file mode 100644 index 0000000..94ec819 --- /dev/null +++ b/MLKNumber.h @@ -0,0 +1,27 @@ +/* -*- mode: objc; coding: utf-8 -*- */ +/* Étoilisp/Mulklisp, a Common Lisp subset for the Étoilé runtime. + * Copyright (C) 2008 Matthias Andreas Benkard. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#import "MLKLispValue.h" + + +@interface MLKNumber : MLKLispValue +-(MLKNumber *) add:(MLKNumber *)arg; +-(MLKNumber *) subtract:(MLKNumber *)arg; +-(MLKNumber *) multiplyWith:(MLKNumber *)arg; +-(MLKNumber *) divideBy:(MLKNumber *)arg; +@end diff --git a/MLKNumber.m b/MLKNumber.m new file mode 100644 index 0000000..8ffc0f8 --- /dev/null +++ b/MLKNumber.m @@ -0,0 +1,35 @@ +/* -*- mode: objc; coding: utf-8 -*- */ +/* Étoilisp/Mulklisp, a Common Lisp subset for the Étoilé runtime. + * Copyright (C) 2008 Matthias Andreas Benkard. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#import "MLKNumber.h" + + +@implementation MLKNumber +#define DECLARE_ABSTRACT(SIGNATURE, RETURN_VALUE) \ +SIGNATURE \ +{ \ + [NSException raise:@"MLKInternalInconsistencyError" \ + format:@"Tried to invoke an abstract method."]; \ + return RETURN_VALUE; \ +} + +DECLARE_ABSTRACT (-(MLKNumber *) add:(MLKNumber *)arg, nil) +DECLARE_ABSTRACT (-(MLKNumber *) subtract:(MLKNumber *)arg, nil) +DECLARE_ABSTRACT (-(MLKNumber *) multiplyWith:(MLKNumber *)arg, nil) +DECLARE_ABSTRACT (-(MLKNumber *) divideBy:(MLKNumber *)arg, nil) +@end diff --git a/MLKRatio.h b/MLKRatio.h index b81e91a..7d4768b 100644 --- a/MLKRatio.h +++ b/MLKRatio.h @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -#import "MLKLispValue.h" +#import "MLKNumber.h" #include #include @@ -25,7 +25,7 @@ @class NSString, MLKInteger; -@interface MLKRatio : MLKLispValue +@interface MLKRatio : MLKNumber { mpq_t value; } @@ -42,10 +42,10 @@ negative:(BOOL)negative base:(unsigned int)base; --(MLKRatio *) add:(MLKRatio *)arg; --(MLKRatio *) subtract:(MLKRatio *)arg; --(MLKRatio *) multiplyWith:(MLKRatio *)arg; --(MLKRatio *) divideBy:(MLKRatio *)arg; +-(MLKNumber *) add:(MLKNumber *)arg; +-(MLKNumber *) subtract:(MLKNumber *)arg; +-(MLKNumber *) multiplyWith:(MLKNumber *)arg; +-(MLKNumber *) divideBy:(MLKNumber *)arg; -(NSString *) description; -(NSString *) descriptionWithBase:(int)base; diff --git a/MLKRatio.m b/MLKRatio.m index a61d311..604b457 100644 --- a/MLKRatio.m +++ b/MLKRatio.m @@ -71,11 +71,12 @@ base:base]); } -#define DEFINE_MPQ_TWOARG_OPERATION(SELECTOR, GMPFUN) \ - DEFINE_GMP_OPERATION (SELECTOR (MLKRatio *)arg, \ - mpq, \ - GMPFUN (mpval, self->value, arg->value), \ - MLKRatio, \ +#define DEFINE_MPQ_TWOARG_OPERATION(SELECTOR, GMPFUN) \ + DEFINE_GMP_OPERATION (SELECTOR (MLKNumber *)arg, \ + mpq, \ + GMPFUN (mpval, self->value, ((MLKRatio*)arg)->value), \ + MLKNumber, \ + MLKRatio, \ ratioWithMPQ:) DEFINE_MPQ_TWOARG_OPERATION (add:, mpq_add) diff --git a/MLKRoot.m b/MLKRoot.m index 4ba7ac7..cad22a4 100644 --- a/MLKRoot.m +++ b/MLKRoot.m @@ -19,6 +19,7 @@ #import "MLKCons.h" #import "MLKDynamicContext.h" #import "MLKInterpreter.h" +#import "MLKNumber.h" #import "MLKPackage.h" #import "MLKRoot.h" #import "MLKStream.h" @@ -195,22 +196,22 @@ static id truify (BOOL value) +(NSArray *) add:(NSArray *)args { - RETURN_VALUE ([[args objectAtIndex:0] add:[args objectAtIndex:1]]); + RETURN_VALUE ([((MLKNumber*)[args objectAtIndex:0]) add:[args objectAtIndex:1]]); } +(NSArray *) subtract:(NSArray *)args { - RETURN_VALUE ([[args objectAtIndex:0] subtract:[args objectAtIndex:1]]); + RETURN_VALUE ([((MLKNumber*)[args objectAtIndex:0]) subtract:[args objectAtIndex:1]]); } +(NSArray *) multiply:(NSArray *)args { - RETURN_VALUE ([[args objectAtIndex:0] multiplyWith:[args objectAtIndex:1]]); + RETURN_VALUE ([((MLKNumber*)[args objectAtIndex:0]) multiplyWith:[args objectAtIndex:1]]); } +(NSArray *) divide:(NSArray *)args { - RETURN_VALUE ([[args objectAtIndex:0] divideBy:[args objectAtIndex:1]]); + RETURN_VALUE ([((MLKNumber*)[args objectAtIndex:0]) divideBy:[args objectAtIndex:1]]); } +(NSArray *) list:(NSArray *)args diff --git a/MLKSingleFloat.h b/MLKSingleFloat.h index 0dbec17..2a4fca6 100644 --- a/MLKSingleFloat.h +++ b/MLKSingleFloat.h @@ -41,10 +41,10 @@ -(float) floatValue; -(double) doubleValue; --(MLKFloat *) add:(MLKFloat *)arg; --(MLKFloat *) subtract:(MLKFloat *)arg; --(MLKFloat *) multiplyWith:(MLKFloat *)arg; --(MLKFloat *) divideBy:(MLKFloat *)arg; +-(MLKNumber *) add:(MLKNumber *)arg; +-(MLKNumber *) subtract:(MLKNumber *)arg; +-(MLKNumber *) multiplyWith:(MLKNumber *)arg; +-(MLKNumber *) divideBy:(MLKNumber *)arg; -(NSString *) description; -(NSString *) descriptionForLisp; diff --git a/MLKSingleFloat.m b/MLKSingleFloat.m index c10ccce..9c2902f 100644 --- a/MLKSingleFloat.m +++ b/MLKSingleFloat.m @@ -94,36 +94,36 @@ return value; } --(MLKFloat *) add:(MLKFloat *)arg +-(MLKNumber *) add:(MLKNumber *)arg { if ([arg isKindOfClass:[MLKDoubleFloat class]]) - return [MLKDoubleFloat doubleFloatWithDouble:(value + [arg doubleValue])]; + return [MLKDoubleFloat doubleFloatWithDouble:(value + [(MLKFloat*)arg doubleValue])]; else - return [MLKSingleFloat singleFloatWithFloat:(value + [arg floatValue])]; + return [MLKSingleFloat singleFloatWithFloat:(value + [(MLKFloat*)arg floatValue])]; } --(MLKFloat *) subtract:(MLKFloat *)arg +-(MLKNumber *) subtract:(MLKNumber *)arg { if ([arg isKindOfClass:[MLKDoubleFloat class]]) - return [MLKDoubleFloat doubleFloatWithDouble:(value - [arg doubleValue])]; + return [MLKDoubleFloat doubleFloatWithDouble:(value - [(MLKFloat*)arg doubleValue])]; else - return [MLKSingleFloat singleFloatWithFloat:(value - [arg floatValue])]; + return [MLKSingleFloat singleFloatWithFloat:(value - [(MLKFloat*)arg floatValue])]; } --(MLKFloat *) multiplyWith:(MLKFloat *)arg +-(MLKNumber *) multiplyWith:(MLKNumber *)arg { if ([arg isKindOfClass:[MLKDoubleFloat class]]) - return [MLKDoubleFloat doubleFloatWithDouble:(value * [arg doubleValue])]; + return [MLKDoubleFloat doubleFloatWithDouble:(value * [(MLKFloat*)arg doubleValue])]; else - return [MLKSingleFloat singleFloatWithFloat:(value * [arg floatValue])]; + return [MLKSingleFloat singleFloatWithFloat:(value * [(MLKFloat*)arg floatValue])]; } --(MLKFloat *) divideBy:(MLKFloat *)arg +-(MLKNumber *) divideBy:(MLKNumber *)arg { if ([arg isKindOfClass:[MLKDoubleFloat class]]) - return [MLKDoubleFloat doubleFloatWithDouble:(value / [arg doubleValue])]; + return [MLKDoubleFloat doubleFloatWithDouble:(value / [(MLKFloat*)arg doubleValue])]; else - return [MLKSingleFloat singleFloatWithFloat:(value / [arg floatValue])]; + return [MLKSingleFloat singleFloatWithFloat:(value / [(MLKFloat*)arg floatValue])]; } -(NSString *) description diff --git a/util.h b/util.h index 0e27fc0..092b586 100644 --- a/util.h +++ b/util.h @@ -2,11 +2,11 @@ #include #include -#define DEFINE_GMP_OPERATION(SIGNATURE, TYPE, GMPOP, OBJTYPE, CONSTRUCTOR) \ - -(OBJTYPE *) SIGNATURE \ +#define DEFINE_GMP_OPERATION(SIGNATURE, TYPE, GMPOP, RETTYPE, OBJTYPE, CONSTRUCTOR) \ + -(RETTYPE *) SIGNATURE \ { \ TYPE##_t mpval; \ - OBJTYPE *result; \ + RETTYPE *result; \ \ TYPE##_init (mpval); \ GMPOP; \ -- cgit v1.2.3