From bd0bf6ea7d64d2b3b453db4adb94c98c0b459a24 Mon Sep 17 00:00:00 2001 From: William Carroll Date: Mon, 24 Oct 2022 21:06:01 -0400 Subject: 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 --- users/wpcarro/scratch/compiler/parser.ml | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'users/wpcarro/scratch/compiler/parser.ml') 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 -- cgit 1.4.1