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

@ -540,7 +540,13 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec<Operation>, va
ops.push(Operation { opcode: 30, arg1: Some(table_reg), arg2: Some(key_reg as i64), arg3: Some(reg.register) });
set_register(registers, RegisterState { id: reg.register, used: true, variable: 0, last_used: *op_count });
return reg.register;
}
},
ASTPart::TableSet(tbl_set) => {
let table_reg = do_ast_op(*tbl_set.table, op_count, ops, variables, next_var_id, strings, next_string_id, functions, next_function_id, registers);
let key_reg = do_ast_op(*tbl_set.key, op_count, ops, variables, next_var_id, strings, next_string_id, functions, next_function_id, registers);
let value_reg = do_ast_op(*tbl_set.value, op_count, ops, variables, next_var_id, strings, next_string_id, functions, next_function_id, registers);
ops.push(Operation { opcode: 31, arg1: Some(table_reg), arg2: Some(key_reg as i64), arg3: Some(value_reg) });
},
_ => {}
}
return 0;
@ -578,6 +584,7 @@ fn compile_function(ast: Vec<ASTPart>, args: Option<Vec<String>>, registers: &mu
var.end = ops.len() - 1;
}
}
println!("Compiled operations: {:?}", ops);
return Compiled {
operations: ops,

View file

@ -121,6 +121,15 @@ fn log_ast_part(part: &ASTPart, prefix: String) {
println!("{} Key:", prefix);
log_ast_part(&tbl_get.key, format!("{} ", prefix));
},
ASTPart::TableSet(tbl_set) => {
println!("{}{}: Table Set:", prefix, tbl_set.pos);
println!("{} Table:", prefix);
log_ast_part(&tbl_set.table, format!("{} ", prefix));
println!("{} Key:", prefix);
log_ast_part(&tbl_set.key, format!("{} ", prefix));
println!("{} Value:", prefix);
log_ast_part(&tbl_set.value, format!("{} ", prefix));
},
ASTPart::NOOP => println!("{}NOOP", prefix)
}
}

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);

View file

@ -1,4 +1,2 @@
gethelj a = lőcsve(m) {
gethelj b = m
}
a(69)
gethelj a = {1,2,3}
a[0] = 4