fix some logic errors, simplify *-fns.rs

This commit is contained in:
Daniella / Tove 2023-02-18 20:05:24 +01:00
parent 4d4799a76f
commit 7b5ba3fbf7
Signed by: TudbuT
GPG key ID: 7D63D5634B7C417F
4 changed files with 97 additions and 321 deletions

View file

@ -188,6 +188,7 @@ pub fn dyn_read(stack: &mut Stack) {
stack.get_frame(),
)),
origin: stack.get_frame(),
cname: None,
}))
.spl(),
);
@ -196,109 +197,49 @@ pub fn dyn_read(stack: &mut Stack) {
}
}
pub fn register(r: &mut Stack, o: Arc<Frame>) {
r.define_func(
"dyn-__dump".to_owned(),
AFunc::new(Func {
pub fn dyn_readf(stack: &mut Stack) {
if let Value::Str(n) = stack.pop().lock_ro().native.clone() {
if let Value::Str(s) = stack.pop().lock_ro().native.clone() {
stack.push(
Value::Func(AFunc::new(Func {
ret_count: 0,
to_call: FuncImpl::Native(dyn_dump),
origin: o.clone(),
}),
to_call: FuncImpl::SPL(lexer::lex(s, n.clone(), stack.get_frame())),
origin: stack.get_frame(),
cname: Some(n),
}))
.spl(),
);
r.define_func(
"dyn-def".to_owned(),
AFunc::new(Func {
ret_count: 0,
to_call: FuncImpl::Native(dyn_def),
origin: o.clone(),
}),
);
r.define_func(
"dyn-func".to_owned(),
AFunc::new(Func {
ret_count: 0,
to_call: FuncImpl::Native(dyn_func),
origin: o.clone(),
}),
);
r.define_func(
"dyn-construct".to_owned(),
AFunc::new(Func {
ret_count: 0,
to_call: FuncImpl::Native(dyn_construct),
origin: o.clone(),
}),
);
r.define_func(
"dyn-def-field".to_owned(),
AFunc::new(Func {
ret_count: 0,
to_call: FuncImpl::Native(dyn_def_field),
origin: o.clone(),
}),
);
r.define_func(
"dyn-def-method".to_owned(),
AFunc::new(Func {
ret_count: 0,
to_call: FuncImpl::Native(dyn_def_method),
origin: o.clone(),
}),
);
r.define_func(
"dyn-include".to_owned(),
AFunc::new(Func {
ret_count: 0,
to_call: FuncImpl::Native(dyn_include),
origin: o.clone(),
}),
);
r.define_func(
"dyn-while".to_owned(),
AFunc::new(Func {
ret_count: 0,
to_call: FuncImpl::Native(dyn_while),
origin: o.clone(),
}),
);
r.define_func(
"dyn-if".to_owned(),
AFunc::new(Func {
ret_count: 0,
to_call: FuncImpl::Native(dyn_if),
origin: o.clone(),
}),
);
r.define_func(
"dyn-call".to_owned(),
AFunc::new(Func {
ret_count: 0,
to_call: FuncImpl::Native(dyn_call),
origin: o.clone(),
}),
);
r.define_func(
"dyn-objcall".to_owned(),
AFunc::new(Func {
ret_count: 0,
to_call: FuncImpl::Native(dyn_objcall),
origin: o.clone(),
}),
);
r.define_func(
"dyn-all-types".to_owned(),
AFunc::new(Func {
ret_count: 1,
to_call: FuncImpl::Native(dyn_all_types),
origin: o.clone(),
}),
);
r.define_func(
"dyn-read".to_owned(),
AFunc::new(Func {
ret_count: 1,
to_call: FuncImpl::Native(dyn_read),
origin: o.clone(),
}),
)
} else {
panic!("incorrect usage of dyn-call");
}
} else {
panic!("incorrect usage of dyn-call");
}
}
pub fn register(r: &mut Stack, o: Arc<Frame>) {
let fns: [(&str, fn(&mut Stack), u32); 14] = [
("dyn-__dump", dyn_dump, 0),
("dyn-def", dyn_def, 0),
("dyn-func", dyn_func, 0),
("dyn-construct", dyn_construct, 0),
("dyn-def-field", dyn_def_field, 0),
("dyn-def-method", dyn_def_method, 0),
("dyn-include", dyn_include, 0),
("dyn-while", dyn_while, 0),
("dyn-if", dyn_if, 0),
("dyn-call", dyn_call, 0),
("dyn-objcall", dyn_objcall, 0),
("dyn-all-types", dyn_all_types, 1),
("dyn-read", dyn_read, 1),
("dyn-readf", dyn_readf, 1),
];
for f in fns {
r.define_func(f.0.to_owned(), AFunc::new(Func {
ret_count: f.2,
to_call: FuncImpl::Native(f.1),
origin: o.clone(),
cname: None,
}));
}
}

View file

@ -57,6 +57,7 @@ fn read_block(str_words: &[String], isfn: bool, origin: Arc<Frame>) -> (Option<u
ret_count: block.0.expect("LEXERR: Expected `{ <type> <...> |`."),
to_call: FuncImpl::SPL(block.1),
origin: origin.to_owned(),
cname: None,
}))))
}
"construct" => {

View file

@ -189,7 +189,11 @@ impl Stack {
}
pub fn call(&mut self, func: &AFunc) {
self.frames.push(Arc::new(Frame::new(func.origin.clone())));
let mut f = Frame::new(func.origin.clone());
if let Some(ref cname) = func.cname {
f.origin.file = cname.clone();
}
self.frames.push(Arc::new(f));
func.to_call.call(self);
self.frames.pop().unwrap();
}
@ -222,6 +226,7 @@ impl Stack {
stack.push(stack.get_var(tmpname.clone()))
}))),
origin: frame.clone(),
cname: Some("RUNTIME".to_owned()),
}),
);
let tmpname = name.clone();
@ -234,6 +239,7 @@ impl Stack {
stack.set_var(tmpname.clone(), v);
}))),
origin: frame.clone(),
cname: Some("RUNTIME".to_owned()),
}),
);
frame.variables.lock().insert(name, Value::Null.spl());
@ -400,6 +406,7 @@ pub struct Func {
pub ret_count: u32,
pub to_call: FuncImpl,
pub origin: Arc<Frame>,
pub cname: Option<String>,
}
impl PartialEq for Func {
@ -464,6 +471,7 @@ impl Type {
stack.push(o.lock_ro().property_map.get(&tmpname).unwrap().clone());
}))),
origin: origin.clone(),
cname: Some("RUNTIME".to_owned()),
}),
);
let tmpname = name.clone();
@ -477,6 +485,7 @@ impl Type {
o.lock().property_map.insert(tmpname.clone(), v);
}))),
origin,
cname: Some("RUNTIME".to_owned()),
}),
);
self.properties.push(name);
@ -603,6 +612,7 @@ impl Words {
ret_count: rem,
to_call: FuncImpl::SPL(words),
origin: stack.get_frame(),
cname: None,
}),
),
Keyword::Construct(name, fields, methods) => {
@ -627,6 +637,7 @@ impl Words {
ret_count: v.0,
to_call: FuncImpl::SPL(v.1),
origin: origin.clone(),
cname: None,
}),
)
},
@ -686,6 +697,7 @@ impl Words {
stack.push(ftmp.clone().spl());
}))),
origin: stack.get_frame(),
cname: None,
}));
}
} else {
@ -726,6 +738,7 @@ impl Words {
stack.push(ftmp.clone().spl());
}))),
origin: stack.get_frame(),
cname: None,
}));
}
} else {

View file

@ -313,220 +313,41 @@ pub fn call(stack: &mut Stack) {
}
pub fn register(r: &mut Stack, o: Arc<Frame>) {
r.define_func(
"pop".to_owned(),
AFunc::new(Func {
ret_count: 0,
to_call: FuncImpl::Native(pop),
let fns: [(&str, fn(&mut Stack), u32); 27] = [
("pop", pop, 0),
("dup", dup, 2),
("clone", clone, 1),
("swap", swap, 2),
("print", print, 0),
("call", call, 0),
("gettype", gettype, 1),
("settype", settype, 1),
("anew", array_new, 1),
("array-len", array_len, 1),
("array-get", array_get, 1),
("array-set", array_set, 1),
("eq", eq, 1),
("lt", lt, 1),
("gt", gt, 1),
("not", not, 1),
("+", plus, 1),
("-", minus, 1),
("/", slash, 1),
("*", star, 1),
("_int", to_int, 1),
("_long", to_long, 1),
("_mega", to_mega, 1),
("_float", to_float, 1),
("_double", to_double, 1),
("_array", to_array, 1),
("_str", to_str, 1),
];
for f in fns {
r.define_func(f.0.to_owned(), AFunc::new(Func {
ret_count: f.2,
to_call: FuncImpl::Native(f.1),
origin: o.clone(),
}),
);
r.define_func(
"dup".to_owned(),
AFunc::new(Func {
ret_count: 2,
to_call: FuncImpl::Native(dup),
origin: o.clone(),
}),
);
r.define_func(
"clone".to_owned(),
AFunc::new(Func {
ret_count: 1,
to_call: FuncImpl::Native(clone),
origin: o.clone(),
}),
);
r.define_func(
"swap".to_owned(),
AFunc::new(Func {
ret_count: 2,
to_call: FuncImpl::Native(swap),
origin: o.clone(),
}),
);
r.define_func(
"print".to_owned(),
AFunc::new(Func {
ret_count: 0,
to_call: FuncImpl::Native(print),
origin: o.clone(),
}),
);
r.define_func(
"call".to_owned(),
AFunc::new(Func {
ret_count: 0,
to_call: FuncImpl::Native(call),
origin: o.clone(),
}),
);
r.define_func(
"gettype".to_owned(),
AFunc::new(Func {
ret_count: 1,
to_call: FuncImpl::Native(gettype),
origin: o.clone(),
}),
);
r.define_func(
"settype".to_owned(),
AFunc::new(Func {
ret_count: 1,
to_call: FuncImpl::Native(settype),
origin: o.clone(),
}),
);
r.define_func(
"anew".to_owned(),
AFunc::new(Func {
ret_count: 1,
to_call: FuncImpl::Native(array_new),
origin: o.clone(),
}),
);
r.define_func(
"array-len".to_owned(),
AFunc::new(Func {
ret_count: 1,
to_call: FuncImpl::Native(array_len),
origin: o.clone(),
}),
);
r.define_func(
"array-get".to_owned(),
AFunc::new(Func {
ret_count: 1,
to_call: FuncImpl::Native(array_get),
origin: o.clone(),
}),
);
r.define_func(
"array-set".to_owned(),
AFunc::new(Func {
ret_count: 0,
to_call: FuncImpl::Native(array_set),
origin: o.clone(),
}),
);
r.define_func(
"eq".to_owned(),
AFunc::new(Func {
ret_count: 0,
to_call: FuncImpl::Native(eq),
origin: o.clone(),
}),
);
r.define_func(
"lt".to_owned(),
AFunc::new(Func {
ret_count: 0,
to_call: FuncImpl::Native(lt),
origin: o.clone(),
}),
);
r.define_func(
"gt".to_owned(),
AFunc::new(Func {
ret_count: 0,
to_call: FuncImpl::Native(gt),
origin: o.clone(),
}),
);
r.define_func(
"not".to_owned(),
AFunc::new(Func {
ret_count: 0,
to_call: FuncImpl::Native(not),
origin: o.clone(),
}),
);
r.define_func(
"+".to_owned(),
AFunc::new(Func {
ret_count: 1,
to_call: FuncImpl::Native(plus),
origin: o.clone(),
}),
);
r.define_func(
"-".to_owned(),
AFunc::new(Func {
ret_count: 1,
to_call: FuncImpl::Native(minus),
origin: o.clone(),
}),
);
r.define_func(
"/".to_owned(),
AFunc::new(Func {
ret_count: 1,
to_call: FuncImpl::Native(slash),
origin: o.clone(),
}),
);
r.define_func(
"*".to_owned(),
AFunc::new(Func {
ret_count: 1,
to_call: FuncImpl::Native(star),
origin: o.clone(),
}),
);
r.define_func(
"_int".to_owned(),
AFunc::new(Func {
ret_count: 1,
to_call: FuncImpl::Native(to_int),
origin: o.clone(),
}),
);
r.define_func(
"_long".to_owned(),
AFunc::new(Func {
ret_count: 1,
to_call: FuncImpl::Native(to_long),
origin: o.clone(),
}),
);
r.define_func(
"_mega".to_owned(),
AFunc::new(Func {
ret_count: 1,
to_call: FuncImpl::Native(to_mega),
origin: o.clone(),
}),
);
r.define_func(
"_float".to_owned(),
AFunc::new(Func {
ret_count: 1,
to_call: FuncImpl::Native(to_float),
origin: o.clone(),
}),
);
r.define_func(
"_double".to_owned(),
AFunc::new(Func {
ret_count: 1,
to_call: FuncImpl::Native(to_double),
origin: o.clone(),
}),
);
r.define_func(
"_array".to_owned(),
AFunc::new(Func {
ret_count: 1,
to_call: FuncImpl::Native(to_array),
origin: o.clone(),
}),
);
r.define_func(
"_str".to_owned(),
AFunc::new(Func {
ret_count: 1,
to_call: FuncImpl::Native(to_str),
origin: o.clone(),
}),
);
cname: None,
}));
}
}