about summary refs log tree commit diff
path: root/users/wpcarro/scratch/compiler/parser.ml
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2022-10-25T01·06-0400
committerwpcarro <wpcarro@gmail.com>2022-10-25T04·32+0000
commitbd0bf6ea7d64d2b3b453db4adb94c98c0b459a24 (patch)
treeedeed28057cd2288825078433e6cb8f673b62b0b /users/wpcarro/scratch/compiler/parser.ml
parentee235235b98d01b00de5a446b48d6dec574b1458 (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/parser.ml')
-rw-r--r--users/wpcarro/scratch/compiler/parser.ml23
1 files changed, 11 insertions, 12 deletions
diff --git a/users/wpcarro/scratch/compiler/parser.ml b/users/wpcarro/scratch/compiler/parser.ml
index 75cbe04a3f72..dc66f2506ed3 100644
--- a/users/wpcarro/scratch/compiler/parser.ml
+++ b/users/wpcarro/scratch/compiler/parser.ml
@@ -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 }
-
-let get (i : int) (xs : 'a array) : 'a option =
-  if i >= Array.length xs then None else Some xs.(i)
+type state = { i : int; tokens : token vec }
 
-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