Add errors, move compiling to examples

This commit is contained in:
Sofia 2023-08-02 19:17:57 +03:00
parent e9aab4f43e
commit c7f11e5091
10 changed files with 92 additions and 24 deletions

56
Cargo.lock generated
View File

@ -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"

View File

@ -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"

11
examples/easiest.rs Normal file
View File

@ -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);
}

View File

@ -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},
};

View File

@ -101,12 +101,11 @@ impl<'a> Cursor<'a> {
}
}
pub fn tokenize<T: Into<String>>(to_tokenize: T) -> Result<Vec<FullToken>, String> {
pub fn tokenize<T: Into<String>>(to_tokenize: T) -> Result<Vec<FullToken>, 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<T: Into<String>>(to_tokenize: T) -> Result<Vec<FullToken>, 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),
}

View File

@ -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<String, ReidError> {
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())
}