add null, prepare for function
This commit is contained in:
parent
b56bb9a48c
commit
315ccc1504
2 changed files with 34 additions and 8 deletions
10
src/lexer.rs
10
src/lexer.rs
|
@ -40,11 +40,11 @@ fn is_sep(char: &str) -> bool {
|
|||
let chars = vec!["(",")","[","]","{","}",",",".","="];
|
||||
return chars.contains(&char);
|
||||
}
|
||||
fn is_mul_sep(char: &str, next_char: &str) -> bool {
|
||||
/*fn is_mul_sep(char: &str, next_char: &str) -> bool {
|
||||
let chars = vec!["=>"];
|
||||
let check = String::from(char) + next_char;
|
||||
return chars.contains(&check.as_str());
|
||||
}
|
||||
}*/
|
||||
|
||||
fn read_string(splitted: &Vec<&str>, pos: &mut usize, out: &mut Vec<Token>) {
|
||||
let mut str = String::from("");
|
||||
|
@ -100,7 +100,7 @@ fn generate_combinations(words: Vec<&str>) -> Vec<String> {
|
|||
return result;
|
||||
}
|
||||
fn read_identifier(splitted: &Vec<&str>, pos: &mut usize, out: &mut Vec<Token>) {
|
||||
let keywords = vec!["kraf","piszolj","ha nem geny akkor geny","ha nem geny","nem piszv","kopva","gethelj","ha geny","jukadban","lőcsve","nem reti","csecs","megint","reti","piszv","amíg geny"];
|
||||
let keywords = vec!["kraf","piszolj","ha nem geny akkor geny","ha nem geny","nem piszv","kopva","gethelj","ha geny","jukadban","lőcsve","nem reti","csecs","megint","reti","piszv","amíg geny","nincs hám"];
|
||||
let mut raw_keywords: Vec<String> = vec![];
|
||||
for keyword in &keywords {
|
||||
let spi: Vec<&str> = keyword.split(" ").collect();
|
||||
|
@ -239,10 +239,12 @@ pub fn lex(input: String) -> Vec<Token> {
|
|||
read_comment(&splitted, &mut pos, true);
|
||||
} else if is_mul_operator(char, splitted[pos]) {
|
||||
out.push(Token { typ: TokenType::OPERATOR, value: String::from(char) + splitted[pos], pos: pos-1 });
|
||||
pos += 1;
|
||||
} else if is_operator(char) {
|
||||
out.push(Token { typ: TokenType::OPERATOR, value: String::from(char), pos: pos-1 });
|
||||
} else if is_mul_sep(char, splitted[pos]) {
|
||||
/*} else if is_mul_sep(char, splitted[pos]) {
|
||||
out.push(Token { typ: TokenType::SEPARATOR, value: String::from(char) + splitted[pos], pos: pos-1 });
|
||||
pos += 1;*/
|
||||
} else if is_sep(char) {
|
||||
out.push(Token { typ: TokenType::SEPARATOR, value: String::from(char), pos: pos-1 });
|
||||
} else {
|
||||
|
|
|
@ -7,11 +7,13 @@ pub enum ASTPart {
|
|||
String(AstString),
|
||||
Number(AstNumber),
|
||||
Boolean(AstBool),
|
||||
Null(AstNull),
|
||||
Assigment(AstAssigment),
|
||||
Operation(AstOperation),
|
||||
VarRead(AstVarRead),
|
||||
Call(AstCall),
|
||||
VarUpdate(AstVarUpdate),
|
||||
Function(AstFunction),
|
||||
NOOP
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
|
@ -30,6 +32,10 @@ pub struct AstBool {
|
|||
pub pos: usize
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct AstNull {
|
||||
pub pos: usize
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct AstAssigment {
|
||||
pub variable: String,
|
||||
pub value: Box<ASTPart>,
|
||||
|
@ -59,6 +65,13 @@ pub struct AstVarUpdate {
|
|||
pub value: Box<ASTPart>,
|
||||
pub pos: usize
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct AstFunction {
|
||||
pub name: String,
|
||||
pub args: Vec<String>,
|
||||
pub body: Vec<ASTPart>,
|
||||
pub pos: usize
|
||||
}
|
||||
|
||||
fn is_end(input: &Token, end: &Vec<Token>) -> bool {
|
||||
for token in end {
|
||||
|
@ -124,6 +137,9 @@ fn shunt(input: Vec<ASTPart>) -> ASTPart {
|
|||
ASTPart::Boolean(_) => {
|
||||
output.push(part);
|
||||
},
|
||||
ASTPart::Null(_) => {
|
||||
output.push(part);
|
||||
},
|
||||
ASTPart::Operation(op) => {
|
||||
if *op.left != ASTPart::NOOP && *op.right != ASTPart::NOOP {
|
||||
output.push(part);
|
||||
|
@ -203,10 +219,18 @@ fn read_exp(pos: &mut usize, input: &Vec<Token>, ends: &Vec<Token>, parse_ends:
|
|||
expressions.push(ASTPart::String(AstString { value: token.value.clone(), pos: token.pos }));
|
||||
} else if token.typ == TokenType::NUMBER {
|
||||
expressions.push(ASTPart::Number(AstNumber { value: token.value.parse().unwrap(), pos: token.pos }));
|
||||
} else if token.typ == TokenType::KEYWORD && token.value == "piszv" {
|
||||
} else if token.typ == TokenType::KEYWORD {
|
||||
if token.value == "piszv" {
|
||||
expressions.push(ASTPart::Boolean(AstBool { value: true, pos: token.pos }));
|
||||
} else if token.typ == TokenType::KEYWORD && token.value == "nem piszv" {
|
||||
} else if token.value == "nem piszv" {
|
||||
expressions.push(ASTPart::Boolean(AstBool { value: false, pos: token.pos }));
|
||||
} else if token.value == "nincs hám" {
|
||||
expressions.push(ASTPart::Null(AstNull { pos: token.pos }));
|
||||
} else if token.value == "lőcsve" {
|
||||
panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos);
|
||||
} else {
|
||||
panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos);
|
||||
}
|
||||
} else if token.typ == TokenType::IDENTIFIER {
|
||||
if next_token.typ == TokenType::SEPARATOR && next_token.value == "(" {
|
||||
let var = ASTPart::VarRead(AstVarRead { variable: token.value.clone(), pos: token.pos });
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue