1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-21 02:27:10 +03:00
mal/impls/rust/step2_eval.rs
Nicolas Boulenguez 033892777a Merge eval-ast and macro expansion into EVAL, add DEBUG-EVAL
See issue #587.
* Merge eval-ast and eval into a single conditional.
* Expand macros during the apply phase, removing lots of duplicate
  tests, and increasing the overall consistency by allowing the macro
  to be computed instead of referenced by name (`((defmacro! cond
  (...)))` is currently illegal for example).
* Print "EVAL: $ast" at the top of EVAL if DEBUG-EVAL exists in the
  MAL environment.
* Remove macroexpand and quasiquoteexpand special forms.
* Use pattern-matching style in process/step*.txt.

Unresolved issues:
c.2: unable to reproduce with gcc 11.12.0.
elm: the directory is unchanged.
groovy: sometimes fail, but not on each rebuild.
nasm: fails some new soft tests, but the issue is unreproducible when
  running the interpreter manually.
objpascal: unreproducible with fpc 3.2.2.
ocaml: unreproducible with 4.11.1.
perl6: unreproducible with rakudo 2021.09.

Unrelated changes:
Reduce diff betweens steps.
Prevent defmacro! from mutating functions: c forth logo miniMAL vb.
dart: fix recent errors and warnings
ocaml: remove metadata from symbols.

Improve the logo implementation.
Encapsulate all representation in types.lg and env.lg, unwrap numbers.
Replace some manual iterations with logo control structures.
Reduce the diff between steps.
Use native iteration in env_get and env_map
Rewrite the reader with less temporary strings.
Reduce the number of temporary lists (for example, reverse iteration
with butlast requires O(n^2) allocations).
It seems possible to remove a few exceptions: GC settings
(Dockerfile), NO_SELF_HOSTING (IMPLS.yml) and step5_EXCLUDES
(Makefile.impls) .
2024-08-05 11:40:49 -05:00

133 lines
3.7 KiB
Rust

use std::rc::Rc;
//use std::collections::HashMap;
use fnv::FnvHashMap;
#[macro_use]
extern crate lazy_static;
extern crate fnv;
extern crate itertools;
extern crate regex;
extern crate rustyline;
use rustyline::error::ReadlineError;
use rustyline::Editor;
#[macro_use]
#[allow(dead_code)]
mod types;
use crate::types::MalErr::ErrString;
use crate::types::MalVal::{Hash, Int, List, Nil, Sym, Vector};
use crate::types::{error, format_error, func, MalArgs, MalErr, MalRet, MalVal};
mod printer;
mod reader;
// TODO: figure out a way to avoid including env
#[allow(dead_code)]
mod env;
pub type Env = FnvHashMap<String, MalVal>;
// read
fn read(str: &str) -> MalRet {
reader::read_str(str.to_string())
}
// eval
fn eval_ast(v: &MalArgs, env: &Env) -> Result<MalArgs, MalErr> {
let mut lst: MalArgs = vec![];
for a in v.iter() {
match eval(a.clone(), env.clone()) {
Ok(elt) => lst.push(elt),
Err(e) => return Err(e),
}
}
return Ok(lst);
}
fn eval(ast: MalVal, env: Env) -> MalRet {
// println!("EVAL: {}", print(&ast)),
match ast {
Sym(sym) => Ok(env
.get(&sym)
.ok_or(ErrString(format!("'{}' not found", sym)))?
.clone()),
Vector(ref v, _) => match eval_ast(&v, &env) {
Ok(lst) => Ok(vector!(lst)),
Err(e) => Err(e),
}
Hash(hm, _) => {
let mut new_hm: FnvHashMap<String, MalVal> = FnvHashMap::default();
for (k, v) in hm.iter() {
new_hm.insert(k.to_string(), eval(v.clone(), env.clone())?);
}
Ok(Hash(Rc::new(new_hm), Rc::new(Nil)))
}
List(ref l, _) => {
if l.len() == 0 {
return Ok(ast);
}
match eval_ast(&l, &env) {
Ok(el) => {
let ref f = el[0].clone();
f.apply(el[1..].to_vec())
}
Err(e) => return Err(e),
}
}
_ => Ok(ast),
}
}
// print
fn print(ast: &MalVal) -> String {
ast.pr_str(true)
}
fn rep(str: &str, env: &Env) -> Result<String, MalErr> {
let ast = read(str)?;
let exp = eval(ast, env.clone())?;
Ok(print(&exp))
}
fn int_op(op: fn(i64, i64) -> i64, a: MalArgs) -> MalRet {
match (a[0].clone(), a[1].clone()) {
(Int(a0), Int(a1)) => Ok(Int(op(a0, a1))),
_ => error("invalid int_op args"),
}
}
fn main() {
// `()` can be used when no completer is required
let mut rl = Editor::<()>::new();
if rl.load_history(".mal-history").is_err() {
eprintln!("No previous history.");
}
let mut repl_env = Env::default();
repl_env.insert("+".to_string(), func(|a: MalArgs| int_op(|i, j| i + j, a)));
repl_env.insert("-".to_string(), func(|a: MalArgs| int_op(|i, j| i - j, a)));
repl_env.insert("*".to_string(), func(|a: MalArgs| int_op(|i, j| i * j, a)));
repl_env.insert("/".to_string(), func(|a: MalArgs| int_op(|i, j| i / j, a)));
loop {
let readline = rl.readline("user> ");
match readline {
Ok(line) => {
rl.add_history_entry(&line);
rl.save_history(".mal-history").unwrap();
if line.len() > 0 {
match rep(&line, &repl_env) {
Ok(out) => println!("{}", out),
Err(e) => println!("Error: {}", format_error(e)),
}
}
}
Err(ReadlineError::Interrupted) => continue,
Err(ReadlineError::Eof) => break,
Err(err) => {
println!("Error: {:?}", err);
break;
}
}
}
}