Skip to content

Commit

Permalink
2.8: Add boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
nibral committed Jan 13, 2019
1 parent b0ae349 commit 9b60d31
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 11 deletions.
9 changes: 9 additions & 0 deletions ast/ast.go
Expand Up @@ -150,3 +150,12 @@ func (ie *InfixExpression) String() string {

return out.String()
}

type Boolean struct {
Token token.Token
Value bool
}

func (b *Boolean) expressionNode() {}
func (b *Boolean) TokenLiteral() string { return b.Token.Literal }
func (b *Boolean) String() string { return b.Token.Literal }
9 changes: 9 additions & 0 deletions parser/parser.go
Expand Up @@ -62,6 +62,8 @@ func New(l *lexer.Lexer) *Parser {
p.registerPrefix(token.INT, p.parseIntegerLiteral)
p.registerPrefix(token.BANG, p.parsePrefixExpression)
p.registerPrefix(token.MINUS, p.parsePrefixExpression)
p.registerPrefix(token.TRUE, p.parseBoolean)
p.registerPrefix(token.FALSE, p.parseBoolean)

p.infixParseFns = make(map[token.TokenType]infixParseFn)
p.registerInfix(token.PLUS, p.parseInfixExpression)
Expand Down Expand Up @@ -280,3 +282,10 @@ func (p *Parser) parseInfixExpression(left ast.Expression) ast.Expression {

return expression
}

func (p *Parser) parseBoolean() ast.Expression {
return &ast.Boolean{
Token: p.curToken,
Value: p.curTokenIs(token.TRUE),
}
}
153 changes: 142 additions & 11 deletions parser/parser_test.go
Expand Up @@ -166,12 +166,14 @@ func TestIntegerLiteralExpression(t *testing.T) {

func TestParsingPrefixExpressions(t *testing.T) {
prefixTests := []struct {
input string
operator string
integerValue int64
input string
operator string
value interface{}
}{
{"!5;", "!", 5},
{"-15;", "-", 15},
{"!true;", "!", true},
{"!false;", "!", false},
}

for _, tt := range prefixTests {
Expand Down Expand Up @@ -199,7 +201,7 @@ func TestParsingPrefixExpressions(t *testing.T) {
t.Fatalf("exp.Operator is not '%s'. got=%s",
tt.operator, exp.Operator)
}
if !testIntegerLiteral(t, exp.Right, tt.integerValue) {
if !testLiteralExpression(t, exp.Right, tt.value) {
return
}
}
Expand All @@ -226,12 +228,77 @@ func testIntegerLiteral(t *testing.T, il ast.Expression, value int64) bool {
return true
}

func testIdentifier(t *testing.T, exp ast.Expression, value string) bool {
ident, ok := exp.(*ast.Identifier)
if !ok {
t.Errorf("exp not *ast.Identifier. got=%T", exp)
return false
}

if ident.Value != value {
t.Errorf("ident.Value not %s. got=%s", value, ident.Value)
return false
}

if ident.TokenLiteral() != value {
t.Errorf("ident.TokenLiteral not %s. got=%s", value,
ident.TokenLiteral())
return false
}

return true
}

func testLiteralExpression(
t *testing.T,
exp ast.Expression,
expected interface{},
) bool {
switch v := expected.(type) {
case int:
return testIntegerLiteral(t, exp, int64(v))
case int64:
return testIntegerLiteral(t, exp, v)
case string:
return testIdentifier(t, exp, v)
case bool:
return testBooleanLiteral(t, exp, v)
}
t.Errorf("type of exp not handled. got=%T", exp)
return false
}

func testInfixExpression(t *testing.T, exp ast.Expression, left interface{},
operator string, right interface{}) bool {

opExp, ok := exp.(*ast.InfixExpression)
if !ok {
t.Errorf("exp is not ast.InfixExpression. got=%T(%s)", exp, exp)
return false
}

if !testLiteralExpression(t, opExp.Left, left) {
return false
}

if opExp.Operator != operator {
t.Errorf("exp.Operator is not '%s'. got=%q", operator, opExp.Operator)
return false
}

if !testLiteralExpression(t, opExp.Right, right) {
return false
}

return true
}

func TestParsingInfixExpressions(t *testing.T) {
infixTests := []struct {
input string
leftValue int64
leftValue interface{}
operator string
rightValue int64
rightValue interface{}
}{
{"5 + 5;", 5, "+", 5},
{"5 - 5;", 5, "-", 5},
Expand All @@ -241,6 +308,9 @@ func TestParsingInfixExpressions(t *testing.T) {
{"5 < 5;", 5, "<", 5},
{"5 == 5;", 5, "==", 5},
{"5 != 5;", 5, "!=", 5},
{"true == true", true, "==", true},
{"true != false", true, "!=", false},
{"false == false", false, "==", false},
}

for _, tt := range infixTests {
Expand All @@ -265,16 +335,12 @@ func TestParsingInfixExpressions(t *testing.T) {
t.Fatalf("exp is not ast.InfixExpression. got=%T", stmt.Expression)
}

if !testIntegerLiteral(t, exp.Left, tt.leftValue) {
return
}

if exp.Operator != tt.operator {
t.Fatalf("exp.Operator is not '%s'. got=%s",
tt.operator, exp.Operator)
}

if !testIntegerLiteral(t, exp.Right, tt.rightValue) {
if !testInfixExpression(t, stmt.Expression, tt.leftValue, tt.operator, tt.rightValue) {
return
}
}
Expand Down Expand Up @@ -333,6 +399,22 @@ func TestOperatorPrecedenceParsing(t *testing.T) {
"3 + 4 * 5 == 3 * 1 + 4 * 5",
"((3 + (4 * 5)) == ((3 * 1) + (4 * 5)))",
},
{
"true",
"true",
},
{
"false",
"false",
},
{
"3 > 5 == false",
"((3 > 5) == false)",
},
{
"3 < 5 == true",
"((3 < 5) == true)",
},
}

for _, tt := range tests {
Expand All @@ -347,3 +429,52 @@ func TestOperatorPrecedenceParsing(t *testing.T) {
}
}
}

func TestBooleanExpression(t *testing.T) {
input := `true;`

l := lexer.New(input)
p := New(l)
program := p.ParseProgram()
checkParserErrors(t, p)

if len(program.Statements) != 1 {
t.Fatalf("program has not enough statements. got=%d", len(program.Statements))
}
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
if !ok {
t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T", program.Statements[0])
}

boolean, ok := stmt.Expression.(*ast.Boolean)
if !ok {
t.Fatalf("exp not *ast.Boolean. got=%T", stmt.Expression)
}
if boolean.Value != true {
t.Errorf("boolean.Value not %v. got=%v", true, boolean.Value)
}
if boolean.TokenLiteral() != "true" {
t.Errorf("boolean.TokenLiteral not %s. got=%s", "true", boolean.TokenLiteral())
}
}

func testBooleanLiteral(t *testing.T, exp ast.Expression, value bool) bool {
bo, ok := exp.(*ast.Boolean)
if !ok {
t.Errorf("exp not *ast.Boolean. got=%T", exp)
return false
}

if bo.Value != value {
t.Errorf("bo.Value not %t. got=%t", value, bo.Value)
return false
}

if bo.TokenLiteral() != fmt.Sprintf("%t", value) {
t.Errorf("bo.TokenLiteral not %t. got=%s",
value, bo.TokenLiteral())
return false
}

return true
}

0 comments on commit 9b60d31

Please sign in to comment.