diff --git a/Cargo.lock b/Cargo.lock index c158548..c638dd3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -48,6 +48,24 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" +[[package]] +name = "proc-macro2" +version = "1.0.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50f3b39ccfb720540debaa0164757101c08ecb8d326b15358ce76a62c7e85965" +dependencies = [ + "proc-macro2", +] + [[package]] name = "regex" version = "1.9.1" @@ -82,6 +100,7 @@ name = "reid" version = "0.1.0" dependencies = [ "llvm-sys", + "thiserror", ] [[package]] @@ -89,3 +108,40 @@ name = "semver" version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b0293b4b29daaf487284529cc2f5675b8e57c61f70167ba415a463651fd6a918" + +[[package]] +name = "syn" +version = "2.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04361975b3f5e348b2189d8dc55bc942f278b2d482a6a0365de5bdd62d351567" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "1.0.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "611040a08a0439f8248d1990b111c95baa9c704c805fa1f62104b39655fd7f90" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "090198534930841fab3a5d1bb637cde49e339654e606195f8d9c76eeb081dc96" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "unicode-ident" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" diff --git a/Cargo.toml b/Cargo.toml index 9e665a2..4d8f5f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,5 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] - +## LLVM Bindings llvm-sys = "160" +## Make it easier to generate errors +thiserror = "1.0.44" \ No newline at end of file diff --git a/examples/easiest.rs b/examples/easiest.rs new file mode 100644 index 0000000..09dd5d0 --- /dev/null +++ b/examples/easiest.rs @@ -0,0 +1,11 @@ +use reid::compile; + +pub static EASIEST: &str = include_str!("./reid/easiest.reid"); + +fn main() { + let text = match compile(EASIEST) { + Ok(t) => t, + Err(e) => panic!("{}", e), + }; + println!("{}", text); +} diff --git a/reid/easiest.reid b/examples/reid/easiest.reid similarity index 100% rename from reid/easiest.reid rename to examples/reid/easiest.reid diff --git a/reid/easy.reid b/examples/reid/easy.reid similarity index 100% rename from reid/easy.reid rename to examples/reid/easy.reid diff --git a/reid/hard.reid b/examples/reid/hard.reid similarity index 100% rename from reid/hard.reid rename to examples/reid/hard.reid diff --git a/reid/medium.reid b/examples/reid/medium.reid similarity index 100% rename from reid/medium.reid rename to examples/reid/medium.reid diff --git a/src/codegen.rs b/src/codegen.rs index 238eea1..a6412c7 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -1,10 +1,7 @@ use std::collections::{hash_map, HashMap}; use crate::{ - ast::{ - BinaryOperator, BlockLevelStatement, Expression, FunctionDefinition, Literal, - TopLevelStatement, - }, + ast::{BinaryOperator, BlockLevelStatement, Expression, FunctionDefinition, TopLevelStatement}, llvm_ir::{IRBlock, IRModule, IRValue, IRValueType}, }; diff --git a/src/lexer.rs b/src/lexer.rs index 828c344..b16527c 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -101,12 +101,11 @@ impl<'a> Cursor<'a> { } } -pub fn tokenize>(to_tokenize: T) -> Result, String> { +pub fn tokenize>(to_tokenize: T) -> Result, Error> { let to_tokenize = to_tokenize.into(); - let mut position = (0, 1); let mut cursor = Cursor { char_stream: to_tokenize.chars(), - position, + position: (0, 1), }; let mut tokens = Vec::new(); @@ -167,24 +166,25 @@ pub fn tokenize>(to_tokenize: T) -> Result, Strin '}' => Token::BraceClose, ',' => Token::Comma, // Invalid token - _ => Err(format!( - "Unknown token '{}' at {}, {}", - character, position.0, position.1 - ))?, + _ => Err(Error::InvalidToken(*character, cursor.position))?, }; tokens.push(FullToken { token: variant, - position, + position: cursor.position, }); } - position.0 += 1; - tokens.push(FullToken { token: Token::Eof, - position, + position: cursor.position, }); Ok(tokens) } + +#[derive(thiserror::Error, Debug)] +pub enum Error { + #[error("Invalid token '{}' at Ln {}, Col {}", .0, (.1).1, (.1).0)] + InvalidToken(char, Position), +} diff --git a/src/main.rs b/src/lib.rs similarity index 71% rename from src/main.rs rename to src/lib.rs index 80dbff2..7acf0f8 100644 --- a/src/main.rs +++ b/src/lib.rs @@ -1,10 +1,5 @@ use crate::{ast::TopLevelStatement, lexer::Token, llvm_ir::IRModule, token_stream::TokenStream}; -pub static EASIEST: &str = include_str!("../reid/easiest.reid"); -pub static EASY: &str = include_str!("../reid/easy.reid"); -pub static MEDIUM: &str = include_str!("../reid/medium.reid"); -pub static HARD: &str = include_str!("../reid/hard.reid"); - mod ast; mod codegen; mod lexer; @@ -17,8 +12,14 @@ mod token_stream; // 3. Make it so all codegen is done with a Block-struct, that represents a // single proper block -fn main() { - let tokens = lexer::tokenize(EASIEST).unwrap(); +#[derive(thiserror::Error, Debug)] +pub enum ReidError { + #[error(transparent)] + LexerError(#[from] lexer::Error), +} + +pub fn compile(source: &str) -> Result { + let tokens = lexer::tokenize(source)?; dbg!(&tokens); @@ -36,5 +37,6 @@ fn main() { for statement in statements { statement.codegen(&mut module); } - println!("{}", module.print_to_string().unwrap()); + let text = module.print_to_string().unwrap(); + Ok(text.to_owned()) }