Refactor VM and operators
This commit is contained in:
@@ -2,6 +2,7 @@ package object
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
@@ -33,6 +34,33 @@ func (s *String) String() string {
|
||||
return s.Value
|
||||
}
|
||||
|
||||
func (s *String) Add(other Object) (Object, error) {
|
||||
if !AssertTypes(other, StringType) {
|
||||
return nil, NewBinaryOpError(s, other, "+")
|
||||
}
|
||||
return &String{s.Value + other.(*String).Value}, nil
|
||||
}
|
||||
|
||||
func (s *String) Mul(other Object) (Object, error) {
|
||||
if !AssertTypes(other, IntegerType) {
|
||||
return nil, NewBinaryOpError(s, other, "*")
|
||||
}
|
||||
return &String{strings.Repeat(s.Value, int(other.(*Integer).Value))}, nil
|
||||
}
|
||||
|
||||
func (s *String) Get(index Object) (Object, error) {
|
||||
if !AssertTypes(index, IntegerType) {
|
||||
return nil, fmt.Errorf("invalid type for string index, expected Integer got %s", index.Type())
|
||||
}
|
||||
|
||||
i := int(index.(*Integer).Value)
|
||||
if i < 0 || i >= len(s.Value) {
|
||||
return &String{}, nil
|
||||
}
|
||||
|
||||
return &String{string(s.Value[i])}, nil
|
||||
}
|
||||
|
||||
func (s *String) Compare(other Object) int {
|
||||
if obj, ok := other.(*String); ok {
|
||||
switch {
|
||||
|
||||
Reference in New Issue
Block a user