spl/src/main.rs

55 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-17 20:00:39 +01:00
use std::{
2023-02-17 22:47:45 +01:00
io::{stdout, Write},
sync::Arc,
2023-02-17 20:00:39 +01:00
vec,
};
2023-02-17 22:47:45 +01:00
fn main() {
let rt = Runtime::new();
let mut stack = Stack::new();
fn print(stack: &mut Stack) {
let s = stack.pop();
let s = s.lock();
if let Constant::Str(ref s) = s.native {
print!("{s}");
stdout().lock().flush().unwrap();
}
}
stack.define_func(
"print".to_owned(),
Arc::new(Func {
ret_count: 0,
to_call: FuncImpl::Native(print),
2023-02-17 20:00:39 +01:00
origin: FrameInfo {
file: "RUNTIME".to_owned(),
},
2023-02-17 22:47:45 +01:00
}),
);
2023-02-17 20:00:39 +01:00
rt.set();
Words {
words: vec![
2023-02-17 22:47:45 +01:00
Word::Key(Keyword::Func(
"println".to_owned(),
0,
Words {
words: vec![
Word::Call("print".to_owned(), true, 0),
Word::Const(Constant::Str("\n".to_owned())),
Word::Call("print".to_owned(), true, 0),
],
},
)),
Word::Key(Keyword::Def("helloworld".to_owned())),
2023-02-17 20:00:39 +01:00
Word::Const(Constant::Str("Hello, World".to_owned())),
2023-02-17 22:47:45 +01:00
Word::Call("=helloworld".to_owned(), false, 0),
Word::Call("helloworld".to_owned(), false, 0),
Word::Call("println".to_owned(), true, 0),
2023-02-17 20:00:39 +01:00
],
}
2023-02-17 22:47:45 +01:00
.exec(&mut stack);
2023-02-18 01:53:48 +01:00
lex("func println { | print \"\\n\" print } def helloworld \"Hello, World\" =helloworld helloworld println".to_owned(), "TEST".to_owned()).exec(&mut stack);
2023-02-17 20:00:39 +01:00
Runtime::reset();
}