Skip to content

Commit

Permalink
make curly braces optional (#42)
Browse files Browse the repository at this point in the history
* make curly braces optional

Signed-off-by: Flipez <code@brauser.io>

* update docs

Signed-off-by: Flipez <code@brauser.io>
  • Loading branch information
Flipez authored Jan 16, 2022
1 parent 119d400 commit 00d2e78
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 29 deletions.
5 changes: 3 additions & 2 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,16 @@ func (ie *IfExpression) TokenLiteral() string { return ie.Token.Literal }
func (ie *IfExpression) String() string {
var out bytes.Buffer

out.WriteString("if")
out.WriteString("if ")
out.WriteString(ie.Condition.String())
out.WriteString(" ")
out.WriteString(ie.Consequence.String())

if ie.Alternative != nil {
out.WriteString("else ")
out.WriteString(" else ")
out.WriteString(ie.Alternative.String())
}
out.WriteString(" end")

return out.String()
}
Expand Down
13 changes: 13 additions & 0 deletions docs/content/en/docs/control_expressions/if.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ With `if` and `else` keywords the flow of a program can be controlled.
puts("is not a string")
}

// which prints
is a string
```

{{< alert icon="👉" text="Since `0.13` curly braces are completely optional (closing brace needs to be replaced with `end`" />}}

```js
🚀 > if (a.type() == "STRING")
puts("is a string")
else
puts("is not a string")
end

// which prints
is a string
```
8 changes: 4 additions & 4 deletions docs/content/en/docs/home.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ input = a

increase = 0
foreach i, number in input {
if (number > input[i-1]) {
if (number > input[i-1])
increase = increase + 1
}
end
}
puts(increase + 1)

Expand All @@ -27,9 +27,9 @@ foreach i, number in input {
sum = number + input[i+1] + input[i+2]
sum_two = input[i+1] + input[i+2] + input[i+3]

if (sum_two > sum) {
if (sum_two > sum)
increase = increase + 1
}
end
}
puts(increase + 1)
```
12 changes: 6 additions & 6 deletions docs/content/en/docs/specification/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ Implicit and explicit return statements are supported.

```js
fibonacci = def (x) {
if (x == 0) {
if (x == 0)
0
} else {
if (x == 1) {
else
if (x == 1)
return 1;
} else {
else
fibonacci(x - 1) + fibonacci(x - 2);
}
}
end
end
};
```

Expand Down
1 change: 0 additions & 1 deletion evaluator/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,6 @@ func evalIfExpression(ie *ast.IfExpression, env *object.Environment) object.Obje
if isError(condition) {
return condition
}

if isTruthy(condition) {
return Eval(ie.Consequence, env)
} else if ie.Alternative != nil {
Expand Down
4 changes: 4 additions & 0 deletions evaluator/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,15 @@ func TestIfElseExpressions(t *testing.T) {
}{
{"if (true) { 10 }", 10},
{"if (false) { 10 }", nil},
{"if (true)\n 10 end", 10},
{"if (false) \n 10 end", nil},
{"if (1) { 10 }", 10},
{"if (1) \n 10 end", 10},
{"if (1 < 2) { 10 }", 10},
{"if (1 > 2) { 10 }", nil},
{"if (1 > 2) { 10 } else { 20 }", 20},
{"if (1 < 2) { 10 } else { 20 }", 10},
{"if (1 < 2) \n 10 \n else \n 20 end", 10},
}

for _, tt := range tests {
Expand Down
12 changes: 6 additions & 6 deletions examples/aoc/2021/day-2/complete.rl
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ aim = 0
foreach i, line in input {
command = line.split(" ")[0]
value = line.strip().split(" ")[1].plz_i()
if (command == "forward") {
if (command == "forward")
hor = hor + value
depth = depth + (value * aim)
}
end

if (command == "down") {
if (command == "down")
aim = aim + value
}
end

if (command == "up") {
if (command == "up")
aim = aim - value
}
end
}

puts(hor * depth)
15 changes: 15 additions & 0 deletions examples/misc/if.rl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
if (false)
puts("true1")
else
puts("false1")
end

if (true)
puts("true2")
else
puts("false2")
end

if (true)
puts("true3")
end
19 changes: 9 additions & 10 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,6 @@ func (p *Parser) parseGroupedExpression() ast.Expression {

func (p *Parser) parseIfExpression() ast.Expression {
expression := &ast.IfExpression{Token: p.curToken}

if !p.expectPeek(token.LPAREN) {
return nil
}
Expand All @@ -412,22 +411,22 @@ func (p *Parser) parseIfExpression() ast.Expression {
return nil
}

if !p.expectPeek(token.LBRACE) {
return nil
if p.peekTokenIs(token.LBRACE) {
p.nextToken()
}

expression.Consequence = p.parseBlockStatement()

if p.peekTokenIs(token.ELSE) {
if p.curTokenIs(token.RBRACE) {
p.nextToken()
}

if !p.expectPeek(token.LBRACE) {
return nil
}
if p.curTokenIs(token.ELSE) {

if p.peekTokenIs(token.LBRACE) {
p.nextToken()
}
expression.Alternative = p.parseBlockStatement()
}

return expression
}

Expand All @@ -437,7 +436,7 @@ func (p *Parser) parseBlockStatement() *ast.BlockStatement {

p.nextToken()

for !p.curTokenIs(token.RBRACE) && !p.curTokenIs(token.EOF) {
for !p.curTokenIs(token.RBRACE) && !p.curTokenIs(token.EOF) && !p.curTokenIs(token.END) && !p.curTokenIs(token.ELSE) {
stmt := p.parseStatement()
if stmt != nil {
block.Statements = append(block.Statements, stmt)
Expand Down
2 changes: 2 additions & 0 deletions token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const (
FALSE = "FALSE"
IF = "IF"
ELSE = "ELSE"
END = "END"
RETURN = "RETURN"

EQ = "=="
Expand All @@ -62,6 +63,7 @@ var keywords = map[string]TokenType{
"true": TRUE,
"false": FALSE,
"if": IF,
"end": END,
"else": ELSE,
"return": RETURN,
"foreach": FOREACH,
Expand Down

1 comment on commit 00d2e78

@vercel
Copy link

@vercel vercel bot commented on 00d2e78 Jan 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.