spl/src/main.rs

56 lines
1.6 KiB
Rust
Raw Normal View History

2023-02-18 01:53:48 +01:00
use spl::{lexer::lex, runtime::*};
2023-02-17 22:47:45 +01:00
2023-02-19 04:58:17 +01:00
use std::{env::args, fs};
2023-02-17 20:00:39 +01:00
2023-02-19 02:03:06 +01:00
fn main() -> OError {
2023-02-17 22:47:45 +01:00
let rt = Runtime::new();
2023-02-17 20:00:39 +01:00
rt.set();
2023-02-19 04:57:32 +01:00
let mut stack = Stack::new_in(FrameInfo {
file: "std.spl".to_owned(),
function: "root".to_owned(),
});
fn argv(stack: &mut Stack) -> OError {
stack.push(Value::Array(args().into_iter().map(|x| Value::Str(x).spl()).collect()).spl());
Ok(())
2023-02-17 20:00:39 +01:00
}
2023-02-19 04:57:32 +01:00
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()))
};
2023-02-19 04:58:17 +01:00
stack.push(
Value::Str(
fs::read_to_string(s).map_err(|x| stack.error(ErrorKind::IO(format!("{x:?}"))))?,
)
.spl(),
);
2023-02-19 04:57:32 +01:00
Ok(())
}
2023-02-19 04:58:17 +01:00
stack.define_func(
"argv".to_owned(),
AFunc::new(Func {
ret_count: 1,
to_call: FuncImpl::Native(argv),
origin: stack.get_frame(),
fname: None,
name: "argv".to_owned(),
}),
);
stack.define_func(
"read-file".to_owned(),
AFunc::new(Func {
ret_count: 1,
to_call: FuncImpl::Native(read_file),
origin: stack.get_frame(),
fname: None,
name: "read-file".to_owned(),
}),
);
2023-02-19 04:57:32 +01:00
let words = lex(fs::read_to_string("std.spl").unwrap()).map_err(|x| Error {
2023-02-19 02:03:06 +01:00
kind: ErrorKind::LexError(format!("{x:?}")),
stack: Vec::new(),
})?;
words.exec(&mut stack)?;
2023-02-17 20:00:39 +01:00
Runtime::reset();
2023-02-19 02:03:06 +01:00
Ok(())
2023-02-17 20:00:39 +01:00
}