mirror of
https://github.com/MorizzG/MLox.git
synced 2025-12-06 04:22:41 +00:00
changed tokens from mutable to ref
so that when record is copied it's still the same field
This commit is contained in:
parent
54dc78f0b8
commit
8d445cc9e4
1 changed files with 13 additions and 13 deletions
|
|
@ -6,33 +6,33 @@ open Lexer
|
||||||
open Stmt
|
open Stmt
|
||||||
|
|
||||||
type parse_result = (stmt_node list, parser_error list) result
|
type parse_result = (stmt_node list, parser_error list) result
|
||||||
type state = { mutable tokens : token list; is_in_loop : bool }
|
type state = { tokens : token list ref; is_in_loop : bool }
|
||||||
type stmt_result = (stmt_node, parser_error) result
|
type stmt_result = (stmt_node, parser_error) result
|
||||||
type expr_result = (expr_node, parser_error) result
|
type expr_result = (expr_node, parser_error) result
|
||||||
|
|
||||||
let with_is_in_loop (f : state -> 'a) (state : state) : 'a =
|
let with_is_in_loop (f : state -> 'a) (state : state) : 'a =
|
||||||
let new_state = { state with is_in_loop = true } in
|
let new_state = { state with is_in_loop = true } in
|
||||||
let result = f new_state in
|
let result = f new_state in
|
||||||
state.tokens <- new_state.tokens;
|
(* state.tokens <- new_state.tokens; *)
|
||||||
result
|
result
|
||||||
|
|
||||||
let make_state tokens = { tokens; is_in_loop = false }
|
let make_state tokens = { tokens; is_in_loop = false }
|
||||||
|
|
||||||
let is_at_end state =
|
let is_at_end state =
|
||||||
assert (not (List.is_empty state.tokens));
|
assert (not (List.is_empty !(state.tokens)));
|
||||||
(List.hd state.tokens).token_type = Eof
|
(List.hd !(state.tokens)).token_type = Eof
|
||||||
|
|
||||||
let advance state = state.tokens <- List.tl state.tokens
|
(* let advance state = state.tokens <- List.tl state.tokens *)
|
||||||
|
let advance state = state.tokens := List.tl !(state.tokens)
|
||||||
let next state =
|
let peek state = List.hd !(state.tokens)
|
||||||
let token = List.hd state.tokens in
|
|
||||||
advance state;
|
|
||||||
token
|
|
||||||
|
|
||||||
let peek state = List.hd state.tokens
|
|
||||||
let peek_tt (state : state) : token_type = (peek state).token_type
|
let peek_tt (state : state) : token_type = (peek state).token_type
|
||||||
let cur_pos state = (peek state).pos
|
let cur_pos state = (peek state).pos
|
||||||
|
|
||||||
|
let next state =
|
||||||
|
let token = peek state in
|
||||||
|
advance state;
|
||||||
|
token
|
||||||
|
|
||||||
let advance_if state tt =
|
let advance_if state tt =
|
||||||
if peek_tt state = tt then (
|
if peek_tt state = tt then (
|
||||||
advance state;
|
advance state;
|
||||||
|
|
@ -384,5 +384,5 @@ let parse (tokens : token list) : parse_result =
|
||||||
let tokens =
|
let tokens =
|
||||||
List.filter (fun tok -> match tok.token_type with Comment _ -> false | _ -> true) tokens
|
List.filter (fun tok -> match tok.token_type with Comment _ -> false | _ -> true) tokens
|
||||||
in
|
in
|
||||||
let state = make_state tokens in
|
let state = make_state (ref tokens) in
|
||||||
parse_impl state
|
parse_impl state
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue