lama_byterun/src/Language.ml

103 lines
3 KiB
OCaml
Raw Normal View History

2018-02-20 01:28:29 +03:00
(* Opening a library for generic programming (https://github.com/dboulytchev/GT).
The library provides "@type ..." syntax extension and plugins like show, etc.
*)
2018-02-25 14:48:13 +03:00
open GT
(* Opening a library for combinator-based syntax analysis *)
open Ostap.Combinators
2018-02-20 01:28:29 +03:00
(* Simple expressions: syntax and semantics *)
module Expr =
struct
(* The type for expressions. Note, in regular OCaml there is no "@type..."
notation, it came from GT.
*)
@type t =
(* integer constant *) | Const of int
(* variable *) | Var of string
(* binary operator *) | Binop of string * t * t with show
(* Available binary operators:
!! --- disjunction
&& --- conjunction
==, !=, <=, <, >=, > --- comparisons
+, - --- addition, subtraction
*, /, % --- multiplication, division, reminder
*)
(* State: a partial map from variables to integer values. *)
type state = string -> int
(* Empty state: maps every variable into nothing. *)
let empty = fun x -> failwith (Printf.sprintf "Undefined variable %s" x)
(* Update: non-destructively "modifies" the state s by binding the variable x
to value v and returns the new state.
*)
let update x v s = fun y -> if x = y then v else s y
(* Expression evaluator
val eval : state -> t -> int
Takes a state and an expression, and returns the value of the expression in
the given state.
*)
let eval _ = failwith "Not implemented yet"
2018-02-25 19:10:00 +03:00
(* Expression parser. You can use the following terminals:
2018-02-25 19:12:57 +03:00
IDENT --- a non-empty identifier a-zA-Z[a-zA-Z0-9_]* as a string
2018-02-25 19:10:00 +03:00
DECIMAL --- a decimal constant [0-9]+ as a string
*)
2018-02-25 14:48:13 +03:00
ostap (
parse: empty {failwith "Not implemented yet"}
)
2018-02-20 01:28:29 +03:00
end
(* Simple statements: syntax and sematics *)
module Stmt =
struct
(* The type for statements *)
@type t =
(* read into the variable *) | Read of string
(* write the value of an expression *) | Write of Expr.t
(* assignment *) | Assign of string * Expr.t
(* composition *) | Seq of t * t with show
(* The type of configuration: a state, an input stream, an output stream *)
type config = Expr.state * int list * int list
(* Statement evaluator
val eval : config -> t -> config
Takes a configuration and a statement, and returns another configuration
*)
let eval _ = failwith "Not implemented yet"
2018-02-25 14:48:13 +03:00
(* Statement parser *)
ostap (
parse: empty {failwith "Not implemented yet"}
)
2018-02-20 01:28:29 +03:00
end
2018-02-25 14:48:13 +03:00
(* The top-level definitions *)
(* The top-level syntax category is statement *)
type t = Stmt.t
(* Top-level evaluator
2018-03-06 18:37:17 +03:00
eval : t -> int list -> int list
2018-02-25 14:48:13 +03:00
Takes a program and its input stream, and returns the output stream
*)
2018-03-06 18:37:17 +03:00
let eval p i =
2018-02-25 14:48:13 +03:00
let _, _, o = Stmt.eval (Expr.empty, i, []) p in o