mirror of
https://github.com/ProgramSnail/Lama.git
synced 2025-12-16 19:58:46 +00:00
Added driver and parsing holes
This commit is contained in:
parent
63e4ec632b
commit
337a5d1a67
4 changed files with 77 additions and 6 deletions
41
src/Driver.ml
Normal file
41
src/Driver.ml
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
open Ostap
|
||||||
|
|
||||||
|
let parse infile =
|
||||||
|
let s = Util.read infile in
|
||||||
|
Util.parse
|
||||||
|
(object
|
||||||
|
inherit Matcher.t s
|
||||||
|
inherit Util.Lexers.decimal s
|
||||||
|
inherit Util.Lexers.skip [
|
||||||
|
Matcher.Skip.whitespaces " \t\n";
|
||||||
|
Matcher.Skip.lineComment "--";
|
||||||
|
Matcher.Skip.nestedComment "(*" "*)"
|
||||||
|
] s
|
||||||
|
end
|
||||||
|
)
|
||||||
|
(ostap (!(Language.parse) -EOF))
|
||||||
|
|
||||||
|
let main =
|
||||||
|
try
|
||||||
|
let interpret = Sys.argv.(1) = "-i" in
|
||||||
|
let stack = Sys.argv.(1) = "-s" in
|
||||||
|
let infile = Sys.argv.(2) in
|
||||||
|
match parse infile with
|
||||||
|
| `Ok ((_, stmt) as prog) ->
|
||||||
|
let rec read acc =
|
||||||
|
try
|
||||||
|
let r = read_int () in
|
||||||
|
Printf.printf "> ";
|
||||||
|
read (acc @ [r])
|
||||||
|
with End_of_file -> acc
|
||||||
|
in
|
||||||
|
let input = read [] in
|
||||||
|
let output =
|
||||||
|
if interpret
|
||||||
|
then Language.eval prog input
|
||||||
|
else SM.eval (SM.compile prog) input
|
||||||
|
in
|
||||||
|
List.iter (fun i -> Printf.printf "%d\n" i) output
|
||||||
|
| `Fail er -> Printf.eprintf "Syntax error: %s\n" er
|
||||||
|
with Invalid_argument _ ->
|
||||||
|
Printf.printf "Usage: rc [-i] <input file.expr>\n"
|
||||||
|
|
@ -3,6 +3,9 @@
|
||||||
*)
|
*)
|
||||||
open GT
|
open GT
|
||||||
|
|
||||||
|
(* Opening a library for combinator-based syntax analysis *)
|
||||||
|
open Ostap.Combinators
|
||||||
|
|
||||||
(* Simple expressions: syntax and semantics *)
|
(* Simple expressions: syntax and semantics *)
|
||||||
module Expr =
|
module Expr =
|
||||||
struct
|
struct
|
||||||
|
|
@ -43,6 +46,11 @@ module Expr =
|
||||||
*)
|
*)
|
||||||
let eval _ = failwith "Not implemented yet"
|
let eval _ = failwith "Not implemented yet"
|
||||||
|
|
||||||
|
(* Statement parser *)
|
||||||
|
ostap (
|
||||||
|
parse: empty {failwith "Not implemented yet"}
|
||||||
|
)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
(* Simple statements: syntax and sematics *)
|
(* Simple statements: syntax and sematics *)
|
||||||
|
|
@ -67,4 +75,26 @@ module Stmt =
|
||||||
*)
|
*)
|
||||||
let eval _ = failwith "Not implemented yet"
|
let eval _ = failwith "Not implemented yet"
|
||||||
|
|
||||||
|
(* Statement parser *)
|
||||||
|
ostap (
|
||||||
|
parse: empty {failwith "Not implemented yet"}
|
||||||
|
)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
(* The top-level definitions *)
|
||||||
|
|
||||||
|
(* The top-level syntax category is statement *)
|
||||||
|
type t = Stmt.t
|
||||||
|
|
||||||
|
(* Top-level evaluator
|
||||||
|
|
||||||
|
eval : t -> int list -> int list
|
||||||
|
|
||||||
|
Takes a program and its input stream, and returns the output stream
|
||||||
|
*)
|
||||||
|
let eval p i =
|
||||||
|
let _, _, o = Stmt.eval (Expr.empty, i, []) p in o
|
||||||
|
|
||||||
|
(* Top-level parser *)
|
||||||
|
let parse = Stmt.parse
|
||||||
|
|
@ -2,7 +2,7 @@ TOPFILE = rc
|
||||||
OCAMLC = ocamlc
|
OCAMLC = ocamlc
|
||||||
OCAMLOPT = ocamlopt
|
OCAMLOPT = ocamlopt
|
||||||
OCAMLDEP = ocamldep
|
OCAMLDEP = ocamldep
|
||||||
SOURCES = Syntax.ml Embedding.ml SM.ml
|
SOURCES = Language.ml Embedding.ml SM.ml Driver.ml
|
||||||
LIBS = GT.cma unix.cma re.cma re_emacs.cma re_str.cma
|
LIBS = GT.cma unix.cma re.cma re_emacs.cma re_str.cma
|
||||||
CAMLP5 = -pp "camlp5o -I `ocamlfind -query GT.syntax` -I `ocamlfind -query ostap.syntax` pa_ostap.cmo pa_gt.cmo -L `ocamlfind -query GT.syntax`"
|
CAMLP5 = -pp "camlp5o -I `ocamlfind -query GT.syntax` -I `ocamlfind -query ostap.syntax` pa_ostap.cmo pa_gt.cmo -L `ocamlfind -query GT.syntax`"
|
||||||
PXFLAGS = $(CAMLP5)
|
PXFLAGS = $(CAMLP5)
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ type prg = insn list
|
||||||
(* The type for the stack machine configuration: a stack and a configuration from statement
|
(* The type for the stack machine configuration: a stack and a configuration from statement
|
||||||
interpreter
|
interpreter
|
||||||
*)
|
*)
|
||||||
type config = int list * Syntax.Stmt.config
|
type config = int list * Language.Stmt.config
|
||||||
|
|
||||||
(* Stack machine interpreter
|
(* Stack machine interpreter
|
||||||
|
|
||||||
|
|
@ -26,7 +26,7 @@ let eval _ = failwith "Not yet implemented"
|
||||||
|
|
||||||
(* Stack machine compiler
|
(* Stack machine compiler
|
||||||
|
|
||||||
val compile : Syntax.Stmt.t -> prg
|
val compile : Language.Stmt.t -> prg
|
||||||
|
|
||||||
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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue