pub struct CodeGenerator {
pub chunk: Chunk,
line: usize,
variables: Vec<String>,
functions: Vec<String>,
local_scopes: Vec<Vec<String>>,
errors: Vec<CompilerError>,
}
Expand description
Compiles AST nodes into bytecode instructions
Fields§
§chunk: Chunk
The bytecode chunk being constructed
line: usize
Current line number for debugging information
variables: Vec<String>
Global variable names
functions: Vec<String>
Function names for tracking declarations
local_scopes: Vec<Vec<String>>
Stack of scopes for tracking local variables
errors: Vec<CompilerError>
Accumulated errors during compilation
Implementations§
Source§impl CodeGenerator
impl CodeGenerator
Sourcefn set_current_location(&mut self, location: &Location)
fn set_current_location(&mut self, location: &Location)
Updates the current line from a source location
Sourcefn add_error(&mut self, message: String)
fn add_error(&mut self, message: String)
Creates a CompilerError with the current location and adds it to the error list
Sourcepub fn compile_statements(
&mut self,
statements: &[Statement],
) -> CompileResult<()>
pub fn compile_statements( &mut self, statements: &[Statement], ) -> CompileResult<()>
Sourcefn emit_constant(&mut self, value: Value) -> Result<(), ()>
fn emit_constant(&mut self, value: Value) -> Result<(), ()>
Adds a constant value to the chunk and emits code to load it
§Arguments
value
- The constant value to add
Sourcefn patch_jump(&mut self, offset: usize)
fn patch_jump(&mut self, offset: usize)
Patches a previously emitted jump instruction with the actual offset
§Arguments
offset
- The position of the jump offset to patch
fn begin_scope(&mut self)
fn end_scope(&mut self)
Trait Implementations§
Source§impl Visitor<Result<(), ()>> for CodeGenerator
impl Visitor<Result<(), ()>> for CodeGenerator
Source§fn visit_expression(&mut self, expr: &Expression) -> Result<(), ()>
fn visit_expression(&mut self, expr: &Expression) -> Result<(), ()>
Visit a general expression
Source§fn visit_function_declaration_statement(
&mut self,
fn_decl: &FunctionDeclarationStmt,
) -> Result<(), ()>
fn visit_function_declaration_statement( &mut self, fn_decl: &FunctionDeclarationStmt, ) -> Result<(), ()>
Visit a function declaration statement
Source§fn visit_return_statement(
&mut self,
return_stmt: &ReturnStatement,
) -> Result<(), ()>
fn visit_return_statement( &mut self, return_stmt: &ReturnStatement, ) -> Result<(), ()>
Visit a return statement
Source§fn visit_expression_statement(&mut self, expr: &Expression) -> Result<(), ()>
fn visit_expression_statement(&mut self, expr: &Expression) -> Result<(), ()>
Visit an expression statement
Source§fn visit_let_statement(&mut self, let_stmt: &LetStatement) -> Result<(), ()>
fn visit_let_statement(&mut self, let_stmt: &LetStatement) -> Result<(), ()>
Visit a variable declaration statement
Source§fn visit_assignment_statement(
&mut self,
assign_stmt: &AssignmentStatement,
) -> Result<(), ()>
fn visit_assignment_statement( &mut self, assign_stmt: &AssignmentStatement, ) -> Result<(), ()>
Visit a variable assignment statement
Source§fn visit_call_expression(
&mut self,
call_expr: &FunctionCallExpr,
) -> Result<(), ()>
fn visit_call_expression( &mut self, call_expr: &FunctionCallExpr, ) -> Result<(), ()>
Visit a function call expression
Source§fn visit_literal_expression(&mut self, lit_expr: &LiteralExpr) -> Result<(), ()>
fn visit_literal_expression(&mut self, lit_expr: &LiteralExpr) -> Result<(), ()>
Visit a literal expression (e.g., 42, “hello”)
Source§fn visit_binary_expression(&mut self, bin_expr: &BinaryExpr) -> Result<(), ()>
fn visit_binary_expression(&mut self, bin_expr: &BinaryExpr) -> Result<(), ()>
Visit a binary expression (e.g., a + b)
Source§fn visit_unary_expression(&mut self, unary_expr: &UnaryExpr) -> Result<(), ()>
fn visit_unary_expression(&mut self, unary_expr: &UnaryExpr) -> Result<(), ()>
Visit a unary expression (e.g., -x)
Source§fn visit_variable_expression(
&mut self,
var_expr: &VariableExpr,
) -> Result<(), ()>
fn visit_variable_expression( &mut self, var_expr: &VariableExpr, ) -> Result<(), ()>
Visit a variable reference expression
Source§fn visit_type_definition_statement(
&mut self,
_stmt: &TypeDefinitionStmt,
) -> Result<(), ()>
fn visit_type_definition_statement( &mut self, _stmt: &TypeDefinitionStmt, ) -> Result<(), ()>
Visit a type definition statement
Source§fn visit_conditional_expression(
&mut self,
cond_expr: &ConditionalExpr,
) -> Result<(), ()>
fn visit_conditional_expression( &mut self, cond_expr: &ConditionalExpr, ) -> Result<(), ()>
Visit a conditional expression (if/else)
Source§fn visit_if_statement(&mut self, if_stmt: &IfStatement) -> Result<(), ()>
fn visit_if_statement(&mut self, if_stmt: &IfStatement) -> Result<(), ()>
Visit a conditional statement (if/else)
Source§fn visit_block_expression(&mut self, block_expr: &BlockExpr) -> Result<(), ()>
fn visit_block_expression(&mut self, block_expr: &BlockExpr) -> Result<(), ()>
Visit a block expression
Source§fn visit_function_type_expression(
&mut self,
_func_type_expr: &FunctionTypeExpr,
) -> Result<(), ()>
fn visit_function_type_expression( &mut self, _func_type_expr: &FunctionTypeExpr, ) -> Result<(), ()>
Visit a function type expression (e.g., fn(i32, string) -> string)
Auto Trait Implementations§
impl Freeze for CodeGenerator
impl RefUnwindSafe for CodeGenerator
impl Send for CodeGenerator
impl Sync for CodeGenerator
impl Unpin for CodeGenerator
impl UnwindSafe for CodeGenerator
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more