Struct CodeGenerator

Source
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

Source

pub fn new() -> Self

Creates a new compiler with an empty chunk

Source

fn set_current_location(&mut self, location: &Location)

Updates the current line from a source location

Source

fn add_error(&mut self, message: String)

Creates a CompilerError with the current location and adds it to the error list

Source

fn compile(self, statements: &[Statement]) -> CompileResult<Chunk>

Compiles a list of statements into bytecode

§Arguments
  • statements - The statements to compile
§Returns

A CompileResult containing the compiled bytecode chunk or errors

Source

pub fn compile_statements( &mut self, statements: &[Statement], ) -> CompileResult<()>

Compiles statements and appends them to the existing chunk

§Arguments
  • statements - The statements to compile
§Returns

CompileResult indicating success or containing errors

Source

pub fn get_chunk(&self) -> &Chunk

Gets a reference to the current chunk

Source

fn emit_byte(&mut self, byte: u8)

Emits a single byte to the bytecode chunk

§Arguments
  • byte - The byte to emit
Source

fn emit_op(&mut self, op: OpCode)

Emits an opcode to the bytecode chunk

§Arguments
  • op - The opcode to emit
Source

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
Source

fn emit_jump(&mut self, op: OpCode) -> usize

Emits a jump instruction with placeholder offset

§Arguments
  • op - The jump opcode (Jump or JumpIfFalse)
§Returns

The position where the jump offset needs to be patched later

Source

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
Source

fn begin_scope(&mut self)

Source

fn end_scope(&mut self)

Trait Implementations§

Source§

impl Visitor<Result<(), ()>> for CodeGenerator

Source§

fn visit_statement(&mut self, stmt: &Statement) -> Result<(), ()>

Visit a general statement
Source§

fn visit_expression(&mut self, expr: &Expression) -> Result<(), ()>

Visit a general expression
Source§

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<(), ()>

Visit a return statement
Source§

fn visit_expression_statement(&mut self, expr: &Expression) -> Result<(), ()>

Visit an expression statement
Source§

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<(), ()>

Visit a variable assignment statement
Source§

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<(), ()>

Visit a literal expression (e.g., 42, “hello”)
Source§

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<(), ()>

Visit a unary expression (e.g., -x)
Source§

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<(), ()>

Visit a type definition statement
Source§

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<(), ()>

Visit a conditional statement (if/else)
Source§

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<(), ()>

Visit a function type expression (e.g., fn(i32, string) -> string)

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.