Skip to content

Commit

Permalink
Add review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardoLR10 committed Mar 12, 2024
1 parent 5bfa415 commit f1506b9
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 64 deletions.
5 changes: 3 additions & 2 deletions Chime/Headers/Utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ NSInteger packFullWord(NSArray *instructions, NSMapTable *keywords);

NSMapTable *parseCommandLine(int argc, const char *argv[]);

void handleProgramAndDialect(NSMapTable *cmds, NSString *candidateProgram,
NSString *candidateDialect);
void errorMissingLoadFilePath();

void errorMissingSaveFilePath();

#endif /* Utilities_h */
15 changes: 12 additions & 3 deletions Chime/Main.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,32 @@ int main(int argc, const char *argv[]) {

NSMutableString *candidateProgram = [[NSMutableString alloc] init];
NSMutableString *candidateDialect = [[NSMutableString alloc] init];
handleProgramAndDialect(cmds, candidateProgram, candidateDialect);
[candidateProgram setString:[cmds objectForKey:@"ProgramFilePath"]];
[candidateDialect setString:[cmds objectForKey:@"DialectFilePath"]];

NSString *candidateLoad = [cmds objectForKey:@"LoadFilePath"];

if (candidateLoad != nil) {
[vm LoadBytecode:candidateLoad];
} else {
if ([candidateProgram length] == 0) {
errorMissingProgramFilePath();
}

if ([candidateDialect length] == 0) {
errorMissingDialectFilePath();
}

[vm LoadProgram:[NSString stringWithString:candidateProgram]
usingKeywords:[NSString stringWithString:candidateDialect]];
}

NSString *candidateSave = [cmds objectForKey:@"SaveFilePath"];
if (candidateSave != nil) {
[vm SaveProgram:candidateSave];
} else {
[vm Evaluate];
}

[vm Evaluate];
}
return 0;
}
37 changes: 30 additions & 7 deletions Chime/Parser.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ - (void)loadKeywords:(char *)filepath {

NSData *data = [[NSData alloc] initWithContentsOfFile:@(filepath)];

if (data.length == 0) {
@throw [NSException
exceptionWithName:@"Could not load Chime dialect"
reason:[NSString
stringWithFormat:@"Could not find file \"%@\"",
@(filepath)]
userInfo:nil];
}

if (data == nil) {
NSLog(@"Could not read keywords from file \"%s\"", filepath);
}
Expand Down Expand Up @@ -339,18 +348,32 @@ - (NSMutableArray *)passTwo {
return [trimmedProgram copy];
}

- (NSMutableArray *)ParseBytecode:(char *)filePath {
NSData *data = [[NSData alloc] initWithContentsOfFile:@(filePath)];
- (NSMutableArray *)ParseBytecode:(char *)filepath {
NSData *data = [[NSData alloc] initWithContentsOfFile:@(filepath)];

if (data.length == 0) {
@throw [NSException
exceptionWithName:@"Could not load Chime bytecode"
reason:[NSString
stringWithFormat:@"Could not find file \"%@\"",
@(filepath)]
userInfo:nil];
}

return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}

- (NSMutableArray *)ParseProgram:(char *)filePath
- (NSMutableArray *)ParseProgram:(char *)filepath
usingKeywords:(char *)keywordsPath {
NSData *data = [[NSData alloc] initWithContentsOfFile:@(filePath)];
NSData *data = [[NSData alloc] initWithContentsOfFile:@(filepath)];

if (data == nil) {
NSLog(@"Could not read file \"%s\"", filePath);
return nil;
if (data.length == 0) {
@throw [NSException
exceptionWithName:@"Could not load Chime program"
reason:[NSString
stringWithFormat:@"Could not find file \"%@\"",
@(filepath)]
userInfo:nil];
}

NSString *stringProgram =
Expand Down
76 changes: 24 additions & 52 deletions Chime/Utilities.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,20 @@ NSInteger packFullWord(NSArray *instructions, NSMapTable *keywordsMap) {
void errorMissingProgramFilePath() {
@throw [NSException
exceptionWithName:@"Missing program in command line"
reason:[NSString stringWithFormat:
@"Expected the filepath of a program as "
@"the first argument in command line"]
reason:[NSString
stringWithFormat:
@"Expected the filepath of a program alonside "
@"the flag \"-p\" or \"--program\""]
userInfo:nil];
}

void errorMissingDialectFilePath() {
@throw [NSException
exceptionWithName:@"Missing dialect set in command line"
reason:[NSString stringWithFormat:
@"Expected the filepath of a dialect as "
@"the second argument in command line"]
reason:[NSString
stringWithFormat:
@"Expected the filepath of a dialect alongside "
@"the flag \"-d\" or \"--dialect\""]
userInfo:nil];
}

Expand Down Expand Up @@ -93,62 +95,32 @@ void errorExtraArguments(char *arg) {
}

NSMapTable *parseCommandLine(int argc, const char *argv[]) {
#define SaveArg(keyName, errorMessageFunc) \
if (argv[i + 1] == NULL) { \
errorMessageFunc; \
} else { \
[cmds setObject:@(argv[i + 1]) forKey:@keyName]; \
i++; \
}
NSMapTable *cmds = [[NSMapTable alloc] init];

// Jumping the program in argv
BOOL programCaptured = NO;
for (int i = 1; i < argc; i++) {
if ((strcmp(argv[i], "-l") == 0) || (strcmp(argv[i], "--load") == 0)) {
if (argv[i + 1] == NULL) {
errorMissingLoadFilePath();
} else {
[cmds setObject:@(argv[i + 1]) forKey:@"LoadFilePath"];
i++;
}
SaveArg("LoadFilePath", errorMissingLoadFilePath());
} else if ((strcmp(argv[i], "-s") == 0) ||
(strcmp(argv[i], "--save") == 0)) {
if (argv[i + 1] == NULL) {
errorMissingSaveFilePath();
} else {
[cmds setObject:@(argv[i + 1]) forKey:@"SaveFilePath"];
i++;
}
SaveArg("SaveFilePath", errorMissingSaveFilePath());
} else if ((strcmp(argv[i], "-p") == 0) ||
(strcmp(argv[i], "--program") == 0)) {
SaveArg("ProgramFilePath", errorMissingProgramFilePath());
} else if ((strcmp(argv[i], "-d") == 0) ||
(strcmp(argv[i], "--dialect") == 0)) {
SaveArg("DialectFilePath", errorMissingDialectFilePath());
} else {
if (([cmds objectForKey:@"ProgramFilePath"] != nil) &&
([cmds objectForKey:@"DialectFilePath"] != nil)) {
errorExtraArguments(argv[i]);
} else {
if (!programCaptured) {
if (argv[i] == NULL) {
errorMissingProgramFilePath();
}
[cmds setObject:@(argv[i]) forKey:@"ProgramFilePath"];
programCaptured = YES;
} else {
if (argv[i] == NULL) {
errorMissingDialectFilePath();
}
[cmds setObject:@(argv[i]) forKey:@"DialectFilePath"];
}
}
errorExtraArguments(argv[i]);
}
}

return cmds;
}

void handleProgramAndDialect(NSMapTable *cmds,
NSMutableString *candidateProgram,
NSMutableString *candidateDialect) {
[candidateProgram setString:[cmds objectForKey:@"ProgramFilePath"]];
if (candidateProgram == nil) {
errorMissingProgramFilePath();
}

[candidateDialect setString:[cmds objectForKey:@"DialectFilePath"]];
if (candidateDialect == nil) {
errorMissingDialectFilePath();
}

return;
}

0 comments on commit f1506b9

Please sign in to comment.