Skip to content

Commit

Permalink
types: add Type() method to types
Browse files Browse the repository at this point in the history
  • Loading branch information
rpbeltran committed Jan 5, 2025
1 parent 4ba33ad commit 6a21a76
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 2 deletions.
2 changes: 0 additions & 2 deletions benchmarks/numbers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func BenchmarkMakeNumber(b *testing.B) {

func BenchmarkNumbersAddition(b *testing.B) {
// add 2**63-1 to an accumulator b.N times

inc, err := types.MakeNumber("7")
if err != nil {
b.Fatalf("Error making number 7: %v", err)
Expand All @@ -41,7 +40,6 @@ func BenchmarkNumbersAddition(b *testing.B) {

func BenchmarkNumbersAdditionInplace(b *testing.B) {
// add 2**63-1 to an accumulator b.N times

inc, err := types.MakeNumber("7")
if err != nil {
b.Fatalf("Error making number 7: %v", err)
Expand Down
4 changes: 4 additions & 0 deletions types/boolean.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ func MakeBool(value bool) *PrimitiveBool {
return &PrimitiveBool{value}
}

func (boolean *PrimitiveBool) Type() string {
return "bool"
}

// Comparisons

func (boolean *PrimitiveBool) Equal(other Primitive) bool {
Expand Down
7 changes: 7 additions & 0 deletions types/boolean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import (
"testing"
)

func TestBoolType(t *testing.T) {
expected := "bool"
if actual := MakeBool(true).Type(); actual != expected {
t.Fatalf("Expected (true).type() == %q, instead got %q", expected, actual)
}
}

// Test logical operators

type binop_bool_test_case struct {
Expand Down
4 changes: 4 additions & 0 deletions types/numbers.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func MakeNumber(value string) (*PrimitiveNum, error) {
return &PrimitiveNum{num}, nil
}

func (num *PrimitiveNum) Type() string {
return "num"
}

// Comparisons

func (num *PrimitiveNum) Equal(other Primitive) bool {
Expand Down
7 changes: 7 additions & 0 deletions types/numbers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ func makeNumOrFail(value string, t *testing.T) *PrimitiveNum {
return num
}

func TestNumType(t *testing.T) {
expected := "num"
if actual := makeNumOrFail("100", t).Type(); actual != expected {
t.Fatalf("Expected (100).type() == %q, instead got %q", expected, actual)
}
}

func TestNumNonArithmetic(t *testing.T) {
if actual, err := makeNumOrFail("100", t).Concatenate(makeNumOrFail("100", t)); err == nil {
t.Fatalf("100 ++ 100 should have errored but instead succeeded and returned %s", actual.Display())
Expand Down
2 changes: 2 additions & 0 deletions types/types.go → types/primitive.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

type Primitive interface {
Type() string

// - Math
Add(other Primitive) (Primitive, error)
AddInplace(other Primitive) error
Expand Down
4 changes: 4 additions & 0 deletions types/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ func MakeString(value string) *PrimitiveStr {
return &PrimitiveStr{value}
}

func (str *PrimitiveStr) Type() string {
return "str"
}

// Comparisons

func (str *PrimitiveStr) Equal(other Primitive) bool {
Expand Down
7 changes: 7 additions & 0 deletions types/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import (
"testing"
)

func TestStrType(t *testing.T) {
expected := "str"
if actual := MakeString("foo").Type(); actual != expected {
t.Fatalf("Expected (\"foo\").type() == %q, instead got %q", expected, actual)
}
}

// String Operations

func TestStrConcatenate(t *testing.T) {
Expand Down

0 comments on commit 6a21a76

Please sign in to comment.