Add binding parsing
This commit is contained in:
parent
7d99920a71
commit
6cecd6af52
@ -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";
|
||||||
|
|
||||||
|
@ -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
4
src/semantic/mod.rs
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
pub fn check_ast() {
|
||||||
|
|
||||||
|
}
|
@ -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."))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -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> {
|
||||||
|
@ -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 }
|
||||||
|
Loading…
Reference in New Issue
Block a user