Skip to content

Commit

Permalink
Merge pull request #978 from srliao/srl-20201003-switch-improvement
Browse files Browse the repository at this point in the history
allow condition to be optional in switch statement
  • Loading branch information
srliao authored Oct 4, 2022
2 parents 7820452 + b5edd13 commit 26e4bbf
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions pkg/gcs/ast/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (t Token) precedence() precedence {
return Lowest
}

//Parse returns the ActionList and any error that prevents the ActionList from being parsed
// Parse returns the ActionList and any error that prevents the ActionList from being parsed
func (p *Parser) Parse() (*ActionList, error) {
var err error
for state := parseRows; state != nil; {
Expand Down Expand Up @@ -364,9 +364,17 @@ func (p *Parser) parseSwitch() (Stmt, error) {
Pos: n.pos,
}

stmt.Condition, err = p.parseExpr(Lowest)
if err != nil {
return nil, err
//condition can be optional; if next item is itemLeftBrace then simply set condition to 1
if n := p.peek(); n.Typ != itemLeftBrace {
stmt.Condition, err = p.parseExpr(Lowest)
if err != nil {
return nil, err
}
} else {
stmt.Condition = &NumberLit{
Pos: n.pos,
IntVal: 1,
}
}

if n := p.next(); n.Typ != itemLeftBrace {
Expand Down Expand Up @@ -703,7 +711,7 @@ func (p *Parser) parseCallArgs() ([]Expr, error) {
return args, nil
}

//check if it's a valid character action, assuming current token is "character"
// check if it's a valid character action, assuming current token is "character"
func (p *Parser) peekValidCharAction() bool {
p.next()
//check if this is character stats etc or an action
Expand All @@ -716,7 +724,7 @@ func (p *Parser) peekValidCharAction() bool {
return true
}

//parseBlock return a node contain and BlockStmt
// parseBlock return a node contain and BlockStmt
func (p *Parser) parseBlock() (*BlockStmt, error) {
//should be surronded by {}
n, err := p.consume(itemLeftBrace)
Expand Down Expand Up @@ -777,7 +785,7 @@ func (p *Parser) parseExpr(pre precedence) (Expr, error) {
return leftExp, nil
}

//next is an identifier
// next is an identifier
func (p *Parser) parseIdent() (Expr, error) {
n := p.next()
return &Ident{Pos: n.pos, Value: n.Val}, nil
Expand Down

0 comments on commit 26e4bbf

Please sign in to comment.