Commit c2a40723 authored by Krzysztof Kaczor's avatar Krzysztof Kaczor
Browse files

extracted ast builder class

parent 51d1b989
//
// Created by Krzysztof Kaczor on 1/5/15.
// Copyright (c) 2015 indie. All rights reserved.
//
#import <Foundation/Foundation.h>
@class ASTNode;
@interface AstBuilder : NSObject
- (instancetype)initWithDebug:(BOOL)aDebug;
- (ASTNode *)getAstFromSource:(NSString *)sourceCode;
+ (instancetype)builderWithDebug:(BOOL)aDebug;
@end
\ No newline at end of file
//
// Created by Krzysztof Kaczor on 1/5/15.
// Copyright (c) 2015 indie. All rights reserved.
//
#import "AstBuilder.h"
#import "ASTNode.h"
#import "Lexer.h"
#import "bridge.h"
@implementation AstBuilder {
BOOL debug;
}
- (instancetype)initWithDebug:(BOOL)aDebug {
self = [super init];
if (self) {
debug = aDebug;
}
return self;
}
+ (instancetype)builderWithDebug:(BOOL)aDebug {
return [[[self alloc] initWithDebug:aDebug] autorelease];
}
- (ASTNode*) getAstFromSource: (NSString*) sourceCode {
Lexer *lexer = [[Lexer alloc] initWithSourceCode:sourceCode];
if(debug)
{
NSLog(@"Lexer Tokens");
NSLog(@"============");
[lexer debugTokens];
NSLog(@"============\n");
NSLog(@"AST Parser");
NSLog(@"===========");
}
ASTNode* rootNode = bridge_yyparse(lexer, debug);
[lexer release];
return rootNode;
}
@end
\ No newline at end of file
......@@ -9,6 +9,7 @@
#import <Foundation/Foundation.h>
#import "AST.h"
#import "ObjCASTTransform.h"
#import "AstBuilder.h"
@interface Compiler : NSObject
{
......@@ -19,6 +20,7 @@
NSString *errors;
NSString *prefix;
id output;
AstBuilder* astBuilder;
}
- (id)initWithSourceCode:(NSString *)code;
......
......@@ -11,8 +11,11 @@
#import "ASTContext.h"
#import "AST.h"
#import "bridge.h"
#import "AstBuilder.h"
@implementation Compiler
@implementation Compiler {
BOOL debug;
}
- (id)initWithSourceCode:(NSString *)code {
return [self initWithSourceCode:code andPrefix:nil];
......@@ -36,6 +39,9 @@
}
ctx = [[ASTContext alloc] init];
debug = NO;
astBuilder = [[AstBuilder builderWithDebug:debug] retain];
}
return self;
}
......@@ -57,27 +63,12 @@
- (void)compile
{
BOOL debug = NO;
Lexer *lexer = [[Lexer alloc] initWithSourceCode:sourceCode];
if(debug)
{
NSLog(@"Lexer Tokens");
NSLog(@"============");
[lexer debugTokens];
NSLog(@"============\n");
NSLog(@"AST Parser");
NSLog(@"===========");
}
rootNode = (ASTNode *)bridge_yyparse(lexer, debug);
rootNode = [astBuilder getAstFromSource:sourceCode];
if(rootNode != nil)
{
if (prefix) {
transform = [ASTTransform createWithAST:rootNode
forPrefix:prefix];
transform = [ASTTransform createWithAST:rootNode forPrefix:prefix];
transformed = [transform transform];
NSString *program = [transformed toCode];
......@@ -114,4 +105,11 @@
return errors;
}
- (void)dealloc {
[astBuilder release];
[output release];
[errors release];
[super dealloc];
}
@end
//
// AstBuildingTests.m
// AstBuilderTests.m
// phoenix
//
// Created by Krzysztof Kaczor on 12/28/14.
......@@ -10,11 +10,11 @@
#import <XCTest/XCTest.h>
#import "Compiler.h"
@interface AstBuildingTests : XCTestCase
@interface AstBuilderTests : XCTestCase
@end
@implementation AstBuildingTests
@implementation AstBuilderTests
- (void)setUp {
[super setUp];
......
......@@ -162,7 +162,8 @@
6B8A837B1A50A0A1006A1AB7 /* libphoenixLib.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 6B8A821D1A509E4E006A1AB7 /* libphoenixLib.a */; };
6B8A837E1A50A17D006A1AB7 /* IdentifierExpression.m in Sources */ = {isa = PBXBuildFile; fileRef = 6B8A82721A509F7A006A1AB7 /* IdentifierExpression.m */; };
6B8A837F1A50A18B006A1AB7 /* VariableDeclaration.m in Sources */ = {isa = PBXBuildFile; fileRef = 6B8A82901A509F7A006A1AB7 /* VariableDeclaration.m */; };
6B8A83821A50A1F4006A1AB7 /* AstBuildingTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 6B8A83811A50A1F4006A1AB7 /* AstBuildingTests.m */; };
6B8A83821A50A1F4006A1AB7 /* AstBuilderTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 6B8A83811A50A1F4006A1AB7 /* AstBuilderTests.m */; };
6BC9568F1A5B3FB300F11BCD /* AstBuilder.m in Sources */ = {isa = PBXBuildFile; fileRef = C81A59614D248ACC71C5B97E /* AstBuilder.m */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
......@@ -357,7 +358,9 @@
6B8A82D91A509F7A006A1AB7 /* Regex.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Regex.m; sourceTree = "<group>"; };
6B8A82DB1A509F7A006A1AB7 /* NSString+Extension.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSString+Extension.h"; sourceTree = "<group>"; };
6B8A82DC1A509F7A006A1AB7 /* NSString+Extension.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSString+Extension.m"; sourceTree = "<group>"; };
6B8A83811A50A1F4006A1AB7 /* AstBuildingTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AstBuildingTests.m; sourceTree = "<group>"; };
6B8A83811A50A1F4006A1AB7 /* AstBuilderTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AstBuilderTests.m; sourceTree = "<group>"; };
C81A59614D248ACC71C5B97E /* AstBuilder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AstBuilder.m; sourceTree = "<group>"; };
C81A5BC72EDDE699A40B2ECF /* AstBuilder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AstBuilder.h; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
......@@ -434,7 +437,7 @@
isa = PBXGroup;
children = (
6B8A822C1A509E4E006A1AB7 /* Supporting Files */,
6B8A83811A50A1F4006A1AB7 /* AstBuildingTests.m */,
6B8A83811A50A1F4006A1AB7 /* AstBuilderTests.m */,
);
name = PhoenixLibTests;
path = phoenixLibTests;
......@@ -453,6 +456,8 @@
children = (
6B8A82351A509F7A006A1AB7 /* Compiler.h */,
6B8A82361A509F7A006A1AB7 /* Compiler.m */,
C81A59614D248ACC71C5B97E /* AstBuilder.m */,
C81A5BC72EDDE699A40B2ECF /* AstBuilder.h */,
);
path = Compiler;
sourceTree = "<group>";
......@@ -898,6 +903,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
6BC9568F1A5B3FB300F11BCD /* AstBuilder.m in Sources */,
6B8A837F1A50A18B006A1AB7 /* VariableDeclaration.m in Sources */,
6B8A837E1A50A17D006A1AB7 /* IdentifierExpression.m in Sources */,
6B8A835A1A509F7A006A1AB7 /* ObjCMainFunctionDeclaration.m in Sources */,
......@@ -980,7 +986,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
6B8A83821A50A1F4006A1AB7 /* AstBuildingTests.m in Sources */,
6B8A83821A50A1F4006A1AB7 /* AstBuilderTests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
......
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