diff --git a/.gitignore b/.gitignore index 86eda0c..d0a6340 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ /hbbytecode/src/opcode.rs /hbbytecode/src/ops.rs /hblang/src/instrs.rs +/.rgignore rust-ice-* diff --git a/hblang/src/parser.rs b/hblang/src/parser.rs index 8355084..0a521cf 100644 --- a/hblang/src/parser.rs +++ b/hblang/src/parser.rs @@ -1326,17 +1326,17 @@ impl ArenaChunk { impl Drop for ArenaChunk { fn drop(&mut self) { - log::inf!( - "dropping chunk of size: {}", - (Self::LAYOUT.size() - (self.end as usize - self.base as usize)) - * !self.end.is_null() as usize - ); + //log::inf!( + // "dropping chunk of size: {}", + // (Self::LAYOUT.size() - (self.end as usize - self.base as usize)) + // * !self.end.is_null() as usize + //); let mut current = self.base; while !current.is_null() { let next = Self::next(current); unsafe { std::alloc::dealloc(current, Self::LAYOUT) }; current = next; - log::dbg!("deallocating full chunk"); + //log::dbg!("deallocating full chunk"); } } } diff --git a/hblang/src/son.rs b/hblang/src/son.rs index ebb7b05..297d1c6 100644 --- a/hblang/src/son.rs +++ b/hblang/src/son.rs @@ -1,11 +1,11 @@ #![allow(dead_code)] use { crate::{ - ident::{self, Ident}, - instrs::{self, *}, - lexer::{self, TokenKind}, + ident::Ident, + instrs::{self}, + lexer::{self}, log, - parser::{self, find_symbol, idfl, CtorField, Expr, ExprRef, FileId, Pos}, + parser::{self, Expr, ExprRef, FileId, Pos}, HashMap, }, std::{ @@ -769,7 +769,7 @@ pub struct Codegen { impl Codegen { pub fn generate(&mut self) { - self.find_or_declare(0, 0, Err("main"), ""); + self.find_or_declare(0, 0, None, "main"); self.complete_call_graph_low(); } @@ -786,11 +786,11 @@ impl Codegen { self.tys.structs.len() as u32 - 1 } - fn expr_ctx(&mut self, expr: &Expr, mut ctx: Ctx) -> Option { - todo!() + fn expr_ctx(&mut self, expr: &Expr, ctx: Ctx) -> Option { + self.report_unhandled_ast(expr, "bruh"); } - #[must_use] + //#[must_use] fn complete_call_graph(&mut self) { self.complete_call_graph_low(); } @@ -817,7 +817,7 @@ impl Codegen { &mut self, pos: Pos, file: FileId, - name: Result, + name: Option, lit_name: &str, ) -> ty::Kind { todo!() @@ -885,44 +885,97 @@ impl Codegen { #[cfg(test)] mod tests { use { - super::parser, - crate::{codegen::LoggedMem, log, parser::FileId}, + crate::parser::{self, FileId}, std::io, }; const README: &str = include_str!("../README.md"); fn generate(ident: &'static str, input: &'static str, output: &mut String) { - todo!() + fn find_block(mut input: &'static str, test_name: &'static str) -> &'static str { + const CASE_PREFIX: &str = "#### "; + const CASE_SUFFIX: &str = "\n```hb"; + loop { + let Some(pos) = input.find(CASE_PREFIX) else { + unreachable!("test {test_name} not found"); + }; + + input = unsafe { input.get_unchecked(pos + CASE_PREFIX.len()..) }; + if !input.starts_with(test_name) { + continue; + } + input = unsafe { input.get_unchecked(test_name.len()..) }; + if !input.starts_with(CASE_SUFFIX) { + continue; + } + input = unsafe { input.get_unchecked(CASE_SUFFIX.len()..) }; + + let end = input.find("```").unwrap_or(input.len()); + break unsafe { input.get_unchecked(..end) }; + } + } + + let input = find_block(input, ident); + + let mut module_map = Vec::new(); + let mut last_start = 0; + let mut last_module_name = "test"; + for (i, m) in input.match_indices("// in module: ") { + parser::test::format(ident, input[last_start..i].trim()); + module_map.push((last_module_name, &input[last_start..i])); + let (module_name, _) = input[i + m.len()..].split_once('\n').unwrap(); + last_module_name = module_name; + last_start = i + m.len() + module_name.len() + 1; + } + parser::test::format(ident, input[last_start..].trim()); + module_map.push((last_module_name, input[last_start..].trim())); + + let loader = |path: &str, _: &str| { + module_map + .iter() + .position(|&(name, _)| name == path) + .map(|i| i as FileId) + .ok_or(io::Error::from(io::ErrorKind::NotFound)) + }; + + let mut codegen = super::Codegen { + files: module_map + .iter() + .map(|&(path, content)| parser::Ast::new(path, content.to_owned(), &loader)) + .collect(), + ..Default::default() + }; + + codegen.generate(); } crate::run_tests! { generate: arithmetic => README; - variables => README; - functions => README; - comments => README; - if_statements => README; - loops => README; - fb_driver => README; - pointers => README; - structs => README; - different_types => README; - struct_operators => README; - directives => README; - global_variables => README; - generic_types => README; - generic_functions => README; - c_strings => README; - struct_patterns => README; - arrays => README; - struct_return_from_module_function => README; - //comptime_pointers => README; - sort_something_viredly => README; - hex_octal_binary_literals => README; - comptime_min_reg_leak => README; - // structs_in_registers => README; - comptime_function_from_another_file => README; - inline => README; - inline_test => README; + //variables => README; + //functions => README; + //comments => README; + //if_statements => README; + //loops => README; + //fb_driver => README; + //pointers => README; + //structs => README; + //different_types => README; + //struct_operators => README; + //directives => README; + //global_variables => README; + //generic_types => README; + //generic_functions => README; + //c_strings => README; + //struct_patterns => README; + //arrays => README; + //struct_return_from_module_function => README; + ////comptime_pointers => README; + //sort_something_viredly => README; + //hex_octal_binary_literals => README; + //comptime_min_reg_leak => README; + ////structs_in_registers => README; + //comptime_function_from_another_file => README; + //inline => README; + //inline_test => README; } }