diff --git a/src/main.rs b/src/main.rs index d9e79af..42c23cc 100755 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,12 @@ fn get_copyright() -> String { format!("Misti {}\nCopyright (c) {} Fernando Enrique Araoz Morales", VERSION, year) } +/// # Misti +/// +/// Usage: +/// - `misti` : Compiles the current project according to the settings in the misti.json file +/// - `misti --watch, -w` : Starts the compiler in watch mode +/// - `misti -i FILE -o OUTPUT` : Compiles FILE and writes the result in OUTPUT fn main() -> io::Result<()> { print!("{}", get_copyright()); repl::run() diff --git a/src/repl/mod.rs b/src/repl/mod.rs index 0cbf20c..5a83968 100755 --- a/src/repl/mod.rs +++ b/src/repl/mod.rs @@ -1,6 +1,7 @@ use std::io::{self, Write}; use super::lexic; +use super::syntax; fn compile(input: &String) { let _tokens = lexic::get_tokens(input); @@ -8,9 +9,11 @@ fn compile(input: &String) { match _tokens { Ok(tokens) => { for token in tokens { - print!("[{}] ", token.value); + print!("[{:?} {}] ", token.token_type, token.value); } println!(""); + + let _ast = syntax::construct_ast(Vec::new()); }, Err(error) => { eprintln!("Error scanning.\n{} at pos {}", error.reason, error.position) diff --git a/src/syntax/expression.rs b/src/syntax/expression.rs new file mode 100644 index 0000000..f3539e4 --- /dev/null +++ b/src/syntax/expression.rs @@ -0,0 +1,25 @@ +use crate::token::Token; + +use super::types::Expression; + + +pub fn try_parse(tokens: &Vec, pos: usize) -> Option { + None +} + + +#[cfg(test)] +mod tests { + use super::*; + use crate::lexic::get_tokens; + + #[test] + fn should_parse_a_number() { + let tokens = get_tokens(&String::from("40")).unwrap(); + let expression = try_parse(&tokens, 0).unwrap(); + + match expression { + Expression::Number(value) => assert_eq!("40", value), + } + } +} diff --git a/src/syntax/grammar.md b/src/syntax/grammar.md new file mode 100644 index 0000000..3af9772 --- /dev/null +++ b/src/syntax/grammar.md @@ -0,0 +1,23 @@ +# Grammar + +## Module + +A module is (commonly) a single source file. + +- `module = variable_binding*` + + +### `variable_binding` + +A declaration with `var` or `val`. + +- `var = "var"` +- `val = "val"` +- `variable_binding = (var | val), identifier, "=", expression` + +### `expression` + +For now just a number + +- `expression = number` + diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs index 2816caf..c412f75 100755 --- a/src/syntax/mod.rs +++ b/src/syntax/mod.rs @@ -1,7 +1,11 @@ - use super::token::Token; +mod expression; +mod types; + /// Constructs the Misti AST from a vector of tokens -pub fn _construct_ast(_tokens: Vec) -> Result<(), String> { +pub fn construct_ast(_tokens: Vec) -> Result { Err(String::from("NOT IMPLEMENTED")) } + + diff --git a/src/syntax/types.rs b/src/syntax/types.rs new file mode 100644 index 0000000..197e7bf --- /dev/null +++ b/src/syntax/types.rs @@ -0,0 +1,17 @@ + +pub struct ModuleAST { + bindings: Vec, +} + +pub enum Binding { + Val(ValBinding), +} + +pub struct ValBinding { + identifier: String, + expression: Expression, +} + +pub enum Expression { + Number(String), +}