Skip to content

Commit

Permalink
👷 Rename eval error obj + add function inspect
Browse files Browse the repository at this point in the history
  • Loading branch information
ChmielewskiKamil committed Dec 1, 2024
1 parent 16d3c45 commit 17d5cbb
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
4 changes: 2 additions & 2 deletions evaluator/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func evalBlockStatement(stmts []ast.Statement, env *object.Environment) object.O
if result != nil {
// If we encounter a return statement (return value object) just bubble
// it up and let the outer block handle it. Same for error.
if result.Type() == object.EVAL_ERROR || result.Type() == object.RETURN_VALUE_OBJ {
if result.Type() == object.EVAL_ERROR_OBJ || result.Type() == object.RETURN_VALUE_OBJ {
return result
}
}
Expand Down Expand Up @@ -270,7 +270,7 @@ func newError(format string, a ...interface{}) *object.EvalError {
// when we know that left returned an error.
func isError(obj object.Object) bool {
if obj != nil {
return obj.Type() == object.EVAL_ERROR
return obj.Type() == object.EVAL_ERROR_OBJ
}
return false
}
57 changes: 55 additions & 2 deletions object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
package object

import (
"bytes"
"fmt"
"math/big"
"solbot/ast"
)

type ObjectType string

const (
EVAL_ERROR = "EVAL_ERROR"
EVAL_ERROR_OBJ = "EVAL_ERROR"
RETURN_VALUE_OBJ = "RETURN_VALUE"
INTEGER_OBJ = "INTEGER"
BOOLEAN_OBJ = "BOOLEAN"
FUNCTION_OBJ = "FUNCTION"
)

type Object interface {
Expand All @@ -38,12 +41,23 @@ type (
Boolean struct {
Value bool
}

// TODO: This looks like 1:1 copy of the FunctionDeclaration ast struct
// Name is probably needed as well to create the binding.
Function struct {
Params *ast.ParamList
Results *ast.ParamList
Body *ast.BlockStatement
Mutability ast.Mutability
Visibility ast.Visibility
Env *Environment // TODO: Do I need the env if Solidity does not have closures?
}
)

func (o *EvalError) Inspect() string {
return fmt.Sprintf("Evaluation error: %s", o.Message)
}
func (o *EvalError) Type() ObjectType { return EVAL_ERROR }
func (o *EvalError) Type() ObjectType { return EVAL_ERROR_OBJ }

func (o *ReturnValue) Inspect() string {
return o.Value.Inspect()
Expand All @@ -55,3 +69,42 @@ func (o *Integer) Type() ObjectType { return INTEGER_OBJ }

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

func (o *Function) Inspect() string {
var out bytes.Buffer

// non-nil slice; it is initialized so we can append elements right away
// if there are no params this will just be empty string
params := []string{}
for _, p := range o.Params.List {
var str string
if p.DataLocation != ast.NO_DATA_LOCATION {
str = p.Type.String() + " " + p.DataLocation.String() + " " + p.Name.String()
} else {
str = p.Type.String() + " " + p.Name.String()
}
params = append(params, str)
}

results := []string{}
for _, r := range o.Results.List {
var str string
if r.DataLocation != ast.NO_DATA_LOCATION {
str = r.Type.String() + " " + r.DataLocation.String() + " " + r.Name.String()
} else {
str = r.Type.String() + " " + r.Name.String()
}
results = append(results, str)
}

out.WriteString("function")
out.WriteString(" ")
out.WriteString(" ")
out.WriteString(" ")
out.WriteString(" ")
out.WriteString(" ")

return out.String()
}

func (o *Function) Type() ObjectType { return FUNCTION_OBJ }

0 comments on commit 17d5cbb

Please sign in to comment.