Skip to content

Commit

Permalink
Implement algorithm parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
stoeffn committed Feb 6, 2018
1 parent 21940bd commit 002c138
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ @implementation PSAlgorithmNode (JavaScript)
- (nonnull NSString *) javaScriptCode {
if (self.typeIdentifier) {
return [[NSString alloc] initWithFormat: @"%@.prototype.%@ = function(%@) { %@ };",
self.typeIdentifier, self.identifier, self.parametersJavaScriptCode, self.bodyNode];
self.typeIdentifier, self.identifier, self.parametersJavaScriptCode, self.bodyNode.javaScriptCode];
}

return [[NSString alloc] initWithFormat: @"var %@ = function(%@) { %@ };",
self.identifier, self.parametersJavaScriptCode, self.bodyNode];
self.identifier, self.parametersJavaScriptCode, self.bodyNode.javaScriptCode];
}

- (nonnull NSString *) parametersJavaScriptCode {
Expand Down
56 changes: 49 additions & 7 deletions PseudoKit/Parser/PSParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@
#import "Macros.h"
#import "PSParser.h"
#import "PSToken.h"
#import "PSAlgorithmNode.h"
#import "PSBinaryOperationNode.h"
#import "PSBinaryOperationNode+Types.h"
#import "PSCompoundNode.h"
#import "PSConditionNode.h"
#import "PSControlFlowNode.h"
#import "PSControlFlowNode+Types.h"
#import "PSLiteralNode.h"
#import "PSLiteralNode+Types.h"
#import "PSBinaryOperationNode.h"
#import "PSBinaryOperationNode+Types.h"
#import "PSUnaryOperationNode.h"
#import "PSUnaryOperationNode+Types.h"
#import "PSControlFlowNode.h"
#import "PSControlFlowNode+Types.h"

@implementation PSParser

Expand Down Expand Up @@ -125,7 +126,7 @@ - (nullable PSNode *) conditionWithError: (NSError * __nullable __autoreleasing
PSNode *elseNode;

if (self.lexer.currentToken.type == PSTokenTypesElse) {
[self.lexer advance];
[self.lexer expectTokenTypes: PSTokenTypesElse error: error];
elseNode = [self blockListWithStopTokenType: @(PSTokenTypesPoint) error: error];
}

Expand All @@ -139,8 +140,49 @@ - (nullable PSNode *) conditionWithError: (NSError * __nullable __autoreleasing
#pragma mark Algorithms

- (nullable PSNode *) algorithmWithError: (NSError * __nullable __autoreleasing * __null_unspecified) error {
NSLog(@"Algorithms are not supported yet.");
return NULL;
PSToken *typeIdentifierToken;
PSToken *returnTypeIdentifierToken;
PSToken *algorithmToken = [self.lexer expectTokenTypes: PSTokenTypesAlgorithm error: error];
PSToken *identifierToken = [self.lexer expectTokenTypes: PSTokenTypesIdentifier error: error];

if (self.lexer.currentToken.type == PSTokenTypesPoint) {
[self.lexer expectTokenTypes: PSTokenTypesPoint error: error];
typeIdentifierToken = identifierToken;
identifierToken = [self.lexer expectTokenTypes: PSTokenTypesIdentifier error: error];
}

NSArray<PSNode *> *parameterNodes = [self parameterListWithError: error];
[self.lexer expectTokenTypes: PSTokenTypesColon error: error];

if (self.lexer.currentToken.type == PSTokenTypesIdentifier && self.lexer.nextToken.type == PSTokenTypesColon) {
returnTypeIdentifierToken = [self.lexer expectTokenTypes: PSTokenTypesIdentifier error: error];
[self.lexer expectTokenTypes: PSTokenTypesColon error: error];
}

PSNode *bodyNode = [self blockListWithStopTokenType: @(PSTokenTypesPoint) error: error];

if (*error) return NULL;
return [[PSAlgorithmNode alloc] initWithToken: algorithmToken
typeIdentifier: typeIdentifierToken.string
identifier: identifierToken.string
parameterNodes: parameterNodes
returnTypeIdentifier: returnTypeIdentifierToken.string
bodyNode: bodyNode];
}

- (nullable NSArray<PSNode *> *) parameterListWithError: (NSError * __nullable __autoreleasing * __null_unspecified) error {
NSMutableArray<PSNode *> *parameters = [[NSMutableArray alloc] init];
[self.lexer expectTokenTypes: PSTokenTypesOpeningParenthesis error: error];

while (self.lexer.currentToken.type == PSTokenTypesIdentifier) {
PSToken *typeIdentifier = [self.lexer expectTokenTypes: PSTokenTypesIdentifier error: error];
PSToken *identifier = [self.lexer expectTokenTypes: PSTokenTypesIdentifier error: error];
}

[self.lexer expectTokenTypes: PSTokenTypesClosingParanthesis error: error];

if (*error) return NULL;
return parameters;
}

#pragma mark Loops
Expand Down

0 comments on commit 002c138

Please sign in to comment.