1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

v.token,v.parser: use p.tok.kind.precedence(), instead of p.tok.precedence(), since token.Token struct values, are much larger, than just the token.Kind enum values

This commit is contained in:
Delyan Angelov 2023-01-04 17:50:51 +02:00
parent f9e8a91cd0
commit 66438391d0
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 13 additions and 10 deletions

View File

@ -453,7 +453,7 @@ pub fn (mut p Parser) expr_with_left(left ast.Expr, precedence int, is_stmt_iden
return node return node
} }
// Infix // Infix
for precedence < p.tok.precedence() { for precedence < p.tok.kind.precedence() {
if p.tok.kind == .dot { if p.tok.kind == .dot {
// no spaces or line break before dot in map_init // no spaces or line break before dot in map_init
if p.inside_map_init && p.tok.pos - p.prev_tok.pos > p.prev_tok.len { if p.inside_map_init && p.tok.pos - p.prev_tok.pos > p.prev_tok.len {
@ -567,7 +567,7 @@ fn (mut p Parser) infix_expr(left ast.Expr) ast.Expr {
p.or_is_handled = true p.or_is_handled = true
p.register_auto_import('sync') p.register_auto_import('sync')
} }
precedence := p.tok.precedence() precedence := p.tok.kind.precedence()
mut pos := p.tok.pos() mut pos := p.tok.pos()
p.next() p.next()
if p.inside_if_cond { if p.inside_if_cond {

View File

@ -386,10 +386,6 @@ pub fn (t Token) debug() string {
} }
// Representation of highest and lowest precedence // Representation of highest and lowest precedence
/*
pub const lowest_prec = 0
pub const highest_prec = 8
*/
pub enum Precedence { pub enum Precedence {
lowest lowest
cond // OR or AND cond // OR or AND
@ -400,14 +396,15 @@ pub enum Precedence {
sum // + - | ^ sum // + - | ^
product // * / << >> >>> & product // * / << >> >>> &
// mod // % // mod // %
prefix // -X or !X prefix // -X or !X; TODO: seems unused
postfix // ++ or -- postfix // ++ or --
call // func(X) or foo.method(X) call // func(X) or foo.method(X)
index // array[index], map[key] index // array[index], map[key]
highest
} }
pub fn build_precedences() []Precedence { pub fn build_precedences() []Precedence {
mut p := []Precedence{len: int(Kind._end_)} mut p := []Precedence{len: int(Kind._end_), init: Precedence.lowest}
p[Kind.lsbr] = .index p[Kind.lsbr] = .index
p[Kind.nilsbr] = .index p[Kind.nilsbr] = .index
p[Kind.dot] = .call p[Kind.dot] = .call
@ -462,12 +459,18 @@ pub fn build_precedences() []Precedence {
const precedences = build_precedences() const precedences = build_precedences()
// precedence returns a tokens precedence if defined, otherwise lowest_prec // precedence returns a tokens precedence if defined, otherwise 0
[inline] [direct_array_access; inline]
pub fn (tok Token) precedence() int { pub fn (tok Token) precedence() int {
return int(token.precedences[tok.kind]) return int(token.precedences[tok.kind])
} }
// precedence returns the precedence of the given token `kind` if defined, otherwise 0
[direct_array_access; inline]
pub fn (kind Kind) precedence() int {
return int(token.precedences[kind])
}
// is_scalar returns true if the token is a scalar // is_scalar returns true if the token is a scalar
[inline] [inline]
pub fn (tok Token) is_scalar() bool { pub fn (tok Token) is_scalar() bool {