Commit 2dda7e53 authored by Gregory Casamento's avatar Gregory Casamento
Browse files

Add scripts and separate classes out.

parent f8f4ec80
This diff is collapsed.
......@@ -9,365 +9,47 @@
#import <Foundation/Foundation.h>
#import "Types.h"
@class ParenthesizedExpression;
// Define a subclass for future expansion...
@interface ASTSymbolTable : NSMutableDictionary
@end
// Context
@interface ASTContext : NSObject
//exported variable declarations
@property (nonatomic, retain) NSMutableArray *exportedVars;
@property (nonatomic, assign) NSInteger exportedIndex;
//scoped symbols for type inference
@property (nonatomic, retain) NSMutableArray *symbols;
@property (nonatomic, assign) NSInteger symbolsIndex;
//index for IDS
@property (nonatomic, assign) NSInteger generateIDIndex;
- (NSString *) variableDeclaration;
- (NSString *) declarationSeparator;
// Methods.
- (NSString *)generateID;
- (void) exportVar: (NSString *)name;
- (NSString *)getExportedVars;
- (void) saveExported;
- (void) restoreExported;
- (void) saveSymbols;
- (void) restoreSymbols;
- (void) addSymbolName: (NSString *)name
type: (GenericType *)type;
- (GenericType *)inferSymbol: (NSString *)name;
@end
#import "ASTContext.h"
#import "ASTNode.h"
#import "ArrayLiteral.h"
#import "AssignmentOperator.h"
#import "BinaryExpression.h"
#import "BinaryOperator.h"
#import "BreakStatement.h"
#import "DeclarationStatement.h"
#import "DictionaryItem.h"
#import "DictionaryLiteral.h"
#import "ExpressionList.h"
#import "FunctionCallExpression.h"
#import "FunctionDeclaration.h"
#import "FunctionParameter.h"
#import "IdentifierExpression.h"
#import "IfStatement.h"
#import "ImportStatement.h"
#import "LabelStatement.h"
#import "LiteralExpression.h"
#import "NSString+Extension.h"
#import "NamedExpression.h"
#import "OptionalChainExprStatement.h"
#import "ParenthesizedExpression.h"
#import "PostfixOperator.h"
#import "PrefixOperator.h"
#import "ReturnStatement.h"
#import "StatementNode.h"
#import "StatementsNode.h"
#import "TernaryOperator.h"
#import "TypeExpression.h"
#import "VariableDeclaration.h"
#import "WhileStatement.h"
#import <math.h>
// Global context....
static ASTContext *ctx = nil; // Initialized top ASTContext when first context is created...
// Node
@interface ASTNode: NSObject
{
GenericType *type;
}
@property (nonatomic, retain) GenericType *type;
- (NSString *)toCode;
- (GenericType *) getType;
- (GenericType *) inferType;
- (void) setType: (GenericType *)type;
- (void) setTypeIfEmpty: (GenericType *)type;
@end
// Literal expression...
@interface LiteralExpression: ASTNode
@property (nonatomic, retain) NSString *value;
- (id) init: (NSString *)literal;
// - (NSString *) toCode
// - (GenericType *) inferType;
@end
// Identifier expression...
@interface IdentifierExpression: ASTNode
@property (nonatomic, retain) NSString *name;
- (id) init: (NSString *)identifier;
// - (NSString *) toCode
// - (GenericType *) inferType;
@end
@interface BinaryOperator : ASTNode
@property (nonatomic, retain) ASTNode *rightOperand;
@property (nonatomic, retain) NSString *binaryOperator;
- (id) initWithRightOperand: (ASTNode *)rightOperand
binaryOperator: (NSString *)binaryOperator;
- (NSString *) codeForIndex: (NSInteger)index;
// - (NSString *) toCode
// - (GenericType *) inferType;
@end
@interface AssignmentOperator : ASTNode
@property (nonatomic, retain) ASTNode *rightOperand;
- (id) initWithRightOperand: (ASTNode *)rightOperand;
// - (NSString *) toCode
// - (GenericType *) inferType;
@end
@interface TernaryOperator : ASTNode
@property (nonatomic, retain) ASTNode *trueOperand;
@property (nonatomic, retain) ASTNode *falseOperand;
- (id) initWithTrueOperand: (ASTNode *)trueOperand
falseOperand: (ASTNode *)falseOperand;
// - (NSString *) toCode
// - (GenericType *) inferType;
@end
@interface PrefixOperator : ASTNode
@property (nonatomic, retain) ASTNode *operand;
@property (nonatomic, retain) NSString *prefixOperator;
- (id) init: (ASTNode *)operand
: (NSString *)prefixOperator;
// - (NSString *) toCode
// - (GenericType *) inferType;
@end
@interface PostfixOperator : ASTNode
@property (nonatomic, retain) ASTNode *operand;
@property (nonatomic, retain) NSString *postfixOperator;
- (id) init: (ASTNode *)operand
: (NSString *)prefixOperator;
// - (NSString *) toCode
// - (GenericType *) inferType;
@end
///
@interface BinaryExpression: ASTNode
@property (nonatomic, retain) ASTNode *current;
@property (nonatomic, retain) BinaryExpression *next;
- (id) initWithExpression: (ASTNode *)expression;
- (id) initWithExpression: (ASTNode *)expression
next: (BinaryExpression *)next;
- (NSString *) leftAndRightTypeToCodeLeft: (ParenthesizedExpression *)left
right: (ParenthesizedExpression *)right;
- (NSString *) leftTupleAndRightExpressionToCodeLeft: (ParenthesizedExpression *)left
right: (ASTNode *)right;
// - (NSString *)toCode;
// - (GenericType *)inferType;
@end
@interface NamedExpression: ASTNode
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) ASTNode *expr;
- (id) initWithName: (NSString *)name
expr: (ASTNode *)expr;
// - (NSString *)toCode;
// - (GenericType *)inferType;
@end
@interface TypeExpression: ASTNode
@property (nonatomic, retain) GenericType *linkedType;
- (id) initWithLinkedType: (GenericType *)linkedType;
// - (NSString *)toCode;
// - (GenericType *)inferType;
@end
@interface ExpressionList : ASTNode
@property (nonatomic, retain) ASTNode *current;
@property (nonatomic, retain) ExpressionList *next;
- (id)initWithExpr: (ASTNode *)expr
next: (ExpressionList *)next;
// - (NSString *)toCode;
// - (GenericType *)inferType;
@end
@interface ParenthesizedExpression : ASTNode
@property (nonatomic,retain) ASTNode *expression;
@property (nonatomic,assign) BOOL allowInlineTuple;
- (id) initWithExpression: (ASTNode *)expression;
- (NSString *) toInlineTuple: (ExpressionList *) list;
- (BOOL) isList;
- (NSArray *) toExpressionArray;
- (NSArray *) toTypesArray;
- (NSString *) toTupleInitializer: (NSString *)variableName;
// - (NSString *)toCode;
// - (GenericType *)inferType;
@end
@interface FunctionCallExpression : ASTNode
@property (nonatomic, retain) ASTNode *function;
@property (nonatomic, retain) ParenthesizedExpression *parenthesized;
- (id) initWithFunction: (ASTNode *)function
parenthesized: (ParenthesizedExpression *)parenthesized;
// - (NSString *)toCode;
// - (GenericType *)inferType;
@end
@interface VariableDeclaration: ASTNode
@property (nonatomic, retain) ExpressionList *initializer;
@property (nonatomic, assign) BOOL exportVariables;
- (id) initWithInitializer: (ExpressionList *)initializer;
- (void) exportSymbols: (ASTNode *)expression;
// - (NSString *) toCode;
@end
@interface ArrayLiteral: ASTNode
@property (nonatomic, retain) ASTNode *items;
- (id) initWithItems: (ASTNode *)items;
// - (NSString *)toCode;
// - (GenericType *)inferType;
@end
@interface DictionaryLiteral : ASTNode
@property (nonatomic, retain) ASTNode *pairs;
- (id) initWithPairs: (ASTNode *)pairs;
// - (NSString *)toCode;
// - (GenericType *)inferType;
@end
@interface DictionaryItem: ASTNode
@property (nonatomic, retain) ASTNode *key;
@property (nonatomic, retain) ASTNode *value;
- (id) initWithKey: (ASTNode *)key value: (ASTNode *)value;
// - (NSString *)toCode;
// - (GenericType *)inferType;
@end
@interface FunctionParameter : ASTNode
@property (nonatomic, assign) BOOL inoutVal;
@property (nonatomic, assign) BOOL letVal;
@property (nonatomic, assign) BOOL hashVal;
@property (nonatomic, retain) NSString *external;
@property (nonatomic, retain) NSString *local;
@property (nonatomic, retain) ASTNode *defVal;
- (id) initWithInoutVal: (BOOL)inoutVal
letVal: (BOOL)letVal
hashVal: (BOOL)hashVal
external: (NSString *)external
local: (NSString *)local
defVal: (ASTNode *)defVal;
// - (NSString *)toCode;
// - (GenericType *)inferType;
@end
@interface FunctionDeclaration : ASTNode
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) ASTNode *signature;
@property (nonatomic, retain) ASTNode *body;
- (id) initWithName: (NSString *)name
signature: (ASTNode *)signature
body: (ASTNode *)body;
// - (NSString *)toCode;
// - (GenericType *)inferType;
@end
/*** Statements ***/
@interface WhileStatement : ASTNode
@property (nonatomic, retain) ASTNode *whileCondition;
@property (nonatomic, retain) ASTNode *codeBlock;
- (id)initWithWhileCondition: (ASTNode *)whileCondition
codeBlock: (ASTNode *)codeBlock;
// - (NSString *)toCode;
@end
@interface LabelStatement : ASTNode
@property (nonatomic, retain) NSString *labelName;
@property (nonatomic, retain) ASTNode *loop;
- (id) initWithLabelName: (NSString *)labelName
loop: (ASTNode *)loop;
// - (NSString *)toCode;
@end
@interface BreakStatement : ASTNode
@property (nonatomic, retain) NSString *labelName;
- (id) initWithLabelId: (NSString *)labelName;
// - (NSString *)toCode;
@end
@interface ReturnStatement : ASTNode
@property (nonatomic, retain) ASTNode *returnExpr;
- (id) initWithReturnExpr: (ASTNode *)returnExpr;
// - (NSString *)toCode;
// - (GenericType *)inferType;
@end
@interface OptionalChainExprStatement : ASTNode
@property (nonatomic, retain) ASTNode *optChainExpr;
- (id) initWithOptChainExpr: (ASTNode *)optChainExpr;
// - (NSString *)toCode;
// - (GenericType *)inferType;
@end
@interface IfStatement : ASTNode
@property (nonatomic, retain) ASTNode *ifCondition;
@property (nonatomic, retain) ASTNode *body;
@property (nonatomic, retain) ASTNode *elseClause;
- (id) initWithIfCondition: (ASTNode *)ifCondition
body: (ASTNode *)body
elseClause: (ASTNode *)elseClause;
// - (NSString *)toCode;
@end
@interface ImportStatement : ASTNode
@property (nonatomic, retain) NSString *path;
- (id) initWithPath: (NSString *)path;
// - (NSString *)toCode;
@end
@interface StatementNode : ASTNode
@property (nonatomic, retain) ASTNode *statement;
- (id) initWithStatement: (ASTNode *)statement;
// - (NSString *)toCode;
@end
@interface DeclarationStatement : ASTNode
@property (nonatomic, retain) ASTNode *declaration;
- (id) initWithDeclaration: (ASTNode *)declaration;
// - (NSString *)toCode;
@end
@interface StatementsNode : ASTNode
@property (nonatomic, retain) ASTNode *current;
@property (nonatomic, retain) ASTNode *next;
@property (nonatomic, assign) BOOL firstStatement;
- (id) initWithCurrent: (ASTNode *)current;
- (id) initWithCurrent: (ASTNode *)current
next: (StatementsNode *)next;
// - (NSString *)toCode;
- (NSInteger) getStatementsCount;
@end
#define AS(X,Y) ([X class] == Y)?X:nil
#define ASTSymbolTable NSMutableDictionary
NSString *tabulate(NSString *code);
This diff is collapsed.
#import <Foundation/Foundation.h>
@interface ASTContext : NSObject
//exported variable declarations
@property (nonatomic, retain) NSMutableArray *exportedVars;
@property (nonatomic, assign) NSInteger exportedIndex;
//scoped symbols for type inference
@property (nonatomic, retain) NSMutableArray *symbols;
@property (nonatomic, assign) NSInteger symbolsIndex;
//index for IDS
@property (nonatomic, assign) NSInteger generateIDIndex;
- (NSString *) variableDeclaration;
- (NSString *) declarationSeparator;
// Methods.
- (NSString *)generateID;
- (void) exportVar: (NSString *)name;
- (NSString *)getExportedVars;
- (void) saveExported;
- (void) restoreExported;
- (void) saveSymbols;
- (void) restoreSymbols;
- (void) addSymbolName: (NSString *)name
type: (GenericType *)type;
- (GenericType *)inferSymbol: (NSString *)name;
@end
#import "AST.h"
@implementation ASTContext
@synthesize exportedIndex, exportedVars, symbols, symbolsIndex, generateIDIndex;
- (id) init
{
if(ctx != nil)
{
return ctx;
}
else
{
self = [super init];
if(self != nil)
{
ctx = self; // set global context...
self.exportedVars = [[NSMutableArray alloc] init]; // array of arrays of exported variables...
self.exportedIndex = 0;
self.symbols = [[NSMutableArray alloc] init]; // Array of ASTSymbolTable objects...
self.symbolsIndex = -1;
self.generateIDIndex = 0;
}
return self;
}
return nil;
}
- (NSString *)variableDeclaration
{
return @"id"; // temporary
}
- (NSString *)declarationSeparator
{
return @","; // temporary...
}
// Methods.
- (NSString *)generateID
{
return [NSString stringWithFormat:@"_ref%ld",
(long)self.generateIDIndex++];
}
- (BOOL) _find: (NSString *)name
{
if([self.exportedVars count] < self.symbolsIndex)
{
[self saveExported];
}
NSArray *array = [self.exportedVars objectAtIndex:self.exportedIndex];
return [array containsObject:name];
}
- (void) exportVar: (NSString *)name
{
if(![self _find:name])
{
[[self.exportedVars objectAtIndex:self.exportedIndex] addObject:name];
}
}
- (NSString *)getExportedVars
{
if ([[self.exportedVars objectAtIndex: self.exportedIndex - 1] count] > 0)
{
NSString *result = @"";
result = [result stringByAppendingString:[self variableDeclaration]];
for (NSString *variable in [self.exportedVars objectAtIndex:self.exportedIndex])
{
result = [result stringByAppendingString:
[variable stringByAppendingString: [self declarationSeparator]]];
}
result = [result substringFromIndex:
[result lengthOfBytesUsingEncoding:NSUTF8StringEncoding] - 1];
result = [result stringByAppendingString:@";\n"];
return result;
}
return nil;
}
- (void) saveExported
{
self.exportedIndex++;
[self.exportedVars addObject:[NSMutableArray array]];
}
- (void) restoreExported
{
if(self.exportedIndex > 0)
{
[self.exportedVars removeLastObject];
self.exportedIndex--;
}
}
- (void) saveSymbols
{
self.symbolsIndex++;
[self.symbols addObject:[NSMutableArray array]];
}
- (void) restoreSymbols
{
if(self.symbolsIndex > 0)
{
[self.symbols removeLastObject];
self.symbolsIndex--;
}
}
- (void) addSymbolName: (NSString *)name
type: (GenericType *)type
{
if([self.symbols count] < self.symbolsIndex)
{
ASTSymbolTable *table = [[ASTSymbolTable alloc] init];
[self.symbols addObject:table];
}
if(self.symbolsIndex < 0)
{
[self saveSymbols];
}
[[self.symbols objectAtIndex:self.symbolsIndex] setObject:type
forKey:name];
}
- (GenericType *)inferSymbol: (NSString *)name
{
for(NSInteger i = self.symbolsIndex; i >= 0; --i)
{
GenericType *type = [[self.symbols objectAtIndex:i] objectForKey:name];
if(type != nil)
{
return type;
}
}
return nil;
}
@end
#import <Foundation/Foundation.h>
@interface ASTNode: NSObject
{
GenericType *type;
}
@property (nonatomic, retain) GenericType *type;
- (NSString *)toCode;
- (GenericType *) getType;
- (GenericType *) inferType;
- (void) setType: (GenericType *)type;
- (void) setTypeIfEmpty: (GenericType *)type;
@end
#import "AST.h"
@implementation ASTNode
@dynamic type;
- (id) init
{
self = [super init];
if(self)