explicit library api and remove dependency on stack overflow introspection tool

This commit is contained in:
Matthieu Jolimaitre 2024-10-24 18:48:49 +02:00
parent dbeec4ee76
commit e0ffe2a858
4 changed files with 9 additions and 115 deletions

View file

@ -3,7 +3,6 @@ use std::io::{stdin, stdout, Write};
use microlang::Context;
pub fn main() {
unsafe { backtrace_on_stack_overflow::enable() };
let mut context = Context::empty();
loop {
print!("> ");

View file

@ -3,8 +3,10 @@ use std::{env::args, fs};
use microlang::{Context, Value};
pub fn main() {
unsafe { backtrace_on_stack_overflow::enable() };
// Context instantiation.
let mut context = Context::empty();
// Definition of built-in function.
context.define_built("print", |values, _| {
for val in values {
print!("{} ", val.serialize());
@ -13,9 +15,14 @@ pub fn main() {
Ok(Value::None)
});
// Script to evaluate.
let lines = fs::read_to_string(args().nth(1).expect("Pass a file as arg 1.")).expect("File could not be read.");
// Script evaluation.
match context.eval(lines) {
// May return errors raised during parsing or actual evaluation.
Err(err) => panic!("Failed : {err:?}"),
// Success returns the parsed AST alongside the value resulting from the evaluation.
Ok((_, value)) => println!("{}", value.serialize()),
}
}