diff --git a/lex.ml b/lex.ml index 3c0a252..d6e2425 100644 --- a/lex.ml +++ b/lex.ml @@ -1,4 +1,4 @@ -open Ast.Value +open Token exception Invalid_character of char exception Expected of char @@ -48,7 +48,7 @@ let expect_token str tok seq = aux str seq |> Option.map (fun s -> tok, s) let find_token seq = - !Token.tokens |> List.find_map + !tokens |> List.find_map (fun (s, t) -> expect_token s t seq) (* same as take_while f seq, drop_while f seq *) @@ -79,7 +79,7 @@ let tokenize str = let str, seq = partition_while ((<>) '"') seq in let str = String (String.of_seq str) in let seq = expect_char '"' seq in - cons (Value str) (aux seq) + cons str (aux seq) (* number (int, float) *) else if is_digit x then @@ -90,7 +90,7 @@ let tokenize str = then Float (float_of_string n) else Int (int_of_string n) in - cons (Value n) (aux seq) + cons n (aux seq) (* idents *) else if is_ident_start x then diff --git a/parser.ml b/parser.ml index 12d2331..b2d99a7 100644 --- a/parser.ml +++ b/parser.ml @@ -208,8 +208,10 @@ and value seq = match seq () with | Seq.Nil -> raise End_of_tokens | Seq.Cons (x, seq) -> begin match x with - | Token.Value x -> Value x, seq - | Ident id -> Var id, seq + | Token.Ident id -> Var id, seq + | Int x -> Value (Int x), seq + | Float x -> Value (Float x), seq + | String x -> Value (String x), seq | LParen -> let e, seq = expr min_int seq in let _, seq = token RParen seq in diff --git a/token.ml b/token.ml index 6c28600..997f9db 100644 --- a/token.ml +++ b/token.ml @@ -1,5 +1,7 @@ type t = - | Value of Ast.Value.t + | Int of int + | Float of float + | String of string | Ident of string | Plus | Minus @@ -24,7 +26,9 @@ let tokens = ref [ ] let to_string = function - | Value v -> Ast.Value.to_string v + | Int n -> string_of_int n + | Float n -> string_of_float n + | String x -> x | Ident s -> s | t -> begin match List.find_opt (fun (_, tok) -> t = tok) !tokens with