From 6cecd6af52feab5cf846dfe257df85dbb460c1b0 Mon Sep 17 00:00:00 2001 From: Araozu Date: Tue, 17 Jan 2023 08:04:11 -0500 Subject: [PATCH] Add binding parsing --- src/main.rs | 1 + src/repl/mod.rs | 2 +- src/semantic/mod.rs | 4 ++++ src/syntax/mod.rs | 15 +++++++++++++-- src/syntax/types.rs | 2 +- src/syntax/val_binding.rs | 4 ++-- 6 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 src/semantic/mod.rs diff --git a/src/main.rs b/src/main.rs index 42c23cc..df06de2 100755 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod repl; mod syntax; mod lexic; mod token; +mod semantic; const VERSION: &str = "0.0.1"; diff --git a/src/repl/mod.rs b/src/repl/mod.rs index 5a83968..7bd8c67 100755 --- a/src/repl/mod.rs +++ b/src/repl/mod.rs @@ -13,7 +13,7 @@ fn compile(input: &String) { } println!(""); - let _ast = syntax::construct_ast(Vec::new()); + let _ast = syntax::construct_ast(&Vec::new()); }, Err(error) => { eprintln!("Error scanning.\n{} at pos {}", error.reason, error.position) diff --git a/src/semantic/mod.rs b/src/semantic/mod.rs new file mode 100644 index 0000000..7f79cb4 --- /dev/null +++ b/src/semantic/mod.rs @@ -0,0 +1,4 @@ + +pub fn check_ast() { + +} diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs index cfc3d0f..ed3bfb2 100755 --- a/src/syntax/mod.rs +++ b/src/syntax/mod.rs @@ -7,8 +7,19 @@ mod types; use types::ModuleAST; /// Constructs the Misti AST from a vector of tokens -pub fn construct_ast<'a>(_tokens: Vec) -> Result, String> { - Err(String::from("NOT IMPLEMENTED")) +pub fn construct_ast<'a>(tokens: &'a Vec) -> Result, String> { + 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.")) + } + } } diff --git a/src/syntax/types.rs b/src/syntax/types.rs index 5662bb5..9be6918 100644 --- a/src/syntax/types.rs +++ b/src/syntax/types.rs @@ -1,6 +1,6 @@ pub struct ModuleAST<'a> { - bindings: Vec>, + pub bindings: Vec>, } pub enum Binding<'a> { diff --git a/src/syntax/val_binding.rs b/src/syntax/val_binding.rs index b614c41..f91a3cf 100644 --- a/src/syntax/val_binding.rs +++ b/src/syntax/val_binding.rs @@ -7,7 +7,7 @@ use super::expression; // - 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 pub fn try_parse<'a>(tokens: &'a Vec, pos: usize) -> Option { - 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); if identifier.is_none() { return None } @@ -15,7 +15,7 @@ pub fn try_parse<'a>(tokens: &'a Vec, pos: usize) -> Option { let equal_operator = try_operator(tokens, pos + 2, String::from("=")); 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); if expression.is_none() { return None }