diff options
author | William Carroll <wpcarro@gmail.com> | 2022-10-25T01·06-0400 |
---|---|---|
committer | wpcarro <wpcarro@gmail.com> | 2022-10-25T04·32+0000 |
commit | bd0bf6ea7d64d2b3b453db4adb94c98c0b459a24 (patch) | |
tree | edeed28057cd2288825078433e6cb8f673b62b0b /users/wpcarro/scratch/compiler/expr_parser.ml | |
parent | ee235235b98d01b00de5a446b48d6dec574b1458 (diff) |
feat(wpcarro/compiler): Support Vector type r/5197
Support an array that dynamically resizes itself, and replace usages of `List`, `Array`, and `Queue` with `Vec`. Change-Id: I910b140b7c1bdddae40e08f8191986dccbc6fddf Reviewed-on: https://cl.tvl.fyi/c/depot/+/7080 Tested-by: BuildkiteCI Reviewed-by: wpcarro <wpcarro@gmail.com>
Diffstat (limited to 'users/wpcarro/scratch/compiler/expr_parser.ml')
-rw-r--r-- | users/wpcarro/scratch/compiler/expr_parser.ml | 35 |
1 files changed, 13 insertions, 22 deletions
diff --git a/users/wpcarro/scratch/compiler/expr_parser.ml b/users/wpcarro/scratch/compiler/expr_parser.ml index 8ac6756a4af3..797592931a2c 100644 --- a/users/wpcarro/scratch/compiler/expr_parser.ml +++ b/users/wpcarro/scratch/compiler/expr_parser.ml @@ -31,24 +31,15 @@ open Parser open Inference open Debug open Prettify - -let to_array (q : 'a Queue.t) : 'a array = - let result = Array.make (Queue.length q) "" in - let i = ref 0 in - Queue.iter - (fun x -> - result.(!i) <- x; - i := !i + 1) - q; - result +open Vec type literal = LiteralBool of bool | LiteralInt of int let ( let* ) = Option.bind let map = Option.map -let tokenize (x : string) : token array = - let q = Queue.create () in +let tokenize (x : string) : token vec = + let xs = Vec.create () in let i = ref 0 in while !i < String.length x do match x.[!i] with @@ -62,13 +53,13 @@ let tokenize (x : string) : token array = i := !i + 1 done; curr := !curr ^ "\""; - Queue.push !curr q; + Vec.append !curr xs; i := !i + 1 | '(' -> - Queue.push "(" q; + Vec.append "(" xs; i := !i + 1 | ')' -> - Queue.push ")" q; + Vec.append ")" xs; i := !i + 1 | _ -> let token = ref "" in @@ -76,9 +67,9 @@ let tokenize (x : string) : token array = token := !token ^ String.make 1 x.[!i]; i := !i + 1 done; - Queue.push !token q + Vec.append !token xs done; - to_array q + xs let parse_symbol (p : parser) : string option = let* x = p#curr in @@ -108,7 +99,7 @@ let parse_literal (p : parser) : Types.value option = p#advance; Some (ValueLiteral (LiteralInt n)) | _ -> - if String.starts_with "\"" x then + if String.starts_with ~prefix:"\"" x then begin p#advance; Some (ValueLiteral (LiteralString x)) @@ -158,10 +149,10 @@ and parse_binding (p : parser) : Types.value option = Some (Types.ValueBinder (name, value, body)) | _ -> parse_funcdef p -let print_tokens (xs : string array) = - xs |> Array.to_list - |> List.map (Printf.sprintf "\"%s\"") - |> String.concat ", " +let print_tokens (xs : string vec) : unit = + xs + |> Vec.map (Printf.sprintf "\"%s\"") + |> Vec.join ", " |> Printf.sprintf "tokens: [ %s ]" |> print_string |> print_newline |