spice/lib/spice.ml

41 lines
854 B
OCaml

module Ast = Spice_syntax.Ast
module Code = Spice_runtime.Code
module Value = Spice_runtime.Value
open Spice_syntax
open Spice_runtime
open Spice_compile
exception Error of string
let failf f =
Fmt.kstr (fun s -> raise (Error s)) f
let parse input =
let lexbuf = Lexing.from_string input ~with_positions:true in
try Parser.modl Lexer.read lexbuf
with
| Parser.Error -> failf "syntax error"
| Lexer.Error msg -> failf "syntax error: %s" msg
type program =
{ main : Code.funct }
let compile ast =
try
{
main =
Ir.lower ast ~lib:Std.lib
|> Bcc.compile_lambda
}
with Ir.Error msg ->
failf "compile error: %s" msg
let run { main } =
try
let lib = Value.native_lib Std.lib in
let args = [] in
Interp.run main lib args
with Interp.Runtime_error msg ->
failf "runtime error: %s" msg