shithub: martian9

ref: b6f4824d97a68ecfa763e1edcbef629ff3ba1cfc
dir: /m9.ml/

View raw version
(*
  Martian Scheme
  Copyright 2020, McKay Marston

  This is a project for me to
    1) Get more familiar with OCaml.
    2) Try to provide a natively supported r7rs-small scheme for Plan9.

  It is heavily inspired by s9fes (http://www.t3x.org/s9fes), and the
  make a lisp project (https://github.com/kanaka/mal - thanks
  https://github.com/chouser for the fantastic implementation!)
 *)
module T = Types.Types

let repl_env = Env.make (Some Core.base)
let nameplate = "Martian9 Scheme v0.1"
let read str = Reader.read str
let print exp = Printer.print exp true
let rep str env = print (Eval.eval (read str) env)

let rec main =
  try
    Core.init Core.base;
    Env.set
      repl_env
      (Types.symbol "eval")
      (Types.proc (function
          | [ ast ] -> Eval.eval ast repl_env
          | _ -> T.Nil));
    ignore (rep "(define load-file (lambda (f) (eval (read-string (string \"(begin \" (slurp f) \")\")))))" repl_env);
    if Array.length Sys.argv > 1
    then print_endline (rep ("(load-file \"" ^ Sys.argv.(1) ^ "\")") repl_env)
    else (
      print_endline nameplate;
      while true do
        print_string "m9> ";
        let line = read_line () in
        try print_endline (rep line repl_env) with
        | End_of_file -> ()
        | Invalid_argument x ->
          output_string stderr ("Invalid argument: " ^ x ^ "\n");
          flush stderr
      done)
  with
  | End_of_file -> ()
;;