added table functions, fixed a bug with var update

This commit is contained in:
afonya 2025-06-16 16:52:58 +02:00
parent d22014a1e0
commit c9a626cdab
Signed by: afonya
GPG key ID: EBB9C4CAFAAFB2DC
3 changed files with 72 additions and 12 deletions

View file

@ -556,12 +556,12 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec<Operation>, va
print_error(&err, &ctx);
process::exit(1);
}
let value_reg = do_ast_op(*upd.value, op_count, ops, variables, next_var_id, strings, next_string_id, functions, next_function_id, registers, ctx, traceback);
let reg = get_register_by_variable(registers, get_variable_by_name(variables, &upd.variable, ops.len(), traceback).expect("Variable should exist").clone());
if reg.id != 0 {
ops.push(Operation { opcode: 8, arg1: Some(reg.id), arg2: None, arg3: None, pos: upd.pos as u32 });
set_register(registers, RegisterState { id: reg.id, used: false, variable: 0, last_used: 0 });
}
let value_reg = do_ast_op(*upd.value, op_count, ops, variables, next_var_id, strings, next_string_id, functions, next_function_id, registers, ctx, traceback);
ops.push(Operation { opcode: 7, arg1: Some(value_reg), arg2: Some(get_variable_by_name(variables, &upd.variable, ops.len(), traceback).expect("Variable should exist").id as i64), arg3: None, pos: upd.pos as u32 });
set_register(registers, RegisterState { id: value_reg, used: true, variable: get_variable_by_name(variables, &upd.variable, ops.len(), traceback).expect("Variable should exist").id, last_used: *op_count });
garbage_collect_registers(registers);

View file

@ -477,6 +477,60 @@ fn szaft_betuve(machine: &mut Machine, op: &DecompiledOperation, args: Vec<VMMem
return VMMemory::String(VMMemoryString { value: str, variable_id: 0 })
}
fn tabla_hozzaad(machine: &mut Machine, op: &DecompiledOperation, args: Vec<VMMemory>) -> VMMemory {
arg_expect(&args, 0, "table", machine, op);
let mut tbl = match &args[0] {
VMMemory::Table(tbl) => tbl.clone(),
_ => {
error(String::from("Expected a table"), machine, op);
return VMMemory::Null(VMMemoryNull { variable_id: 0 });
}
};
if args.len() < 2 {
error(String::from("Expected at least 2 arguments"), machine, op);
return VMMemory::Null(VMMemoryNull { variable_id: 0 });
}
let val = &args[1];
let mut next_index = 0;
for kv in &tbl.values {
if let VMMemory::Number(n) = &kv.key {
if n.value as usize > next_index {
next_index = (n.value as usize) + 1;
}
}
}
tbl.values.push(TableValue { key: VMMemory::Number(VMMemoryNumber { value: next_index as i64, variable_id: 0 }), value: val.clone() });
return VMMemory::Table(VMMemoryTable { values: tbl.values.clone(), variable_id: 0 });
}
fn tabla_torol(machine: &mut Machine, op: &DecompiledOperation, args: Vec<VMMemory>) -> VMMemory {
arg_expect(&args, 0, "table", machine, op);
arg_expect(&args, 1, "number", machine, op);
let mut tbl = match &args[0] {
VMMemory::Table(tbl) => tbl.clone(),
_ => {
error(String::from("Expected a table"), machine, op);
return VMMemory::Null(VMMemoryNull { variable_id: 0 });
}
};
let num = match &args[1] {
VMMemory::Number(n) => n.value as usize,
_ => {
error(String::from("Expected a number"), machine, op);
return VMMemory::Null(VMMemoryNull { variable_id: 0 });
}
};
for i in 0..tbl.values.len() {
let kv = &tbl.values[i];
if let VMMemory::Number(n) = &kv.key {
if n.value as usize == num {
tbl.values.remove(i);
break;
}
}
}
return VMMemory::Table(VMMemoryTable { values: tbl.values.clone(), variable_id: 0 });
}
pub fn generate() -> HashMap<String, VMMemory> {
let mut mem: HashMap<String, VMMemory> = HashMap::new();
@ -545,5 +599,17 @@ pub fn generate() -> HashMap<String, VMMemory> {
];
mem.insert(String::from("szaft"), VMMemory::Table(VMMemoryTable { values: szaft, variable_id: 0 }));
let table: Vec<TableValue> = vec![
TableValue {
key: VMMemory::String(VMMemoryString { value: String::from("hozzáad"), variable_id: 0 }),
value: VMMemory::NativeFunction(VMMemoryNativeFunction { func: tabla_hozzaad, variable_id: 0 }),
},
TableValue {
key: VMMemory::String(VMMemoryString { value: String::from("töröl"), variable_id: 0 }),
value: VMMemory::NativeFunction(VMMemoryNativeFunction { func: tabla_torol, variable_id: 0 }),
}
];
mem.insert(String::from("tábla"), VMMemory::Table(VMMemoryTable { values: table, variable_id: 0 }));
return mem;
}

View file

@ -1,11 +1,5 @@
ugass(szaft.csemerd fel(szaft"test"szaft))
ugass(szaft.csemerd le(szaft"TesT"szaft))
gethelj str = szaft"test"szaft
ugass(szaft.hossz(str))
ugass(szaft.ismételd(str, 3))
ugass(szaft.uno reverse(str))
ugass(szaft.darabos(str, 0, 1))
ugass(szaft.keres(str, szaft"st"szaft))
ugass(szaft.átrak(str, szaft"st"szaft, szaft"bemb"szaft))
ugass(szaft.számmá(str, 0))
ugass(szaft.betűvé(116))
gethelj a = {1,2}
a = tábla.hozzáad(a, 1)
ugass(a)
a = tábla.töröl(a, 0)
ugass(a)