added more enviroment functions, fixed a bug with function calls
This commit is contained in:
parent
00207f544a
commit
f817a83f1d
4 changed files with 182 additions and 26 deletions
|
@ -1,4 +1,4 @@
|
||||||
use std::{collections::HashMap, fs, io::{BufReader, Read, Write}, net::{self, TcpListener, TcpStream}, process, thread::sleep, time::{Duration, UNIX_EPOCH}, vec};
|
use std::{collections::HashMap, fs, io::{stdin, 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}};
|
||||||
|
|
||||||
|
@ -204,6 +204,27 @@ fn bimbabemb(machine: &mut Machine, op: &DecompiledOperation, args: Vec<VMMemory
|
||||||
};
|
};
|
||||||
return convert_type(&val, &target);
|
return convert_type(&val, &target);
|
||||||
}
|
}
|
||||||
|
fn mennyiazido(machine: &mut Machine, op: &DecompiledOperation, _args: Vec<VMMemory>) -> VMMemory {
|
||||||
|
let now = match UNIX_EPOCH.elapsed() {
|
||||||
|
Ok(duration) => duration.as_millis() as f64,
|
||||||
|
Err(e) => {
|
||||||
|
error(format!("Failed to get elapsed time: {}", e), machine, op);
|
||||||
|
0.0
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return VMMemory::Number(VMMemoryNumber { value: now, variable_id: 0 });
|
||||||
|
}
|
||||||
|
fn joink(machine: &mut Machine, op: &DecompiledOperation, _args: Vec<VMMemory>) -> VMMemory {
|
||||||
|
let mut str = String::new();
|
||||||
|
match stdin().read_line(&mut str) {
|
||||||
|
Err(e) => {
|
||||||
|
error(format!("Failed to read from stdin: {}", e), machine, op);
|
||||||
|
return VMMemory::Null(VMMemoryNull { variable_id: 0 });
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
};
|
||||||
|
return VMMemory::String(VMMemoryString { value: str, variable_id: 0 });
|
||||||
|
}
|
||||||
|
|
||||||
fn nerd_abs(machine: &mut Machine, op: &DecompiledOperation, args: Vec<VMMemory>) -> VMMemory {
|
fn nerd_abs(machine: &mut Machine, op: &DecompiledOperation, args: Vec<VMMemory>) -> VMMemory {
|
||||||
arg_expect(&args, 0, "number", machine, op);
|
arg_expect(&args, 0, "number", machine, op);
|
||||||
|
@ -805,8 +826,8 @@ fn intezo_info(machine: &mut Machine, op: &DecompiledOperation, args: Vec<VMMemo
|
||||||
let res = match fs::metadata(str) {
|
let res = match fs::metadata(str) {
|
||||||
Ok(exists) => exists,
|
Ok(exists) => exists,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error(format!("Failed to check if file exists: {}", e), machine, op);
|
error(format!("Failed to get file metadata: {}", e), machine, op);
|
||||||
panic!()
|
return VMMemory::Null(VMMemoryNull { variable_id: 0 });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let modf = match res.modified() {
|
let modf = match res.modified() {
|
||||||
|
@ -838,6 +859,113 @@ fn intezo_info(machine: &mut Machine, op: &DecompiledOperation, args: Vec<VMMemo
|
||||||
}
|
}
|
||||||
return VMMemory::Table(res_tbl);
|
return VMMemory::Table(res_tbl);
|
||||||
}
|
}
|
||||||
|
fn intezo_mappit(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()
|
||||||
|
};
|
||||||
|
match fs::create_dir_all(str) {
|
||||||
|
Err(e) => {
|
||||||
|
error(format!("Failed to create directory: {}", e), machine, op);
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
return VMMemory::Null(VMMemoryNull { variable_id: 0 });
|
||||||
|
}
|
||||||
|
fn intezo_mappattorol(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(_) => {
|
||||||
|
error(format!("Folder does not exist"), machine, op);
|
||||||
|
return VMMemory::Null(VMMemoryNull { variable_id: 0 });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if !res {
|
||||||
|
error(format!("Folder does not exist"), machine, op);
|
||||||
|
}
|
||||||
|
match fs::remove_dir_all(&str) {
|
||||||
|
Err(e) => {
|
||||||
|
error(format!("Failed to remove directory: {}", e), machine, op);
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
};
|
||||||
|
return VMMemory::Null(VMMemoryNull { variable_id: 0 });
|
||||||
|
}
|
||||||
|
fn intezo_fajlttorol(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(_) => {
|
||||||
|
error(format!("File does not exist"), machine, op);
|
||||||
|
return VMMemory::Null(VMMemoryNull { variable_id: 0 });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if !res {
|
||||||
|
error(format!("File does not exist"), machine, op);
|
||||||
|
}
|
||||||
|
match fs::remove_file(&str) {
|
||||||
|
Err(e) => {
|
||||||
|
error(format!("Failed to remove file: {}", e), machine, op);
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
};
|
||||||
|
return VMMemory::Null(VMMemoryNull { variable_id: 0 });
|
||||||
|
}
|
||||||
|
fn intezo_olvass(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(_) => {
|
||||||
|
error(format!("File does not exist"), machine, op);
|
||||||
|
return VMMemory::Null(VMMemoryNull { variable_id: 0 });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if !res {
|
||||||
|
error(format!("File does not exist"), machine, op);
|
||||||
|
}
|
||||||
|
let data = match fs::read_to_string(&str) {
|
||||||
|
Ok(data) => data,
|
||||||
|
Err(e) => {
|
||||||
|
error(format!("Failed to read file: {}", e), machine, op);
|
||||||
|
return VMMemory::Null(VMMemoryNull { variable_id: 0 });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return VMMemory::String(VMMemoryString { value: data, variable_id: 0 });
|
||||||
|
}
|
||||||
|
fn intezo_irj(machine: &mut Machine, op: &DecompiledOperation, args: Vec<VMMemory>) -> VMMemory {
|
||||||
|
arg_expect(&args, 0, "string", machine, op);
|
||||||
|
arg_expect(&args, 1, "string", machine, op);
|
||||||
|
let str = match &args[0] {
|
||||||
|
VMMemory::String(s) => s.value.clone(),
|
||||||
|
_ => String::new()
|
||||||
|
};
|
||||||
|
let str2 = match &args[1] {
|
||||||
|
VMMemory::String(s) => s.value.clone(),
|
||||||
|
_ => String::new()
|
||||||
|
};
|
||||||
|
match fs::write(&str, str2) {
|
||||||
|
Err(e) => {
|
||||||
|
error(format!("Failed to write file: {}", e), machine, op);
|
||||||
|
return VMMemory::Null(VMMemoryNull { variable_id: 0 });
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
};
|
||||||
|
return VMMemory::Null(VMMemoryNull { variable_id: 0 });
|
||||||
|
}
|
||||||
|
|
||||||
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();
|
||||||
|
@ -847,6 +975,8 @@ pub fn generate() -> HashMap<String, VMMemory> {
|
||||||
mem.insert(String::from("csömör"), VMMemory::NativeFunction(VMMemoryNativeFunction { func: csomor, variable_id: 0 }));
|
mem.insert(String::from("csömör"), VMMemory::NativeFunction(VMMemoryNativeFunction { func: csomor, variable_id: 0 }));
|
||||||
mem.insert(String::from("tarh"), VMMemory::NativeFunction(VMMemoryNativeFunction { func: tarh, variable_id: 0 }));
|
mem.insert(String::from("tarh"), VMMemory::NativeFunction(VMMemoryNativeFunction { func: tarh, variable_id: 0 }));
|
||||||
mem.insert(String::from("bimbabemb"), VMMemory::NativeFunction(VMMemoryNativeFunction { func: bimbabemb, variable_id: 0 }));
|
mem.insert(String::from("bimbabemb"), VMMemory::NativeFunction(VMMemoryNativeFunction { func: bimbabemb, variable_id: 0 }));
|
||||||
|
mem.insert(String::from("mennyi az idő"), VMMemory::NativeFunction(VMMemoryNativeFunction { func: mennyiazido, variable_id: 0 }));
|
||||||
|
mem.insert(String::from("joink"), VMMemory::NativeFunction(VMMemoryNativeFunction { func: joink, variable_id: 0 }));
|
||||||
|
|
||||||
let nerd: Vec<TableValue> = vec![
|
let nerd: Vec<TableValue> = vec![
|
||||||
TableValue {
|
TableValue {
|
||||||
|
@ -972,5 +1102,37 @@ pub fn generate() -> HashMap<String, VMMemory> {
|
||||||
];
|
];
|
||||||
mem.insert(String::from("szenvedés"), VMMemory::Table(VMMemoryTable { values: szenvedes, variable_id: 0 }));
|
mem.insert(String::from("szenvedés"), VMMemory::Table(VMMemoryTable { values: szenvedes, variable_id: 0 }));
|
||||||
|
|
||||||
|
let intezo: Vec<TableValue> = vec![
|
||||||
|
TableValue {
|
||||||
|
key: VMMemory::String(VMMemoryString { value: String::from("létezik"), variable_id: 0 }),
|
||||||
|
value: VMMemory::NativeFunction(VMMemoryNativeFunction { func: intezo_letezik, variable_id: 0 }),
|
||||||
|
},
|
||||||
|
TableValue {
|
||||||
|
key: VMMemory::String(VMMemoryString { value: String::from("infó"), variable_id: 0 }),
|
||||||
|
value: VMMemory::NativeFunction(VMMemoryNativeFunction { func: intezo_info, variable_id: 0 }),
|
||||||
|
},
|
||||||
|
TableValue {
|
||||||
|
key: VMMemory::String(VMMemoryString { value: String::from("mappít"), variable_id: 0 }),
|
||||||
|
value: VMMemory::NativeFunction(VMMemoryNativeFunction { func: intezo_mappit, variable_id: 0 }),
|
||||||
|
},
|
||||||
|
TableValue {
|
||||||
|
key: VMMemory::String(VMMemoryString { value: String::from("mappát töröl"), variable_id: 0 }),
|
||||||
|
value: VMMemory::NativeFunction(VMMemoryNativeFunction { func: intezo_mappattorol, variable_id: 0 }),
|
||||||
|
},
|
||||||
|
TableValue {
|
||||||
|
key: VMMemory::String(VMMemoryString { value: String::from("fájlt töröl"), variable_id: 0 }),
|
||||||
|
value: VMMemory::NativeFunction(VMMemoryNativeFunction { func: intezo_fajlttorol, variable_id: 0 }),
|
||||||
|
},
|
||||||
|
TableValue {
|
||||||
|
key: VMMemory::String(VMMemoryString { value: String::from("olvass"), variable_id: 0 }),
|
||||||
|
value: VMMemory::NativeFunction(VMMemoryNativeFunction { func: intezo_olvass, variable_id: 0 }),
|
||||||
|
},
|
||||||
|
TableValue {
|
||||||
|
key: VMMemory::String(VMMemoryString { value: String::from("írj"), variable_id: 0 }),
|
||||||
|
value: VMMemory::NativeFunction(VMMemoryNativeFunction { func: intezo_irj, variable_id: 0 }),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
mem.insert(String::from("intéző"), VMMemory::Table(VMMemoryTable { values: intezo, variable_id: 0 }));
|
||||||
|
|
||||||
return mem;
|
return mem;
|
||||||
}
|
}
|
|
@ -195,13 +195,14 @@ fn main() {
|
||||||
let mut longest = 0;
|
let mut longest = 0;
|
||||||
for op in &func.body {
|
for op in &func.body {
|
||||||
let op_name = operation_to_name(op.opcode);
|
let op_name = operation_to_name(op.opcode);
|
||||||
let str = format!(" {}{} {}{} {}{} {} at {}",
|
let str = format!(" {}{} {}{} {}{} {}{} at {}",
|
||||||
|
" ".repeat(longest_cols[0] - op_name.len()),
|
||||||
op_name,
|
op_name,
|
||||||
" ".repeat(longest_cols[0] - op_name.len()),
|
|
||||||
op.arg1,
|
|
||||||
" ".repeat(longest_cols[1] - op.arg1.to_string().len()),
|
" ".repeat(longest_cols[1] - op.arg1.to_string().len()),
|
||||||
op.arg2,
|
op.arg1,
|
||||||
" ".repeat(longest_cols[2] - op.arg2.to_string().len()),
|
" ".repeat(longest_cols[2] - op.arg2.to_string().len()),
|
||||||
|
op.arg2,
|
||||||
|
" ".repeat(longest_cols[3] - op.arg3.to_string().len()),
|
||||||
op.arg3,
|
op.arg3,
|
||||||
op.pos);
|
op.pos);
|
||||||
longest = longest.max(str.len()+4);
|
longest = longest.max(str.len()+4);
|
||||||
|
|
|
@ -213,7 +213,7 @@ fn shunt(input: Vec<ASTPart>, ctx: &Context) -> ASTPart {
|
||||||
output.push(part);
|
output.push(part);
|
||||||
},
|
},
|
||||||
ASTPart::Call(_) => {
|
ASTPart::Call(_) => {
|
||||||
stack.push(part);
|
output.push(part);
|
||||||
},
|
},
|
||||||
ASTPart::VarRead(_) => {
|
ASTPart::VarRead(_) => {
|
||||||
output.push(part);
|
output.push(part);
|
||||||
|
|
29
test.asl
29
test.asl
|
@ -1,18 +1,11 @@
|
||||||
gethelj a = 125
|
gethelj time = mennyi az idő()
|
||||||
gethelj b = 567
|
ugass(time)
|
||||||
gethelj c = 8765
|
gethelj bemenet = joink()
|
||||||
gethelj d = 123
|
ugass(bemenet)
|
||||||
gethelj e = 123
|
gethelj time2 = mennyi az idő()
|
||||||
gethelj f = 123
|
gethelj msg = szaft"Took: "szaft+bimbabemb(time2-time, szaft"string"szaft)+szaft"ms"szaft
|
||||||
gethelj g = 123
|
ugass(msg)
|
||||||
gethelj h = 123
|
|
||||||
gethelj i = 123
|
ugass(intéző.infó(szaft"test.asl"szaft))
|
||||||
gethelj j = 123
|
ugass(intéző.olvass(szaft"test.asl"szaft))
|
||||||
gethelj k = 123
|
ugass(intéző.írj(szaft"bimbabemb.txt"szaft,szaft"óóóóó bimbabemb!"szaft))
|
||||||
gethelj l = 123
|
|
||||||
gethelj m = 123
|
|
||||||
gethelj n = 123
|
|
||||||
gethelj o = 123
|
|
||||||
gethelj p = 123
|
|
||||||
gethelj q = 123
|
|
||||||
ugass(a+b+c)
|
|
Loading…
Add table
Add a link
Reference in a new issue