diff --git a/README.md b/README.md index 41a8d729dd..269e3cc309 100644 --- a/README.md +++ b/README.md @@ -5,18 +5,16 @@ An experimental statically-typed compiled programming language made with LLVM an Syntax is subject to change any time right now. It has a rusty style for now. ```rust -mod Main { - pub fn main() -> i32 { - let b: i32 = factorial(4); - return b; - } +pub fn main() -> i32 { + let b: i32 = factorial(4); + return b; +} - pub fn factorial(n: i32) -> i32 { - if n == 1 { - return n; - } else { - return n * factorial(n - 1); - } +pub fn factorial(n: i32) -> i32 { + if n == 1 { + return n; + } else { + return n * factorial(n - 1); } } ``` diff --git a/edb/src/main.rs b/edb/src/main.rs index 7b2633acf3..b925ca7b75 100644 --- a/edb/src/main.rs +++ b/edb/src/main.rs @@ -114,30 +114,18 @@ fn main() -> Result<()> { if bin { std::fs::write( path.join("src").join("main.ed"), - format!( - r#" -mod {} {{ - pub fn main() -> i32 {{ - return 0; - }} -}}"#, - name - ), + r#"pub fn main() -> i32 {{ + return 0; +}"#, )?; } if lib { std::fs::write( path.join("src").join("lib.ed"), - format!( - r#" -mod {} {{ - pub fn hello_world() -> i32 {{ - return 0; - }} -}}"#, - name - ), + r#"pub fn main() -> i32 {{ + return 0; +}"#, )?; } diff --git a/lib/edlang_driver/src/lib.rs b/lib/edlang_driver/src/lib.rs index 3357fcfd67..ed236fe461 100644 --- a/lib/edlang_driver/src/lib.rs +++ b/lib/edlang_driver/src/lib.rs @@ -99,10 +99,13 @@ pub fn compile(args: &CompilerArgs) -> Result { for path in files { let source = std::fs::read_to_string(&path)?; - let modules_ast = edlang_parser::parse_ast(&source); + let module_ast = edlang_parser::parse_ast( + &source, + &path.file_stem().expect("no file stem").to_string_lossy(), + ); - let modules_temp = match modules_ast { - Ok(modules) => modules, + let module_temp = match module_ast { + Ok(module) => module, Err(error) => { let path = path.display().to_string(); let report = edlang_parser::error_to_report(&path, &error)?; @@ -110,7 +113,7 @@ pub fn compile(args: &CompilerArgs) -> Result { std::process::exit(1) } }; - modules.push((path, source, modules_temp)); + modules.push((path, source, module_temp)); } let session = Session { diff --git a/lib/edlang_driver/tests/common.rs b/lib/edlang_driver/tests/common.rs index f83d94cb45..9b4514a294 100644 --- a/lib/edlang_driver/tests/common.rs +++ b/lib/edlang_driver/tests/common.rs @@ -34,7 +34,7 @@ pub fn compile_program( name: &str, library: bool, ) -> Result> { - let modules = edlang_parser::parse_ast(source).unwrap(); + let module = edlang_parser::parse_ast(source, name).unwrap(); let test_dir = tempfile::tempdir().unwrap(); let test_dir_path = test_dir.path().canonicalize()?; @@ -61,7 +61,7 @@ pub fn compile_program( output_asm: false, }; - let program_ir = lower_modules(&[modules]).unwrap(); + let program_ir = lower_modules(&[module]).unwrap(); let object_path = edlang_codegen_llvm::compile(&session, &program_ir).unwrap(); diff --git a/lib/edlang_ir/src/lib.rs b/lib/edlang_ir/src/lib.rs index 54f81eb1d7..42e0a558fc 100644 --- a/lib/edlang_ir/src/lib.rs +++ b/lib/edlang_ir/src/lib.rs @@ -33,6 +33,7 @@ pub struct ProgramBody { pub structs: BTreeMap, /// The function signatures. pub function_signatures: BTreeMap, TypeInfo)>, + pub file_names: BTreeMap, } #[derive(Debug, Clone)] diff --git a/lib/edlang_lowering/src/lib.rs b/lib/edlang_lowering/src/lib.rs index 31a9ddcc13..762b3b69a4 100644 --- a/lib/edlang_lowering/src/lib.rs +++ b/lib/edlang_lowering/src/lib.rs @@ -15,33 +15,27 @@ mod common; pub mod errors; mod prepass; -pub fn lower_modules(modules: &[Vec]) -> Result { +pub fn lower_modules(modules: &[ast::Module]) -> Result { let mut ctx = BuildCtx::default(); // resolve symbols - for (file_id, modules) in modules.iter().enumerate() { - for module in modules { - ctx = prepass::prepass_module(ctx, module, file_id)?; - } + for (file_id, module) in modules.iter().enumerate() { + ctx = prepass::prepass_module(ctx, module, file_id)?; } // resolve imports - for (file_id, modules) in modules.iter().enumerate() { - for module in modules { - ctx = prepass::prepass_imports(ctx, module, file_id)?; - } + for (file_id, module) in modules.iter().enumerate() { + ctx = prepass::prepass_imports(ctx, module, file_id)?; } - for modules in modules { - for mod_def in modules { - let id = *ctx - .body - .top_level_module_names - .get(&mod_def.name.name) - .expect("module should exist"); + for mod_def in modules { + let id = *ctx + .body + .top_level_module_names + .get(&mod_def.name.name) + .expect("module should exist"); - ctx = lower_module(ctx, mod_def, id)?; - } + ctx = lower_module(ctx, mod_def, id)?; } Ok(ctx.body) diff --git a/lib/edlang_parser/src/grammar.lalrpop b/lib/edlang_parser/src/grammar.lalrpop index d564dd1507..d26e9a23f9 100644 --- a/lib/edlang_parser/src/grammar.lalrpop +++ b/lib/edlang_parser/src/grammar.lalrpop @@ -3,7 +3,7 @@ use crate::lexer::LexicalError; use edlang_ast as ast; use std::str::FromStr; -grammar; +grammar<'module_name>(module_name: &'module_name str); extern { type Location = usize; @@ -478,8 +478,16 @@ pub(crate) Import: ast::Import = { } } -pub Modules: Vec = { - => <> +pub TopLevelModule: ast::Module = { + ?> > => ast::Module { + name: ast::Ident { + name: module_name.to_string(), + span: ast::Span::new(0, 0), + }, + imports: imports.unwrap_or(vec![]), + contents, + span: ast::Span::new(lo, hi), + } } pub Module: ast::Module = { diff --git a/lib/edlang_parser/src/lib.rs b/lib/edlang_parser/src/lib.rs index 65df848c31..35f0033590 100644 --- a/lib/edlang_parser/src/lib.rs +++ b/lib/edlang_parser/src/lib.rs @@ -22,10 +22,11 @@ pub mod grammar { pub fn parse_ast( source: &str, -) -> Result, ParseError> { + module_name: &str, +) -> Result> { let lexer = Lexer::new(source); - let parser = grammar::ModulesParser::new(); - parser.parse(lexer) + let parser = grammar::TopLevelModuleParser::new(); + parser.parse(module_name, lexer) } pub fn print_report<'a>(