-
Notifications
You must be signed in to change notification settings - Fork 0
FEAT: USE ANTLR TO COMPILE THE PSEUDOCODE #37
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
Open
Sherlemious
wants to merge
6
commits into
main
Choose a base branch
from
claude/improve-backend-compiler-011CUJz44V6WbMv8RWWdrJJp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
744301b
Feat: Build proper compiler with Lark parser and AST
claude 024a437
Test: Add compiler test suite and validation scripts
claude 0654208
Refactor: Clean up redundant parser and grammar files
claude 017ae06
Fix: Critical compiler improvements - type hints, error reporting, BY…
claude eaba0b7
Fix: Complete position extraction for all AST transformer methods
claude beffeb2
Cleanup: Remove unused code and add comprehensive API testing
claude 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
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 |
|---|---|---|
| @@ -0,0 +1,324 @@ | ||
| """ | ||
| Abstract Syntax Tree (AST) Node Definitions for IGCSE Pseudocode | ||
|
|
||
| This module defines all AST node classes that represent the structure | ||
| of parsed pseudocode. Each node type corresponds to a language construct. | ||
| """ | ||
|
|
||
| from typing import List, Optional, Any | ||
| from dataclasses import dataclass | ||
|
|
||
|
|
||
| @dataclass | ||
| class ASTNode: | ||
| """Base class for all AST nodes""" | ||
| line: int | ||
| column: int | ||
|
|
||
|
|
||
| # ============================================================================ | ||
| # Program Structure | ||
| # ============================================================================ | ||
|
|
||
| @dataclass | ||
| class Program(ASTNode): | ||
| """Root node representing the entire program""" | ||
| statements: List[ASTNode] | ||
|
|
||
|
|
||
| # ============================================================================ | ||
| # Declarations | ||
| # ============================================================================ | ||
|
|
||
| @dataclass | ||
| class Declaration(ASTNode): | ||
| """Variable declaration: DECLARE x : INTEGER""" | ||
| name: str | ||
| type_: str | ||
| is_array: bool = False | ||
| dimensions: Optional[List[Any]] = None # For arrays: [1:10] or [1:10, 1:5] | ||
|
|
||
|
|
||
| @dataclass | ||
| class ConstantDeclaration(ASTNode): | ||
| """Constant declaration: CONSTANT PI = 3.14""" | ||
| name: str | ||
| value: Any | ||
|
|
||
|
|
||
| # ============================================================================ | ||
| # Expressions | ||
| # ============================================================================ | ||
|
|
||
| @dataclass | ||
| class NumberLiteral(ASTNode): | ||
| """Numeric literal: 42, 3.14""" | ||
| value: float | ||
|
|
||
|
|
||
| @dataclass | ||
| class StringLiteral(ASTNode): | ||
| """String literal: "Hello World" """ | ||
| value: str | ||
|
|
||
|
|
||
| @dataclass | ||
| class BooleanLiteral(ASTNode): | ||
| """Boolean literal: TRUE, FALSE""" | ||
| value: bool | ||
|
|
||
|
|
||
| @dataclass | ||
| class Identifier(ASTNode): | ||
| """Variable or function name""" | ||
| name: str | ||
|
|
||
|
|
||
| @dataclass | ||
| class BinaryOp(ASTNode): | ||
| """Binary operation: a + b, x * y""" | ||
| operator: str # +, -, *, /, MOD, DIV, AND, OR, etc. | ||
| left: ASTNode | ||
| right: ASTNode | ||
|
|
||
|
|
||
| @dataclass | ||
| class UnaryOp(ASTNode): | ||
| """Unary operation: -x, NOT flag""" | ||
| operator: str # -, NOT | ||
| operand: ASTNode | ||
|
|
||
|
|
||
| @dataclass | ||
| class Comparison(ASTNode): | ||
| """Comparison: a = b, x < y""" | ||
| operator: str # =, <>, <, >, <=, >= | ||
| left: ASTNode | ||
| right: ASTNode | ||
|
|
||
|
|
||
| @dataclass | ||
| class ArrayAccess(ASTNode): | ||
| """Array access: arr[i] or arr[i, j]""" | ||
| name: str | ||
| indices: List[ASTNode] | ||
|
|
||
|
|
||
| @dataclass | ||
| class FunctionCall(ASTNode): | ||
| """Function call: LENGTH(str), ROUND(x, 2)""" | ||
| name: str | ||
| arguments: List[ASTNode] | ||
|
|
||
|
|
||
| # ============================================================================ | ||
| # Statements | ||
| # ============================================================================ | ||
|
|
||
| @dataclass | ||
| class Assignment(ASTNode): | ||
| """Assignment: x = 5 or x <- 5""" | ||
| target: ASTNode # Can be Identifier or ArrayAccess | ||
| value: ASTNode | ||
|
|
||
|
|
||
| @dataclass | ||
| class Input(ASTNode): | ||
| """Input statement: INPUT x""" | ||
| variable: ASTNode # Can be Identifier or ArrayAccess | ||
|
|
||
|
|
||
| @dataclass | ||
| class Output(ASTNode): | ||
| """Output statement: OUTPUT "Result:", x""" | ||
| expressions: List[ASTNode] | ||
|
|
||
|
|
||
| # ============================================================================ | ||
| # Control Flow - Conditionals | ||
| # ============================================================================ | ||
|
|
||
| @dataclass | ||
| class IfStatement(ASTNode): | ||
| """ | ||
| If statement: | ||
| IF condition THEN | ||
| statements | ||
| ENDIF | ||
| """ | ||
| condition: ASTNode | ||
| then_body: List[ASTNode] | ||
| elif_parts: Optional[List['ElifPart']] = None | ||
| else_body: Optional[List[ASTNode]] = None | ||
|
|
||
|
|
||
| @dataclass | ||
| class ElifPart(ASTNode): | ||
| """ELSEIF part of an if statement""" | ||
| condition: ASTNode | ||
| body: List[ASTNode] | ||
|
|
||
|
|
||
| @dataclass | ||
| class CaseStatement(ASTNode): | ||
| """ | ||
| Case statement: | ||
| CASE OF variable | ||
| value1: statements | ||
| value2: statements | ||
| OTHERWISE: statements | ||
| ENDCASE | ||
| """ | ||
| expression: ASTNode | ||
| cases: List['CaseBranch'] | ||
| otherwise: Optional[List[ASTNode]] = None | ||
|
|
||
|
|
||
| @dataclass | ||
| class CaseBranch(ASTNode): | ||
| """Single branch in a case statement""" | ||
| value: ASTNode | ||
| body: List[ASTNode] | ||
|
|
||
|
|
||
| # ============================================================================ | ||
| # Control Flow - Loops | ||
| # ============================================================================ | ||
|
|
||
| @dataclass | ||
| class ForLoop(ASTNode): | ||
| """ | ||
| For loop: | ||
| FOR i = 1 TO 10 STEP 1 | ||
| statements | ||
| NEXT i | ||
| """ | ||
| variable: str | ||
| start: ASTNode | ||
| end: ASTNode | ||
| step: Optional[ASTNode] = None | ||
| body: List[ASTNode] = None | ||
|
|
||
|
|
||
| @dataclass | ||
| class WhileLoop(ASTNode): | ||
| """ | ||
| While loop: | ||
| WHILE condition DO | ||
| statements | ||
| ENDWHILE | ||
| """ | ||
| condition: ASTNode | ||
| body: List[ASTNode] | ||
|
|
||
|
|
||
| @dataclass | ||
| class RepeatUntilLoop(ASTNode): | ||
| """ | ||
| Repeat-until loop: | ||
| REPEAT | ||
| statements | ||
| UNTIL condition | ||
| """ | ||
| body: List[ASTNode] | ||
| condition: ASTNode | ||
|
|
||
|
|
||
| # ============================================================================ | ||
| # Functions and Procedures | ||
| # ============================================================================ | ||
|
|
||
| @dataclass | ||
| class Parameter: | ||
| """Function/procedure parameter""" | ||
| name: str | ||
| type_: str | ||
| by_ref: bool = False # BYREF vs BYVAL | ||
|
|
||
|
|
||
| @dataclass | ||
| class ProcedureDeclaration(ASTNode): | ||
| """ | ||
| Procedure declaration: | ||
| PROCEDURE MyProc(x : INTEGER, BYREF y : REAL) | ||
| statements | ||
| ENDPROCEDURE | ||
| """ | ||
| name: str | ||
| parameters: List[Parameter] | ||
| body: List[ASTNode] | ||
|
|
||
|
|
||
| @dataclass | ||
| class FunctionDeclaration(ASTNode): | ||
| """ | ||
| Function declaration: | ||
| FUNCTION Add(a : INTEGER, b : INTEGER) RETURNS INTEGER | ||
| statements | ||
| RETURN result | ||
| ENDFUNCTION | ||
| """ | ||
| name: str | ||
| parameters: List[Parameter] | ||
| return_type: str | ||
| body: List[ASTNode] | ||
|
|
||
|
|
||
| @dataclass | ||
| class ReturnStatement(ASTNode): | ||
| """Return statement: RETURN value""" | ||
| value: ASTNode | ||
|
|
||
|
|
||
| @dataclass | ||
| class CallStatement(ASTNode): | ||
| """Procedure call: CALL MyProc(x, y)""" | ||
| name: str | ||
| arguments: List[ASTNode] | ||
|
|
||
|
|
||
| # ============================================================================ | ||
| # File Operations | ||
| # ============================================================================ | ||
|
|
||
| @dataclass | ||
| class OpenFile(ASTNode): | ||
| """OPENFILE filename FOR mode""" | ||
| filename: ASTNode | ||
| mode: str # READ, WRITE, APPEND | ||
|
|
||
|
|
||
| @dataclass | ||
| class ReadFile(ASTNode): | ||
| """READFILE filename, variable""" | ||
| filename: ASTNode | ||
| variable: ASTNode | ||
|
|
||
|
|
||
| @dataclass | ||
| class WriteFile(ASTNode): | ||
| """WRITEFILE filename, data""" | ||
| filename: ASTNode | ||
| data: ASTNode | ||
|
|
||
|
|
||
| @dataclass | ||
| class CloseFile(ASTNode): | ||
| """CLOSEFILE filename""" | ||
| filename: ASTNode | ||
|
|
||
|
|
||
| # ============================================================================ | ||
| # Special Nodes | ||
| # ============================================================================ | ||
|
|
||
| @dataclass | ||
| class Comment(ASTNode): | ||
| """Comment node (usually filtered out)""" | ||
| text: str | ||
|
|
||
|
|
||
| @dataclass | ||
| class EmptyStatement(ASTNode): | ||
| """Empty statement (placeholder)""" | ||
| pass | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type hint for
bodyisList[ASTNode], but the default value isNone. This is a type hint violation. To correctly represent an optional list, you should useOptional[List[ASTNode]].