added table set

This commit is contained in:
afonya2 2025-06-04 18:52:59 +02:00
parent 7276312961
commit 9a915cd274
Signed by: afonya
GPG key ID: EBB9C4CAFAAFB2DC
4 changed files with 52 additions and 5 deletions

View file

@ -22,6 +22,7 @@ pub enum ASTPart {
Return(AstReturn),
Table(AstTable),
TableGet(AstTableGet),
TableSet(AstTableSet),
NOOP
}
#[derive(Debug, Clone, PartialEq)]
@ -140,6 +141,13 @@ pub struct AstTableGet {
pub key: Box<ASTPart>,
pub pos: usize
}
#[derive(Debug, Clone, PartialEq)]
pub struct AstTableSet {
pub table: Box<ASTPart>,
pub key: Box<ASTPart>,
pub value: Box<ASTPart>,
pub pos: usize
}
fn is_end(input: &Token, end: &Vec<Token>) -> bool {
for token in end {
@ -629,6 +637,31 @@ fn next_operation(pos: &mut usize, input: &Vec<Token>, op_ends: &Vec<Token>, par
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 key_ends: Vec<Token> = vec![
Token { typ: TokenType::SEPARATOR, value: String::from("]"), pos: 0 }
];
let keyy = read_exp(pos, input, &key_ends, &key_ends);
match keyy {
ASTPart::Table(_) => {
panic!("Table keys cannot be tables at {}", input[*pos].pos);
},
ASTPart::Function(_) => {
panic!("Table keys cannot be functions at {}", input[*pos].pos);
},
_ => {}
}
if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != "]" {
panic!("Unexpected end of key at {}", input[*pos].pos);
}
*pos += 1;
if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != "=" {
panic!("Expected = after key at {}", input[*pos].pos);
}
*pos += 1;
let value = read_exp(pos, input, op_ends, parse_ends);
return ASTPart::TableSet(AstTableSet { table: Box::new(ASTPart::VarRead(AstVarRead { variable: token.value.clone(), pos: token.pos })), key: Box::new(keyy), value: Box::new(value), pos: token.pos });
} else if next_token.typ == TokenType::SEPARATOR && next_token.value == "=" {
*pos += 1;
let value = read_exp(pos, input, op_ends, parse_ends);