Intermediate

This commit is contained in:
Dmitry Boulytchev 2018-02-25 14:59:59 +03:00
parent f4ee6c5c5e
commit 4847ee60fb
3 changed files with 8 additions and 8 deletions

View file

@ -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

View file

@ -94,7 +94,7 @@ module Stmt =
(* Statement evaluator (* Statement evaluator
val eval : config -> t -> config val eval : config -> t -> config
Takes a configuration and a statement, and returns another configuration Takes a configuration and a statement, and returns another configuration
*) *)

View file

@ -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
@ -52,7 +52,7 @@ let run i p = let (_, (_, _, o)) = eval ([], (Expr.empty, i, [])) p in o
Takes a program in the source language and returns an equivalent program for the Takes a program in the source language and returns an equivalent program for the
stack machine stack machine
*) *)
let rec compile = let rec compile =
let rec expr = function let rec expr = function