microlang/example/run.rs

28 lines
770 B
Rust

use std::{env::args, fs};
use microlang::{Context, Value};
pub fn main() {
// Context instantiation.
let mut context = Context::empty();
// Definition of built-in function.
context.define_built("print", |values, _| {
for val in values {
print!("{} ", val.serialize());
}
println!();
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()),
}
}