aboutsummaryrefslogtreecommitdiff
path: root/lib/syntax/S.ml
diff options
context:
space:
mode:
Diffstat (limited to 'lib/syntax/S.ml')
-rw-r--r--lib/syntax/S.ml95
1 files changed, 65 insertions, 30 deletions
diff --git a/lib/syntax/S.ml b/lib/syntax/S.ml
index 6bdbc9d..3b24aff 100644
--- a/lib/syntax/S.ml
+++ b/lib/syntax/S.ml
@@ -13,64 +13,79 @@
*)
+type 'a repr = Report.t list -> 'a * Report.t list
+
type pos = Lexing.position * Lexing.position
+(** Starting and ending position for the given location *)
+
type ('a, 'b) variable = { pos : 'a; name : string; index : 'b option }
+(** Describe a variable, using the name in capitalized text, and an optionnal
+ index.
+
+ If missing, the index should be considered as [0].*)
(** Represent the evaluation over an expression *)
module type Expression = sig
- type 'a obs
type t
- type repr = Report.t list -> t * Report.t list
-
- type variable = { pos : pos; name : string; index : repr option }
- (**
- Describe a variable, using the name in capitalized text, and an optionnal
- index.
+ (** Internal type used in the evaluation *)
- If missing, the index should be considered as [0].
- *)
+ type t'
+ (** External type used outside of the module *)
- val ident : variable -> repr
+ val v : t * Report.t list -> t' * Report.t list
+ val ident : (pos, t repr) variable -> t repr
(*
Basic values, text, number…
*)
- val integer : pos -> string -> repr
- val literal : pos -> string -> repr
+ val integer : pos -> string -> t repr
+ val literal : pos -> string -> t repr
- val function_ : pos -> T.function_ -> repr list -> repr
+ val function_ : pos -> T.function_ -> t repr list -> t repr
(** Call a function. The functions list is hardcoded in lib/lexer.mll *)
- val uoperator : pos -> T.uoperator -> repr -> repr
+ val uoperator : pos -> T.uoperator -> t repr -> t repr
(** Unary operator like [-123] or [+'Text']*)
- val boperator : pos -> T.boperator -> repr -> repr -> repr
+ val boperator : pos -> T.boperator -> t repr -> t repr -> t repr
(** Binary operator, for a comparaison, or an operation *)
end
module type Instruction = sig
- type repr
+ type t
+ (** Internal type used in the evaluation *)
+
+ type t'
+ (** External type used outside of the module *)
+
+ val v : t * Report.t list -> t' * Report.t list
+
type expression
- type variable
- val call : pos -> T.keywords -> expression list -> repr
+ val call : pos -> T.keywords -> expression list -> t repr
(** Call for an instruction like [GT] or [*CLR] *)
- val location : pos -> string -> repr
+ val location : pos -> string -> t repr
(** Label for a loop *)
- val comment : pos -> repr
+ val comment : pos -> t repr
(** Comment *)
- val expression : expression -> repr
+ val expression : expression -> t repr
(** Raw expression *)
- type clause = pos * expression * repr list
+ type clause = pos * expression * t repr list
- val if_ : pos -> clause -> elifs:clause list -> else_:repr list -> repr
- val act : pos -> label:expression -> repr list -> repr
- val assign : pos -> variable -> T.assignation_operator -> expression -> repr
+ val if_ : pos -> clause -> elifs:clause list -> else_:t repr list -> t repr
+ val act : pos -> label:expression -> t repr list -> t repr
+
+ val assign :
+ pos ->
+ (pos, expression) variable ->
+ T.assignation_operator ->
+ expression ->
+ t repr
end
module type Location = sig
@@ -82,11 +97,31 @@ end
module type Analyzer = sig
module Expression : Expression
+ module Instruction : Instruction with type expression = Expression.t' repr
+ module Location : Location with type instruction = Instruction.t' repr
+end
+
+(** Helper module used in order to convert elements from the differents
+ representation levels *)
+module Helper (E : sig
+ type t
+ (** Internal type used in the evaluation *)
+
+ type t'
+ (** External type used outside of the module *)
+
+ val v : t * Report.t list -> t' * Report.t list
+end) : sig
+ val v : E.t repr -> E.t' repr
- module Instruction :
- Instruction
- with type expression = Expression.repr
- and type variable = Expression.variable
+ val variable : (pos, E.t repr) variable -> (pos, E.t' repr) variable
+ (** Convert a variable from the [Expression.t] into [Expression.t'] *)
+end = struct
+ let v : E.t repr -> E.t' repr =
+ fun v report ->
+ let value, report = v report in
+ E.v (value, report)
- module Location : Location with type instruction = Instruction.repr
+ let variable : (pos, E.t repr) variable -> (pos, E.t' repr) variable =
+ fun variable -> { variable with index = Option.map v variable.index }
end