Compare commits
No commits in common. "8ef4d035a3769381e6a9595b759d2bdeddfa2207" and "298e88f1a5b98b24b6ab9ef673dd44d4c0078c97" have entirely different histories.
8ef4d035a3
...
298e88f1a5
@ -1,4 +0,0 @@
|
||||
(** Maximum Number of variabnles declared in a sngle node.
|
||||
* This corresponds to the sum of the number of local, input and output
|
||||
* variables. *)
|
||||
let maxvar = 100
|
@ -65,21 +65,19 @@ let _ =
|
||||
(** Main functionality below *)
|
||||
print_verbose "Parsing the source file...";
|
||||
let ast =
|
||||
let inchan = open_in !source_file in
|
||||
try
|
||||
begin
|
||||
let inchan = open_in !source_file in
|
||||
let res = Parser.main Lexer.token (Lexing.from_channel inchan) in
|
||||
close_in inchan; res
|
||||
end
|
||||
with
|
||||
| Lexer.Lexing_error s ->
|
||||
(close_in_noerr inchan;
|
||||
exit_error (Format.sprintf "Error code:\n\t%s\n\n" s); exit 0)
|
||||
(exit_error (Format.sprintf "Error code:\n\t%s\n\n" s); exit 0)
|
||||
| Utils.MyParsingError (s, l) ->
|
||||
begin
|
||||
close_in_noerr inchan;
|
||||
Format.printf "Syntax error at %a: %s\n\n"
|
||||
Pp.pp_loc (l, !source_file) s;
|
||||
Pp.pp_loc l s;
|
||||
exit 0
|
||||
end in
|
||||
|
||||
|
@ -4,9 +4,9 @@
|
||||
|
||||
let current_location () = symbol_start_pos (), symbol_end_pos ()
|
||||
|
||||
let defined_nodes : (ident, t_node) Hashtbl.t = Hashtbl.create Config.maxvar
|
||||
let defined_nodes : (ident, t_node) Hashtbl.t = Hashtbl.create 100
|
||||
|
||||
let defined_vars : (ident, t_var * bool) Hashtbl.t = Hashtbl.create Config.maxvar
|
||||
let defined_vars : (ident, t_var) Hashtbl.t = Hashtbl.create 100
|
||||
|
||||
let fetch_node (n: ident) =
|
||||
match Hashtbl.find_opt defined_nodes n with
|
||||
@ -20,19 +20,7 @@
|
||||
| None ->
|
||||
raise (MyParsingError
|
||||
("The var "^n^" does not exist.", current_location()))
|
||||
| Some (var, _) -> var
|
||||
|
||||
let fetch_var_def (n: ident) : t_var =
|
||||
match Hashtbl.find_opt defined_vars n with
|
||||
| None ->
|
||||
raise (MyParsingError
|
||||
("The var "^n^" does not exist.", current_location()))
|
||||
| Some (var, true) ->
|
||||
raise (MyParsingError
|
||||
("The variable "^n^" is defined for the second time.",
|
||||
current_location()))
|
||||
| Some (var, false) ->
|
||||
(Hashtbl.replace defined_vars n (var, true) ; var)
|
||||
| Some var -> var
|
||||
|
||||
let concat_varlist (t1, e1) (t2, e2) = (t1 @ t2, e1 @ e2)
|
||||
|
||||
@ -217,14 +205,11 @@ param:
|
||||
(list_repeat (List.length idents) typ,
|
||||
match typ with
|
||||
| TBool ->
|
||||
List.map (fun s ->
|
||||
Hashtbl.add defined_vars s (BVar s, false); BVar s) idents
|
||||
List.map (fun s -> Hashtbl.add defined_vars s (BVar s); BVar s) idents
|
||||
| TReal ->
|
||||
List.map (fun s ->
|
||||
Hashtbl.add defined_vars s (RVar s, false); RVar s) idents
|
||||
List.map (fun s -> Hashtbl.add defined_vars s (RVar s); RVar s) idents
|
||||
| TInt ->
|
||||
List.map (fun s ->
|
||||
Hashtbl.add defined_vars s (IVar s, false); IVar s) idents) }
|
||||
List.map (fun s -> Hashtbl.add defined_vars s (IVar s); IVar s) idents) }
|
||||
;
|
||||
|
||||
ident_comma_list:
|
||||
@ -248,12 +233,12 @@ equation:
|
||||
|
||||
pattern:
|
||||
| IDENT
|
||||
{ let v = fetch_var_def $1 in (type_var v, [v]) }
|
||||
{ let v = fetch_var $1 in (type_var v, [v]) }
|
||||
| LPAREN ident_comma_list_patt RPAREN { $2 };
|
||||
|
||||
ident_comma_list_patt:
|
||||
| IDENT { make_ident (fetch_var_def $1) }
|
||||
| IDENT COMMA ident_comma_list_patt { add_ident (fetch_var_def $1) $3 }
|
||||
| IDENT { make_ident (fetch_var $1) }
|
||||
| IDENT COMMA ident_comma_list_patt { add_ident (fetch_var $1) $3 }
|
||||
|
||||
expr:
|
||||
/* Note: EQUAL does not follow the nomenclature CMP_, ... */
|
||||
|
25
src/pp.ml
25
src/pp.ml
@ -6,25 +6,12 @@ open Ast
|
||||
| TBool :: t -> Format.fprintf fmt "bool %a" debug_type_pp t
|
||||
| TReal :: t -> Format.fprintf fmt "real %a" debug_type_pp t
|
||||
|
||||
let pp_loc fmt ((start, stop), file) =
|
||||
let spos, epos =
|
||||
Lexing.(start.pos_cnum, stop.pos_cnum) in
|
||||
let f = open_in file in
|
||||
try
|
||||
begin
|
||||
let rec aux linenum curpos =
|
||||
let line = input_line f in
|
||||
let nextpos = curpos + (String.length line) + 1 in
|
||||
if nextpos >= epos then
|
||||
Format.fprintf fmt "<line %d: %s >" linenum line
|
||||
else
|
||||
aux (linenum + 1) nextpos
|
||||
in
|
||||
aux 1 0;
|
||||
close_in f
|
||||
end
|
||||
with e ->
|
||||
(close_in_noerr f; Format.fprintf fmt "???")
|
||||
let pp_loc fmt (start, stop) =
|
||||
Lexing.(
|
||||
Format.fprintf fmt "%s: <l: %d, c: %d> -- <l: %d, c: %d>"
|
||||
start.pos_fname
|
||||
start.pos_lnum start.pos_cnum
|
||||
stop.pos_lnum stop.pos_cnum)
|
||||
|
||||
let rec pp_varlist fmt : t_varlist -> unit = function
|
||||
| ([], []) -> ()
|
||||
|
Loading…
Reference in New Issue
Block a user