Skip to content

Commit

Permalink
refactor call function
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Frangiamore authored and Luca Frangiamore committed May 17, 2024
1 parent 2b7b6be commit e1b1854
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
58 changes: 33 additions & 25 deletions Evaluator/CallFunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func evalCallFunc(expression Parser.ExpresionCallFunc, env *Environment) (iObject, error) {
envFunc := &Environment{
funcEnv := &Environment{
variables: make(map[string]iObject),
functions: make(map[string]Parser.FuncDeclarationStatement),
externals: env,
Expand All @@ -16,53 +16,45 @@ func evalCallFunc(expression Parser.ExpresionCallFunc, env *Environment) (iObjec
}
var fun Parser.FuncDeclarationStatement
var e error
switch ident := expression.Identifier.(type) {
switch ident := expression.Called.(type) {
case Parser.ExpresionLeaf:
funcBuiltInObject, ok := env.getBuiltInFunc(ident.Value)
if ok == nil {
return callBuiltInFunc(expression, funcBuiltInObject, envFunc)
return callBuiltInFunc(expression, funcBuiltInObject, funcEnv)
}
fun, e = env.getFunction(ident.Value)
if e != nil {
inlineVar, e := env.getVariable(ident.Value)
if e != nil {
return nil, e
}
inlineFun, ok := inlineVar.(Parser.FuncDeclarationStatement)
if !ok {
return nil, errors.New("not a function")
}
fun = inlineFun
if t, e := getFunctionInLeaf(env, ident); e == nil {
fun = t.(Parser.FuncDeclarationStatement)
}

default:
hashGet, ok := expression.Identifier.(Parser.ExpresionGetValueHash)
if ok {
hashGet, isHashGet := expression.Called.(Parser.ExpresionGetValueHash)
if isHashGet {
hash, e := evalExpresion(hashGet.Value, env)
if e != nil {
return nil, e
}
if lib, ok := hash.(libraryObject); ok {
envFunc.functions = lib.env.functions
funcEnv.functions = lib.env.functions
fun, e = lib.env.getFunction(strings.Split(hashGet.Index.ToString(), `"`)[1])
}
envFunc.addVariable("this", hash)
funcEnv.addVariable("this", hash)
}
funct, e := evalExpresion(expression.Identifier, env)
funct, e := evalExpresion(expression.Called, env)
if e != nil {
return nil, e
}
tfun := fun
fun, ok = funct.(Parser.FuncDeclarationStatement)
if !ok {
fun, isHashGet = funct.(Parser.FuncDeclarationStatement)
if !isHashGet {
fun = tfun
}
}
if len(fun.Params) != len(expression.Values) {
if len(fun.Params) != len(expression.Params) {
return nil, errors.New("not enough parms")
}
evalParms(expression.Values, fun.Params, envFunc)
evalParms(expression.Params, fun.Params, funcEnv)

valExp, e := Eval(fun.Body.(*Parser.StatementNode), envFunc)
valExp, e := Eval(fun.Body.(*Parser.StatementNode), funcEnv)
if e != nil {
return nil, e

Expand All @@ -74,8 +66,24 @@ func evalCallFunc(expression Parser.ExpresionCallFunc, env *Environment) (iObjec
return v.Value, nil
}

func getFunctionInLeaf(envSource *Environment, ident Parser.ExpresionLeaf) (iObject, error) {

fun, e := envSource.getFunction(ident.Value)
if e != nil {
inlineVar, e := envSource.getVariable(ident.Value)
if e != nil {
return nil, e
}
inlineFun, ok := inlineVar.(Parser.FuncDeclarationStatement)
if !ok {
return nil, errors.New("not a function")
}
return inlineFun, nil
}
return fun, e
}
func callBuiltInFunc(expression Parser.ExpresionCallFunc, funcBuiltInObject builtInFuncObject, env *Environment) (iObject, error) {
err := evalParms(expression.Values, funcBuiltInObject.NameParams, env)
err := evalParms(expression.Params, funcBuiltInObject.NameParams, env)
if err != nil {
return nil, err
}
Expand Down
12 changes: 6 additions & 6 deletions Parser/ExpresionObjects.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,22 @@ func (_ ExpresionLeaf) New(t Token.Token) ExpresionLeaf {
}

type ExpresionCallFunc struct {
Values []IExpresion
Identifier IExpresion
Params []IExpresion
NameFunc IExpresion
}

func (e *ExpresionCallFunc) AddParm(value IExpresion) {
e.Values = append(e.Values, value)
e.Params = append(e.Params, value)
}
func (e ExpresionCallFunc) ToString() string {
r := e.Identifier.ToString() + "("
r := e.NameFunc.ToString() + "("
i := 0
for {

if i == len(e.Values) {
if i == len(e.Params) {
break
}
r += e.Values[i].ToString() + ","
r += e.Params[i].ToString() + ","
i++
}
r += ")"
Expand Down

0 comments on commit e1b1854

Please sign in to comment.