Skip to content

Commit

Permalink
parser: simplify for iterations
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Dec 4, 2024
1 parent adf80e5 commit d223fb3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 86 deletions.
150 changes: 65 additions & 85 deletions compiler/parser/expr.v
Original file line number Diff line number Diff line change
Expand Up @@ -44,123 +44,103 @@ fn (mut p Parser) parse_and_expr() ast.Expr {

fn (mut p Parser) parse_equality_expr() ast.Expr {
mut left := p.parse_relational_expr()
for {
if p.tok.kind in [.eq, .ne] {
op := p.tok.kind
p.next()
right := p.parse_relational_expr()
left = ast.BinaryExpr{
left: left
op: if op == .eq { .eq } else { .ne }
right: right
pos: left.pos.extend(right.pos)
}
} else {
break
for p.tok.kind in [.eq, .ne] {
op := p.tok.kind
p.next()
right := p.parse_relational_expr()
left = ast.BinaryExpr{
left: left
op: if op == .eq { .eq } else { .ne }
right: right
pos: left.pos.extend(right.pos)
}
}
return left
}

fn (mut p Parser) parse_relational_expr() ast.Expr {
mut left := p.parse_shift_expr()
for {
if p.tok.kind in [.gt, .lt, .le, .or_else, .kw_in, .not_in, .kw_is, .not_is] {
op := p.tok.kind
p.next()
right := p.parse_shift_expr()
left = ast.BinaryExpr{
left: left
op: match op {
.gt { .gt }
.lt { .lt }
.ge { .ge }
.or_else { .or_else }
.kw_in { .kw_in }
.kw_is { .kw_is }
else { .unknown }
}
right: right
pos: left.pos.extend(right.pos)
for p.tok.kind in [.gt, .lt, .le, .or_else, .kw_in, .not_in, .kw_is, .not_is] {
op := p.tok.kind
p.next()
right := p.parse_shift_expr()
left = ast.BinaryExpr{
left: left
op: match op {
.gt { .gt }
.lt { .lt }
.ge { .ge }
.or_else { .or_else }
.kw_in { .kw_in }
.kw_is { .kw_is }
else { .unknown }
}
} else {
break
right: right
pos: left.pos.extend(right.pos)
}
}
return left
}

fn (mut p Parser) parse_shift_expr() ast.Expr {
mut left := p.parse_additive_expr()
for {
if p.tok.kind in [.amp, .pipe, .xor, .lshift, .rshift] {
op := p.tok.kind
p.next()
right := p.parse_additive_expr()
left = ast.BinaryExpr{
left: left
op: match op {
.amp { .amp }
.pipe { .pipe }
.xor { .xor }
.lshift { .lshift }
.rshift { .rshift }
else { .unknown }
}
right: right
pos: left.pos.extend(right.pos)
for p.tok.kind in [.amp, .pipe, .xor, .lshift, .rshift] {
op := p.tok.kind
p.next()
right := p.parse_additive_expr()
left = ast.BinaryExpr{
left: left
op: match op {
.amp { .amp }
.pipe { .pipe }
.xor { .xor }
.lshift { .lshift }
.rshift { .rshift }
else { .unknown }
}
} else {
break
right: right
pos: left.pos.extend(right.pos)
}
}
return left
}

fn (mut p Parser) parse_additive_expr() ast.Expr {
mut left := p.parse_multiplicative_expr()
for {
if p.tok.kind in [.plus, .minus] {
op := p.tok.kind
p.next()
right := p.parse_multiplicative_expr()
left = ast.BinaryExpr{
left: left
op: match op {
.plus { .plus }
.minus { .minus }
else { .unknown }
}
right: right
pos: left.pos.extend(right.pos)
for p.tok.kind in [.plus, .minus] {
op := p.tok.kind
p.next()
right := p.parse_multiplicative_expr()
left = ast.BinaryExpr{
left: left
op: match op {
.plus { .plus }
.minus { .minus }
else { .unknown }
}
} else {
break
right: right
pos: left.pos.extend(right.pos)
}
}
return left
}

fn (mut p Parser) parse_multiplicative_expr() ast.Expr {
mut left := p.parse_unary_expr()
for {
if p.tok.kind in [.mul, .div, .mod] {
op := p.tok.kind
p.next()
right := p.parse_unary_expr()
left = ast.BinaryExpr{
left: left
op: match op {
.mul { .mul }
.div { .div }
.mod { .mod }
else { .unknown }
}
right: right
pos: left.pos.extend(right.pos)
for p.tok.kind in [.mul, .div, .mod] {
op := p.tok.kind
p.next()
right := p.parse_unary_expr()
left = ast.BinaryExpr{
left: left
op: match op {
.mul { .mul }
.div { .div }
.mod { .mod }
else { .unknown }
}
} else {
break
right: right
pos: left.pos.extend(right.pos)
}
}
return left
Expand Down
2 changes: 1 addition & 1 deletion compiler/parser/mod.v
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn (mut p Parser) expect(kind token.Kind) {
} else {
p.prev_expect_pos = p.tok.pos
}

if !p.accept(kind) {
if p.expect_is_called {
p.next()
Expand Down

0 comments on commit d223fb3

Please sign in to comment.