Skip to content

Commit

Permalink
refactor managing library
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 20bedf6 commit 2b7b6be
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 41 deletions.
23 changes: 1 addition & 22 deletions Evaluator/BuiltIn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
17 changes: 11 additions & 6 deletions Evaluator/CallFunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package Evaluator
import (
"FLanguage/Parser"
"errors"
"strings"
)

func evalCallFunc(expression Parser.ExpresionCallFunc, env *Environment) (iObject, error) {
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions Evaluator/Expresion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
10 changes: 5 additions & 5 deletions Evaluator/StandardLibrary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down
9 changes: 9 additions & 0 deletions Evaluator/StructObject.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
15 changes: 7 additions & 8 deletions Library/Tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ 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;
}

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<len(list)){
Tree_AddNode(list[i],node);
AddNode(list[i],node);
ordered[i]=v;
i=i+1;
}
Expand All @@ -25,20 +25,19 @@ Ff MakeTree(list){
Ff interateTreeNodes(node,i,list){

if(len(node[0])!=0){
Tree_interateTreeNodes(node[0],i,list);
interateTreeNodes(node[0],i,list);
}
list[i[0]]=node[1];
i[0]=i[0]+1;

if(len(node[2])!=0){
Tree_interateTreeNodes(node[2],i,list);
interateTreeNodes(node[2],i,list);
}
}
Ff FromTreeToList(node,n){

let i=[0];
let list=newArray(n,0);
Tree_interateTreeNodes(node,i,list);
interateTreeNodes(node,i,list);
ret list;
}
END

0 comments on commit 2b7b6be

Please sign in to comment.