From 65c985676245455ddf051f5674953820c88f0c09 Mon Sep 17 00:00:00 2001 From: Leo Dev Date: Tue, 2 Apr 2024 23:54:40 +0200 Subject: [PATCH] update --- src/lexer.rs | 6 +++--- src/main.rs | 11 ++++++----- src/parser.rs | 16 ++++++++++++++-- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/lexer.rs b/src/lexer.rs index 2a8c54d..de20db4 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -20,12 +20,12 @@ pub enum TokenType { #[derive(Clone)] pub struct Token { pub token_type: TokenType, - pub token_value: Vec + pub token_values: Vec } impl<'a> fmt::Debug for Token { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "(TokenType: {:?}, TokenValue: {})", self.token_type, self.token_value.join(", ")) + write!(f, "(TokenType: {:?}, TokenValue: {})", self.token_type, self.token_values.join(", ")) } } @@ -91,7 +91,7 @@ pub fn lex(mut code: &str, use_whitespace: bool) -> Vec { if (!use_whitespace && s.token_type!=TokenType::Whitespace) || use_whitespace { tokens.push(Token { token_type: s.token_type, - token_value: vcaps, + token_values: vcaps, }); } } else { diff --git a/src/main.rs b/src/main.rs index f69b2ba..6dc5963 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,16 +4,17 @@ mod parser; use parser::parse; fn main() { - let input = "int main() {"; + let input = "int main() {}"; + println!("\n\n\n\n"); // let input = "test->xy"; let mut tokens = lex(input, false); let ast = *parse(&mut tokens); - for t in tokens { - println!("{:?}", t); - } + // for t in tokens { + // println!("{:?}", t); + // } let mut _x = 1; for a in ast { - println!("{:?}: {:?}", a.type_, a.values); + println!("{:?}: {:?}\n", a.type_, a.values); _x+=1; } } diff --git a/src/parser.rs b/src/parser.rs index dca0d5b..eb31c7e 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -4,7 +4,8 @@ use std::fmt; #[derive(Debug, Clone)] pub enum AstTypes { - FunctionDecleration + FunctionDecleration, + Other } pub struct Ast { @@ -22,7 +23,8 @@ impl<'a> fmt::Debug for Ast { pub enum AstDef { Normal(TokenType), Repeated(TokenType), - Optional(TokenType) + Optional(TokenType), + Else } pub struct PNode { @@ -40,6 +42,10 @@ pub fn parse(tokens: &mut Vec) -> Box> { ast_type: AstTypes::FunctionDecleration, ast_match: vec![AstDef::Normal(TokenType::Keyword)] }, + PNode { + ast_type: AstTypes::Other, + ast_match: vec![AstDef::Else] + }, ]; let tokens_len = tokens.len(); let mut ast: Vec = Vec::new(); @@ -62,8 +68,14 @@ pub fn parse(tokens: &mut Vec) -> Box> { is_match = true; } else { is_match = false; + break; } } + AstDef::Else => { + matched.push(tokens[i].clone()); + tokens.drain(0..1); + is_match = true; + } _ => {} } }