34 lines
846 B
Rust
34 lines
846 B
Rust
use std::{env::args, fs};
|
|
|
|
use microlang::{
|
|
eval::{Context, Value},
|
|
prelude::prelude,
|
|
};
|
|
|
|
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)
|
|
});
|
|
|
|
// Import prelude.
|
|
prelude(&mut context);
|
|
|
|
// 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()),
|
|
}
|
|
}
|