summaryrefslogtreecommitdiff
path: root/MLKRoot.m
diff options
context:
space:
mode:
authorMatthias Andreas Benkard <matthias@benkard.de>2008-07-07 20:39:34 +0200
committerMatthias Andreas Benkard <matthias@benkard.de>2008-07-07 20:39:34 +0200
commit3658d999dab28812d1a1b6c3527ed386a668b9e7 (patch)
tree0fec7c3803e9674c917caa8b934d74b2c8f913a1 /MLKRoot.m
parentdc2ec73e0293f926d97c472dc86eab7adcaa9c2c (diff)
Implement GENSYM.
Diffstat (limited to 'MLKRoot.m')
-rw-r--r--MLKRoot.m47
1 files changed, 47 insertions, 0 deletions
diff --git a/MLKRoot.m b/MLKRoot.m
index cad22a4..645e92f 100644
--- a/MLKRoot.m
+++ b/MLKRoot.m
@@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+#import "MLKBinding.h"
#import "MLKCons.h"
#import "MLKDynamicContext.h"
#import "MLKInterpreter.h"
@@ -325,4 +326,50 @@ static id truify (BOOL value)
{
RETURN_VALUE (stringify (denullify ([args objectAtIndex:0])));
}
+
++(NSArray *) gensym:(NSArray *)args
+{
+ NSString *prefix;
+ NSString *suffix;
+ MLKBinding *gensymCounter = [[MLKDynamicContext currentContext]
+ bindingForSymbol:
+ [[MLKPackage findPackage:@"COMMON-LISP"]
+ intern:@"*GENSYM-COUNTER*"]];
+
+ if ([args count] > 0)
+ {
+ id x = [args objectAtIndex:0];
+ if ([x isKindOfClass:[NSString class]])
+ {
+ prefix = x;
+ suffix = [[gensymCounter value] descriptionForLisp];
+ [gensymCounter
+ setValue:[[gensymCounter value]
+ add:[MLKInteger integerWithInt:1]]];
+ }
+ else if ([x isKindOfClass:[MLKInteger class]])
+ {
+ // x must be an integer.
+ prefix = @"G";
+ suffix = [x descriptionForLisp];
+ }
+ else
+ [NSException raise:@"MLKTypeError"
+ format:@"%@ is not of type (OR INTEGER STRING).", x];
+ }
+ else
+ {
+ prefix = @"G";
+ suffix = [[gensymCounter value] descriptionForLisp];
+ [gensymCounter
+ setValue:[[gensymCounter value]
+ add:[MLKInteger integerWithInt:1]]];
+ }
+
+ RETURN_VALUE (([MLKSymbol symbolWithName:[NSString stringWithFormat:@"%@%@",
+ prefix,
+ suffix]
+ package:nil]));
+}
+
@end