Add binding parsing

This commit is contained in:
Araozu 2023-01-17 08:04:11 -05:00
parent 7d99920a71
commit 6cecd6af52
6 changed files with 22 additions and 6 deletions

View File

@ -5,6 +5,7 @@ mod repl;
mod syntax; mod syntax;
mod lexic; mod lexic;
mod token; mod token;
mod semantic;
const VERSION: &str = "0.0.1"; const VERSION: &str = "0.0.1";

View File

@ -13,7 +13,7 @@ fn compile(input: &String) {
} }
println!(""); println!("");
let _ast = syntax::construct_ast(Vec::new()); let _ast = syntax::construct_ast(&Vec::new());
}, },
Err(error) => { Err(error) => {
eprintln!("Error scanning.\n{} at pos {}", error.reason, error.position) eprintln!("Error scanning.\n{} at pos {}", error.reason, error.position)

4
src/semantic/mod.rs Normal file
View File

@ -0,0 +1,4 @@
pub fn check_ast() {
}

View File

@ -7,8 +7,19 @@ mod types;
use types::ModuleAST; use types::ModuleAST;
/// Constructs the Misti AST from a vector of tokens /// Constructs the Misti AST from a vector of tokens
pub fn construct_ast<'a>(_tokens: Vec<Token>) -> Result<ModuleAST<'a>, String> { pub fn construct_ast<'a>(tokens: &'a Vec<Token>) -> Result<ModuleAST<'a>, String> {
Err(String::from("NOT IMPLEMENTED")) let maybe_binding = val_binding::try_parse(tokens, 0);
match maybe_binding {
Some(binding) => {
Ok(ModuleAST {
bindings: vec![binding]
})
}
None => {
Err(String::from("Syntax error."))
}
}
} }

View File

@ -1,6 +1,6 @@
pub struct ModuleAST<'a> { pub struct ModuleAST<'a> {
bindings: Vec<Binding<'a>>, pub bindings: Vec<Binding<'a>>,
} }
pub enum Binding<'a> { pub enum Binding<'a> {

View File

@ -7,7 +7,7 @@ use super::expression;
// - NotFound: the first token (var | val) was not found, so the parser should try other options // - NotFound: the first token (var | val) was not found, so the parser should try other options
// - Error: token (var | val) was found, but then other expected tokens were not found // - Error: token (var | val) was found, but then other expected tokens were not found
pub fn try_parse<'a>(tokens: &'a Vec<Token>, pos: usize) -> Option<Binding> { pub fn try_parse<'a>(tokens: &'a Vec<Token>, pos: usize) -> Option<Binding> {
let val_token = try_token_type(tokens, pos, TokenType::VAL)?; let _ = try_token_type(tokens, pos, TokenType::VAL)?;
let identifier = try_token_type(tokens, pos + 1, TokenType::Identifier); let identifier = try_token_type(tokens, pos + 1, TokenType::Identifier);
if identifier.is_none() { return None } if identifier.is_none() { return None }
@ -15,7 +15,7 @@ pub fn try_parse<'a>(tokens: &'a Vec<Token>, pos: usize) -> Option<Binding> {
let equal_operator = try_operator(tokens, pos + 2, String::from("=")); let equal_operator = try_operator(tokens, pos + 2, String::from("="));
if equal_operator.is_none() { return None } if equal_operator.is_none() { return None }
let equal_operator = equal_operator.unwrap(); let _ = equal_operator.unwrap();
let expression = expression::try_parse(tokens, pos + 3); let expression = expression::try_parse(tokens, pos + 3);
if expression.is_none() { return None } if expression.is_none() { return None }