This commit is contained in:
2024-04-02 23:54:40 +02:00
parent 55fe45717c
commit 65c9856762
3 changed files with 23 additions and 10 deletions
+3 -3
View File
@@ -20,12 +20,12 @@ pub enum TokenType {
#[derive(Clone)]
pub struct Token {
pub token_type: TokenType,
pub token_value: Vec<String>
pub token_values: Vec<String>
}
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<Token> {
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 {
+6 -5
View File
@@ -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;
}
}
+14 -2
View File
@@ -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<Token>) -> Box<Vec<Ast>> {
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<Ast> = Vec::new();
@@ -62,8 +68,14 @@ pub fn parse(tokens: &mut Vec<Token>) -> Box<Vec<Ast>> {
is_match = true;
} else {
is_match = false;
break;
}
}
AstDef::Else => {
matched.push(tokens[i].clone());
tokens.drain(0..1);
is_match = true;
}
_ => {}
}
}