diff --git a/src/compiler.rs b/src/compiler.rs index 74e9ac1..6c3463c 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -556,12 +556,12 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, 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); diff --git a/src/enviroment.rs b/src/enviroment.rs index f154b8f..5c71d6a 100644 --- a/src/enviroment.rs +++ b/src/enviroment.rs @@ -477,6 +477,60 @@ fn szaft_betuve(machine: &mut Machine, op: &DecompiledOperation, args: Vec) -> 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 { + 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 { let mut mem: HashMap = HashMap::new(); @@ -545,5 +599,17 @@ pub fn generate() -> HashMap { ]; mem.insert(String::from("szaft"), VMMemory::Table(VMMemoryTable { values: szaft, variable_id: 0 })); + let table: Vec = 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; } \ No newline at end of file diff --git a/test.asl b/test.asl index 9715387..cbe0f58 100644 --- a/test.asl +++ b/test.asl @@ -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)) \ No newline at end of file +gethelj a = {1,2} +a = tábla.hozzáad(a, 1) +ugass(a) +a = tábla.töröl(a, 0) +ugass(a) \ No newline at end of file