Skip to content

Commit

Permalink
Reference type
Browse files Browse the repository at this point in the history
  • Loading branch information
ldemailly committed Sep 3, 2024
1 parent 7189b8a commit 78f4158
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 45 deletions.
64 changes: 32 additions & 32 deletions eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (s *State) evalIndexAssigment(which ast.Node, index, value object.Object) o
if index.Type() != object.INTEGER {
return s.NewError("index assignment to array with non integer index: " + index.Inspect())
}
idx := index.(object.Integer).Value
idx := object.Value(index).(object.Integer).Value
if idx < 0 {
idx = int64(object.Len(val)) + idx
}
Expand All @@ -70,7 +70,7 @@ func (s *State) evalIndexAssigment(which ast.Node, index, value object.Object) o
}
return value
case object.MAP:
m := val.(object.Map)
m := object.Value(val).(object.Map)
m = m.Set(index, value)
oerr := s.env.Set(id.Literal(), m)
if oerr.Type() == object.ERROR {
Expand Down Expand Up @@ -265,7 +265,7 @@ func (s *State) evalInternal(node any) object.Object { //nolint:funlen,gocyclo,g
return *oerr
}
if f.Type() == object.EXTENSION {
return s.applyExtension(f.(object.Extension), args)
return s.applyExtension(object.Value(f).(object.Extension), args)
}
name := node.Function.Value().Literal()
return s.applyFunction(name, f, args)
Expand Down Expand Up @@ -329,7 +329,7 @@ func (s *State) evalPrintLogError(node *ast.Builtin) object.Object {
return r
}
if isString := r.Type() == object.STRING; isString {
buf.WriteString(r.(object.String).Value)
buf.WriteString(object.Value(r).(object.String).Value)
} else {
buf.WriteString(r.Inspect())
}
Expand Down Expand Up @@ -412,15 +412,15 @@ func (s *State) evalIndexRangeExpression(left object.Object, leftIdx, rightIdx a
return s.NewError("range index not integer")
}
num := object.Len(left)
l := leftIndex.(object.Integer).Value
l := object.Value(leftIndex).(object.Integer).Value
if l < 0 { // negative is relative to the end.
l = int64(num) + l
}
var r int64
if nilRight {
r = int64(num)
} else {
r = rightIndex.(object.Integer).Value
r = object.Value(rightIndex).(object.Integer).Value
if r < 0 {
r = int64(num) + r
}
Expand All @@ -432,7 +432,7 @@ func (s *State) evalIndexRangeExpression(left object.Object, leftIdx, rightIdx a
r = min(r, int64(num))
switch {
case left.Type() == object.STRING:
str := left.(object.String).Value
str := object.Value(left).(object.String).Value
return object.String{Value: str[l:r]}
case left.Type() == object.ARRAY:
return object.NewArray(object.Elements(left)[l:r])
Expand All @@ -452,8 +452,8 @@ func (s *State) evalIndexExpression(left, index object.Object) object.Object {
}
switch {
case left.Type() == object.STRING && idxOrZero.Type() == object.INTEGER:
idx := idxOrZero.(object.Integer).Value
str := left.(object.String).Value
idx := object.Value(idxOrZero).(object.Integer).Value
str := object.Value(left).(object.String).Value
num := len(str)
if idx < 0 { // negative is relative to the end.
idx = int64(num) + idx
Expand All @@ -474,7 +474,7 @@ func (s *State) evalIndexExpression(left, index object.Object) object.Object {
}

func evalMapIndexExpression(hash, key object.Object) object.Object {
m := hash.(object.Map)
m := object.Value(hash).(object.Map)
v, ok := m.Get(key)
if !ok {
return object.NULL
Expand All @@ -483,7 +483,7 @@ func evalMapIndexExpression(hash, key object.Object) object.Object {
}

func evalArrayIndexExpression(array, index object.Object) object.Object {
idx := index.(object.Integer).Value
idx := object.Value(index).(object.Integer).Value
maxV := int64(object.Len(array) - 1)
if idx < 0 { // negative is relative to the end.
idx = maxV + 1 + idx // elsewhere we use len() but here maxV is len-1
Expand Down Expand Up @@ -522,7 +522,7 @@ func (s *State) applyExtension(fn object.Extension, args []object.Object) object
}
// Auto promote integer to float if needed.
if fn.ArgTypes[i] == object.FLOAT && arg.Type() == object.INTEGER {
args[i] = object.Float{Value: float64(arg.(object.Integer).Value)}
args[i] = object.Float{Value: float64(object.Value(arg).(object.Integer).Value)}
continue
}
if fn.ArgTypes[i] != arg.Type() {
Expand All @@ -537,7 +537,7 @@ func (s *State) applyExtension(fn object.Extension, args []object.Object) object
}

func (s *State) applyFunction(name string, fn object.Object, args []object.Object) object.Object {
function, ok := fn.(object.Function)
function, ok := object.Value(fn).(object.Function)
if !ok {
return s.NewError("not a function: " + fn.Type().String() + ":" + fn.Inspect())
}
Expand Down Expand Up @@ -622,7 +622,7 @@ func extendFunctionEnv(
oerr := env.Set(param.Value().Literal(), args[paramIdx])
log.LogVf("set %s to %s - %s", param.Value().Literal(), args[paramIdx].Inspect(), oerr.Inspect())
if oerr.Type() == object.ERROR {
oe, _ := oerr.(object.Error)
oe, _ := object.Value(oerr).(object.Error)
return nil, &oe
}
}
Expand All @@ -644,7 +644,7 @@ func (s *State) evalExpressions(exps []ast.Node) ([]object.Object, *object.Error
for _, e := range exps {
evaluated := s.evalInternal(e)
if rt := evaluated.Type(); rt == object.ERROR {
oerr := evaluated.(object.Error)
oerr := object.Value(evaluated).(object.Error)
return nil, &oerr
}
result = append(result, evaluated)
Expand Down Expand Up @@ -743,14 +743,14 @@ func (s *State) evalForSpecialForms(fe *ast.ForExpression) (object.Object, bool)
if end.Type() != object.INTEGER {
return s.NewError("for var = n:m m not an integer: " + end.Inspect()), true
}
startInt := start.(object.Integer)
return s.evalForInteger(fe, &startInt, end.(object.Integer), name), true
startInt := object.Value(start).(object.Integer)
return s.evalForInteger(fe, &startInt, object.Value(end).(object.Integer), name), true
}
// Evaluate:
v := s.Eval(ie.Right)
switch v.Type() {
case object.INTEGER:
return s.evalForInteger(fe, nil, v.(object.Integer), name), true
return s.evalForInteger(fe, nil, object.Value(v).(object.Integer), name), true
case object.ERROR:
return v, true
case object.ARRAY, object.MAP, object.STRING:
Expand Down Expand Up @@ -819,7 +819,7 @@ func (s *State) evalForExpression(fe *ast.ForExpression) object.Object {
case object.ERROR:
return condition
case object.INTEGER:
return s.evalForInteger(fe, nil, condition.(object.Integer), "")
return s.evalForInteger(fe, nil, object.Value(condition).(object.Integer), "")
default:
return s.NewError("for condition is not a boolean nor integer nor assignment: " + condition.Inspect())
}
Expand Down Expand Up @@ -860,7 +860,7 @@ func (s *State) evalPrefixExpression(operator token.Type, right object.Object) o
return s.evalMinusPrefixOperatorExpression(right)
case token.BITNOT, token.BITXOR:
if right.Type() == object.INTEGER {
return object.Integer{Value: ^right.(object.Integer).Value}
return object.Integer{Value: ^object.Value(right).(object.Integer).Value}
}
return s.NewError("bitwise not of " + right.Inspect())
case token.PLUS:
Expand All @@ -887,10 +887,10 @@ func (s *State) evalBangOperatorExpression(right object.Object) object.Object {
func (s *State) evalMinusPrefixOperatorExpression(right object.Object) object.Object {
switch right.Type() {
case object.INTEGER:
value := right.(object.Integer).Value
value := object.Value(right).(object.Integer).Value
return object.Integer{Value: -value}
case object.FLOAT:
value := right.(object.Float).Value
value := object.Value(right).(object.Float).Value
return object.Float{Value: -value}
default:
return s.NewError("minus of " + right.Inspect())
Expand Down Expand Up @@ -932,13 +932,13 @@ func (s *State) evalInfixExpression(operator token.Type, left, right object.Obje
}

func (s *State) evalStringInfixExpression(operator token.Type, left, right object.Object) object.Object {
leftVal := left.(object.String).Value
leftVal := object.Value(left).(object.String).Value
switch {
case operator == token.PLUS && right.Type() == object.STRING:
rightVal := right.(object.String).Value
rightVal := object.Value(right).(object.String).Value
return object.String{Value: leftVal + rightVal}
case operator == token.ASTERISK && right.Type() == object.INTEGER:
rightVal := right.(object.Integer).Value
rightVal := object.Value(right).(object.Integer).Value
n := len(leftVal) * int(rightVal)
if rightVal < 0 {
return s.NewError("right operand of * on strings must be a positive integer")
Expand All @@ -959,7 +959,7 @@ func (s *State) evalArrayInfixExpression(operator token.Type, left, right object
return s.NewError("right operand of * on arrays must be an integer")
}
// TODO: go1.23 use slices.Repeat
rightVal := right.(object.Integer).Value
rightVal := object.Value(right).(object.Integer).Value
if rightVal < 0 {
return s.NewError("right operand of * on arrays must be a positive integer")
}
Expand All @@ -982,8 +982,8 @@ func (s *State) evalArrayInfixExpression(operator token.Type, left, right object
}

func evalMapInfixExpression(operator token.Type, left, right object.Object) object.Object {
leftMap := left.(object.Map)
rightMap := right.(object.Map)
leftMap := object.Value(left).(object.Map)
rightMap := object.Value(right).(object.Map)
switch operator {
case token.PLUS: // concat / append
return leftMap.Append(rightMap)
Expand All @@ -998,8 +998,8 @@ func evalMapInfixExpression(operator token.Type, left, right object.Object) obje
// https://github.com/golang/go/issues/48522
// would need getters/setters which is not very go idiomatic.
func (s *State) evalIntegerInfixExpression(operator token.Type, left, right object.Object) object.Object {
leftVal := left.(object.Integer).Value
rightVal := right.(object.Integer).Value
leftVal := object.Value(left).(object.Integer).Value
rightVal := object.Value(right).(object.Integer).Value

switch operator {
case token.PLUS:
Expand Down Expand Up @@ -1040,9 +1040,9 @@ func (s *State) evalIntegerInfixExpression(operator token.Type, left, right obje
func (s *State) getFloatValue(o object.Object) (float64, *object.Error) {
switch o.Type() {
case object.INTEGER:
return float64(o.(object.Integer).Value), nil
return float64(object.Value(o).(object.Integer).Value), nil
case object.FLOAT:
return o.(object.Float).Value, nil
return object.Value(o).(object.Float).Value, nil
default:
e := s.NewError("not converting to float: " + o.Type().String())
return math.NaN(), &e
Expand Down
4 changes: 2 additions & 2 deletions eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func testEval(t *testing.T, input string) object.Object {
}

func testIntegerObject(t *testing.T, obj object.Object, expected int64) bool {
result, ok := obj.(object.Integer)
result, ok := object.Value(obj).(object.Integer)
if !ok {
t.Errorf("object is not Integer. got=%T (%+v)", obj, obj)
return false
Expand Down Expand Up @@ -175,7 +175,7 @@ func TestBangOperator(t *testing.T) {
}

func testBooleanObject(t *testing.T, obj object.Object, expected bool) {
result, ok := obj.(object.Boolean)
result, ok := object.Value(obj).(object.Boolean)
if !ok {
t.Errorf("object is not Boolean. got=%T (%+v)", obj, obj)
return
Expand Down
3 changes: 1 addition & 2 deletions examples/boxed_text.gr
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,10 @@ func boxMatrixA(matrix) {
widths = matrixWidths(matrix)
maxes = maxWidths(widths)

lines = []
// Top line
topLine = box.topLeft + join(apply(w => box.horizontal * w,
maxes), box.topT) + box.topRight
lines = lines + [topLine]
lines = [topLine]

// Middle content (rows and separators)
lines = forRecursive(len(matrix), func(rowIndex, lines) {
Expand Down
14 changes: 9 additions & 5 deletions extensions/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,11 @@ func createJSONAndEvalFunctions(c *Config) {
MustCreate(jsonFn)
jsonFn.Name = "type"
jsonFn.Callback = object.ShortCallback(func(args []object.Object) object.Object {
return object.String{Value: args[0].Type().String()}
obj := args[0]
if r, ok := obj.(object.Reference); ok {
return object.String{Value: "&" + r.Name + ".(" + r.Value().Type().String() + ")"}
}
return object.String{Value: obj.Type().String()}
})
MustCreate(jsonFn)
jsonFn.Name = "eval"
Expand Down Expand Up @@ -290,7 +294,7 @@ func createStrFunctions() {
if a.Type() != object.STRING {
strs[i] = a.Inspect()
} else {
strs[i] = a.(object.String).Value
strs[i] = object.Value(a).(object.String).Value
}
totalLen += len(strs[i]) + sepLen
}
Expand Down Expand Up @@ -349,14 +353,14 @@ func createMisc() {
case object.NIL:
return object.Integer{Value: 0}
case object.BOOLEAN:
if o.(object.Boolean).Value {
if object.Value(o).(object.Boolean).Value {
return object.Integer{Value: 1}
}
return object.Integer{Value: 0}
case object.FLOAT:
return object.Integer{Value: int64(o.(object.Float).Value)}
return object.Integer{Value: int64(object.Value(o).(object.Float).Value)}
case object.STRING:
i, serr := strconv.ParseInt(o.(object.String).Value, 0, 64)
i, serr := strconv.ParseInt(object.Value(o).(object.String).Value, 0, 64)
if serr != nil {
return s.Error(serr)
}
Expand Down
Loading

0 comments on commit 78f4158

Please sign in to comment.