-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* implement type float * object/object: extend CompareObjects by FLOAT_OBJ * evaluator: make evalInfixExpression for numbers more predictable * object: add more tests for number types * add float docs Signed-off-by: Flipez <code@brauser.io> * catch division by zero Signed-off-by: Flipez <code@brauser.io> Co-authored-by: Markus Freitag <fmarkus@mailbox.org>
- Loading branch information
1 parent
520d817
commit 119d400
Showing
15 changed files
with
547 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
title: "Float" | ||
menu: | ||
docs: | ||
parent: "literals" | ||
--- | ||
|
||
|
||
|
||
|
||
## Literal Specific Methods | ||
|
||
### plz_s() | ||
> Returns `STRING` | ||
Returns a string representation of the float. | ||
|
||
|
||
```js | ||
🚀 > a = 123.456 | ||
=> 123.456 | ||
🚀 > a.plz_s() | ||
=> "123.456" | ||
``` | ||
|
||
|
||
|
||
## Generic Literal Methods | ||
|
||
### methods() | ||
> Returns `ARRAY` | ||
Returns an array of all supported methods names. | ||
|
||
```js | ||
🚀 > "test".methods() | ||
=> [count, downcase, find, reverse!, split, lines, upcase!, strip!, downcase!, size, plz_i, replace, reverse, strip, upcase] | ||
``` | ||
|
||
### type() | ||
> Returns `STRING` | ||
Returns the type of the object. | ||
|
||
```js | ||
🚀 > "test".type() | ||
=> "STRING" | ||
``` | ||
|
||
### wat() | ||
> Returns `STRING` | ||
Returns the supported methods with usage information. | ||
|
||
```js | ||
🚀 > true.wat() | ||
=> BOOLEAN supports the following methods: | ||
plz_s() | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package object | ||
|
||
import ( | ||
"fmt" | ||
"hash/fnv" | ||
"strconv" | ||
) | ||
|
||
type Float struct { | ||
Value float64 | ||
} | ||
|
||
func (f *Float) Inspect() string { return f.toString() } | ||
func (f *Float) Type() ObjectType { return FLOAT_OBJ } | ||
func (f *Float) HashKey() HashKey { | ||
h := fnv.New64a() | ||
h.Write([]byte(fmt.Sprintf("%f", f.Value))) | ||
|
||
return HashKey{Type: f.Type(), Value: h.Sum64()} | ||
} | ||
|
||
func init() { | ||
objectMethods[FLOAT_OBJ] = map[string]ObjectMethod{ | ||
"plz_s": ObjectMethod{ | ||
description: "Returns a string representation of the float.", | ||
example: `🚀 > a = 123.456 | ||
=> 123.456 | ||
🚀 > a.plz_s() | ||
=> "123.456"`, | ||
returnPattern: [][]string{ | ||
[]string{STRING_OBJ}, | ||
}, | ||
method: func(o Object, args []Object) Object { | ||
f := o.(*Float) | ||
return &String{Value: f.toString()} | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (f *Float) InvokeMethod(method string, env Environment, args ...Object) Object { | ||
return objectMethodLookup(f, method, args) | ||
} | ||
|
||
func (f *Float) TryInteger() Object { | ||
if i := int64(f.Value); f.Value == float64(i) { | ||
return &Integer{Value: i} | ||
} | ||
return f | ||
} | ||
|
||
func (f *Float) toString() string { | ||
if f.Value == float64(int64(f.Value)) { | ||
return fmt.Sprintf("%.1f", f.Value) | ||
} | ||
return strconv.FormatFloat(f.Value, 'f', -1, 64) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package object_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/flipez/rocket-lang/object" | ||
) | ||
|
||
func testFloatObject(t *testing.T, obj object.Object, expected float64) bool { | ||
result, ok := obj.(*object.Float) | ||
if !ok { | ||
t.Errorf("object is not Float. got=%T (%+v)", obj, obj) | ||
return false | ||
} | ||
if result.Value != expected { | ||
t.Errorf("object has wrong value. got=%f, want=%f", result.Value, expected) | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
func TestFloatObjectMethods(t *testing.T) { | ||
tests := []inputTestCase{ | ||
{`2.1.plz_s()`, "2.1"}, | ||
{`10.0.type()`, "FLOAT"}, | ||
{`2.2.nope()`, "Failed to invoke method: nope"}, | ||
{`(2.0.wat().lines().size() == 2.0.methods().size() + 1).plz_s()`, "true"}, | ||
} | ||
|
||
testInput(t, tests) | ||
} | ||
|
||
func TestFloatHashKey(t *testing.T) { | ||
float1_1 := &object.Float{Value: 1.0} | ||
float1_2 := &object.Float{Value: 1.0} | ||
float2 := &object.Float{Value: 2.0} | ||
|
||
if float1_1.HashKey() != float1_2.HashKey() { | ||
t.Errorf("float with same content have different hash keys") | ||
} | ||
|
||
if float1_1.HashKey() == float2.HashKey() { | ||
t.Errorf("float with different content have same hash keys") | ||
} | ||
} | ||
|
||
func TestFloatInspect(t *testing.T) { | ||
float1 := &object.Float{Value: 1.0} | ||
|
||
if float1.Inspect() != "1.0" { | ||
t.Errorf("float inspect does not match value") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
119d400
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: