added booleans, variable updates and function execution
This commit is contained in:
parent
28c440539d
commit
b074f18648
4 changed files with 51 additions and 3 deletions
|
@ -124,7 +124,7 @@ fn read_identifier(splitted: &Vec<&str>, pos: &mut usize, out: &mut Vec<Token>)
|
||||||
identifier += char;
|
identifier += char;
|
||||||
}
|
}
|
||||||
//Lets break it up to tokens
|
//Lets break it up to tokens
|
||||||
let words: Vec<&str> = identifier.split(" ").collect();
|
let words: Vec<&str> = identifier.trim().split(" ").collect();
|
||||||
let mut state: Vec<&str> = vec![];
|
let mut state: Vec<&str> = vec![];
|
||||||
let mut state_pos: usize = 0;
|
let mut state_pos: usize = 0;
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
|
@ -159,7 +159,9 @@ fn read_identifier(splitted: &Vec<&str>, pos: &mut usize, out: &mut Vec<Token>)
|
||||||
}
|
}
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
pre_out.push(Token { typ: TokenType::IDENTIFIER, value: state.join(" "), pos: state_pos });
|
if state_pos > 0 {
|
||||||
|
pre_out.push(Token { typ: TokenType::IDENTIFIER, value: state.join(" "), pos: state_pos });
|
||||||
|
}
|
||||||
//Check for invalid keywords!
|
//Check for invalid keywords!
|
||||||
for token in &mut pre_out {
|
for token in &mut pre_out {
|
||||||
if token.typ == TokenType::KEYWORD {
|
if token.typ == TokenType::KEYWORD {
|
||||||
|
|
|
@ -9,11 +9,17 @@ fn log_ast_part(part: &ASTPart, prefix: String) {
|
||||||
match part {
|
match part {
|
||||||
ASTPart::Number(number) => println!("{}{}: Number: {}", prefix, number.pos, number.value),
|
ASTPart::Number(number) => println!("{}{}: Number: {}", prefix, number.pos, number.value),
|
||||||
ASTPart::String(str) => println!("{}{}: String: {}", prefix, str.pos, str.value),
|
ASTPart::String(str) => println!("{}{}: String: {}", prefix, str.pos, str.value),
|
||||||
|
ASTPart::Boolean(bool) => println!("{}{}: Boolean: {}", prefix, bool.pos, bool.value),
|
||||||
ASTPart::Assigment(assigment) => {
|
ASTPart::Assigment(assigment) => {
|
||||||
println!("{}{}: Assigment: {}", prefix, assigment.pos, assigment.variable);
|
println!("{}{}: Assigment: {}", prefix, assigment.pos, assigment.variable);
|
||||||
println!("{} Value:", prefix);
|
println!("{} Value:", prefix);
|
||||||
log_ast_part(&assigment.value, format!("{} ", prefix));
|
log_ast_part(&assigment.value, format!("{} ", prefix));
|
||||||
},
|
},
|
||||||
|
ASTPart::VarUpdate(v_update) => {
|
||||||
|
println!("{}{}: Update: {}", prefix, v_update.pos, v_update.variable);
|
||||||
|
println!("{} Value:", prefix);
|
||||||
|
log_ast_part(&v_update.value, format!("{} ", prefix));
|
||||||
|
},
|
||||||
ASTPart::Operation(operation) => {
|
ASTPart::Operation(operation) => {
|
||||||
println!("{}{}: Operation: {}", prefix, operation.pos, operation.operator);
|
println!("{}{}: Operation: {}", prefix, operation.pos, operation.operator);
|
||||||
println!("{} Left:", prefix);
|
println!("{} Left:", prefix);
|
||||||
|
|
|
@ -6,10 +6,12 @@ use crate::lexer::{Token, TokenType};
|
||||||
pub enum ASTPart {
|
pub enum ASTPart {
|
||||||
String(AstString),
|
String(AstString),
|
||||||
Number(AstNumber),
|
Number(AstNumber),
|
||||||
|
Boolean(AstBool),
|
||||||
Assigment(AstAssigment),
|
Assigment(AstAssigment),
|
||||||
Operation(AstOperation),
|
Operation(AstOperation),
|
||||||
VarRead(AstVarRead),
|
VarRead(AstVarRead),
|
||||||
Call(AstCall),
|
Call(AstCall),
|
||||||
|
VarUpdate(AstVarUpdate),
|
||||||
NOOP
|
NOOP
|
||||||
}
|
}
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
@ -23,6 +25,11 @@ pub struct AstNumber {
|
||||||
pub pos: usize
|
pub pos: usize
|
||||||
}
|
}
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub struct AstBool {
|
||||||
|
pub value: bool,
|
||||||
|
pub pos: usize
|
||||||
|
}
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct AstAssigment {
|
pub struct AstAssigment {
|
||||||
pub variable: String,
|
pub variable: String,
|
||||||
pub value: Box<ASTPart>,
|
pub value: Box<ASTPart>,
|
||||||
|
@ -46,6 +53,12 @@ pub struct AstCall {
|
||||||
pub args: Vec<ASTPart>,
|
pub args: Vec<ASTPart>,
|
||||||
pub pos: usize
|
pub pos: usize
|
||||||
}
|
}
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub struct AstVarUpdate {
|
||||||
|
pub variable: String,
|
||||||
|
pub value: Box<ASTPart>,
|
||||||
|
pub pos: usize
|
||||||
|
}
|
||||||
|
|
||||||
fn is_end(input: &Token, end: &Vec<Token>) -> bool {
|
fn is_end(input: &Token, end: &Vec<Token>) -> bool {
|
||||||
for token in end {
|
for token in end {
|
||||||
|
@ -107,6 +120,9 @@ fn shunt(input: Vec<ASTPart>) -> ASTPart {
|
||||||
ASTPart::VarRead(_) => {
|
ASTPart::VarRead(_) => {
|
||||||
output.push(part);
|
output.push(part);
|
||||||
},
|
},
|
||||||
|
ASTPart::Boolean(_) => {
|
||||||
|
output.push(part);
|
||||||
|
},
|
||||||
ASTPart::Operation(op) => {
|
ASTPart::Operation(op) => {
|
||||||
if *op.left != ASTPart::NOOP && *op.right != ASTPart::NOOP {
|
if *op.left != ASTPart::NOOP && *op.right != ASTPart::NOOP {
|
||||||
output.push(part);
|
output.push(part);
|
||||||
|
@ -186,6 +202,10 @@ 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 }));
|
expressions.push(ASTPart::String(AstString { value: token.value.clone(), pos: token.pos }));
|
||||||
} else if token.typ == TokenType::NUMBER {
|
} else if token.typ == TokenType::NUMBER {
|
||||||
expressions.push(ASTPart::Number(AstNumber { value: token.value.parse().unwrap(), pos: token.pos }));
|
expressions.push(ASTPart::Number(AstNumber { value: token.value.parse().unwrap(), pos: token.pos }));
|
||||||
|
} else if token.typ == TokenType::KEYWORD && token.value == "piszv" {
|
||||||
|
expressions.push(ASTPart::Boolean(AstBool { value: true, pos: token.pos }));
|
||||||
|
} else if token.typ == TokenType::KEYWORD && token.value == "nem piszv" {
|
||||||
|
expressions.push(ASTPart::Boolean(AstBool { value: false, pos: token.pos }));
|
||||||
} else if token.typ == TokenType::IDENTIFIER {
|
} else if token.typ == TokenType::IDENTIFIER {
|
||||||
if next_token.typ == TokenType::SEPARATOR && next_token.value == "(" {
|
if next_token.typ == TokenType::SEPARATOR && next_token.value == "(" {
|
||||||
let var = ASTPart::VarRead(AstVarRead { variable: token.value.clone(), pos: token.pos });
|
let var = ASTPart::VarRead(AstVarRead { variable: token.value.clone(), pos: token.pos });
|
||||||
|
@ -221,6 +241,14 @@ fn read_exp(pos: &mut usize, input: &Vec<Token>, ends: &Vec<Token>, parse_ends:
|
||||||
|
|
||||||
fn next_operation(pos: &mut usize, input: &Vec<Token>, op_ends: &Vec<Token>, parse_ends: &Vec<Token>) -> ASTPart {
|
fn next_operation(pos: &mut usize, input: &Vec<Token>, op_ends: &Vec<Token>, parse_ends: &Vec<Token>) -> ASTPart {
|
||||||
let token = &input[*pos];
|
let token = &input[*pos];
|
||||||
|
let mut next_token = &Token {
|
||||||
|
typ: TokenType::OPEND,
|
||||||
|
value: String::from("END"),
|
||||||
|
pos: 0
|
||||||
|
};
|
||||||
|
if *pos+1 < input.len() {
|
||||||
|
next_token = &input[*pos+1]
|
||||||
|
}
|
||||||
if is_end(token, &parse_ends) {
|
if is_end(token, &parse_ends) {
|
||||||
return ASTPart::NOOP;
|
return ASTPart::NOOP;
|
||||||
}
|
}
|
||||||
|
@ -244,6 +272,17 @@ fn next_operation(pos: &mut usize, input: &Vec<Token>, op_ends: &Vec<Token>, par
|
||||||
} else {
|
} else {
|
||||||
panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos);
|
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 });
|
||||||
|
return read_call(var, pos, input);
|
||||||
|
} else if next_token.typ == TokenType::SEPARATOR && next_token.value == "=" {
|
||||||
|
*pos += 1;
|
||||||
|
let value = read_exp(pos, input, op_ends, parse_ends);
|
||||||
|
return ASTPart::VarUpdate(AstVarUpdate { variable: token.value.clone(), value: Box::new(value), pos: token.pos });
|
||||||
|
} else {
|
||||||
|
panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos);
|
panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos);
|
||||||
}
|
}
|
||||||
|
|
3
test.as
3
test.as
|
@ -1,3 +1,4 @@
|
||||||
gethelj a = 1
|
gethelj a = 1
|
||||||
gethelj b = a + 2
|
gethelj b = a + 2
|
||||||
gethelj c = ugass(a,b)
|
ugass(a,b)
|
||||||
|
gethelj c = piszv & nem piszv
|
Loading…
Add table
Add a link
Reference in a new issue