added table get

This commit is contained in:
afonya2 2025-06-03 19:51:19 +02:00
parent 4faabe0d9c
commit 9b0335e8d6
Signed by: afonya
GPG key ID: EBB9C4CAFAAFB2DC
5 changed files with 54 additions and 4 deletions

View file

@ -21,6 +21,7 @@ pub enum ASTPart {
Continue(AstContinue),
Return(AstReturn),
Table(AstTable),
TableGet(AstTableGet),
NOOP
}
#[derive(Debug, Clone, PartialEq)]
@ -133,6 +134,12 @@ pub struct TableValue {
pub value: ASTPart,
pub pos: usize
}
#[derive(Debug, Clone, PartialEq)]
pub struct AstTableGet {
pub table: Box<ASTPart>,
pub key: Box<ASTPart>,
pub pos: usize
}
fn is_end(input: &Token, end: &Vec<Token>) -> bool {
for token in end {
@ -207,6 +214,9 @@ fn shunt(input: Vec<ASTPart>) -> ASTPart {
ASTPart::Table(_) => {
output.push(part);
},
ASTPart::TableGet(_) => {
output.push(part);
},
ASTPart::Operation(op) => {
if *op.left != ASTPart::NOOP && *op.right != ASTPart::NOOP {
output.push(part);
@ -418,6 +428,27 @@ fn read_exp(pos: &mut usize, input: &Vec<Token>, ends: &Vec<Token>, parse_ends:
if next_token.typ == TokenType::SEPARATOR && next_token.value == "(" {
let var = ASTPart::VarRead(AstVarRead { variable: token.value.clone(), pos: token.pos });
expressions.push(read_call(var, pos, input));
} else if next_token.typ == TokenType::SEPARATOR && next_token.value == "[" {
let var_pos = token.pos;
let key_ends: Vec<Token> = vec![
Token { typ: TokenType::SEPARATOR, value: String::from("]"), pos: 0 }
];
*pos += 1;
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;
expressions.push(ASTPart::TableGet(AstTableGet { table: Box::new(ASTPart::VarRead(AstVarRead { variable: token.value.clone(), pos: var_pos })), key: Box::new(keyy), pos: var_pos+1 }));
} else {
expressions.push(ASTPart::VarRead(AstVarRead { variable: token.value.clone(), pos: token.pos }));
}