Added attempt at internal ast

This commit is contained in:
Denis Merigoux 2020-03-12 18:04:27 +01:00
parent 4a56886a0f
commit 90354f911d
5 changed files with 178 additions and 1 deletions

View File

@ -2,7 +2,7 @@ build: format
dune build
format:
dune build @fmt --auto-promote
dune build @fmt --auto-promote | true
test: build
dune exec src/main.exe -- --debug --backend LaTeX --output \

View File

@ -13,6 +13,7 @@
the License. *)
open Cli
module I = Ir
(** Entry function for the executable. Returns a negative number in case of error. *)
let driver (source_files : string list) (debug : bool) (backend : string) (output_file : string) :

39
src/lawspec/ir/id.ml Normal file
View File

@ -0,0 +1,39 @@
(* This file is part of the Lawspec compiler, a specification language for tax and social benefits
computation rules. Copyright (C) 2019 Inria, contributor: Denis Merigoux
<denis.merigoux@inria.fr>
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing permissions and limitations under
the License. *)
module WithId (X : sig
type t
val to_string : t -> string
end) : sig
type t
val fresh : X.t -> t
val to_string : t -> string
val compare : t -> t -> int
end = struct
let counter : int ref = ref 0
type t = { id : int; value : X.t }
let fresh (x : X.t) =
counter := !counter + 1;
{ id = !counter; value = x }
let to_string (x : t) = X.to_string x.value ^ "_" ^ string_of_int x.id
let compare (x1 : t) (x2 : t) = x1.id - x2.id
end

84
src/lawspec/ir/ir.ml Normal file
View File

@ -0,0 +1,84 @@
(* This file is part of the Lawspec compiler, a specification language for tax and social benefits
computation rules. Copyright (C) 2019 Inria, contributor: Denis Merigoux
<denis.merigoux@inria.fr>
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing permissions and limitations under
the License. *)
module Field = Id.WithId (struct
type t = string Pos.marked
(** The position corresponds to the declaration *)
let to_string x = Pos.unmark x
end)
module Class = Id.WithId (struct
type t = string
let to_string (x : string) = x
end)
module Constructor = Id.WithId (struct
type t = string Pos.marked
(** The position corresponds to the declaration *)
let to_string x = Pos.unmark x
end)
module Field = Id.WithId (struct
type t = string Pos.marked
(** The position corresponds to the declaration *)
let to_string x = Pos.unmark x
end)
module Enum = Id.WithId (struct
type t = string Pos.marked
(** The position corresponds to the declaration *)
let to_string x = Pos.unmark x
end)
module ClassMap = Map.Make (Class)
module EnumMap = Map.Make (Enum)
module ConstructorMap = Map.Make (Constructor)
module FieldMap = Map.Make (Field)
type base_typ = Integer | Boolean
type typ = BaseTyp of base_typ | EnumTyp of Enum.t | ClassTyp of Class.t
type expression = unit
type setter_method = unit
type function_method = unit
type assert_method = unit
type meta_assert_method = unit
type field_typ = { field_typ_typ : typ }
type method_t =
| SetterMethod of setter_method Pos.marked
| FunctionMethod of function_method Pos.marked
| AssertMethod of assert_method Pos.marked
| MetaAssertMethod of meta_assert_method Pos.marked
type class_t = {
class_fields : typ FieldMap.t;
class_methods : method_t Pos.marked list;
class_inherits : Class.t list;
}
type enum = typ ConstructorMap.t
type program = { program_classes : class_t ClassMap.t; program_enums : enum EnumMap.t }

53
src/lawspec/pos.ml Normal file
View File

@ -0,0 +1,53 @@
(* This file is part of the Lawspec compiler, a specification language for tax and social benefits
computation rules. Copyright (C) 2019 Inria, contributor: Denis Merigoux
<denis.merigoux@inria.fr>
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing permissions and limitations under
the License. *)
(** {1 Source code position} *)
type t = { pos_filename : string; pos_loc : Lexing.position * Lexing.position }
(** A position in the source code is a file, as well as begin and end location of the form col:line *)
let to_string (pos : t) : string =
let s, e = pos.pos_loc in
Printf.sprintf "in file %s, from %d:%d to %d:%d" pos.pos_filename s.Lexing.pos_lnum
(s.Lexing.pos_cnum - s.Lexing.pos_bol + 1)
e.Lexing.pos_lnum
(e.Lexing.pos_cnum - e.Lexing.pos_bol + 1)
let to_string_short (pos : t) : string =
let s, e = pos.pos_loc in
Printf.sprintf "%s;%d:%d--%d:%d" pos.pos_filename s.Lexing.pos_lnum
(s.Lexing.pos_cnum - s.Lexing.pos_bol + 1)
e.Lexing.pos_lnum
(e.Lexing.pos_cnum - e.Lexing.pos_bol + 1)
type 'a marked = 'a * t
(** Everything related to the source code should keep its position stored, to improve error messages *)
(** Placeholder position *)
let no_pos : t =
let zero_pos =
{ Lexing.pos_fname = ""; Lexing.pos_lnum = 0; Lexing.pos_cnum = 0; Lexing.pos_bol = 0 }
in
{ pos_filename = "unknown position"; pos_loc = (zero_pos, zero_pos) }
let unmark ((x, _) : 'a marked) : 'a = x
let get_position ((_, x) : 'a marked) : t = x
let map_under_mark (f : 'a -> 'b) ((x, y) : 'a marked) : 'b marked = (f x, y)
let same_pos_as (x : 'a) ((_, y) : 'b marked) : 'a marked = (x, y)
let unmark_option (x : 'a marked option) : 'a option =
match x with Some x -> Some (unmark x) | None -> None