add null, prepare for function

This commit is contained in:
afonya2 2025-05-23 21:07:43 +02:00
parent b56bb9a48c
commit 315ccc1504
Signed by: afonya
GPG key ID: EBB9C4CAFAAFB2DC
2 changed files with 34 additions and 8 deletions

View file

@ -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" {
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::KEYWORD {
if token.value == "piszv" {
expressions.push(ASTPart::Boolean(AstBool { value: true, pos: token.pos }));
} 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 });