summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Benkard <mulk@minimulk.mst-plus>2008-07-06 23:53:04 +0200
committerMatthias Benkard <mulk@minimulk.mst-plus>2008-07-06 23:53:04 +0200
commit26a11662d6d371a1d0c44de6bb028a1523ba4dc5 (patch)
tree9532f796a01ba5b3000d3e5bd6062c59f2a934b6
parentbb79a71e7aa85536d3d60d50913d5fc39c4ac07b (diff)
Add class MLKNumber, unify arithmetic interface.
-rw-r--r--MLKDoubleFloat.h8
-rw-r--r--MLKDoubleFloat.m16
-rw-r--r--MLKFloat.h12
-rw-r--r--MLKFloat.m10
-rw-r--r--MLKInteger.h12
-rw-r--r--MLKInteger.m11
-rw-r--r--MLKNumber.h27
-rw-r--r--MLKNumber.m35
-rw-r--r--MLKRatio.h12
-rw-r--r--MLKRatio.m11
-rw-r--r--MLKRoot.m9
-rw-r--r--MLKSingleFloat.h8
-rw-r--r--MLKSingleFloat.m24
-rw-r--r--util.h6
14 files changed, 133 insertions, 68 deletions
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 <http://www.gnu.org/licenses/>.
*/
-#import "MLKLispValue.h"
+#import "MLKNumber.h"
#import <Foundation/NSString.h>
@@ -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 <Foundation/NSException.h>
-@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 <http://www.gnu.org/licenses/>.
*/
-#import "MLKLispValue.h"
+#import "MLKNumber.h"
#include <stdarg.h>
#include <stdio.h>
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+
+#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 <http://www.gnu.org/licenses/>.
+ */
+
+#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 <http://www.gnu.org/licenses/>.
*/
-#import "MLKLispValue.h"
+#import "MLKNumber.h"
#include <stdarg.h>
#include <stdio.h>
@@ -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 <Foundation/NSException.h>
#include <Foundation/NSNull.h>
-#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; \