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

feat: support floating numbers #12

Merged
merged 1 commit into from
Jul 30, 2023
Merged
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
20 changes: 10 additions & 10 deletions evaluator/evaluator.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package evaluator

import (
"github.com/grahms/samoralang/ast"

Check failure on line 4 in evaluator/evaluator.go

View workflow job for this annotation

GitHub Actions / build

cannot find package "github.com/grahms/samoralang/ast" in any of:
"github.com/grahms/samoralang/object"
)

Expand Down Expand Up @@ -144,7 +144,7 @@
}

func evalMinusPrefixOperatorExpression(right object.Object) object.Object {
if right.Type() != object.INTEGER_OBJ {
if right.Type() != object.IntegerObj {
return newError("unknown operator: -%s", right.Type())
}
value := right.(*object.Integer).Value
Expand All @@ -154,23 +154,23 @@
func evalInfixExpression(operator string, left, right object.Object) object.Object {
switch {

case left.Type() == object.INTEGER_OBJ && right.Type() == object.INTEGER_OBJ:
case left.Type() == object.IntegerObj && right.Type() == object.IntegerObj:
return evalIntegerInfixExpression(operator, left, right)
case left.Type() == object.FLOAT_OBJ && right.Type() == object.FLOAT_OBJ:
case left.Type() == object.FloatObj && right.Type() == object.FloatObj:
return evalFloatInfixExpression(operator, left, right)
case left.Type() == object.INTEGER_OBJ && right.Type() == object.FLOAT_OBJ:
case left.Type() == object.IntegerObj && right.Type() == object.FloatObj:
return evalMixedInfixExpression(operator, left, right)
case left.Type() == object.FLOAT_OBJ && right.Type() == object.INTEGER_OBJ:
case left.Type() == object.FloatObj && right.Type() == object.IntegerObj:
return evalMixedInfixExpression(operator, left, right)
case operator == "==":
return nativeBoolToBooleanObject(left == right)
case operator == "!=":
return nativeBoolToBooleanObject(left != right)
case left.Type() != right.Type():
return newError("type mismatch: %s %s %s", left.Type(), operator, right.Type())
case left.Type() == object.STRING_OBJ && right.Type() == object.STRING_OBJ:
case left.Type() == object.StringObj && right.Type() == object.StringObj:
return evalStringInfixExpression(operator, left, right)
case left.Type() == object.STRING_OBJ && right.Type() == object.STRING_OBJ:
case left.Type() == object.StringObj && right.Type() == object.StringObj:
return evalStringInfixExpression(operator, left, right)

default:
Expand Down Expand Up @@ -306,7 +306,7 @@
var result object.Object
for _, stmt := range block.Statements {
result = Eval(stmt, env)
if result != nil && result.Type() == object.RETURN_VALUE_OBJ {
if result != nil && result.Type() == object.ReturnValueObj {
return result
}
}
Expand Down Expand Up @@ -373,9 +373,9 @@

func evalIndexExpression(left, index object.Object) object.Object {
switch {
case left.Type() == object.ARRAY_OBJ && index.Type() == object.INTEGER_OBJ:
case left.Type() == object.ArrayObj && index.Type() == object.IntegerObj:
return evalArrayIndexExpression(left, index)
case left.Type() == object.HASH_OBJ:
case left.Type() == object.HashObj:
return evalHashIndexExpression(left, index)

default:
Expand Down
42 changes: 21 additions & 21 deletions object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ type Hashable interface {
}

const (
INTEGER_OBJ = "INTEGER"
FLOAT_OBJ = "FLOAT"
BOOL_OBJ = "BOOLEAN"
NULL_OBJ = "NULL"
RETURN_VALUE_OBJ = "RETURN_VALUE"
ERROR_OBJ = "ERROR"
FUNCTION_OBJ = "FUNCTION"
STRING_OBJ = "STRING"
BUILTIN_OBJ = "BUILTIN"
ARRAY_OBJ = "ARRAY"
HASH_OBJ = "HASH"
IntegerObj = "INTEGER"
FloatObj = "FLOAT"
BoolObj = "BOOLEAN"
NullObj = "NULL"
ReturnValueObj = "RETURN_VALUE"
ErrorObj = "ERROR"
FunctionObj = "FUNCTION"
StringObj = "STRING"
BuiltinObj = "BUILTIN"
ArrayObj = "ARRAY"
HashObj = "HASH"
)

type Object interface {
Expand All @@ -37,39 +37,39 @@ type Integer struct {
}

func (i *Integer) Inspect() string { return fmt.Sprintf("%d", i.Value) }
func (i *Integer) Type() ObjectType { return INTEGER_OBJ }
func (i *Integer) Type() ObjectType { return IntegerObj }

type Float struct {
Value float64
}

func (f *Float) Inspect() string { return fmt.Sprintf("%f", f.Value) }
func (f *Float) Type() ObjectType { return FLOAT_OBJ }
func (f *Float) Type() ObjectType { return FloatObj }

type Boolean struct {
Value bool
}

func (b *Boolean) Type() ObjectType { return BOOL_OBJ }
func (b *Boolean) Type() ObjectType { return BoolObj }
func (b *Boolean) Inspect() string { return fmt.Sprintf("%t", b.Value) }

type Null struct{}

func (n *Null) Type() ObjectType { return NULL_OBJ }
func (n *Null) Type() ObjectType { return NullObj }
func (n *Null) Inspect() string { return "null" }

type ReturnValue struct {
Value Object
}

func (rv *ReturnValue) Type() ObjectType { return RETURN_VALUE_OBJ }
func (rv *ReturnValue) Type() ObjectType { return ReturnValueObj }
func (rv *ReturnValue) Inspect() string { return rv.Value.Inspect() }

type Error struct {
Message string
}

func (e *Error) Type() ObjectType { return ERROR_OBJ }
func (e *Error) Type() ObjectType { return ErrorObj }
func (e *Error) Inspect() string { return "ERROR: " + e.Message }

type Function struct {
Expand All @@ -78,7 +78,7 @@ type Function struct {
Env *Environment
}

func (f *Function) Type() ObjectType { return FUNCTION_OBJ }
func (f *Function) Type() ObjectType { return FunctionObj }
func (f *Function) Inspect() string {
var out bytes.Buffer
params := []string{}
Expand All @@ -99,14 +99,14 @@ type String struct {
Value string
}

func (s *String) Type() ObjectType { return STRING_OBJ }
func (s *String) Type() ObjectType { return StringObj }
func (s *String) Inspect() string { return s.Value }

type Array struct {
Elements []Object
}

func (a *Array) Type() ObjectType { return ARRAY_OBJ }
func (a *Array) Type() ObjectType { return ArrayObj }
func (a *Array) Inspect() string {
var out bytes.Buffer
elements := []string{}
Expand All @@ -128,7 +128,7 @@ type Hash struct {
Pairs map[HashKey]HashPair
}

func (h *Hash) Type() ObjectType { return HASH_OBJ }
func (h *Hash) Type() ObjectType { return HashObj }
func (h *Hash) Inspect() string {
var out bytes.Buffer
pairs := []string{}
Expand Down
Binary file modified samora
Binary file not shown.
Loading