feat(wpcarro/compiler): Support Vector type

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>
This commit is contained in:
William Carroll 2022-10-24 21:06:01 -04:00 committed by wpcarro
parent ee235235b9
commit bd0bf6ea7d
5 changed files with 192 additions and 95 deletions

View file

@ -1,24 +1,23 @@
(*******************************************************************************
(******************************************************************************
* Defines a generic parser class.
******************************************************************************)
open Vec
exception ParseError of string
type token = string
type state = { i : int; tokens : token array }
type state = { i : int; tokens : token vec }
let get (i : int) (xs : 'a array) : 'a option =
if i >= Array.length xs then None else Some xs.(i)
class parser (tokens : token array) =
class parser (tokens : token vec) =
object (self)
val mutable tokens : token array = tokens
val mutable tokens = tokens
val mutable i = ref 0
method print_state = Printf.sprintf "{ i = %d; }" !i
method advance = i := !i + 1
method prev : token option = get (!i - 1) tokens
method curr : token option = get !i tokens
method next : token option = get (!i + 1) tokens
method prev : token option = Vec.get (!i - 1) tokens
method curr : token option = Vec.get !i tokens
method next : token option = Vec.get (!i + 1) tokens
method consume : token option =
match self#curr with
@ -43,6 +42,6 @@ class parser (tokens : token array) =
end
else false
method exhausted : bool = !i >= Array.length tokens
method exhausted : bool = !i >= Vec.length tokens
method state : state = { i = !i; tokens }
end