spice/lib/spice.ml

41 lines
854 B
OCaml
Raw Permalink Normal View History

2023-12-02 18:50:24 +00:00
module Ast = Spice_syntax.Ast
2023-11-29 21:21:35 +00:00
module Code = Spice_runtime.Code
2023-11-29 21:48:24 +00:00
module Value = Spice_runtime.Value
2023-11-29 18:43:14 +00:00
open Spice_syntax
open Spice_runtime
open Spice_compile
2023-11-29 18:43:14 +00:00
exception Error of string
2023-12-02 22:02:40 +00:00
let failf f =
Fmt.kstr (fun s -> raise (Error s)) f
2023-11-24 04:06:13 +00:00
let parse input =
let lexbuf = Lexing.from_string input ~with_positions:true in
try Parser.modl Lexer.read lexbuf
2023-12-02 22:02:40 +00:00
with
| Parser.Error -> failf "syntax error"
| Lexer.Error msg -> failf "syntax error: %s" msg
2023-11-29 18:43:14 +00:00
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
2023-11-29 21:50:26 +00:00
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