Skip to content

Commit

Permalink
Parse other statements to blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
BalaM314 authored Nov 17, 2023
1 parent 3b52c11 commit d8a9cdf
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function parse(tokens:Token[]):ProgramAST {
case "assignment": case "declaration": case "output": case "input":
getActiveBuffer().push(statement);
break;
case "if":
case "if": case "for": case "while": case "dowhile": case "function": case "procedure":
const node:ProgramASTTreeNode = {
startStatement: statement,
endStatement: null!, //null! goes brr
Expand All @@ -64,7 +64,7 @@ export function parse(tokens:Token[]):ProgramAST {
getActiveBuffer().push(node);
blockStack.push(node);
break;
case "if.end":
case "if.end": case "for.end": case "while.end": case "dowhile.end": case "function.end": case "procedure.end":
const lastNode = blockStack.at(-1);
if(!lastNode) throw new Error(`Invalid statement ${stringifyStatement(statement)}: no open blocks`);
else if(lastNode.startStatement.type == statement.type.split(".")[0]){ //probably bad code
Expand All @@ -89,6 +89,7 @@ export function parseStatement(tokens:Token[]):Statement {
case "keyword.declare": return { type: "declaration", tokens };
case "keyword.output": return { type: "output", tokens };
case "keyword.input": return { type: "input", tokens };
//TODO other block statements
case "keyword.if":
if(tokens.length >= 3 && tokens.at(-1)!.type == "keyword.then")
return { type: "if", tokens };
Expand Down

0 comments on commit d8a9cdf

Please sign in to comment.