diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/syntax/S.ml | 3 | ||||
| -rw-r--r-- | lib/syntax/tree.ml | 46 | ||||
| -rw-r--r-- | lib/syntax/tree.mli | 2 | 
3 files changed, 37 insertions, 14 deletions
| diff --git a/lib/syntax/S.ml b/lib/syntax/S.ml index 63fcd08..6bdbc9d 100644 --- a/lib/syntax/S.ml +++ b/lib/syntax/S.ml @@ -19,7 +19,8 @@ type ('a, 'b) variable = { pos : 'a; name : string; index : 'b option }  (** Represent the evaluation over an expression *)  module type Expression = sig    type 'a obs -  type repr +  type t +  type repr = Report.t list -> t * Report.t list    type variable = { pos : pos; name : string; index : repr option }    (**  diff --git a/lib/syntax/tree.ml b/lib/syntax/tree.ml index ecad1b4..51033a1 100644 --- a/lib/syntax/tree.ml +++ b/lib/syntax/tree.ml @@ -1,3 +1,5 @@ +open StdLabels +  type pos = Lexing.position * Lexing.position  module Ast = struct @@ -34,25 +36,34 @@ module Ast = struct  end  (** Default implementation for the expression *) -module Expression : S.Expression with type repr = pos Ast.expression = struct +module Expression : S.Expression with type t = pos Ast.expression = struct    type 'a obs -  type repr = pos Ast.expression +  type t = pos Ast.expression +  type repr = Report.t list -> t * Report.t list    type variable = { pos : pos; name : string; index : repr option } -  let integer : pos -> string -> repr = fun pos i -> Ast.Integer (pos, i) -  let literal : pos -> string -> repr = fun pos l -> Ast.Literal (pos, l) +  let integer : pos -> string -> repr = fun pos i r -> (Ast.Integer (pos, i), r) +  let literal : pos -> string -> repr = fun pos l r -> (Ast.Literal (pos, l), r)    let function_ : pos -> T.function_ -> repr list -> repr = -   fun pos name args -> Ast.Function (pos, name, args) +   fun pos name args r -> +    let args = List.map ~f:(fun f -> fst (f r)) args in +    (Ast.Function (pos, name, args), r)    let uoperator : pos -> T.uoperator -> repr -> repr = -   fun pos op expression -> Ast.Op (pos, op, expression) +   fun pos op expression r -> +    let expression = fst (expression r) in +    (Ast.Op (pos, op, expression), r)    let boperator : pos -> T.boperator -> repr -> repr -> repr = -   fun pos op op1 op2 -> Ast.BinaryOp (pos, op, op1, op2) +   fun pos op op1 op2 r -> +    let op1 = fst (op1 r) and op2 = fst (op2 r) in +    (Ast.BinaryOp (pos, op, op1, op2), r)    let ident : variable -> repr = -   fun { pos; name; index } -> Ast.Ident { pos; name; index } +   fun { pos; name; index } r -> +    let index = Option.map (fun i -> fst (i r)) index in +    (Ast.Ident { pos; name; index }, r)  end  module Instruction : @@ -65,25 +76,36 @@ module Instruction :    type variable = Expression.variable    let call : pos -> T.keywords -> expression list -> repr = -   fun pos name args -> Ast.Call (pos, name, args) +   fun pos name args -> +    let args = List.map ~f:(fun f -> fst (f [])) args in +    Ast.Call (pos, name, args)    let location : pos -> string -> repr =     fun loc label -> Ast.Location (loc, label)    let comment : pos -> repr = fun pos -> Ast.Comment pos -  let expression : expression -> repr = fun expr -> Ast.Expression expr + +  let expression : expression -> repr = +   fun expr -> Ast.Expression (fst (expr []))    type clause = pos * expression * repr list    let if_ : pos -> clause -> elifs:clause list -> else_:repr list -> repr =     fun pos predicate ~elifs ~else_ -> -    Ast.If { loc = pos; then_ = predicate; elifs; else_ } +    let clause (pos, expr, repr) = (pos, fst (expr []), repr) in +    let elifs = List.map ~f:clause elifs in + +    Ast.If { loc = pos; then_ = clause predicate; elifs; else_ }    let act : pos -> label:expression -> repr list -> repr = -   fun pos ~label statements -> Ast.Act { loc = pos; label; statements } +   fun pos ~label statements -> +    let label = fst (label []) in +    Ast.Act { loc = pos; label; statements }    let assign : pos -> variable -> T.assignation_operator -> expression -> repr =     fun pos_loc { pos; name; index } op expr -> +    let index = Option.map (fun i -> fst (i [])) index +    and expr = fst (expr []) in      Ast.Declaration (pos_loc, { pos; name; index }, op, expr)  end diff --git a/lib/syntax/tree.mli b/lib/syntax/tree.mli index ad052e9..c54a9ff 100644 --- a/lib/syntax/tree.mli +++ b/lib/syntax/tree.mli @@ -46,6 +46,6 @@ end  include    S.Analyzer -    with type Expression.repr = Ast.pos Ast.expression +    with type Expression.t = Ast.pos Ast.expression       and type Instruction.repr = Ast.pos Ast.statement       and type Location.repr = Ast.pos * Ast.pos Ast.statement list | 
