round brackets

This commit is contained in:
2024-07-01 18:05:34 +02:00
parent beff172ff4
commit e9237eaf9a
2 changed files with 80 additions and 40 deletions
+1 -1
View File
@@ -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)
}
+50 -10
View File
@@ -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,13 +32,42 @@ pub fn tokenize(code: String, root: &mut Transpiler) -> Vec<Token> {
let mut tokens: Vec<Token> = 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;
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 {
@@ -49,12 +79,18 @@ pub fn tokenize(code: String, root: &mut Transpiler) -> Vec<Token> {
'a'..='z' | 'A'..='Z' => {
t = 2;
}
'\t' | ' ' => tval = ' ',
'\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!(
@@ -65,18 +101,20 @@ pub fn tokenize(code: String, root: &mut Transpiler) -> Vec<Token> {
});
}
}
if t != token_type && token_type != 0 || tval == ' ' {
if t != token_type && token_type != 0 {
tokens.push(get_token(
token_value.clone(),
token_type,
root.state.clone(),
token_state.clone(),
));
token_type = 0;
token_value = String::new();
}
if tval != ' ' && tval != '\n' {
if tval != '\n' {
token_state = root.state.clone();
token_type = t;
token_value += tval.to_string().as_str();
token_value += tval.to_string().as_str().trim();
}
}
}
}
@@ -93,6 +131,8 @@ pub fn tokenize(code: String, root: &mut Transpiler) -> Vec<Token> {
pub fn parse(code: String, vars: &mut Variables, root: &mut Transpiler) -> Vec<Ast> {
let mut ast: Vec<Ast> = 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;