fix errors and formatting

Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
This commit is contained in:
Ashwin Naren
2025-04-18 22:41:48 -07:00
parent 12ceb9695c
commit fc331a154f
2 changed files with 25 additions and 24 deletions

View File

@@ -212,15 +212,15 @@ macro_rules! emit {
};
}
fn print_location(zelf: &mut Compiler) {
fn print_location(zelf: &Compiler<'_>) {
let start = zelf.source_code.source_location(zelf.current_source_range.start());
let end = zelf.source_code.source_location(zelf.current_source_range.end());
eprintln!("LOCATION: {} from {}:{} to {}:{}", zelf.source_code.path.to_owned(), start.row, start.column, end.row, end.column);
}
/// Better traceback for internal error
fn unwrap_internal<T>(zelf: &mut Compiler, r: InternalResult<T>) -> T {
if let Err(r_err) = r {
fn unwrap_internal<T>(zelf: &Compiler<'_>, r: InternalResult<T>) -> T {
if let Err(ref r_err) = r {
eprintln!("=== CODEGEN PANIC INFO ===");
eprintln!("This IS an internal error: {}", r_err);
print_location(zelf);
@@ -229,7 +229,7 @@ fn unwrap_internal<T>(zelf: &mut Compiler, r: InternalResult<T>) -> T {
r.unwrap()
}
fn compiler_unwrap_option<T>(zelf: &mut Compiler, o: Option<T>) -> T {
fn compiler_unwrap_option<T>(zelf: &Compiler<'_>, o: Option<T>) -> T {
if let None = o {
eprintln!("=== CODEGEN PANIC INFO ===");
eprintln!("This IS an internal error, an option was unwrapped during codegen");
@@ -239,15 +239,15 @@ fn compiler_unwrap_option<T>(zelf: &mut Compiler, o: Option<T>) -> T {
o.unwrap()
}
fn compiler_result_unwrap<T, E>(zelf: &mut Compiler, result: Result<T, E>) -> T {
if let Err(r_err) = r {
eprintln!("=== CODEGEN PANIC INFO ===");
eprintln!("This IS an internal error, an result was unwrapped during codegen");
print_location(zelf);
eprintln!("=== END PANIC INFO ===");
}
r.unwrap()
}
// fn compiler_result_unwrap<T, E: std::fmt::Debug>(zelf: &Compiler<'_>, result: Result<T, E>) -> T {
// if result.is_err() {
// eprintln!("=== CODEGEN PANIC INFO ===");
// eprintln!("This IS an internal error, an result was unwrapped during codegen");
// print_location(zelf);
// eprintln!("=== END PANIC INFO ===");
// }
// result.unwrap()
// }
/// The pattern context holds information about captured names and jump targets.
#[derive(Clone)]
@@ -411,9 +411,9 @@ impl Compiler<'_> {
fn pop_code_object(&mut self) -> CodeObject {
let table = self.pop_symbol_table();
assert!(table.sub_tables.is_empty());
unwrap_internal(self, self.code_stack
.pop())
.finalize_code(self.opts.optimize)
let pop = self.code_stack.pop();
let stack_top = compiler_unwrap_option(self, pop);
unwrap_internal(self, stack_top.finalize_code(self.opts.optimize))
}
// could take impl Into<Cow<str>>, but everything is borrowed from ast structs; we never
@@ -520,7 +520,8 @@ impl Compiler<'_> {
self.current_block().instructions.pop(); // pop Instruction::Pop
}
Stmt::FunctionDef(_) | Stmt::ClassDef(_) => {
let store_inst = compiler_unwrap_option(zelf, self.current_block().instructions.pop()); // pop Instruction::Store
let pop_instructions = self.current_block().instructions.pop();
let store_inst = compiler_unwrap_option(self, pop_instructions); // pop Instruction::Store
emit!(self, Instruction::Duplicate);
self.current_block().instructions.push(store_inst);
}
@@ -578,8 +579,8 @@ impl Compiler<'_> {
self.check_forbidden_name(&name, usage)?;
let symbol_table = self.symbol_table_stack.last().unwrap();
let symbol = unwrap_internal(symbol_table.lookup(name.as_ref()).ok_or_else(||
InternalError::MissingSymbol(name.to_owned())
let symbol = unwrap_internal(self, symbol_table.lookup(name.as_ref()).ok_or_else(||
InternalError::MissingSymbol(name.to_string())
));
let info = self.code_stack.last_mut().unwrap();
let mut cache = &mut info.name_cache;
@@ -1514,8 +1515,8 @@ impl Compiler<'_> {
}
for var in &*code.freevars {
let table = self.symbol_table_stack.last().unwrap();
let symbol = unwrap_internal(table.lookup(var).ok_or_else(|| {
InternalError::MissingSymbol(var)
let symbol = unwrap_internal(self, table.lookup(var).ok_or_else(|| {
InternalError::MissingSymbol(var.to_owned())
}));
let parent_code = self.code_stack.last().unwrap();
let vars = match symbol.scope {
@@ -1599,7 +1600,7 @@ impl Compiler<'_> {
// Check if the class is declared global
let symbol_table = self.symbol_table_stack.last().unwrap();
let symbol = unwrap_internal(symbol_table.lookup(name.as_ref()).ok_or_else(|| Err(InternalError::MissingSymbol(name.to_owned()))));
let symbol = unwrap_internal(self, symbol_table.lookup(name.as_ref()).ok_or_else(|| InternalError::MissingSymbol(name.to_owned())));
let mut global_path_prefix = Vec::new();
if symbol.scope == SymbolScope::GlobalExplicit {
global_path_prefix.append(&mut self.qualified_path);

View File

@@ -155,7 +155,7 @@ impl CodeInfo {
locations.clear()
}
CodeObject {
Ok(CodeObject {
flags,
posonlyarg_count,
arg_count,
@@ -173,7 +173,7 @@ impl CodeInfo {
cellvars: cellvar_cache.into_iter().collect(),
freevars: freevar_cache.into_iter().collect(),
cell2arg,
}
})
}
fn cell2arg(&self) -> Option<Box<[i32]>> {