Struct CompilationContext

Source
pub struct CompilationContext {
    type_registry: TypeRegistry,
    symbol_table: SymbolTable,
}
Expand description

Compilation context that owns the type registry and symbol table

Fields§

§type_registry: TypeRegistry

The type registry that stores all types

§symbol_table: SymbolTable

The symbol table that stores all symbols (variables, types, functions)

Implementations§

Source§

impl CompilationContext

Source

pub fn new() -> Self

Creates a new compilation context with a type registry and symbol table

Initializes the context with all primitive types registered in both the type registry and symbol table. This includes boolean, integer, float, string, and unspecified types.

§Returns

A new CompilationContext instance ready for compilation

Source

pub fn get_type_info(&self, id: &TypeId) -> Option<&TypeInfo>

Gets type information for a given type ID

§Arguments
  • id - The type ID to look up
§Returns

An optional reference to the TypeInfo if the type exists

Source

pub fn get_type_name(&self, type_id: &TypeId) -> String

Gets the name of a type from its TypeId

§Arguments
  • type_id - The type ID to get the name for
§Returns

The name of the type as a String, or a debug representation if the type is unknown

Source

pub fn get_primitive_type_from_id(&self, id: &TypeId) -> Option<PrimitiveType>

Gets the primitive type corresponding to a given type ID

§Arguments
  • id - The type ID to look up
§Returns

An optional PrimitiveType if the type ID corresponds to a primitive type

Source

pub fn is_primitive_type(&self, id: &TypeId) -> bool

Checks if a type ID corresponds to a primitive type

§Arguments
  • id - The type ID to check
§Returns

True if the type is a primitive type, false otherwise

Source

pub fn type_fulfills<F>(&self, type_id: &TypeId, predicate: F) -> bool
where F: Fn(&TypeInfo) -> bool,

Checks if a type fulfills a given predicate function

§Arguments
  • type_id - The type ID to check
  • predicate - A function that takes TypeInfo and returns a boolean
§Returns

True if the type exists and satisfies the predicate, false otherwise

Source

pub fn is_numeric_type(&self, type_id: &TypeId) -> bool

Checks if a type ID corresponds to a numeric type (integer or float)

§Arguments
  • type_id - The type ID to check
§Returns

True if the type is numeric (integer or float), false otherwise

Source

pub fn is_integer_type(&self, type_id: &TypeId) -> bool

Checks if a type ID corresponds to an integer type

§Arguments
  • type_id - The type ID to check
§Returns

True if the type is an integer type (signed or unsigned), false otherwise

Source

pub fn is_float_type(&self, type_id: &TypeId) -> bool

Checks if a type ID corresponds to a floating-point type

§Arguments
  • type_id - The type ID to check
§Returns

True if the type is a floating-point type (f32 or f64), false otherwise

Source

pub fn is_signed_integer_type(&self, type_id: &TypeId) -> bool

Checks if a type ID corresponds to a signed integer type

§Arguments
  • type_id - The type ID to check
§Returns

True if the type is a signed integer type (i32 or i64), false otherwise

Source

pub fn is_unsigned_integer_type(&self, type_id: &TypeId) -> bool

Checks if a type ID corresponds to an unsigned integer type

§Arguments
  • type_id - The type ID to check
§Returns

True if the type is an unsigned integer type (u32 or u64), false otherwise

Source

pub fn get_bit_width(&self, type_id: &TypeId) -> u8

Gets the bit width of a type

§Arguments
  • type_id - The type ID to get the bit width for
§Returns

The bit width of the type, or 0 if the type is not a primitive type

Source

pub fn check_value_in_range(&self, value: &i64, type_id: &TypeId) -> bool

Checks if an integer value is within the valid range for a given type

§Arguments
  • value - The integer value to check
  • type_id - The type ID to check the value against
§Returns

True if the value is within the valid range for the type, false otherwise

Source

pub fn check_float_value_in_range(&self, value: &f64, type_id: &TypeId) -> bool

Checks if a floating-point value is within the valid range for a given type

§Arguments
  • value - The floating-point value to check
  • type_id - The type ID to check the value against
§Returns

True if the value is within the valid range for the type, false otherwise

Source

pub fn define_symbol( &mut self, name: String, kind: SymbolKind, type_id: TypeId, is_mutable: bool, ) -> Result<(), String>

Defines a symbol in the symbol table

§Arguments
  • name - The name of the symbol
  • kind - The kind of symbol (variable, type, function)
  • type_id - The type ID associated with the symbol
  • is_mutable - Whether the symbol is mutable (only relevant for variables)
§Returns

A Result indicating success or an error message if the symbol cannot be defined

Source

pub fn lookup_symbol(&self, name: &str) -> Option<&Symbol>

Looks up a symbol in the symbol table by name

§Arguments
  • name - The name of the symbol to look up
§Returns

An optional reference to the Symbol if found, None otherwise

Source

pub fn register_custom_type( &mut self, name: &str, type_kind: TypeKind, ) -> Result<TypeId, String>

Registers a custom type with the given name and type kind

§Arguments
  • name - The name of the custom type
  • type_kind - The kind of type to register (struct, enum, etc.)
§Returns

A Result containing the TypeId of the registered type or an error message if the name is already defined

Source

pub fn register_struct_type( &mut self, name: String, fields: Vec<(String, TypeId)>, ) -> Result<TypeId, String>

Registers a new struct type with the given name and fields

§Arguments
  • name - The name of the struct type
  • fields - A vector of tuples containing field names and their type IDs
§Returns

A Result containing the TypeId of the registered struct type or an error message

Source

pub fn register_function_type( &mut self, param_types: Vec<TypeId>, return_type: TypeId, ) -> TypeId

Registers a function type and returns its TypeId

Source

pub fn is_function_type(&self, type_id: &TypeId) -> bool

Checks if a type is a function type

Source

pub fn get_function_type(&self, type_id: &TypeId) -> Option<&FunctionType>

Gets function type information

Source

pub fn begin_scope(&mut self)

Begins a new scope by calling the symbol table Used when entering a block, function, or other lexical scope.

Source

pub fn end_scope(&mut self)

Ends the current scope by calling the symbol table Used when exiting a block, function, or other lexical scope.

Trait Implementations§

Source§

impl Default for CompilationContext

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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.