added some more enviroment functions (table, filesystem)
This commit is contained in:
parent
10eef39f12
commit
b35f52d02e
2 changed files with 97 additions and 2 deletions
|
@ -1,4 +1,4 @@
|
||||||
use std::{collections::HashMap, io::{BufReader, Read, Write}, net::{self, TcpListener, TcpStream}, process, thread::sleep, time::Duration, vec};
|
use std::{collections::HashMap, fs, io::{BufReader, Read, Write}, net::{self, TcpListener, TcpStream}, process, thread::sleep, time::{Duration, UNIX_EPOCH}, vec};
|
||||||
|
|
||||||
use crate::{decompiler::DecompiledOperation, errors::{create_error, print_error, ErrorSubType, ErrorType}, virtualmachine::{Machine, TableValue, VMMemory, VMMemoryBoolean, VMMemoryNativeFunction, VMMemoryNull, VMMemoryNumber, VMMemoryString, VMMemoryTable}};
|
use crate::{decompiler::DecompiledOperation, errors::{create_error, print_error, ErrorSubType, ErrorType}, virtualmachine::{Machine, TableValue, VMMemory, VMMemoryBoolean, VMMemoryNativeFunction, VMMemoryNull, VMMemoryNumber, VMMemoryString, VMMemoryTable}};
|
||||||
|
|
||||||
|
@ -739,6 +739,35 @@ fn tabla_kulcsok(machine: &mut Machine, op: &DecompiledOperation, args: Vec<VMMe
|
||||||
}
|
}
|
||||||
return VMMemory::Table(keys);
|
return VMMemory::Table(keys);
|
||||||
}
|
}
|
||||||
|
fn tabla_ertekek(machine: &mut Machine, op: &DecompiledOperation, args: Vec<VMMemory>) -> VMMemory {
|
||||||
|
arg_expect(&args, 0, "table", machine, op);
|
||||||
|
let 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 mut vals = VMMemoryTable {
|
||||||
|
values: vec![],
|
||||||
|
variable_id: 0,
|
||||||
|
};
|
||||||
|
for kv in &tbl.values {
|
||||||
|
vals.values.push(TableValue { key: VMMemory::Number(VMMemoryNumber { value: vals.values.len() as f64, variable_id: 0 }), value: kv.value.clone() });
|
||||||
|
}
|
||||||
|
return VMMemory::Table(vals);
|
||||||
|
}
|
||||||
|
fn tabla_hossz(machine: &mut Machine, op: &DecompiledOperation, args: Vec<VMMemory>) -> VMMemory {
|
||||||
|
arg_expect(&args, 0, "table", machine, op);
|
||||||
|
let tbl = match &args[0] {
|
||||||
|
VMMemory::Table(tbl) => tbl.clone(),
|
||||||
|
_ => {
|
||||||
|
error(String::from("Expected a table"), machine, op);
|
||||||
|
return VMMemory::Null(VMMemoryNull { variable_id: 0 });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return VMMemory::Number(VMMemoryNumber { value: tbl.values.len() as f64, variable_id: 0 });
|
||||||
|
}
|
||||||
|
|
||||||
fn szenvedes_vege(machine: &mut Machine, op: &DecompiledOperation, args: Vec<VMMemory>) -> VMMemory {
|
fn szenvedes_vege(machine: &mut Machine, op: &DecompiledOperation, args: Vec<VMMemory>) -> VMMemory {
|
||||||
arg_expect(&args, 0, "number", machine, op);
|
arg_expect(&args, 0, "number", machine, op);
|
||||||
|
@ -752,6 +781,64 @@ fn szenvedes_vege(machine: &mut Machine, op: &DecompiledOperation, args: Vec<VMM
|
||||||
process::exit(num as i32);
|
process::exit(num as i32);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn intezo_letezik(machine: &mut Machine, op: &DecompiledOperation, args: Vec<VMMemory>) -> VMMemory {
|
||||||
|
arg_expect(&args, 0, "string", machine, op);
|
||||||
|
let str = match &args[0] {
|
||||||
|
VMMemory::String(s) => s.value.clone(),
|
||||||
|
_ => String::new()
|
||||||
|
};
|
||||||
|
let res = match fs::exists(str) {
|
||||||
|
Ok(exists) => exists,
|
||||||
|
Err(e) => {
|
||||||
|
error(format!("Failed to check if file exists: {}", e), machine, op);
|
||||||
|
false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return VMMemory::Boolean(VMMemoryBoolean { value: res, variable_id: 0 });
|
||||||
|
}
|
||||||
|
fn intezo_info(machine: &mut Machine, op: &DecompiledOperation, args: Vec<VMMemory>) -> VMMemory {
|
||||||
|
arg_expect(&args, 0, "string", machine, op);
|
||||||
|
let str = match &args[0] {
|
||||||
|
VMMemory::String(s) => s.value.clone(),
|
||||||
|
_ => String::new()
|
||||||
|
};
|
||||||
|
let res = match fs::metadata(str) {
|
||||||
|
Ok(exists) => exists,
|
||||||
|
Err(e) => {
|
||||||
|
error(format!("Failed to check if file exists: {}", e), machine, op);
|
||||||
|
panic!()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let modf = match res.modified() {
|
||||||
|
Ok(time) => time.duration_since(UNIX_EPOCH).unwrap().as_millis() as f64,
|
||||||
|
Err(e) => {
|
||||||
|
error(format!("Failed to get file modification time: {}", e), machine, op);
|
||||||
|
0.0
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let creatf = match res.created() {
|
||||||
|
Ok(time) => time.duration_since(UNIX_EPOCH).unwrap().as_millis() as f64,
|
||||||
|
Err(e) => {
|
||||||
|
error(format!("Failed to get file modification time: {}", e), machine, op);
|
||||||
|
0.0
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let mut to_tbl: HashMap<String, VMMemory> = HashMap::new();
|
||||||
|
to_tbl.insert(String::from("mappa-e"), VMMemory::Boolean(VMMemoryBoolean { value: res.is_dir(), variable_id: 0 }));
|
||||||
|
to_tbl.insert(String::from("fájl-e"), VMMemory::Boolean(VMMemoryBoolean { value: res.is_file(), variable_id: 0 }));
|
||||||
|
to_tbl.insert(String::from("szerkesztve"), VMMemory::Number(VMMemoryNumber { value: modf, variable_id: 0 }));
|
||||||
|
to_tbl.insert(String::from("létrehozva"), VMMemory::Number(VMMemoryNumber { value: creatf, variable_id: 0 }));
|
||||||
|
to_tbl.insert(String::from("hossz"), VMMemory::Number(VMMemoryNumber { value: res.len() as f64, variable_id: 0 }));
|
||||||
|
let mut res_tbl = VMMemoryTable {
|
||||||
|
values: vec![],
|
||||||
|
variable_id: 0,
|
||||||
|
};
|
||||||
|
for (k, v) in to_tbl {
|
||||||
|
set_mem_tbl_val(&mut res_tbl, VMMemory::String(VMMemoryString { value: k, variable_id: 0 }), v);
|
||||||
|
}
|
||||||
|
return VMMemory::Table(res_tbl);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn generate() -> HashMap<String, VMMemory> {
|
pub fn generate() -> HashMap<String, VMMemory> {
|
||||||
let mut mem: HashMap<String, VMMemory> = HashMap::new();
|
let mut mem: HashMap<String, VMMemory> = HashMap::new();
|
||||||
|
|
||||||
|
@ -865,6 +952,14 @@ pub fn generate() -> HashMap<String, VMMemory> {
|
||||||
TableValue {
|
TableValue {
|
||||||
key: VMMemory::String(VMMemoryString { value: String::from("kulcsok"), variable_id: 0 }),
|
key: VMMemory::String(VMMemoryString { value: String::from("kulcsok"), variable_id: 0 }),
|
||||||
value: VMMemory::NativeFunction(VMMemoryNativeFunction { func: tabla_kulcsok, variable_id: 0 }),
|
value: VMMemory::NativeFunction(VMMemoryNativeFunction { func: tabla_kulcsok, variable_id: 0 }),
|
||||||
|
},
|
||||||
|
TableValue {
|
||||||
|
key: VMMemory::String(VMMemoryString { value: String::from("értékek"), variable_id: 0 }),
|
||||||
|
value: VMMemory::NativeFunction(VMMemoryNativeFunction { func: tabla_ertekek, variable_id: 0 }),
|
||||||
|
},
|
||||||
|
TableValue {
|
||||||
|
key: VMMemory::String(VMMemoryString { value: String::from("hossz"), variable_id: 0 }),
|
||||||
|
value: VMMemory::NativeFunction(VMMemoryNativeFunction { func: tabla_hossz, variable_id: 0 }),
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
mem.insert(String::from("tábla"), VMMemory::Table(VMMemoryTable { values: table, variable_id: 0 }));
|
mem.insert(String::from("tábla"), VMMemory::Table(VMMemoryTable { values: table, variable_id: 0 }));
|
||||||
|
|
|
@ -10,7 +10,7 @@ mod virtualmachine;
|
||||||
mod errors;
|
mod errors;
|
||||||
mod decompiler;
|
mod decompiler;
|
||||||
|
|
||||||
const CLIVER: [u8; 3] = [0, 2, 0];
|
const CLIVER: [u8; 3] = [0, 3, 0];
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct Context {
|
struct Context {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue