Commit 8bf699d6 authored by Gregory Casamento's avatar Gregory Casamento
Browse files

Move ObjC code generation to separate classes.

parent c6dfd4da
This diff is collapsed.
......@@ -42,14 +42,10 @@
#import "VariableDeclaration.h"
#import "WhileStatement.h"
#import <math.h>
#import "Functions.h"
// Global context....
static ASTContext *ctx = nil; // Initialized top ASTContext when first context is created...
#define AS(X,Y) ([X class] == Y)?X:nil
#define ASTSymbolTable NSMutableDictionary
NSString *tabulate(NSString *code);
......@@ -29,3 +29,6 @@
- (GenericType *)inferSymbol: (NSString *)name;
@end
// Global context....
static ASTContext *ctx = nil; // Initialized top ASTContext when first context is created...
#import <Foundation/Foundation.h>
#import "Types.h"
@class GenericType;
#define AS(X,Y) ([[X class] isKindOfClass: Y])?X:nil
#define ASTSymbolTable NSMutableDictionary
@interface ASTNode: NSObject
{
......@@ -9,6 +11,9 @@
@property (nonatomic, retain) GenericType *type;
+ (NSString *)prefix;
+ (void)setPrefix:(NSString *)string;
- (NSString *)toCode;
- (GenericType *) getType;
- (GenericType *) inferType;
......
#import "AST.h"
static NSString *ASTNode_prefix = nil;
@implementation ASTNode
@dynamic type;
/* Part of code generation integration... */
/**
* Prefix returns the prefix used by the Code Generation classes
* so that they can be intstantiated.
*/
+ (NSString *) prefix
{
if(ASTNode_prefix == nil)
{
return @"";
}
return ASTNode_prefix;
}
+ (void) setPrefix: (NSString *)string
{
ASTNode_prefix = string;
[ASTNode_prefix retain];
}
/**
* Override alloc: so that the proper instance is returned.
*/
+ (instancetype)alloc
{
if(self != [ASTNode class])
{
NSString *className = [NSString stringWithFormat:@"%@%@",[self prefix],NSStringFromClass(self)];
Class clz = NSClassFromString(className);
return [clz allocWithZone:NSDefaultMallocZone()];
}
return [super alloc];
}
- (id) init
{
self = [super init];
......
......@@ -14,18 +14,6 @@
return self;
}
- (NSString *)toCode
{
NSString *result = @"[";
ASTNode *data = self.items;
if(data != nil)
{
result = [result stringByAppendingString:[data toCode]];
}
result = [result stringByAppendingString:@"]"];
return result;
}
- (GenericType *)inferType
{
ExpressionList *node = (ExpressionList *)(AS(self.items,
......
......@@ -14,11 +14,6 @@
return self;
}
- (NSString *) toCode
{
return [NSString stringWithFormat:@" = %@",[self.rightOperand toCode]];
}
- (GenericType *) inferType
{
return [self.rightOperand getType];
......
......@@ -109,56 +109,6 @@
return result;
}
- (NSString *)toCode
{
//Check Tuple assignment binary expressions
AssignmentOperator *assignment = (AssignmentOperator *)(AS([self.next current],[AssignmentOperator class]));
if (assignment != nil)
{
self.current.type = [assignment.rightOperand getType];
//check left to right tuple assignment
ParenthesizedExpression *leftTuple = (ParenthesizedExpression *)(AS([self current], [ParenthesizedExpression class]));
ParenthesizedExpression *rightTuple = (ParenthesizedExpression *)(AS(assignment.rightOperand, [ParenthesizedExpression class]));
if(leftTuple && [leftTuple isList] && rightTuple && [rightTuple isList])
{
return [self leftAndRightTypeToCodeLeft:leftTuple
right:rightTuple];
}
else if(leftTuple && [leftTuple isList])
{
return [self leftTupleAndRightExpressionToCodeLeft:leftTuple
right:assignment.rightOperand];
}
}
BinaryOperator *binaryOperator = (BinaryOperator *)(AS([self.next current],[BinaryOperator class]));
//check for custom operators. Example array +=
if(binaryOperator)
{
NSString *customOperator = [[self.current getType]
customBinaryOperator:self.current
:binaryOperator.binaryOperator
:binaryOperator.rightOperand];
return customOperator;
}
//Generic binary expression
NSString *result = @"";
id currentExpression = self.current;
id nextExpression = self.next;
if(currentExpression)
{
result = [result stringByAppendingString: [currentExpression toCode]];
}
if(nextExpression)
{
result = [result stringByAppendingString: [nextExpression toCode]];
}
return result;
}
- (GenericType *)inferType
{
if([self current])
......
......@@ -21,24 +21,6 @@
return [NSString stringWithFormat:@"[%ld]",index];
}
- (NSString *) toCode
{
if([self.binaryOperator isEqualToString:@"."])
{
NSString *right = [self.rightOperand toCode];
NSInteger index = [right integerValue];
if(!isnan(index))
{
return [self codeForIndex:index];
}
return [NSString stringWithFormat: @"%@%@",self.binaryOperator, right];
}
// else...
return [NSString stringWithFormat: @" %@ %@",self.binaryOperator,
[self.rightOperand toCode]];
}
- (GenericType *) inferType
{
return [self.rightOperand getType];
......
......@@ -14,13 +14,4 @@
return self;
}
- (NSString *)toCode
{
NSString *identifier = self.labelName;
if(identifier)
{
return [NSString stringWithFormat:@"break %@;",identifier];
}
return @"break;";
}
@end
......@@ -12,13 +12,4 @@
return self;
}
- (NSString *)toCode
{
VariableDeclaration *varDeclaration = (VariableDeclaration *)(AS(self.declaration, [VariableDeclaration class]));
if(varDeclaration)
{
varDeclaration.exportVariables = NO;
}
return [self.declaration toCode];
}
@end
......@@ -15,13 +15,6 @@
return self;
}
- (NSString *)toCode
{
return [[[@"\n" stringByAppendingString: [self.key toCode]]
stringByAppendingString:@" : "]
stringByAppendingString:[self.value toCode]];
}
- (GenericType *)inferType
{
return [self.value getType];
......
......@@ -14,18 +14,6 @@
return self;
}
- (NSString *)toCode
{
NSString *result = @"{";
ASTNode *data = self.pairs;
if(data)
{
NSString *string = tabulate([[data toCode] stringByAppendingString:@"\n"]);
result = [result stringByAppendingString:string];
}
result = [result stringByAppendingString:@"}"];
return result;
}
- (GenericType *)inferType
{
......
......@@ -16,23 +16,6 @@
return self;
}
- (NSString *)toCode
{
NSString *result = @"";
ASTNode *currentExpression = self.current;
if (currentExpression)
{
result = [result stringByAppendingString:[currentExpression toCode]];
}
ExpressionList *nextExpression = self.next;
if (nextExpression)
{
result = [result stringByAppendingString: [NSString stringWithFormat:@", %@", [nextExpression toCode]]];
}
return result;
}
- (GenericType *)inferType
{
NSMutableArray *types = [[NSMutableArray alloc] initWithCapacity:10];// :[GenericType] = [];
......
#import "ASTNode.h"
@class ParenthesizedExpression;
@interface FunctionCallExpression : ASTNode
@property (nonatomic, retain) ASTNode *function;
......
......@@ -16,13 +16,6 @@
return self;
}
- (NSString *)toCode
{
self.parenthesized.allowInlineTuple = NO;
return [NSString stringWithFormat:@"%@%@",[self.function toCode],[self.parenthesized toCode]];
}
- (GenericType *)inferType
{
FunctionType *funcType = (FunctionType *)(AS([self.function getType], [FunctionType class]));
......
......@@ -18,25 +18,4 @@
return self;
}
- (NSString *)toCode
{
NSString *result = [NSString stringWithFormat:@"%@ (", self.name];
ASTNode *parameters = self.signature;
if (parameters)
{
result = [result stringByAppendingString:[parameters toCode]];
}
result = [result stringByAppendingString:@") {\n"];
ASTNode *statements = self.body;
if(statements)
{
NSString *string = tabulate([statements toCode]);
result = [result stringByAppendingString:string];
}
result = [result stringByAppendingString: @"}"];
return result;
}
@end
......@@ -24,9 +24,4 @@
return self;
}
- (NSString *)toCode
{
return self.local ? self.local : self.external;
}
@end
//
// CodeGenerator.h
// Functions.h
// phoenix
//
// Created by Gregory Casamento on 10/29/14.
// Created by Gregory Casamento on 12/10/14.
// Copyright (c) 2014 indie. All rights reserved.
//
#import <Foundation/Foundation.h>
#ifndef phoenix_Functions_h
#define phoenix_Functions_h
@interface CodeGenerator : NSObject
#import <Foundation/Foundation.h>
NSString *tabulate(NSString *code);
@end
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment