microimprovements to oxidizer

This commit is contained in:
Daniella / Tove 2024-08-28 23:54:16 +02:00
parent fbd6d9d43f
commit ef266f3811
6 changed files with 69 additions and 75 deletions

View file

@ -13,7 +13,7 @@ pub fn dyn_dump(stack: &mut Stack) -> OError {
pub fn dyn_def(stack: &mut Stack) -> OError {
let Value::Str(s) = stack.pop().lock_ro().native.clone() else {
return stack.err(ErrorKind::InvalidCall("dyn-def".to_owned()))
return stack.err(ErrorKind::InvalidCall("dyn-def".to_owned()));
};
Words {
words: vec![Word::Key(Keyword::Def(s))],
@ -23,14 +23,11 @@ pub fn dyn_def(stack: &mut Stack) -> OError {
}
pub fn dyn_func(stack: &mut Stack) -> OError {
let (
Value::Str(s),
Value::Func(f),
) = (
let (Value::Str(s), Value::Func(f)) = (
stack.pop().lock_ro().native.clone(),
stack.pop().lock_ro().native.clone(),
) else {
return stack.err(ErrorKind::InvalidCall("dyn-func".to_owned()))
return stack.err(ErrorKind::InvalidCall("dyn-func".to_owned()));
};
stack.define_func(s, f);
Ok(())
@ -38,7 +35,7 @@ pub fn dyn_func(stack: &mut Stack) -> OError {
pub fn dyn_construct(stack: &mut Stack) -> OError {
let Value::Str(s) = stack.pop().lock_ro().native.clone() else {
return stack.err(ErrorKind::InvalidCall("dyn-construct".to_owned()))
return stack.err(ErrorKind::InvalidCall("dyn-construct".to_owned()));
};
Words {
words: vec![Word::Key(Keyword::Construct(
@ -54,7 +51,7 @@ pub fn dyn_construct(stack: &mut Stack) -> OError {
pub fn dyn_namespace(stack: &mut Stack) -> OError {
let Value::Str(s) = stack.pop().lock_ro().native.clone() else {
return stack.err(ErrorKind::InvalidCall("dyn-construct".to_owned()))
return stack.err(ErrorKind::InvalidCall("dyn-construct".to_owned()));
};
Words {
words: vec![Word::Key(Keyword::Construct(
@ -69,14 +66,11 @@ pub fn dyn_namespace(stack: &mut Stack) -> OError {
}
pub fn dyn_def_field(stack: &mut Stack) -> OError {
let (
Value::Str(s),
Value::Str(name),
) = (
let (Value::Str(s), Value::Str(name)) = (
stack.pop().lock_ro().native.clone(),
stack.pop().lock_ro().native.clone(),
) else {
return stack.err(ErrorKind::InvalidCall("dyn-def-field".to_owned()))
return stack.err(ErrorKind::InvalidCall("dyn-def-field".to_owned()));
};
runtime(|rt| rt.get_type_by_name(&s))
.ok_or_else(|| stack.error(ErrorKind::TypeNotFound(s)))?
@ -86,16 +80,12 @@ pub fn dyn_def_field(stack: &mut Stack) -> OError {
}
pub fn dyn_def_method(stack: &mut Stack) -> OError {
let (
Value::Str(s),
Value::Str(name),
Value::Func(f),
) = (
let (Value::Str(s), Value::Str(name), Value::Func(f)) = (
stack.pop().lock_ro().native.clone(),
stack.pop().lock_ro().native.clone(),
stack.pop().lock_ro().native.clone(),
) else {
return stack.err(ErrorKind::InvalidCall("dyn-def-method".to_owned()))
return stack.err(ErrorKind::InvalidCall("dyn-def-method".to_owned()));
};
runtime(|rt| rt.get_type_by_name(&s))
.ok_or_else(|| stack.error(ErrorKind::TypeNotFound(s)))?
@ -106,14 +96,11 @@ pub fn dyn_def_method(stack: &mut Stack) -> OError {
}
pub fn dyn_include(stack: &mut Stack) -> OError {
let (
Value::Str(b),
Value::Str(a),
) = (
let (Value::Str(b), Value::Str(a)) = (
stack.pop().lock_ro().native.clone(),
stack.pop().lock_ro().native.clone(),
) else {
return stack.err(ErrorKind::InvalidCall("dyn-include".to_owned()))
return stack.err(ErrorKind::InvalidCall("dyn-include".to_owned()));
};
Words {
words: vec![Word::Key(Keyword::Include(a, b))],
@ -123,14 +110,11 @@ pub fn dyn_include(stack: &mut Stack) -> OError {
}
pub fn dyn_while(stack: &mut Stack) -> OError {
let (
Value::Func(blk),
Value::Func(cond),
) = (
let (Value::Func(blk), Value::Func(cond)) = (
stack.pop().lock_ro().native.clone(),
stack.pop().lock_ro().native.clone(),
) else {
return stack.err(ErrorKind::InvalidCall("dyn-while".to_owned()))
return stack.err(ErrorKind::InvalidCall("dyn-while".to_owned()));
};
loop {
cond.to_call.call(stack)?;
@ -144,7 +128,7 @@ pub fn dyn_while(stack: &mut Stack) -> OError {
pub fn dyn_if(stack: &mut Stack) -> OError {
let Value::Func(blk) = stack.pop().lock_ro().native.clone() else {
return stack.err(ErrorKind::InvalidCall("dyn-if".to_owned()))
return stack.err(ErrorKind::InvalidCall("dyn-if".to_owned()));
};
if stack.pop().lock_ro().is_truthy() {
blk.to_call.call(stack)?;
@ -154,7 +138,7 @@ pub fn dyn_if(stack: &mut Stack) -> OError {
pub fn dyn_call(stack: &mut Stack) -> OError {
let Value::Str(mut s) = stack.pop().lock_ro().native.clone() else {
return stack.err(ErrorKind::InvalidCall("dyn-call".to_owned()))
return stack.err(ErrorKind::InvalidCall("dyn-call".to_owned()));
};
let mut words = Vec::new();
let mut ra = 0;
@ -173,7 +157,7 @@ pub fn dyn_call(stack: &mut Stack) -> OError {
pub fn dyn_objcall(stack: &mut Stack) -> OError {
let Value::Str(mut s) = stack.pop().lock_ro().native.clone() else {
return stack.err(ErrorKind::InvalidCall("dyn-objcall".to_owned()))
return stack.err(ErrorKind::InvalidCall("dyn-objcall".to_owned()));
};
let mut words = Vec::new();
let mut ra = 0;
@ -205,7 +189,7 @@ pub fn dyn_all_types(stack: &mut Stack) -> OError {
pub fn dyn_read(stack: &mut Stack) -> OError {
let Value::Str(s) = stack.pop().lock_ro().native.clone() else {
return stack.err(ErrorKind::InvalidCall("dyn-read".to_owned()))
return stack.err(ErrorKind::InvalidCall("dyn-read".to_owned()));
};
stack.push(
Value::Func(AFunc::new(Func {
@ -224,14 +208,11 @@ pub fn dyn_read(stack: &mut Stack) -> OError {
}
pub fn dyn_readf(stack: &mut Stack) -> OError {
let (
Value::Str(s),
Value::Str(n),
) = (
let (Value::Str(s), Value::Str(n)) = (
stack.pop().lock_ro().native.clone(),
stack.pop().lock_ro().native.clone(),
) else {
return stack.err(ErrorKind::InvalidCall("dyn-readf".to_owned()))
return stack.err(ErrorKind::InvalidCall("dyn-readf".to_owned()));
};
stack.push(
Value::Func(AFunc::new(Func {

View file

@ -1,13 +1,14 @@
use spl::{find_in_splpath, lex, oxidizer::RustAppBuilder, start_file};
use spl::{find_in_splpath, lex, oxidizer::RustAppBuilder, start_file_in_runtime, Runtime, SetRuntime};
use std::{env::args, fs};
fn main() {
Runtime::new().set();
let mut args = args().skip(1);
let arg = &args
.next()
.unwrap_or_else(|| find_in_splpath("repl.spl").expect("no file to be run"));
if arg == "--build" || arg == "--run" {
if arg == "--build" || arg == "--run" || arg == "--buildrun" {
let file = args.next().unwrap();
let data = fs::read_to_string(file.clone()).expect("unable to read specified file");
let build_only = arg == "--build";
@ -29,20 +30,24 @@ fn main() {
if build_only {
println!("Building...");
}
let app = builder.build(build_only).unwrap();
let app = builder.build(build_only || arg == "--buildrun").unwrap();
if build_only {
println!("Built! Binary is {}", app.get_binary());
} else {
let mut args: Vec<String> = args.collect();
args.insert(0, file);
let mut command = app.execute(args).unwrap();
app.delete();
if arg != "--buildrun" {
println!("spl: cleaning temporary dir (run with --buildrun to keep)");
app.delete();
}
command.wait().unwrap();
}
return;
}
if let Err(x) = start_file(arg) {
if let Err(x) = start_file_in_runtime(arg) {
println!("{x:?}");
}
Runtime::reset();
}

View file

@ -146,16 +146,16 @@ impl RustAppBuilder {
let mut code = String::new();
for func in self.rust_functions.into_iter().enumerate() {
code += &format!(
"fn spl_oxidizer_{}(stack: &mut Stack) -> OError {{ {} Ok(()) }}",
"fn spl_oxidizer_{}(stack: &mut Stack) -> OError {{ {} Ok(()) }}\n",
func.0, func.1.content
);
runtime_init += &format!(
"rt.native_functions.insert({:?}, (0, FuncImpl::Native(spl_oxidizer_{})));",
"rt.native_functions.insert({:?}, (0, FuncImpl::Native(spl_oxidizer_{})));\n",
func.1.fn_name, func.0
)
}
for (name, data) in self.to_embed.into_iter() {
runtime_init += &format!("rt.embedded_files.insert({:?}, {:?});", name, data);
runtime_init += &format!("rt.embedded_files.insert({:?}, {:?});\n", name, data);
}
fs::write(
format!("{tmp}/spl-{name}/src/main.rs"),
@ -164,21 +164,18 @@ impl RustAppBuilder {
use std::env::args;
pub fn start_file(path: &str) -> Result<Stack, Error> {
fn main() {
let mut rt = Runtime::new();
runtime_init
rt.set();
(start_file_in_runtime(path), Runtime::reset()).0
}
fn main() {
if let Err(x) = start_file(
if let Err(x) = start_file_in_runtime(
&args()
.nth(1)
.unwrap_or_else(|| find_in_splpath("default_file").expect("no file to be run")),
) {
println!("{x:?}");
}
Runtime::reset();
}
}.to_owned().replace("default_file", &self.default_file).replace("runtime_init", &runtime_init) + &code,
)?;

View file

@ -4,12 +4,16 @@ use super::RustFunction;
/// Parses a #-expression and returns the string to be inserted in its place.
fn parse_hash_expr(s: String, name: &str) -> String {
if &s == "drop" {
return "let _ = stack.pop();".to_owned();
}
if &s == "pop" {
return "stack.pop().lock_ro()".to_owned();
}
if &s == "pop_mut" {
return "stack.pop().lock()".to_owned();
}
// TODO: broken. fix.
if &s == "pop:Array" {
return format!("{{ require_array_on_stack!(tmp, stack, {name:?}); tmp }}");
}

View file

@ -517,8 +517,13 @@ impl Stack {
}
pub fn pop_until(&mut self, obj: AMObject) -> Vec<AMObject> {
let Some((idx, ..)) = self.object_stack.iter().enumerate().rfind(|o| *o.1.lock_ro() == *obj.lock_ro()) else {
return Vec::new()
let Some((idx, ..)) = self
.object_stack
.iter()
.enumerate()
.rfind(|o| *o.1.lock_ro() == *obj.lock_ro())
else {
return Vec::new();
};
let items = self.object_stack[idx + 1..].to_vec();
self.object_stack = self.object_stack[0..idx].to_vec();
@ -1182,7 +1187,9 @@ impl Words {
Value::Str(t.lock_ro().get_name()).into()
};
if name.contains(':') {
let Some((a, mut name)) = name.split_once(':') else { unreachable!() };
let Some((a, mut name)) = name.split_once(':') else {
unreachable!()
};
let mut f = stack.get_var(a.to_owned())?;
while let Some((a, b)) = name.split_once(':') {
name = b;

View file

@ -31,7 +31,7 @@ macro_rules! array {
pub fn print(stack: &mut Stack) -> OError {
let Value::Str(s) = stack.pop().lock_ro().native.clone() else {
return stack.err(ErrorKind::InvalidCall("print".to_owned()))
return stack.err(ErrorKind::InvalidCall("print".to_owned()));
};
print!("{s}");
stdout().lock().flush().unwrap();
@ -65,7 +65,7 @@ pub fn swap(stack: &mut Stack) -> OError {
pub fn mswap(stack: &mut Stack) -> OError {
let Value::Mega(i) = stack.pop().lock_ro().native.clone() else {
return stack.err(ErrorKind::InvalidCall("nswap".to_owned()))
return stack.err(ErrorKind::InvalidCall("nswap".to_owned()));
};
let mut array = VecDeque::with_capacity(i as usize);
for _ in 0..i {
@ -80,7 +80,7 @@ pub fn mswap(stack: &mut Stack) -> OError {
pub fn settype(stack: &mut Stack) -> OError {
let Value::Str(s) = stack.pop().lock_ro().native.clone() else {
return stack.err(ErrorKind::InvalidCall("settype".to_owned()))
return stack.err(ErrorKind::InvalidCall("settype".to_owned()));
};
let o = stack.pop();
let kind = runtime(|rt| rt.get_type_by_name(&s))
@ -101,7 +101,7 @@ pub fn gettype(stack: &mut Stack) -> OError {
pub fn array_new(stack: &mut Stack) -> OError {
let Value::Mega(i) = stack.pop().lock_ro().native.clone() else {
return stack.err(ErrorKind::InvalidCall("anew".to_owned()))
return stack.err(ErrorKind::InvalidCall("anew".to_owned()));
};
stack.push(Value::Array(vec![Value::Null.spl(); i as usize]).spl());
Ok(())
@ -110,7 +110,7 @@ pub fn array_new(stack: &mut Stack) -> OError {
pub fn array_len(stack: &mut Stack) -> OError {
let binding = stack.pop();
let Value::Array(ref a) = binding.lock_ro().native else {
return stack.err(ErrorKind::InvalidCall("array-len".to_owned()))
return stack.err(ErrorKind::InvalidCall("array-len".to_owned()));
};
stack.push(Value::Mega(a.len() as i128).spl());
Ok(())
@ -119,10 +119,10 @@ pub fn array_len(stack: &mut Stack) -> OError {
pub fn array_get(stack: &mut Stack) -> OError {
let binding = stack.pop();
let Value::Array(ref a) = binding.lock_ro().native else {
return stack.err(ErrorKind::InvalidCall("array-get".to_owned()))
return stack.err(ErrorKind::InvalidCall("array-get".to_owned()));
};
let Value::Mega(i) = stack.pop().lock_ro().native.clone() else {
return stack.err(ErrorKind::InvalidCall("array-get".to_owned()))
return stack.err(ErrorKind::InvalidCall("array-get".to_owned()));
};
stack.push(a.get(i as usize).ok_or_else(array!(stack, i))?.clone());
Ok(())
@ -131,10 +131,10 @@ pub fn array_get(stack: &mut Stack) -> OError {
pub fn array_set(stack: &mut Stack) -> OError {
let binding = stack.pop();
let Value::Array(ref mut a) = binding.lock().native else {
return stack.err(ErrorKind::InvalidCall("array-set".to_owned()))
return stack.err(ErrorKind::InvalidCall("array-set".to_owned()));
};
let Value::Mega(i) = stack.pop().lock_ro().native.clone() else {
return stack.err(ErrorKind::InvalidCall("array-set".to_owned()))
return stack.err(ErrorKind::InvalidCall("array-set".to_owned()));
};
let o = stack.pop();
stack.push(a.get(i as usize).ok_or_else(array!(stack, i))?.clone());
@ -467,14 +467,14 @@ pub fn to_str(stack: &mut Stack) -> OError {
pub fn call(stack: &mut Stack) -> OError {
let Value::Func(a) = stack.pop().lock_ro().native.clone() else {
return stack.err(ErrorKind::InvalidCall("call".to_owned()))
return stack.err(ErrorKind::InvalidCall("call".to_owned()));
};
stack.call(&a)
}
pub fn callp(stack: &mut Stack) -> OError {
let Value::Func(a) = stack.pop().lock_ro().native.clone() else {
return stack.err(ErrorKind::InvalidCall("callp".to_owned()))
return stack.err(ErrorKind::InvalidCall("callp".to_owned()));
};
stack.call(&a)?;
for _ in 0..a.ret_count {
@ -505,14 +505,14 @@ pub fn mr_trace(stack: &mut Stack) -> OError {
pub fn exit(stack: &mut Stack) -> OError {
let Value::Int(a) = stack.pop().lock_ro().native.clone().try_mega_to_int() else {
return stack.err(ErrorKind::InvalidCall("exit".to_owned()))
return stack.err(ErrorKind::InvalidCall("exit".to_owned()));
};
process::exit(a)
}
pub fn exec(stack: &mut Stack) -> OError {
let Value::Func(a) = stack.pop().lock_ro().native.clone() else {
return stack.err(ErrorKind::InvalidCall("exec".to_owned()))
return stack.err(ErrorKind::InvalidCall("exec".to_owned()));
};
unsafe {
let f = stack.pop_frame(0);
@ -524,7 +524,7 @@ pub fn exec(stack: &mut Stack) -> OError {
pub fn exec2(stack: &mut Stack) -> OError {
let Value::Func(a) = stack.pop().lock_ro().native.clone() else {
return stack.err(ErrorKind::InvalidCall("exec2".to_owned()))
return stack.err(ErrorKind::InvalidCall("exec2".to_owned()));
};
unsafe {
let f = stack.pop_frame(0);
@ -538,7 +538,7 @@ pub fn exec2(stack: &mut Stack) -> OError {
pub fn exec3(stack: &mut Stack) -> OError {
let Value::Func(a) = stack.pop().lock_ro().native.clone() else {
return stack.err(ErrorKind::InvalidCall("exec3".to_owned()))
return stack.err(ErrorKind::InvalidCall("exec3".to_owned()));
};
unsafe {
let f = stack.pop_frame(0);
@ -554,7 +554,7 @@ pub fn exec3(stack: &mut Stack) -> OError {
pub fn stop(stack: &mut Stack) -> OError {
let Value::Int(i) = stack.pop().lock_ro().native.clone().try_mega_to_int() else {
return stack.err(ErrorKind::InvalidCall("stop".to_owned()))
return stack.err(ErrorKind::InvalidCall("stop".to_owned()));
};
stack.return_accumultor += i as u32;
Ok(())
@ -580,7 +580,7 @@ pub fn get_env(stack: &mut Stack) -> OError {
pub fn read_file(stack: &mut Stack) -> OError {
let Value::Str(s) = stack.pop().lock_ro().native.clone() else {
return stack.err(ErrorKind::InvalidCall("read_file".to_owned()))
return stack.err(ErrorKind::InvalidCall("read_file".to_owned()));
};
stack.push(
Value::Str(
@ -600,7 +600,7 @@ pub fn alit_end(stack: &mut Stack) -> OError {
pub fn import(stack: &mut Stack) -> OError {
let Value::Str(mut s) = stack.pop().lock_ro().native.clone() else {
return stack.err(ErrorKind::InvalidCall("import".to_owned()))
return stack.err(ErrorKind::InvalidCall("import".to_owned()));
};
let fallback = s
.as_str()
@ -680,7 +680,7 @@ pub fn readln(stack: &mut Stack) -> OError {
pub fn command(stack: &mut Stack) -> OError {
let binding = stack.pop();
let Value::Array(ref a) = binding.lock_ro().native else {
return stack.err(ErrorKind::InvalidCall("command".to_owned()))
return stack.err(ErrorKind::InvalidCall("command".to_owned()));
};
let mut args = Vec::new();
for item in a.iter() {
@ -704,7 +704,7 @@ pub fn command(stack: &mut Stack) -> OError {
pub fn command_wait(stack: &mut Stack) -> OError {
let binding = stack.pop();
let Value::Array(ref a) = binding.lock_ro().native else {
return stack.err(ErrorKind::InvalidCall("command".to_owned()))
return stack.err(ErrorKind::InvalidCall("command".to_owned()));
};
let mut args = Vec::new();
for item in a.iter() {