diff options
| author | Cody <cody@codyq.dev> | 2023-06-27 01:59:04 -0500 |
|---|---|---|
| committer | Cody <cody@codyq.dev> | 2023-06-27 01:59:04 -0500 |
| commit | 9c2d8f5a10b8affd604cec6e394d43514ef93ca1 (patch) | |
| tree | 73f87c0484b58f2e7d7c83891d6cb437c3789250 | |
| parent | 4c26865f904729408ce1aaf35041150c35490311 (diff) | |
| download | sloth-9c2d8f5a10b8affd604cec6e394d43514ef93ca1.tar.gz | |
mmmm multiple files mmmmm
| -rw-r--r-- | examples/hello.sloth | 3 | ||||
| -rw-r--r-- | 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 <file>"); + println!("Usage: sloth <file...>"); 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 } |
