mirror of
https://github.com/ProgramSnail/Lama.git
synced 2025-12-06 14:58:50 +00:00
Intermediate
This commit is contained in:
parent
f4ee6c5c5e
commit
4847ee60fb
3 changed files with 8 additions and 8 deletions
|
|
@ -21,7 +21,7 @@ let main =
|
||||||
let stack = Sys.argv.(1) = "-s" in
|
let stack = Sys.argv.(1) = "-s" in
|
||||||
let infile = Sys.argv.(2) in
|
let infile = Sys.argv.(2) in
|
||||||
match parse infile with
|
match parse infile with
|
||||||
| `Ok ((_, stmt) as prog) ->
|
| `Ok prog ->
|
||||||
let rec read acc =
|
let rec read acc =
|
||||||
try
|
try
|
||||||
let r = read_int () in
|
let r = read_int () in
|
||||||
|
|
@ -33,7 +33,7 @@ let main =
|
||||||
let output =
|
let output =
|
||||||
if interpret
|
if interpret
|
||||||
then Language.eval prog input
|
then Language.eval prog input
|
||||||
else SM.eval (SM.compile prog) input
|
else SM.run (SM.compile prog) input
|
||||||
in
|
in
|
||||||
List.iter (fun i -> Printf.printf "%d\n" i) output
|
List.iter (fun i -> Printf.printf "%d\n" i) output
|
||||||
| `Fail er -> Printf.eprintf "Syntax error: %s\n" er
|
| `Fail er -> Printf.eprintf "Syntax error: %s\n" er
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
open GT
|
open GT
|
||||||
open Syntax
|
open Language
|
||||||
|
|
||||||
(* The type for the stack machine instructions *)
|
(* The type for the stack machine instructions *)
|
||||||
@type insn =
|
@type insn =
|
||||||
|
|
@ -39,11 +39,11 @@ let rec eval ((stack, ((st, i, o) as c)) as conf) = function
|
||||||
|
|
||||||
(* Top-level evaluation
|
(* Top-level evaluation
|
||||||
|
|
||||||
val run : int list -> prg -> int list
|
val run : prg -> int list -> int list
|
||||||
|
|
||||||
Takes an input stream, a program, and returns an output stream this program calculates
|
Takes an input stream, a program, and returns an output stream this program calculates
|
||||||
*)
|
*)
|
||||||
let run i p = let (_, (_, _, o)) = eval ([], (Expr.empty, i, [])) p in o
|
let run p i = let (_, (_, _, o)) = eval ([], (Expr.empty, i, [])) p in o
|
||||||
|
|
||||||
(* Stack machine compiler
|
(* Stack machine compiler
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue