Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #2

Merged
merged 9 commits into from
Nov 24, 2024
2 changes: 2 additions & 0 deletions parser/compounds.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func (p *Parser) getCompoundParser() func() ast.Statement {
return p.parseArithmeticCommand
case token.DOUBLE_LEFT_BRACKET:
return p.parseTestCommand
case token.LEFT_BRACKET, token.TEST:
return p.parsePosixTestCommand
case token.THEN, token.ELIF, token.ELSE, token.FI, token.DO, token.DONE, token.ESAC:
p.error("`%s` is a reserved keyword, cannot be used a command name", p.curr)
fallthrough
Expand Down
82 changes: 82 additions & 0 deletions parser/conditionals.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package parser
import (
"github.com/yassinebenaid/bunny/ast"
"github.com/yassinebenaid/bunny/token"
"github.com/yassinebenaid/godump"
)

func (p *Parser) parseTestCommand() ast.Statement {
Expand All @@ -20,6 +21,23 @@ func (p *Parser) parseTestCommand() ast.Statement {
return ast.Test{Expr: expr}
}

func (p *Parser) parsePosixTestCommand() ast.Statement {
testKeyword := p.curr.Type == token.TEST

p.proceed()
if p.curr.Type == token.BLANK {
p.proceed()
}
expr := p.parsePosixTestExpression(false)

if !testKeyword && p.curr.Type != token.RIGHT_BRACKET {
p.error("expected `]` to close conditional expression, found `%s`", p.curr)
}
p.proceed()

return ast.Test{Expr: expr}
}

func (p *Parser) parseTestExpression(prefix bool) ast.Expression {
var expr ast.Expression
if p.curr.Type == token.EXCLAMATION {
Expand Down Expand Up @@ -56,6 +74,51 @@ func (p *Parser) parseTestExpression(prefix bool) ast.Expression {
return expr
}

func (p *Parser) parsePosixTestExpression(prefix bool) ast.Expression {
var expr ast.Expression
if p.curr.Type == token.EXCLAMATION {
p.proceed()
expr = ast.Negation{Operand: p.parsePosixTestExpression(true)}
} else if p.curr.Type == token.LEFT_PAREN {
p.proceed()
expr = p.parsePosixTestExpression(false)
if p.curr.Type != token.RIGHT_PAREN {
p.error("expected a closing `)`, found `%s`", p.curr)
}
p.proceed()
if p.curr.Type == token.BLANK {
p.proceed()
}
} else {
expr = p.parseConditionals()
}

for !prefix {
operator := p.parsePosixConditionalBinaryOperator()
if operator == "" {
break
}
if operator == "-a" {
operator = "&&"
} else {
operator = "||"
}

if p.curr.Type == token.BLANK {
p.proceed()
}

expr = ast.BinaryConditional{
Left: expr,
Operator: operator,
Right: p.parsePosixTestExpression(true),
}
}

godump.Dump(expr)
return expr
}

func (p *Parser) parseConditionals() ast.Expression {
if p.curr.Type == token.BLANK {
p.proceed()
Expand Down Expand Up @@ -154,6 +217,25 @@ func (p *Parser) parseConditionalBinaryOperator() string {
return ""
}

func (p *Parser) parsePosixConditionalBinaryOperator() string {
if p.curr.Type == token.MINUS {
switch p.next.Literal {
case "a", "o":
if p.next2.Type != token.BLANK {
break
}

p.proceed()
operator := "-" + p.curr.Literal
p.proceed()
p.proceed()

return operator
}
}
return ""
}

func (p *Parser) parsePatternExpression() ast.Expression {
var exprs []ast.Expression

Expand Down
Loading