Skip to content

Commit

Permalink
implemented else if
Browse files Browse the repository at this point in the history
  • Loading branch information
Glowman554 committed Oct 23, 2023
1 parent effc9b0 commit 7f27f1c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 26 deletions.
65 changes: 40 additions & 25 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,46 @@ export class Parser {
throw new Error("Unexpected EOF");
}

parse_if() {
const if_body: ParserNode[] = [];
this.advance();
const expr = this.expression();
this.expect(LexerTokenType.LBRACE);
if (expr) {
const code_block = this.code_block();
this.expect(LexerTokenType.RBRACE);
this.advance();
if (this.current && this.current.id == LexerTokenType.ID) {
if (this.current.value as string == "else") {
this.advance();
if (this.current.id == LexerTokenType.ID) {
if (this.current.value == "if") {
const else_code_block = this.parse_if();
this.expect(LexerTokenType.RBRACE);
if_body.push(new ParserNode(ParserNodeType.IF, expr, undefined, new If(code_block, else_code_block)));
}
} else {
this.expect(LexerTokenType.LBRACE);
const else_code_block = this.code_block();
this.expect(LexerTokenType.RBRACE);
if_body.push(new ParserNode(ParserNodeType.IF, expr, undefined, new If(code_block, else_code_block)));
}
} else {
this.reverse();
if_body.push(new ParserNode(ParserNodeType.IF, expr, undefined, new If(code_block, undefined)));
}
} else {
this.reverse();
if_body.push(new ParserNode(ParserNodeType.IF, expr, undefined, new If(code_block, undefined)));
}
} else {
throw new Error("Expected expression");
}
return if_body;
}

code_block(): ParserNode[] {
const body: ParserNode[] = [];
let body: ParserNode[] = [];
this.expect(LexerTokenType.LBRACE);
this.advance();
while (this.current) {
Expand Down Expand Up @@ -393,30 +431,7 @@ export class Parser {

case "if":
{
this.advance();
const expr = this.expression();
this.expect(LexerTokenType.LBRACE);
if (expr) {
const code_block = this.code_block();
this.expect(LexerTokenType.RBRACE);
this.advance();
if (this.current && this.current.id == LexerTokenType.ID) {
if (this.current.value as string == "else") {
this.advance_expect(LexerTokenType.LBRACE);
const else_code_block = this.code_block();
this.expect(LexerTokenType.RBRACE);
body.push(new ParserNode(ParserNodeType.IF, expr, undefined, new If(code_block, else_code_block)));
} else {
this.reverse();
body.push(new ParserNode(ParserNodeType.IF, expr, undefined, new If(code_block, undefined)));
}
} else {
this.reverse();
body.push(new ParserNode(ParserNodeType.IF, expr, undefined, new If(code_block, undefined)));
}
} else {
throw new Error("Expected expression");
}
body = [...body, ...this.parse_if()];
}
break;
case "while":
Expand Down
15 changes: 15 additions & 0 deletions tests/if.fl
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,20 @@ function spark(int argc, str[] argv) -> int {
prints("yess");
}

int idx = 0;
while idx < 4 {
if idx == 0 {
prints("ZERO");
} else if idx == 1 {
prints("ONE");
} else if idx == 2 {
prints("TWO");
} else {
prints("OTHER");
}

idx++;
}

return 0;
}
2 changes: 1 addition & 1 deletion tests/if.fl.expect
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"arguments": [],
"output": ["yes", "yess"],
"output": ["yes", "yess", "ZERO", "ONE", "TWO", "OTHER"],
"should_fail": false
}

0 comments on commit 7f27f1c

Please sign in to comment.