From 9fff8733a087bcb72c7e6da0920ffc5d8019c148 Mon Sep 17 00:00:00 2001 From: joe-conigliaro Date: Sat, 25 Apr 2020 18:07:30 +1000 Subject: [PATCH] parser: allow deref assign without parens pt1 --- vlib/v/parser/parser.v | 8 ++++++-- vlib/v/parser/pratt.v | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 02802ce607..b7e40e901c 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -20,6 +20,7 @@ mut: tok token.Token prev_tok token.Token peek_tok token.Token + peek_tok2 token.Token table &table.Table is_c bool is_js bool @@ -179,11 +180,13 @@ pub fn (p &Parser) init_parse_fns() { } pub fn (mut p Parser) read_first_token() { - // need to call next() twice to get peek token and current token + // need to call next() three times to get peek token 1 & 2 and current token + p.next() p.next() p.next() } + pub fn (mut p Parser) open_scope() { p.scope = &ast.Scope{ parent: p.scope @@ -241,7 +244,8 @@ fn (mut p Parser) next_with_comment() { fn (mut p Parser) next() { p.prev_tok = p.tok p.tok = p.peek_tok - p.peek_tok = p.scanner.scan() + p.peek_tok = p.peek_tok2 + p.peek_tok2 = p.scanner.scan() /* if p.tok.kind==.comment { p.comments << ast.Comment{text:p.tok.lit, line_nr:p.tok.line_nr} diff --git a/vlib/v/parser/pratt.v b/vlib/v/parser/pratt.v index c3c300bf8d..3afc4186a4 100644 --- a/vlib/v/parser/pratt.v +++ b/vlib/v/parser/pratt.v @@ -161,6 +161,11 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr { pos: pos } } else if p.tok.kind.is_infix() { + // return early for deref assign `*x = 2` goes to prefix expr + if p.tok.kind == .mul && p.tok.line_nr != p.prev_tok.line_nr && p.peek_tok2.kind == .assign { + return node + } + // continue on infix expr node = p.infix_expr(node) } else if p.tok.kind in [.inc, .dec] { // Postfix