diff --git a/Evaluator/BuiltIn.go b/Evaluator/BuiltIn.go index c5aa0fe..ef93ef1 100644 --- a/Evaluator/BuiltIn.go +++ b/Evaluator/BuiltIn.go @@ -158,29 +158,8 @@ func ImportLibrary(env *Environment) (iObject, error) { } pathLibraryOb.Value = strings.Replace(pathLibraryOb.Value, "/", string(os.PathSeparator), -1) path := filepath.Join(localPath, pathLibraryOb.Value) - println(path) _, e = Run(path, env) - if e != nil { - return nil, e - } - if len(env.variables) > 1 { - for name, f := range env.variables { - if name == "path" { - continue - } - if _, ok := f.(Parser.FuncDeclarationStatement); !ok { - return nil, errors.New("not possible define variables in library") - } - } - } - partsPath := strings.Split(pathLibraryOb.Value, string(os.PathSeparator)) - pathLibraryOb.Value = partsPath[len(partsPath)-1] - var newName string - for name, funct := range env.functions { - newName = pathLibraryOb.Value[:len(pathLibraryOb.Value)-4] + "_" + name - env.externals.addFunction(newName, funct) - } - return nil, nil + return libraryObject{name: pathLibraryOb.Value, env: env}, e } func LoadBuiltInFunction(env *Environment) { env.addBuiltInFunc("len", builtInFuncObject{Name: "len", NameParams: []string{"a"}, BuiltInfunc: lenBuiltin}) diff --git a/Evaluator/CallFunc.go b/Evaluator/CallFunc.go index aa6ad91..9e09fa9 100644 --- a/Evaluator/CallFunc.go +++ b/Evaluator/CallFunc.go @@ -3,6 +3,7 @@ package Evaluator import ( "FLanguage/Parser" "errors" + "strings" ) func evalCallFunc(expression Parser.ExpresionCallFunc, env *Environment) (iObject, error) { @@ -34,22 +35,26 @@ func evalCallFunc(expression Parser.ExpresionCallFunc, env *Environment) (iObjec fun = inlineFun } default: - funct, e := evalExpresion(expression.Identifier, env) - if e != nil { - - return nil, e - } hashGet, ok := expression.Identifier.(Parser.ExpresionGetValueHash) if ok { hash, e := evalExpresion(hashGet.Value, env) if e != nil { return nil, e } + if lib, ok := hash.(libraryObject); ok { + envFunc.functions = lib.env.functions + fun, e = lib.env.getFunction(strings.Split(hashGet.Index.ToString(), `"`)[1]) + } envFunc.addVariable("this", hash) } + funct, e := evalExpresion(expression.Identifier, env) + if e != nil { + return nil, e + } + tfun := fun fun, ok = funct.(Parser.FuncDeclarationStatement) if !ok { - return nil, errors.New("not a function") + fun = tfun } } if len(fun.Params) != len(expression.Values) { diff --git a/Evaluator/Expresion.go b/Evaluator/Expresion.go index a2981d8..d0b9e40 100644 --- a/Evaluator/Expresion.go +++ b/Evaluator/Expresion.go @@ -111,7 +111,11 @@ func evalExpresion(expresion Parser.IExpresion, env *Environment) (iObject, erro if e != nil { return nil, e } + if b, ok := hash.(libraryObject); ok { + return b, nil + } hash, ok := hash.(hashObject).Values[key] + if !ok { return nil, errors.New("element not found") } diff --git a/Evaluator/StandardLibrary_test.go b/Evaluator/StandardLibrary_test.go index 1ff3e41..994c35a 100644 --- a/Evaluator/StandardLibrary_test.go +++ b/Evaluator/StandardLibrary_test.go @@ -9,9 +9,9 @@ import ( func TestBinarySearch(t *testing.T) { ist := ` - import("../Library/BinarySearch.txt"); + let binary=import("../Library/BinarySearch.txt"); let list=[2,6,7,9,22,44,55,66,77,88,99]; - let b=BinarySearch_Run(list,66); + let b=binary{"Run"}(list,66); END ` lexer, e := Lexer.New([]byte(ist)) @@ -46,10 +46,10 @@ func TestBinarySearch(t *testing.T) { func TestTree(t *testing.T) { ist := ` - import("../Library/Tree.txt"); + let tree=import("../Library/Tree.txt"); let list=[22,21,6,7,9,0,55,6,-20,88,99]; - let node=Tree_MakeTree(list); - let orderedList=Tree_FromTreeToList(node,len(list)); + let node=tree{"MakeTree"}(list); + let orderedList=tree{"FromTreeToList"}(node,len(list)); END ` lexer, e := Lexer.New([]byte(ist)) diff --git a/Evaluator/StructObject.go b/Evaluator/StructObject.go index b54c49b..d40a447 100644 --- a/Evaluator/StructObject.go +++ b/Evaluator/StructObject.go @@ -107,3 +107,12 @@ func (b builtInFuncObject) ToString() string { } return r + ")" } + +type libraryObject struct { + name string + env *Environment +} + +func (l libraryObject) ToString() string { + return "Env" + l.name +} diff --git a/Library/Tree.txt b/Library/Tree.txt index 92885e8..ef5b682 100644 --- a/Library/Tree.txt +++ b/Library/Tree.txt @@ -3,9 +3,9 @@ Ff AddNode(value,node){ ret [[],value,[]]; } if(value>node[1]){ - node[2]=Tree_AddNode(value,node[2]); + node[2]=AddNode(value,node[2]); }else{ - node[0]=Tree_AddNode(value,node[0]); + node[0]=AddNode(value,node[0]); } ret node; } @@ -13,10 +13,10 @@ Ff AddNode(value,node){ Ff MakeTree(list){ let i=1; let node=[[],list[0],[]]; - let ordered=newArray(len(list),0); + let ordered=newArray(len(list),0); let v=0; while(i