added table functions, fixed a bug with var update
This commit is contained in:
parent
d22014a1e0
commit
c9a626cdab
3 changed files with 72 additions and 12 deletions
|
@ -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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue