From 9c2d8f5a10b8affd604cec6e394d43514ef93ca1 Mon Sep 17 00:00:00 2001 From: Cody Date: Tue, 27 Jun 2023 01:59:04 -0500 Subject: mmmm multiple files mmmmm --- examples/hello.sloth | 3 +-- sloth/src/main.rs | 60 ++++++++++++++++++++++++++++++++++------------------ 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/examples/hello.sloth b/examples/hello.sloth index 6f56632..619a5a0 100644 --- a/examples/hello.sloth +++ b/examples/hello.sloth @@ -1,6 +1,5 @@ -foreign fn print(x: String) Void; - fn main() Int { print("Hello World\n"); + var x: F = 2; return 0; } diff --git a/sloth/src/main.rs b/sloth/src/main.rs index 3112e28..421bbea 100644 --- a/sloth/src/main.rs +++ b/sloth/src/main.rs @@ -32,16 +32,49 @@ fn main() { if args.len() < 2 { println!("Sloth programming language interpreter\n"); - println!("Usage: sloth "); + println!("Usage: sloth "); return; } - let source_path = &args[1]; - let Ok(source) = fs::read_to_string(source_path) else { - eprintln!("Error while reading '{source_path}'"); + // Reading source files + let mut source = String::new(); + for path in args.iter().skip(1) { + let Ok(contents) = fs::read_to_string(path) else { + eprintln!("Error while reading '{path}'"); + return; + }; + source.push_str(&contents); + let len = contents.lines().collect_vec().len(); + let padding = 1000 - len; + for _ in 0..padding { + source.push('\n'); + } + } + + // Parsing + let tokens = Lexer::new(&source).collect_vec(); + let global_symtable = mk_symtable(); + let mut ast = AstParser::parse(tokens, global_symtable).unwrap(); + + if let Err(error) = analyze(&mut ast) { + eprintln!( + "Error in file {} on line {}: {error}", + args[1 + (error.line() / 1_000) as usize], + error.line() % 1000 + 1, + ); return; - }; + } + + // Generating code for module + let context = Context::create(); + let mut codegen = Codegen::new(&context, "s"); + let mut output_file = File::create("output.o").unwrap(); + + codegen.codegen(&ast); + codegen.write_obj(&mut output_file, FileType::Object); +} +fn mk_symtable() -> SymbolTable { // Symbol table let mut global_symtable = SymbolTable::new(); global_symtable.insert("Void".into(), Symbol::Type(Type::Void)); @@ -93,20 +126,5 @@ fn main() { global_symtable.insert("vsetf".into(), dummyf); global_symtable.insert("vsetb".into(), dummyb); - // Parsing - let tokens = Lexer::new(&source).collect_vec(); - let mut ast = AstParser::parse(tokens, global_symtable).unwrap(); - - if let Err(error) = analyze(&mut ast) { - eprintln!("Error on line {}: {error}", error.line() + 1); - return; - } - - // Generating code for module - let context = Context::create(); - let mut codegen = Codegen::new(&context, "s"); - let mut output_file = File::create("output.o").unwrap(); - - codegen.codegen(&ast); - codegen.write_obj(&mut output_file, FileType::Object); + global_symtable } -- cgit v1.2.3