From 5cebedbb2f762630c8f6ad919e4a9263784a8591 Mon Sep 17 00:00:00 2001 From: Adam McDaniel Date: Thu, 12 Jun 2025 23:01:29 -0400 Subject: [PATCH 1/2] Added shift operator and external modules --- src/frontend/mod.rs | 4 +- src/frontend/parse.rs | 79 ++++++++++---- src/lir/compile.rs | 4 +- src/lir/env.rs | 16 +-- src/lir/expr/const_expr.rs | 6 +- src/lir/expr/declaration.rs | 122 ++++++++++++++++------ src/lir/expr/expression.rs | 8 ++ src/lir/expr/ops/bitwise/mod.rs | 2 + src/lir/expr/ops/bitwise/shift.rs | 167 ++++++++++++++++++++++++++++++ 9 files changed, 335 insertions(+), 73 deletions(-) create mode 100644 src/lir/expr/ops/bitwise/shift.rs diff --git a/src/frontend/mod.rs b/src/frontend/mod.rs index 52498890..82021f1c 100644 --- a/src/frontend/mod.rs +++ b/src/frontend/mod.rs @@ -2,7 +2,7 @@ mod parse; use crate::lir::Expr; pub use parse::{parse_module, parse_source, get_lisp_env}; -fn without_comments(code: impl ToString) -> String { +pub(super) fn without_comments(code: impl ToString) -> String { use no_comment::{languages, IntoWithoutComments}; code.to_string() .chars() @@ -185,7 +185,7 @@ pub fn parse( ret: crate::lir::Type::None, body: debug_body, }); - + expr = crate::lir::Expr::let_consts( vec![ ("free", free), diff --git a/src/frontend/parse.rs b/src/frontend/parse.rs index 0b800a36..de86d71c 100644 --- a/src/frontend/parse.rs +++ b/src/frontend/parse.rs @@ -13,7 +13,7 @@ use std::{ collections::BTreeMap, sync::{Arc, RwLock} }; -use crate::{lir::*, parse::SourceCodeLocation}; +use crate::{frontend::without_comments, lir::*, parse::SourceCodeLocation}; use nom::{ character::complete::{alpha1, alphanumeric1}, combinator::value, @@ -1282,6 +1282,8 @@ lazy_static! { result.insert("*".to_owned(), (20, |a, b| a.mul(b))); result.insert("/".to_owned(), (20, |a, b| a.div(b))); result.insert("%".to_owned(), (20, |a, b| a.rem(b))); + result.insert("<<".to_owned(), (8, |a, b| a.lshift(b))); + result.insert(">>".to_owned(), (8, |a, b| a.rshift(b))); result.insert("==".to_owned(), (5, |a, b| a.eq(b))); result.insert("!=".to_owned(), (5, |a, b| a.neq(b))); result.insert("<".to_owned(), (5, |a, b| a.lt(b))); @@ -2061,24 +2063,40 @@ fn parse_module_file_stmt<'a, E: ParseError<&'a str> + ContextError<&'a str>>( // Open the file if let Ok(contents) = std::fs::read_to_string(format!("{}.sg", name)) { save_source_code_setup(); + let contents = without_comments(contents); setup_source_code_locations(&contents.clone(), Some(name.to_string())); - if let Ok((new_input, module)) = - parse_module_contents::>(name, &contents, true) + + + match parse_module_contents::>(name, &contents, true) { - if !new_input.is_empty() { + Ok((new_input, module)) => { + if !new_input.is_empty() { + return Err(nom::Err::Error(E::from_error_kind( + input, + ErrorKind::Verify, + ))); + } + restore_source_code_setup(); + return Ok((input, Statement::Declaration(module, None))); + } + Err(e) => { + match e { + nom::Err::Error(e) => { + error!("{}", convert_error::<&str>(contents.as_str(), e)); + } + nom::Err::Failure(e) => { + error!("{}", convert_error::<&str>(contents.as_str(), e)); + } + nom::Err::Incomplete(needed) => { + error!("Incomplete: {needed:?}"); + } + } + restore_source_code_setup(); return Err(nom::Err::Error(E::from_error_kind( input, ErrorKind::Verify, ))); } - restore_source_code_setup(); - return Ok((input, Statement::Declaration(module, None))); - } else { - restore_source_code_setup(); - return Err(nom::Err::Error(E::from_error_kind( - input, - ErrorKind::Verify, - ))); } } @@ -2094,6 +2112,7 @@ fn parse_decl<'a, E: ParseError<&'a str> + ContextError<&'a str>>( ) -> IResult<&'a str, Declaration, E> { let (input, _) = whitespace(input)?; let (input, decl) = alt(( + context("function", parse_quick_fun_stmt), context("function", parse_fun_stmt), context("type", parse_type_stmt), context("enum", parse_enum_stmt), @@ -2104,6 +2123,17 @@ fn parse_decl<'a, E: ParseError<&'a str> + ContextError<&'a str>>( context("import", parse_import_stmt), context("module", parse_module_stmt), context("module", parse_module_file_stmt), + context("static", terminated(parse_static_var_stmt, tag(";"))), + + + // context("function", parse_quick_fun_stmt), + // context("function", parse_fun_stmt), + // context("impl", parse_impl_stmt), + // context("enum", parse_enum_stmt), + // context("struct", parse_struct_stmt), + // context("module", parse_module_stmt), + // context("module", parse_module_file_stmt), + // context("import", parse_import_stmt), ))(input)?; match decl { @@ -2630,10 +2660,9 @@ fn parse_var_stmt<'a, E: ParseError<&'a str> + ContextError<&'a str>>( ), )) } - -fn parse_static_var_stmt<'a, E: ParseError<&'a str> + ContextError<&'a str>>( +fn parse_static_var_decl<'a, E: ParseError<&'a str> + ContextError<&'a str>>( input: &'a str, -) -> IResult<&'a str, Statement, E> { +) -> IResult<&'a str, Declaration, E> { // "let" "static" ":" "=" => Statement::Declaration(Declaration::StaticVar(name, Mutability::Immutable, ty, value)), // "let" "static" "mut" ":" "=" => Statement::Declaration(Declaration::StaticVar(name, Mutability::Mutable, ty, value)), let (input, _) = tag("let")(input)?; @@ -2662,13 +2691,17 @@ fn parse_static_var_stmt<'a, E: ParseError<&'a str> + ContextError<&'a str>>( let ty = ty.unwrap_or(Type::None); Ok(( input, - Statement::Declaration( - Declaration::static_var(name.to_owned(), mutability, ty, value), - None, - ), + Declaration::static_var(name.to_owned(), mutability, ty, value), )) } +fn parse_static_var_stmt<'a, E: ParseError<&'a str> + ContextError<&'a str>>( + input: &'a str, +) -> IResult<&'a str, Statement, E> { + let (input, decl) = parse_static_var_decl(input)?; + Ok((input, Statement::Declaration(decl, None))) +} + fn parse_type_stmt<'a, E: ParseError<&'a str> + ContextError<&'a str>>( input: &'a str, ) -> IResult<&'a str, Statement, E> { @@ -2886,6 +2919,8 @@ fn parse_assign_stmt<'a, E: ParseError<&'a str> + ContextError<&'a str>>( Err(_) => { // If that fails, try the compound assignment let (input, op) = alt(( + value(Assign::new(LeftShift), tag("<<=")), + value(Assign::new(RightShift), tag(">>=")), value(Assign::new(Arithmetic::Add), tag("+=")), value(Assign::new(Arithmetic::Subtract), tag("-=")), value(Assign::new(Arithmetic::Multiply), tag("*=")), @@ -3780,15 +3815,15 @@ fn parse_int_literal<'a, E: ParseError<&'a str> + ContextError<&'a str>>( let (input, result) = alt(( map(preceded(tag("0x"), hex_digit1), |s: &str| { - i64::from_str_radix(s, 16).unwrap() + u64::from_str_radix(s, 16).unwrap() as i64 }), // Try octal map(preceded(tag("0o"), oct_digit1), |s: &str| { - i64::from_str_radix(s, 8).unwrap() + u64::from_str_radix(s, 8).unwrap() as i64 }), // Try binary map(preceded(tag("0b"), bin_digit1), |s: &str| { - i64::from_str_radix(s, 2).unwrap() + u64::from_str_radix(s, 2).unwrap() as i64 }), map(digit1, |s: &str| s.parse().unwrap()), ))(input)?; diff --git a/src/lir/compile.rs b/src/lir/compile.rs index 455225e5..1ccba24d 100644 --- a/src/lir/compile.rs +++ b/src/lir/compile.rs @@ -38,9 +38,9 @@ pub trait Compile: TypeCheck + std::fmt::Debug + std::fmt::Display { // First, type check the expression. self.type_check(&Env::default())?; // Then, attempt to compile the expression into a core assembly program. - let mut core_asm = CoreProgram::default(); - info!("Compiling..."); + + let mut core_asm = CoreProgram::default(); if core { // If the expression cannot be compiled into a core assembly program, // then compile it into a standard assembly program. diff --git a/src/lir/env.rs b/src/lir/env.rs index 30441876..c3a58e70 100644 --- a/src/lir/env.rs +++ b/src/lir/env.rs @@ -95,6 +95,8 @@ impl Default for Env { map.insert("==".to_owned(), Box::new(crate::lir::Comparison::Equal)); map.insert("!=".to_owned(), Box::new(crate::lir::Comparison::NotEqual)); map.insert("<".to_owned(), Box::new(crate::lir::Comparison::LessThan)); + map.insert("<<".to_owned(), Box::new(crate::lir::LeftShift)); + map.insert(">>".to_owned(), Box::new(crate::lir::RightShift)); map.insert( "<=".to_owned(), Box::new(crate::lir::Comparison::LessThanOrEqual), @@ -824,7 +826,7 @@ impl Env { Declaration::ExternProc(name, proc) => { self.define_ffi_proc(name, proc.clone()); } - Declaration::StaticVar(name, mutability, ty, _expr) => { + Declaration::StaticVar(name, mutability, ty, ..) => { self.define_static_var(name, *mutability, ty.clone())?; } Declaration::Impl(ty, impls) => { @@ -933,7 +935,7 @@ impl Env { Declaration::ExternProc(_, _) => { // FFI procedures are not defined at runtime. } - Declaration::StaticVar(name, mutability, ty, _expr) => { + Declaration::StaticVar(name, mutability, ty, ..) => { self.define_static_var(name, *mutability, ty.clone())?; } Declaration::Impl(_, _) => { @@ -1014,16 +1016,6 @@ impl Env { _ => { Arc::make_mut(&mut self.consts).insert(name.clone(), ConstExpr::Type(ty.clone())); Arc::make_mut(&mut self.types).insert(name.clone(), ty.clone()); - - if let Ok(simplified) = ty.simplify_until_concrete(self, false) { - if let Ok(size) = simplified.get_size(self) { - self.set_precalculated_size(simplified.clone(), size); - } - if let Type::ConstParam(cexpr) = simplified { - trace!("Found const param \"{name}\": {cexpr}"); - self.define_const(&name, *cexpr); - } - } } } } diff --git a/src/lir/expr/const_expr.rs b/src/lir/expr/const_expr.rs index 8caacbc8..41755dcc 100644 --- a/src/lir/expr/const_expr.rs +++ b/src/lir/expr/const_expr.rs @@ -368,7 +368,8 @@ impl ConstExpr { return constant.eval_checked(env, i); } - return container.eval_checked(env, i)?.field(member).eval_checked(env, i); + return Ok(container.eval_checked(env, i)?.field(member)); + // return Ok(container.eval_checked(env, i)?.field(member).eval_checked(env, i)?); // if let Ok(Some((constant, _))) = member // .clone() // .as_symbol(env) @@ -487,6 +488,9 @@ impl ConstExpr { Self::Symbol(name) => { if let Some(c) = env.get_const(&name) { + if c == &Self::Symbol(name) { + return Ok(c.clone()); + } c.clone().eval_checked(env, i) } else if let Some(t) = env.get_type(&name) { Ok(Self::Type(t.clone())) diff --git a/src/lir/expr/declaration.rs b/src/lir/expr/declaration.rs index fe88bd15..179235f9 100644 --- a/src/lir/expr/declaration.rs +++ b/src/lir/expr/declaration.rs @@ -13,15 +13,55 @@ use core::{ use log::*; use rayon::prelude::*; use serde_derive::{Deserialize, Serialize}; -use std::collections::{BTreeMap, HashSet}; +use std::{collections::{BTreeMap, HashSet}, sync::RwLock}; use std::hash::{Hash, Hasher}; use std::sync::Arc; +#[derive(Debug, Serialize, Deserialize)] +struct HasCompiled { + compiled: Arc>, + is_original: bool, +} + +impl HasCompiled { + fn mark_compiled(&self) { + *self.compiled.write().unwrap() = true; + } + + fn is_compiled(&self) -> bool { + *self.compiled.read().unwrap() + } +} + +impl Default for HasCompiled { + fn default() -> Self { + Self { + compiled: Arc::new(RwLock::new(false)), + is_original: true, + } + } +} + +impl PartialEq for HasCompiled { + fn eq(&self, other: &Self) -> bool { + return *self.compiled.read().unwrap() == *other.compiled.read().unwrap(); + } +} + +impl Clone for HasCompiled { + fn clone(&self) -> Self { + Self { + compiled: self.compiled.clone(), + is_original: false, + } + } +} + /// A declaration of a variable, function, type, etc. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub enum Declaration { /// A static variable declaration. - StaticVar(String, Mutability, Type, Expr), + StaticVar(String, Mutability, Type, Expr, HasCompiled), /// A variable declaration. Var(String, Mutability, Option, Expr), /// A procedure declaration. @@ -62,7 +102,7 @@ impl Declaration { ty: Type, expr: impl Into, ) -> Self { - Self::StaticVar(name.into(), mutability, ty, expr.into()) + Self::StaticVar(name.into(), mutability, ty, expr.into(), HasCompiled::default()) } /// Create a collection of declarations @@ -285,33 +325,47 @@ impl Declaration { // Add the variable to the environment, so that it can be used in the body. pat.declare_let_bind(expr, &expr_ty, env)?; } - Declaration::StaticVar(name, _mutability, ty, expr) => { - // Get the current instruction (for logging) - let current_instruction = output.current_instruction(); - // A log message - let log_message = - format!("Initializing static variable '{name}' with expression '{expr}'"); - - // Get the type of the variable. - let var_ty = ty.clone(); - // Get the size of the variable. - let static_var_size = var_ty.get_size(env)?; - - let name = name.clone(); - // Allocate the global variable. - output.op(CoreOp::Global { - name: name.clone(), - size: static_var_size, - }); - // Compile the expression to leave the value on the stack. - expr.clone().compile_expr(env, output)?; - // Write the value of the expression to the global variable. - output.op(CoreOp::Pop( - Some(Location::Global(name.clone())), - static_var_size, - )); - // Log the instructions for the declaration. - output.log_instructions_after(&name, &log_message, current_instruction); + Declaration::StaticVar(name, _mutability, ty, expr, has_compiled) => { + // Check if the env already has the static variable declared first. + if !has_compiled.is_compiled() && has_compiled.is_original { + has_compiled.mark_compiled(); + // Get the current instruction (for logging) + let current_instruction = output.current_instruction(); + // A log message + let log_message = + format!("Initializing static variable '{name}' with expression '{expr}'"); + + // Get the type of the variable. + let var_ty = ty.clone(); + // Get the size of the variable. + let static_var_size = var_ty.get_size(env)?; + + let name = name.clone(); + // Allocate the global variable. + output.op(CoreOp::Global { + name: name.clone(), + size: static_var_size, + }); + // Compile the expression to leave the value on the stack. + expr.clone().compile_expr(env, output)?; + // Write the value of the expression to the global variable. + output.op(CoreOp::Pop( + Some(Location::Global(name.clone())), + static_var_size, + )); + // Log the instructions for the declaration. + output.log_instructions_after(&name, &log_message, current_instruction); + } + } + Declaration::Module(_name, decls, ..) => { + // Add all the compile-time declarations to the environment. + env.add_compile_time_declaration(self, true)?; + Self::Many(decls.clone()).compile_helper(None, env, output)?; + // Compile all the sub-declarations. + // for decl in decls.iter() { + // // Compile the sub-declaration. + // decl.compile_helper(None, env, output)?; + // } } Declaration::Many(decls) => { for decl in decls.iter() { @@ -409,7 +463,7 @@ impl Declaration { /// Substitute a type symbol for a type. pub(crate) fn substitute(&mut self, substitution_name: &str, substitution_ty: &Type) { match self { - Self::StaticVar(_name, _mutability, expected_ty, expr) => { + Self::StaticVar(_name, _mutability, expected_ty, expr, ..) => { expr.substitute(substitution_name, substitution_ty); expected_ty.substitute(substitution_name, substitution_ty); } @@ -520,7 +574,7 @@ impl TypeCheck for Declaration { expr.type_check(&new_env)?; } // Typecheck a static variable declaration. - Self::StaticVar(name, mutability, expected_ty, expr) => { + Self::StaticVar(name, mutability, expected_ty, expr, ..) => { // Create a new environment with the static variable declared. let mut new_env = env.clone(); new_env.define_static_var(name, *mutability, expected_ty.clone())?; @@ -726,7 +780,7 @@ impl TypeCheck for Declaration { impl Display for Declaration { fn fmt(&self, f: &mut Formatter) -> FmtResult { match self { - Self::StaticVar(name, mutability, ty, expr) => { + Self::StaticVar(name, mutability, ty, expr, ..) => { write!(f, "static {mutability} {name}: {ty} = {expr}")?; } Self::Var(name, _mutability, ty, expr) => { @@ -960,7 +1014,7 @@ where impl Hash for Declaration { fn hash(&self, state: &mut H) { match self { - Self::StaticVar(name, mutability, ty, expr) => { + Self::StaticVar(name, mutability, ty, expr, ..) => { state.write_u8(0); name.hash(state); mutability.hash(state); diff --git a/src/lir/expr/expression.rs b/src/lir/expr/expression.rs index 5c937b38..661eea95 100644 --- a/src/lir/expr/expression.rs +++ b/src/lir/expr/expression.rs @@ -518,6 +518,14 @@ impl Expr { self.unop(Not) } + pub fn lshift(self, other: impl Into) -> Self { + self.binop(LeftShift, other) + } + + pub fn rshift(self, other: impl Into) -> Self { + self.binop(RightShift, other) + } + /// Bitwise this expression with another. pub fn bitxor(self, other: impl Into) -> Self { self.binop(BitwiseXor, other) diff --git a/src/lir/expr/ops/bitwise/mod.rs b/src/lir/expr/ops/bitwise/mod.rs index ff9319c9..29446df0 100644 --- a/src/lir/expr/ops/bitwise/mod.rs +++ b/src/lir/expr/ops/bitwise/mod.rs @@ -12,6 +12,7 @@ mod nor; mod not; mod or; mod xor; +mod shift; pub use and::*; pub use nand::*; @@ -19,3 +20,4 @@ pub use nor::*; pub use not::*; pub use or::*; pub use xor::*; +pub use shift::*; diff --git a/src/lir/expr/ops/bitwise/shift.rs b/src/lir/expr/ops/bitwise/shift.rs new file mode 100644 index 00000000..f7f8f934 --- /dev/null +++ b/src/lir/expr/ops/bitwise/shift.rs @@ -0,0 +1,167 @@ +//! # Bitwise Operations +use crate::{ + asm::{AssemblyProgram, CoreOp, SP}, + lir::*, +}; +use ::core::fmt::{Debug, Display, Formatter, Result as FmtResult}; + +/// A boolean "LeftShift" operation between two values. +#[derive(Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)] +pub struct LeftShift; + +impl BinaryOp for LeftShift { + /// Can this binary operation be applied to the given types? + fn can_apply(&self, lhs: &Type, rhs: &Type, env: &Env) -> Result { + Ok( + (lhs.equals(&Type::Cell, env)? || lhs.equals(&Type::Int, env)?) + && (rhs.equals(&Type::Cell, env)? || rhs.equals(&Type::Int, env)?), + ) + } + + /// Get the type of the result of applying this binary operation to the given types. + fn return_type(&self, lhs: &Expr, rhs: &Expr, env: &Env) -> Result { + if lhs.get_type(env)?.equals(&Type::Cell, env)? + || rhs.get_type(env)?.equals(&Type::Cell, env)? + { + Ok(Type::Cell) + } else { + Ok(Type::Int) + } + } + + /// Evaluate this binary operation on the given constant values. + fn eval(&self, lhs: &ConstExpr, rhs: &ConstExpr, env: &mut Env) -> Result { + match (lhs.clone().eval(env)?, rhs.clone().eval(env)?) { + (ConstExpr::Int(a), ConstExpr::Int(b)) => Ok(ConstExpr::Int(a << b)), + (ConstExpr::Cell(a) | ConstExpr::Int(a), ConstExpr::Cell(b) | ConstExpr::Int(b)) => { + Ok(ConstExpr::Cell(a << b)) + } + _ => Err(Error::InvalidBinaryOp( + self.clone_box(), + Expr::ConstExpr(lhs.clone()), + Expr::ConstExpr(rhs.clone()), + )), + } + } + + /// Compile the binary operation. + fn compile_types( + &self, + _lhs: &Type, + _rhs: &Type, + _env: &mut Env, + output: &mut dyn AssemblyProgram, + ) -> Result<(), Error> { + output.op(CoreOp::LeftShift { + src: SP.deref(), + dst: SP.deref().offset(-1), + }); + output.op(CoreOp::Pop(None, 1)); + Ok(()) + } + + /// Clone this binary operation into a box. + fn clone_box(&self) -> Box { + Box::new(*self) + } +} + +impl Debug for LeftShift { + fn fmt(&self, f: &mut Formatter) -> FmtResult { + write!(f, "<<") + } +} + +impl Display for LeftShift { + fn fmt(&self, f: &mut Formatter) -> FmtResult { + write!(f, "<<") + } +} + + +#[derive(Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)] +pub struct RightShift; + +impl BinaryOp for RightShift { + + /// Can this binary operation be applied to the given types? + fn can_apply(&self, lhs: &Type, rhs: &Type, env: &Env) -> Result { + Ok( + (lhs.equals(&Type::Cell, env)? || lhs.equals(&Type::Int, env)?) + && (rhs.equals(&Type::Cell, env)? || rhs.equals(&Type::Int, env)?), + ) + } + + /// Get the type of the result of applying this binary operation to the given types. + fn return_type(&self, lhs: &Expr, rhs: &Expr, env: &Env) -> Result { + if lhs.get_type(env)?.equals(&Type::Cell, env)? + || rhs.get_type(env)?.equals(&Type::Cell, env)? + { + Ok(Type::Cell) + } else { + Ok(Type::Int) + } + } + + /// Evaluate this binary operation on the given constant values. + fn eval(&self, lhs: &ConstExpr, rhs: &ConstExpr, env: &mut Env) -> Result { + match (lhs.clone().eval(env)?, rhs.clone().eval(env)?) { + (ConstExpr::Int(a), ConstExpr::Int(b)) => Ok(ConstExpr::Int(a >> b)), + (ConstExpr::Cell(a), ConstExpr::Int(b)) => { + Ok(ConstExpr::Cell(((a as u64) << b) as i64)) + } + (ConstExpr::Int(a), ConstExpr::Cell(b)) => { + Ok(ConstExpr::Int(a >> (b as u64) as i64)) + } + (ConstExpr::Cell(a), ConstExpr::Cell(b)) => { + Ok(ConstExpr::Cell((a >> b) as i64)) + } + _ => Err(Error::InvalidBinaryOp( + self.clone_box(), + Expr::ConstExpr(lhs.clone()), + Expr::ConstExpr(rhs.clone()), + )), + } + } + + /// Compile the binary operation. + fn compile_types( + &self, + lhs: &Type, + _rhs: &Type, + env: &mut Env, + output: &mut dyn AssemblyProgram, + ) -> Result<(), Error> { + // If the left hand side is a cell, do logical shift + output.op(if lhs.equals(&Type::Cell, env)? { + CoreOp::LogicalRightShift { + src: SP.deref(), + dst: SP.deref().offset(-1), + } + } else { + CoreOp::ArithmeticRightShift { + src: SP.deref(), + dst: SP.deref().offset(-1), + } + }); + output.op(CoreOp::Pop(None, 1)); + Ok(()) + } + + /// Clone this binary operation into a box. + fn clone_box(&self) -> Box { + Box::new(*self) + } +} + +impl Debug for RightShift { + fn fmt(&self, f: &mut Formatter) -> FmtResult { + write!(f, ">>") + } +} + +impl Display for RightShift { + fn fmt(&self, f: &mut Formatter) -> FmtResult { + write!(f, ">>") + } +} From 6142caffbd1d07728eafd3223b15a308edef65b7 Mon Sep 17 00:00:00 2001 From: Adam McDaniel Date: Fri, 13 Jun 2025 00:39:57 -0400 Subject: [PATCH 2/2] Passing tests --- src/lir/env.rs | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/lir/env.rs b/src/lir/env.rs index c3a58e70..8040db9e 100644 --- a/src/lir/env.rs +++ b/src/lir/env.rs @@ -708,20 +708,22 @@ impl Env { self.save_type_checked_const(ConstExpr::Symbol(module_name.clone())); } - if let Some(found_id) = self.modules.get(module_name) { - // Check if the declarations are the same - if *found_id == *defined_id { - // If they are the same, we don't need to recompile the module - return Ok(()); - } else { - // If they are different, we need to recompile the module - // Arc::make_mut(&mut self.modules).insert(module_name.clone(), *defined_id); - return Err(Error::ModuleRedefined(module_name.clone())) - } - } else { - // If the module is not defined, we need to define it - Arc::make_mut(&mut self.modules).insert(module_name.clone(), *defined_id); - } + + // if let Some(found_id) = self.modules.get(module_name) { + // // Check if the declarations are the same + // if *found_id == *defined_id { + // // If they are the same, we don't need to recompile the module + // return Ok(()); + // } else { + // // If they are different, we need to recompile the module + // // Arc::make_mut(&mut self.modules).insert(module_name.clone(), *defined_id); + // return Err(Error::ModuleRedefined(module_name.clone())) + // } + // } else { + // // If the module is not defined, we need to define it + // Arc::make_mut(&mut self.modules).insert(module_name.clone(), *defined_id); + // } + Arc::make_mut(&mut self.modules).insert(module_name.clone(), *defined_id); let mut exports = vec![]; for decl in Declaration::Many(decls.clone()).flatten().iter() {