Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/frontend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -185,7 +185,7 @@ pub fn parse(
ret: crate::lir::Type::None,
body: debug_body,
});

expr = crate::lir::Expr::let_consts(
vec![
("free", free),
Expand Down
79 changes: 57 additions & 22 deletions src/frontend/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)));
Expand Down Expand Up @@ -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::<VerboseError<&str>>(name, &contents, true)


match parse_module_contents::<VerboseError<&str>>(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,
)));
}
}

Expand All @@ -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),
Expand All @@ -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 {
Expand Down Expand Up @@ -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" <name: Symbol> ":" <ty: Type> "=" <value: ConstExpr> => Statement::Declaration(Declaration::StaticVar(name, Mutability::Immutable, ty, value)),
// "let" "static" "mut" <name: Symbol> ":" <ty: Type> "=" <value: ConstExpr> => Statement::Declaration(Declaration::StaticVar(name, Mutability::Mutable, ty, value)),
let (input, _) = tag("let")(input)?;
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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("*=")),
Expand Down Expand Up @@ -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)?;
Expand Down
4 changes: 2 additions & 2 deletions src/lir/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
46 changes: 20 additions & 26 deletions src/lir/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -706,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() {
Expand Down Expand Up @@ -824,7 +828,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) => {
Expand Down Expand Up @@ -933,7 +937,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(_, _) => {
Expand Down Expand Up @@ -1014,16 +1018,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);
}
}
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/lir/expr/const_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()))
Expand Down
Loading