Skip to content

Commit

Permalink
allow top level module
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Mar 13, 2024
1 parent 0ea6ac3 commit 7c3bc05
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 59 deletions.
20 changes: 9 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
```
Expand Down
24 changes: 6 additions & 18 deletions edb/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}"#,
)?;
}

Expand Down
11 changes: 7 additions & 4 deletions lib/edlang_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,21 @@ pub fn compile(args: &CompilerArgs) -> Result<PathBuf> {
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)?;
edlang_parser::print_report(&path, &source, report)?;
std::process::exit(1)
}
};
modules.push((path, source, modules_temp));
modules.push((path, source, module_temp));
}

let session = Session {
Expand Down
4 changes: 2 additions & 2 deletions lib/edlang_driver/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn compile_program(
name: &str,
library: bool,
) -> Result<CompileResult, Box<dyn std::error::Error>> {
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()?;
Expand All @@ -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();

Expand Down
1 change: 1 addition & 0 deletions lib/edlang_ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct ProgramBody {
pub structs: BTreeMap<DefId, AdtBody>,
/// The function signatures.
pub function_signatures: BTreeMap<DefId, (Vec<TypeInfo>, TypeInfo)>,
pub file_names: BTreeMap<usize, String>,
}

#[derive(Debug, Clone)]
Expand Down
30 changes: 12 additions & 18 deletions lib/edlang_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,27 @@ mod common;
pub mod errors;
mod prepass;

pub fn lower_modules(modules: &[Vec<ast::Module>]) -> Result<ProgramBody, LoweringError> {
pub fn lower_modules(modules: &[ast::Module]) -> Result<ProgramBody, LoweringError> {
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)
Expand Down
14 changes: 11 additions & 3 deletions lib/edlang_parser/src/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -478,8 +478,16 @@ pub(crate) Import: ast::Import = {
}
}

pub Modules: Vec<ast::Module> = {
<Module+> => <>
pub TopLevelModule: ast::Module = {
<lo:@L> <imports:List<Import>?> <contents:List<ModuleStatement>> <hi:@R> => 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 = {
Expand Down
7 changes: 4 additions & 3 deletions lib/edlang_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ pub mod grammar {

pub fn parse_ast(
source: &str,
) -> Result<Vec<edlang_ast::Module>, ParseError<usize, Token, LexicalError>> {
module_name: &str,
) -> Result<edlang_ast::Module, ParseError<usize, Token, LexicalError>> {
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>(
Expand Down

0 comments on commit 7c3bc05

Please sign in to comment.