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

[WIP] #677 Ternary Expression Implementation #794

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions compiler/ast/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,29 @@ func (n *NilExpression) String() string {
return "nil"
}

type TernaryExpression struct {

Choose a reason for hiding this comment

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

exported type TernaryExpression should have comment or be unexported

st0012 marked this conversation as resolved.
Show resolved Hide resolved
*BaseNode
Condition Expression
Consequence Expression
Alternative Expression
}

func (te *TernaryExpression) expressionNode() {}
func (te *TernaryExpression) TokenLiteral() string {
st0012 marked this conversation as resolved.
Show resolved Hide resolved

Choose a reason for hiding this comment

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

exported method TernaryExpression.TokenLiteral should have comment or be unexported

return te.Token.Literal
}
func (te *TernaryExpression) String() string {
var out bytes.Buffer

out.WriteString(te.Condition.String())
out.WriteString(" ? ")
out.WriteString(te.Consequence.String())
out.WriteString(" : ")
out.WriteString(te.Alternative.String())

return out.String()
}

type IfExpression struct {
*BaseNode
Conditionals []*ConditionalExpression
Expand Down
8 changes: 8 additions & 0 deletions compiler/ast/testable_expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ func (ti *TestableIdentifier) ShouldHaveName(expectedName string) {
}
}

// TestableTernaryExpression

Choose a reason for hiding this comment

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

comment on exported type TestableTernaryExpression should be of the form "TestableTernaryExpression ..." (with optional leading article)

Choose a reason for hiding this comment

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

comment on exported type TestableTernaryExpression should be of the form "TestableTernaryExpression ..." (with optional leading article)

type TestableTernaryExpression struct {
*TernaryExpression
t *testing.T
}

// TODO: Test helpers

// TestableIfExpression
type TestableIfExpression struct {
*IfExpression
Expand Down
4 changes: 2 additions & 2 deletions compiler/lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ func (l *Lexer) NextToken() token.Token {
l.readChar()
tok = token.CreateOperator("&&", l.line)
}
case '%':
tok = token.CreateOperator("%", l.line)
case '%', '?':
tok = token.CreateOperator(string(l.ch), l.line)
case '#':
tok.Literal = string(l.absorbComment())
tok.Type = token.Comment
Expand Down
13 changes: 11 additions & 2 deletions compiler/lexer/lexer_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package lexer

import (
"github.com/goby-lang/goby/compiler/token"
"testing"

"github.com/goby-lang/goby/compiler/token"
)

func TestNextToken(t *testing.T) {
Expand Down Expand Up @@ -125,6 +126,8 @@ func TestNextToken(t *testing.T) {
'\"string\"'
"\'string\'"
'\'string\''

flag ? true_result : false_result
`

tests := []struct {
Expand Down Expand Up @@ -439,7 +442,13 @@ func TestNextToken(t *testing.T) {
{token.String, "'string'", 117},
{token.String, "'string'", 118},

{token.EOF, "", 119},
{token.Ident, "flag", 120},
{token.TernaryOperator, "?", 120},
{token.Ident, "true_result", 120},
{token.Colon, ":", 120},
{token.Ident, "false_result", 120},

{token.EOF, "", 121},
}
l := New(input)

Expand Down
4 changes: 4 additions & 0 deletions compiler/parser/expression_parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ func (p *Parser) parseIndexExpression(left ast.Expression) ast.Expression {
return callExpression
}

// TODO: Add Parse Ternary Expression
// func (p *Parser) parseTernaryExpression(cond ast.Expression) ast.Expression {
// }

func (p *Parser) parseInfixExpression(left ast.Expression) ast.Expression {
operator := p.curToken
preced := p.curPrecedence()
Expand Down
3 changes: 2 additions & 1 deletion compiler/parser/expression_parsing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package parser

import (
"fmt"
"testing"

"github.com/goby-lang/goby/compiler/ast"
"github.com/goby-lang/goby/compiler/lexer"
"testing"
)

const (
Expand Down
2 changes: 2 additions & 0 deletions compiler/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ func New(l *lexer.Lexer) *Parser {
p.registerInfix(token.OrEq, p.parseAssignExpression)
p.registerInfix(token.Comma, p.parseMultiVariables)
p.registerInfix(token.ResolutionOperator, p.parseInfixExpression)
// TODO: Add Parse Ternary Expression
// p.registerInfix(token.TernaryOperator, p.parseTernaryExpression)
p.registerInfix(token.Assign, p.parseAssignExpression)
p.registerInfix(token.Range, p.parseRangeExpression)
p.registerInfix(token.Dot, p.parseCallExpressionWithReceiver)
Expand Down
1 change: 1 addition & 0 deletions compiler/parser/precedence/precedence.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var LookupTable = map[token.Type]int{
token.COMP: Compare,
token.And: Logic,
token.Or: Logic,
token.TernaryOperator: Logic,
token.Range: Range,
token.Plus: Sum,
token.Minus: Sum,
Expand Down
2 changes: 2 additions & 0 deletions compiler/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const (
Module = "MODULE"

ResolutionOperator = "::"
TernaryOperator = "?"
)

var keywords = map[string]Type{
Expand Down Expand Up @@ -136,6 +137,7 @@ var operators = map[string]Type{
"..": Range,

"::": ResolutionOperator,
"?": TernaryOperator,
}

var separators = map[string]Type{
Expand Down