From e9237eaf9afb2c77b715dbf3001071e86d510d81 Mon Sep 17 00:00:00 2001 From: Leo Dev Date: Mon, 1 Jul 2024 18:05:34 +0200 Subject: [PATCH] round brackets --- src/main.rs | 2 +- src/parser.rs | 118 +++++++++++++++++++++++++++++++++----------------- 2 files changed, 80 insertions(+), 40 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5e2a96d..1fc786e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ mod variable; fn main() { let mut vars = Variables::new(); let mut trsp = transpiler::Transpiler::default(); - let out = trsp.transpile("int 12 x".to_string(), 0, &mut vars); + let out = trsp.transpile("(xyz)".to_string(), 0, &mut vars); for p in trsp.problems { println!("{:?}: {}", p.problem_type, p.problem_msg) } diff --git a/src/parser.rs b/src/parser.rs index 1b53d59..b68fb92 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -10,8 +10,8 @@ use crate::{ pub enum Token { Number(String, State), Identifier(String, State), - // Curly(String), - // Round(String), + Curly(String, State), + Round(String, State), // Square(String), } pub enum Ast { @@ -22,6 +22,7 @@ pub fn get_token(v: String, t: u32, state: State) -> Token { match t { 1 => Token::Number(v, state), 2 => Token::Identifier(v, state), + 3 => Token::Round(v, state), _ => Token::Number(String::from(""), state), } } @@ -31,52 +32,89 @@ pub fn tokenize(code: String, root: &mut Transpiler) -> Vec { let mut tokens: Vec = Vec::new(); let mut token_value: String = String::new(); let mut token_type: u32 = 0; - //let mut state = State { line: } + let mut stoken: (u8, u8) = (0, 0); + let mut token_state = State { line: 0, column: 0 }; while !code.is_empty() { if let Some(c) = code.chars().next() { code.remove(0); - let mut tval: char = c; - let mut t: u32 = 0; + let mut tval = c; + let mut t: u32 = token_type; root.state.column += 1; - match c { - '0'..='9' => { - if token_type == 2 { + if stoken.1 > 0 { + match c { + ')' => { + if stoken.0 == 1 { + stoken.1 -= 1; + if stoken.1 == 0 { + stoken.0 = 0; + tokens.push(get_token( + token_value.clone(), + token_type, + token_state.clone(), + )); + token_type = 0; + token_value = String::new(); + } else { + token_value += c.to_string().as_str(); + } + } + } + '(' => { + if stoken.0 == 1 { + stoken.1 += 1; + token_value += c.to_string().as_str(); + } + } + _ => token_value += c.to_string().as_str(), + } + } else { + match c { + '0'..='9' => { + if token_type == 2 { + t = 2; + } else { + t = 1; + } + } + 'a'..='z' | 'A'..='Z' => { t = 2; - } else { - t = 1; + } + '\t' | ' ' => tval = '\n', + '\n' | '\r' => { + tval = '\n'; + root.state.line += 1; + root.state.column = 0; + } + '(' => { + stoken.0 = 1; + stoken.1 += 1; + tval = ' '; + t = 3; + } + x => { + root.problems.push(Problem { + problem_msg: format!( + "Invalid character '{}' at {}:{}, LexingError", + x, root.state.line, root.state.column + ), + problem_type: ProblemType::SyntaxError, + }); } } - 'a'..='z' | 'A'..='Z' => { - t = 2; + if t != token_type && token_type != 0 { + tokens.push(get_token( + token_value.clone(), + token_type, + token_state.clone(), + )); + token_type = 0; + token_value = String::new(); } - '\t' | ' ' => tval = ' ', - '\n' | '\r' => { - tval = '\n'; - root.state.line += 1; - root.state.column = 0; + if tval != '\n' { + token_state = root.state.clone(); + token_type = t; + token_value += tval.to_string().as_str().trim(); } - x => { - root.problems.push(Problem { - problem_msg: format!( - "Invalid character '{}' at {}:{}, LexingError", - x, root.state.line, root.state.column - ), - problem_type: ProblemType::SyntaxError, - }); - } - } - if t != token_type && token_type != 0 || tval == ' ' { - tokens.push(get_token( - token_value.clone(), - token_type, - root.state.clone(), - )); - token_type = 0; - token_value = String::new(); - } - if tval != ' ' && tval != '\n' { - token_type = t; - token_value += tval.to_string().as_str(); } } } @@ -93,6 +131,8 @@ pub fn tokenize(code: String, root: &mut Transpiler) -> Vec { pub fn parse(code: String, vars: &mut Variables, root: &mut Transpiler) -> Vec { let mut ast: Vec = Vec::new(); let mut tokens = tokenize(code, root); + println!("{:?}", tokens); + panic!("xd"); let mut pcon: Vec<(u32, u32)> = Vec::new(); while tokens.len() > 0 { let mut drain: usize = 1;