Benjamin Loison a17b3c6fdd Make real type works
Otherwise for the following code:

```
node test (i: real) returns (o: real);
let
    o = 0.0;
tel
```

was experiencing:

```
Fatal error: exception Stdlib.Parsing.Parse_error
Raised at Stdlib__Parsing.yyparse.loop in file "parsing.ml", line 139, characters 8-25
Called from Stdlib__Parsing.yyparse in file "parsing.ml", line 165, characters 4-28
Re-raised at Stdlib__Parsing.yyparse in file "parsing.ml", line 184, characters 8-17
Called from Parser.main in file "parser.ml" (inlined), line 1110, characters 4-44
Called from Main in file "main.ml", line 70, characters 16-68
```

Note that `%token REAL` doesn't help to solve this error, but it doesn't seem to be any reason for not having it.
2022-12-10 00:05:07 +01:00

68 lines
1.8 KiB
OCaml

{
open Lexing
open Ast
open Parser (* The type token is defined in parser.mli *)
exception Lexing_error of string
let id_or_keywork =
let h = Hashtbl.create 100 in
List.iter (fun (s,k) -> Hashtbl.add h s k)
[ ("let", LET);
("tel", TEL);
("node", NODE);
("returns", RETURNS);
("var", VAR);
("int", TYP(Ast.TInt));
("real", TYP(Ast.TReal));
("bool", TYP(Ast.TBool));
("<=", CMP_le);
(">=", CMP_ge);
("not", MO_not);
("mod", BO_mod);
("&&", BO_and);
("and", BO_and);
("||", BO_or);
("or", BO_or);
("<>", CMP_neq);
("if", IF);
("then", THEN);
("else", ELSE);
("merge", TO_merge);
("when", WHEN);
("pre", MO_pre);
("true", CONST_BOOL(true));
("false", CONST_BOOL(false));
("fby", BO_fby);
];
fun s ->
try Hashtbl.find h s with Not_found -> IDENT s
}
let alpha = ['a'-'z' 'A'-'Z']
let digit = ['0'-'9']
let ident = alpha (alpha | digit | '_')*
rule token = parse
['\n' ' ' '\t'] { token lexbuf } (* skip blanks and newlines *)
| ident { id_or_keywork (lexeme lexbuf) }
| digit+ { CONST_INT(int_of_string (lexeme lexbuf)) }
| digit*'.'digit+ { CONST_REAL(float_of_string (lexeme lexbuf)) }
| ',' { COMMA }
| '=' { EQUAL }
| '(' { LPAREN }
| ')' { RPAREN }
| ';' { SEMICOL }
| ':' { COLON }
| '<' { CMP_lt }
| '>' { CMP_gt }
| '+' { PLUS }
| '-' { MINUS }
| '*' { BO_mul }
| '/' { BO_div }
| '%' { BO_mod }
| "->" { BO_arrow }
| eof { EOF }
| _ { raise (Lexing_error (Format.sprintf "Erruer à la vue de %s" (lexeme lexbuf)))}