aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChimrod <>2023-10-13 16:18:34 +0200
committerChimrod <>2023-10-18 09:49:47 +0200
commitf135a5fcb6c9827d07025a143b190cbbf8eddc15 (patch)
tree6dd0ce2c1ab0a827872289336c732378f04f81fd
parent4b47c572fc7122154813c8bb5da477c0f152f002 (diff)
Made the interface more parametrized
-rw-r--r--lib/qparser/parser.mly2
-rw-r--r--lib/syntax/S.ml10
-rw-r--r--lib/syntax/tree.ml8
-rw-r--r--lib/syntax/type_of.ml20
4 files changed, 28 insertions, 12 deletions
diff --git a/lib/qparser/parser.mly b/lib/qparser/parser.mly
index 8547e17..556a9ec 100644
--- a/lib/qparser/parser.mly
+++ b/lib/qparser/parser.mly
@@ -11,7 +11,7 @@
; body : Analyzer.Instruction.t Qsp_syntax.S.repr list
; pos : Qsp_syntax.S.pos
; else_ : (
- ( Analyzer.Instruction.clause list
+ ( (Analyzer.Instruction.expression, Analyzer.Instruction.t) Qsp_syntax.S.clause list
* Analyzer.Instruction.t Qsp_syntax.S.repr list
) option )
}
diff --git a/lib/syntax/S.ml b/lib/syntax/S.ml
index 3b24aff..e6c472d 100644
--- a/lib/syntax/S.ml
+++ b/lib/syntax/S.ml
@@ -24,6 +24,8 @@ type ('a, 'b) variable = { pos : 'a; name : string; index : 'b option }
If missing, the index should be considered as [0].*)
+type ('a, 'b) clause = pos * 'a * 'b repr list
+
(** Represent the evaluation over an expression *)
module type Expression = sig
type t
@@ -75,9 +77,13 @@ module type Instruction = sig
val expression : expression -> t repr
(** Raw expression *)
- type clause = pos * expression * t repr list
+ val if_ :
+ pos ->
+ (expression, t) clause ->
+ elifs:(expression, t) clause list ->
+ else_:t repr list ->
+ t 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 :
diff --git a/lib/syntax/tree.ml b/lib/syntax/tree.ml
index 02c6b36..db8abd9 100644
--- a/lib/syntax/tree.ml
+++ b/lib/syntax/tree.ml
@@ -89,10 +89,12 @@ module Instruction :
let expression : expression -> t S.repr =
fun expr report -> (Ast.Expression (fst (expr [])), report)
- type clause = S.pos * expression * t S.repr list
-
let if_ :
- S.pos -> clause -> elifs:clause list -> else_:t S.repr list -> t S.repr =
+ S.pos ->
+ (expression, t) S.clause ->
+ elifs:(expression, t) S.clause list ->
+ else_:t S.repr list ->
+ t S.repr =
fun pos predicate ~elifs ~else_ report ->
let clause (pos, expr, repr) =
let repr = List.map ~f:(fun instr -> fst @@ instr []) repr in
diff --git a/lib/syntax/type_of.ml b/lib/syntax/type_of.ml
index 83258cc..a04d37b 100644
--- a/lib/syntax/type_of.ml
+++ b/lib/syntax/type_of.ml
@@ -126,8 +126,13 @@ module Expression = struct
let integer : S.pos -> string -> t S.repr =
fun pos value report ->
- let empty =
- match int_of_string_opt value with Some 0 -> true | _ -> false
+ let int_value = int_of_string_opt value in
+
+ let empty, report =
+ match int_value with
+ | Some 0 -> (true, report)
+ | Some _ -> (false, report)
+ | None -> (false, Report.error pos "Invalid integer value" :: report)
in
({ result = Integer; pos; empty }, report)
@@ -308,13 +313,16 @@ module Instruction = struct
let expression : expression -> t S.repr =
fun expression report -> ((), snd (expression report))
- type clause = S.pos * expression * t S.repr list
-
let if_ :
- S.pos -> clause -> elifs:clause list -> else_:t S.repr list -> t S.repr =
+ S.pos ->
+ (expression, t) S.clause ->
+ elifs:(expression, t) S.clause list ->
+ else_:t S.repr list ->
+ t S.repr =
fun _pos clause ~elifs ~else_ report ->
(* Helper function *)
- let fold_clause : t * Report.t list -> clause -> t * Report.t list =
+ let fold_clause :
+ t * Report.t list -> (expression, t) S.clause -> t * Report.t list =
fun ((), report) (_pos, expr, instructions) ->
let result, report = expr report in
let report =