Start simple AST parsing
This commit is contained in:
parent
0a22391bae
commit
e080945d37
@ -14,6 +14,12 @@ fn get_copyright() -> String {
|
|||||||
format!("Misti {}\nCopyright (c) {} Fernando Enrique Araoz Morales", VERSION, year)
|
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<()> {
|
fn main() -> io::Result<()> {
|
||||||
print!("{}", get_copyright());
|
print!("{}", get_copyright());
|
||||||
repl::run()
|
repl::run()
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
|
|
||||||
use super::lexic;
|
use super::lexic;
|
||||||
|
use super::syntax;
|
||||||
|
|
||||||
fn compile(input: &String) {
|
fn compile(input: &String) {
|
||||||
let _tokens = lexic::get_tokens(input);
|
let _tokens = lexic::get_tokens(input);
|
||||||
@ -8,9 +9,11 @@ fn compile(input: &String) {
|
|||||||
match _tokens {
|
match _tokens {
|
||||||
Ok(tokens) => {
|
Ok(tokens) => {
|
||||||
for token in tokens {
|
for token in tokens {
|
||||||
print!("[{}] ", token.value);
|
print!("[{:?} {}] ", token.token_type, token.value);
|
||||||
}
|
}
|
||||||
println!("");
|
println!("");
|
||||||
|
|
||||||
|
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)
|
||||||
|
25
src/syntax/expression.rs
Normal file
25
src/syntax/expression.rs
Normal 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
23
src/syntax/grammar.md
Normal 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`
|
||||||
|
|
@ -1,7 +1,11 @@
|
|||||||
|
|
||||||
use super::token::Token;
|
use super::token::Token;
|
||||||
|
|
||||||
|
mod expression;
|
||||||
|
mod types;
|
||||||
|
|
||||||
/// Constructs the Misti AST from a vector of tokens
|
/// 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"))
|
Err(String::from("NOT IMPLEMENTED"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
17
src/syntax/types.rs
Normal file
17
src/syntax/types.rs
Normal 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),
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user