Start simple AST parsing

master
Araozu 2023-01-05 18:20:58 -05:00
parent 0a22391bae
commit e080945d37
6 changed files with 81 additions and 3 deletions

View File

@ -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()

View File

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

25
src/syntax/expression.rs Normal file
View File

@ -0,0 +1,25 @@
use crate::token::Token;
use super::types::Expression;
pub fn try_parse(tokens: &Vec<Token>, pos: usize) -> Option<Expression> {
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),
}
}
}

23
src/syntax/grammar.md Normal file
View File

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

View File

@ -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<Token>) -> Result<(), String> {
pub fn construct_ast(_tokens: Vec<Token>) -> Result<types::ModuleAST, String> {
Err(String::from("NOT IMPLEMENTED"))
}

17
src/syntax/types.rs Normal file
View File

@ -0,0 +1,17 @@
pub struct ModuleAST {
bindings: Vec<Binding>,
}
pub enum Binding {
Val(ValBinding),
}
pub struct ValBinding {
identifier: String,
expression: Expression,
}
pub enum Expression {
Number(String),
}