-
Notifications
You must be signed in to change notification settings - Fork 1
intermediate representation #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
51aa8f7
initial custom ir creation
jarrodconnolly 0de8996
translator for custom ast to babel ast
jarrodconnolly 12e7131
add new tests
jarrodconnolly dc831e7
llvm translator from our ast into llvm ir
jarrodconnolly f5be4ea
round out cli commands
jarrodconnolly 1303e8b
lint errors
jarrodconnolly 8751d28
add simple arena memory manager, no free for now
jarrodconnolly 4542090
add explicit free command
jarrodconnolly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| node_modules | ||
| .vscode | ||
| *.ll | ||
| *.s | ||
| /output |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| #! /bin/bash | ||
| if [ -z "$1" ]; then | ||
| echo "Usage: $0 <test-name>" | ||
| exit 1 | ||
| fi | ||
| complect -b llvm -f fixtures/${1} -o output/${1}.ll | ||
| llc output/${1}.ll -o output/${1}.s | ||
| clang output/${1}.ll -s -o output/${1} | ||
| ./output/${1} | ||
| valgrind --tool=memcheck --leak-check=full ./output/${1} 2>&1 | grep -E "(allocs| frees| allocated| lost)" | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| make a 0 | ||
| make b 1 | ||
| make t 0 | ||
| make n 12 | ||
| make n 32 | ||
| as n > 0 | ||
| n = n - 1 | ||
| assign t a | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /* Complect - Compiler for the Complect programming language | ||
| * | ||
| * Copyright © 2024 Jarrod Connolly | ||
| * MIT License | ||
| */ | ||
|
|
||
| import { TokenType } from '../tokenizer/token-type.js'; | ||
| import { Program, VariableDeclaration, AssignmentExpression, BinaryExpression, IfStatement, WhileStatement, PrintStatement, FreeStatement, Identifier, NumericLiteral, StringLiteral } from './ir-nodes.js'; | ||
|
|
||
| // ASTBuilder builds an IR AST from tokens using a state machine | ||
| export class ASTBuilder { | ||
| constructor() { | ||
| this.tokens = null; | ||
| this.index = 0; | ||
| } | ||
|
|
||
| // Main entry point: build AST from async generator of tokens | ||
| async build(tokens) { | ||
| const allTokens = []; | ||
| for await (const token of tokens) { | ||
| allTokens.push(token); | ||
| } | ||
| this.tokens = allTokens; | ||
| this.index = 0; | ||
| this.parse(); | ||
| return this.program; | ||
| } | ||
|
|
||
| parse() { | ||
| this.program = new Program(this.parseBlock(null), null); | ||
| } | ||
|
|
||
| parseBlock(endKeyword) { | ||
| const statements = []; | ||
| while (this.index < this.tokens.length) { | ||
| if (endKeyword && this.tokens[this.index].type === TokenType.keyword && this.tokens[this.index].value === endKeyword) { | ||
| this.index++; | ||
| return statements; | ||
| } | ||
| statements.push(this.parseStatement()); | ||
| } | ||
| if (endKeyword) throw new Error(`Expected '${endKeyword}'`); | ||
|
jarrodconnolly marked this conversation as resolved.
|
||
| return statements; | ||
| } | ||
|
|
||
| parseStatement() { | ||
| const token = this.tokens[this.index++]; | ||
| if (token.type === TokenType.keyword) { | ||
| if (token.value === 'make') { | ||
| const idToken = this.tokens[this.index++]; | ||
| if (idToken.type !== TokenType.identifier) throw new Error(`Expected identifier after 'make'`); | ||
| const expr = this.parseFullExpression(); | ||
| return new VariableDeclaration(idToken.value, expr, { start: { line: token.line, column: token.column }, end: { line: token.line, column: token.column } }); | ||
| } else if (token.value === 'assign') { | ||
| const idToken = this.tokens[this.index++]; | ||
| if (idToken.type !== TokenType.identifier) throw new Error(`Expected identifier after 'assign'`); | ||
| const expr = this.parseFullExpression(); | ||
| return new AssignmentExpression(idToken.value, expr, { start: { line: token.line, column: token.column }, end: { line: token.line, column: token.column } }); | ||
| } else if (token.value === 'print') { | ||
| const expr = this.parseFullExpression(); | ||
| return new PrintStatement(expr, { start: { line: token.line, column: token.column }, end: { line: token.line, column: token.column } }); | ||
| } else if (token.value === 'free') { | ||
| const idToken = this.tokens[this.index++]; | ||
| if (idToken.type !== TokenType.identifier) throw new Error(`Expected identifier after 'free'`); | ||
| return new FreeStatement(idToken.value, { start: { line: token.line, column: token.column }, end: { line: token.line, column: token.column } }); | ||
| } else if (token.value === 'if') { | ||
| const test = this.parseFullExpression(); | ||
| const consequent = this.parseBlock('endif'); | ||
| return new IfStatement(test, consequent, { start: { line: token.line, column: token.column }, end: { line: token.line, column: token.column } }); | ||
| } else if (token.value === 'as') { | ||
| const test = this.parseFullExpression(); | ||
| const body = this.parseBlock('repeat'); | ||
| return new WhileStatement(test, body, { start: { line: token.line, column: token.column }, end: { line: token.line, column: token.column } }); | ||
| } else { | ||
| throw new Error(`Unexpected keyword: ${token.value}`); | ||
| } | ||
| } else if (token.type === TokenType.identifier) { | ||
| // assignment | ||
| const opToken = this.tokens[this.index++]; | ||
| if (opToken.type !== TokenType.operator || opToken.value !== '=') throw new Error(`Expected '=' after identifier`); | ||
| const expr = this.parseFullExpression(); | ||
| return new AssignmentExpression(token.value, expr, { start: { line: token.line, column: token.column }, end: { line: token.line, column: token.column } }); | ||
| } else { | ||
| throw new Error(`Unexpected token: ${token.type} ${token.value}`); | ||
| } | ||
| } | ||
|
|
||
| // Parse a full expression, including binary operations | ||
| parseFullExpression() { | ||
| const token = this.tokens[this.index++]; | ||
| let expr = this.parseExpression(token); | ||
| while (this.index < this.tokens.length && this.tokens[this.index].type === TokenType.operator) { | ||
| const opToken = this.tokens[this.index++]; | ||
| const rightToken = this.tokens[this.index++]; | ||
| const right = this.parseExpression(rightToken); | ||
| expr = new BinaryExpression(expr, opToken.value, right, { start: expr.loc.start, end: right.loc.end }); | ||
| } | ||
| return expr; | ||
| } | ||
|
|
||
|
jarrodconnolly marked this conversation as resolved.
|
||
| // Parse a single expression from a token | ||
| parseExpression(token) { | ||
| if (token.type === TokenType.identifier) { | ||
| return new Identifier(token.value, { start: { line: token.line, column: token.column }, end: { line: token.line, column: token.column } }); | ||
| } else if (token.type === TokenType.number) { | ||
| return new NumericLiteral(Number(token.value), { start: { line: token.line, column: token.column }, end: { line: token.line, column: token.column } }); | ||
| } else if (token.type === TokenType.string) { | ||
| return new StringLiteral(token.value, { start: { line: token.line, column: token.column }, end: { line: token.line, column: token.column } }); // Already without quotes from preprocessor | ||
| } else { | ||
| throw new Error(`Unexpected expression token: ${token.type} ${token.value}`); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.