Seperate Value.t and Token.t
This commit is contained in:
parent
c33fd366ee
commit
d6f31ccde0
3 changed files with 14 additions and 8 deletions
8
lex.ml
8
lex.ml
|
@ -1,4 +1,4 @@
|
||||||
open Ast.Value
|
open Token
|
||||||
|
|
||||||
exception Invalid_character of char
|
exception Invalid_character of char
|
||||||
exception Expected 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)
|
aux str seq |> Option.map (fun s -> tok, s)
|
||||||
|
|
||||||
let find_token seq =
|
let find_token seq =
|
||||||
!Token.tokens |> List.find_map
|
!tokens |> List.find_map
|
||||||
(fun (s, t) -> expect_token s t seq)
|
(fun (s, t) -> expect_token s t seq)
|
||||||
|
|
||||||
(* same as take_while f seq, drop_while f 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, seq = partition_while ((<>) '"') seq in
|
||||||
let str = String (String.of_seq str) in
|
let str = String (String.of_seq str) in
|
||||||
let seq = expect_char '"' seq in
|
let seq = expect_char '"' seq in
|
||||||
cons (Value str) (aux seq)
|
cons str (aux seq)
|
||||||
|
|
||||||
(* number (int, float) *)
|
(* number (int, float) *)
|
||||||
else if is_digit x then
|
else if is_digit x then
|
||||||
|
@ -90,7 +90,7 @@ let tokenize str =
|
||||||
then Float (float_of_string n)
|
then Float (float_of_string n)
|
||||||
else Int (int_of_string n)
|
else Int (int_of_string n)
|
||||||
in
|
in
|
||||||
cons (Value n) (aux seq)
|
cons n (aux seq)
|
||||||
|
|
||||||
(* idents *)
|
(* idents *)
|
||||||
else if is_ident_start x then
|
else if is_ident_start x then
|
||||||
|
|
|
@ -208,8 +208,10 @@ and value seq =
|
||||||
match seq () with
|
match seq () with
|
||||||
| Seq.Nil -> raise End_of_tokens
|
| Seq.Nil -> raise End_of_tokens
|
||||||
| Seq.Cons (x, seq) -> begin match x with
|
| Seq.Cons (x, seq) -> begin match x with
|
||||||
| Token.Value x -> Value x, seq
|
| Token.Ident id -> Var id, seq
|
||||||
| Ident id -> Var id, seq
|
| Int x -> Value (Int x), seq
|
||||||
|
| Float x -> Value (Float x), seq
|
||||||
|
| String x -> Value (String x), seq
|
||||||
| LParen ->
|
| LParen ->
|
||||||
let e, seq = expr min_int seq in
|
let e, seq = expr min_int seq in
|
||||||
let _, seq = token RParen seq in
|
let _, seq = token RParen seq in
|
||||||
|
|
8
token.ml
8
token.ml
|
@ -1,5 +1,7 @@
|
||||||
type t =
|
type t =
|
||||||
| Value of Ast.Value.t
|
| Int of int
|
||||||
|
| Float of float
|
||||||
|
| String of string
|
||||||
| Ident of string
|
| Ident of string
|
||||||
| Plus
|
| Plus
|
||||||
| Minus
|
| Minus
|
||||||
|
@ -24,7 +26,9 @@ let tokens = ref [
|
||||||
]
|
]
|
||||||
|
|
||||||
let to_string = function
|
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
|
| Ident s -> s
|
||||||
| t ->
|
| t ->
|
||||||
begin match List.find_opt (fun (_, tok) -> t = tok) !tokens with
|
begin match List.find_opt (fun (_, tok) -> t = tok) !tokens with
|
||||||
|
|
Loading…
Add table
Reference in a new issue