Skip to content

Commit

Permalink
core/optimizer: temporarily disabled optimizer
Browse files Browse the repository at this point in the history
  • Loading branch information
xNaCly committed Nov 25, 2023
1 parent 1bba4f1 commit 4eaa872
Showing 1 changed file with 71 additions and 66 deletions.
137 changes: 71 additions & 66 deletions core/optimizer/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ var alphabetlen = len(alphabet)
// runtime.mapassign_faststr and aeshashbody (watch out for error handling,
// etc)

// TODO: Remove unused variables and functions

// TODO: precompute constants

// TODO: dead code elim, empty if, match, put, for, fun and all references to them
Expand Down Expand Up @@ -62,72 +64,75 @@ func New() *Optimiser {
}

func (o *Optimiser) Start(ast []expr.Node) []expr.Node {
astHolder := &expr.Root{
Children: make([]expr.Node, len(ast)),
}
copy(astHolder.Children, ast)

// walk ast and populate counters for unused variables and functions
for _, node := range ast {
o.walkAst(astHolder, node)
}

// unused variables
for k, v := range o.variables {
if v.Used {
continue
}
if v.Parent == nil {
continue
}
ch := v.Parent.GetChildren()
if ch == nil {
continue
}
for i, c := range ch {
if c == v.Child {
ch = append(ch[:i], ch[i+1:]...)
v.Parent.SetChildren(ch)
t := v.Child.GetToken()
debug.Logf("removed: %T(%s) [%d:%d]\n", v.Child, k, t.Line+1, t.LinePos)
delete(o.variables, k)
o.didOptimise = true
break
}
}
}

// unused functions
for k, v := range o.functions {
if v.Used {
continue
}
if v.Parent == nil {
continue
}
ch := v.Parent.GetChildren()
if ch == nil {
continue
}
for i, c := range ch {
if c == v.Child {
ch = append(ch[:i], ch[i+1:]...)
v.Parent.SetChildren(ch)
t := v.Child.GetToken()
debug.Logf("removed: %T(%s) [%d:%d]\n", v.Child, k, t.Line+1, t.LinePos)
delete(o.functions, k)
o.didOptimise = true
break
}
}
}

if o.didOptimise {
o.didOptimise = false
return o.Start(astHolder.Children)
}

return astHolder.Children
// INFO: disabled due to several bugs

// astHolder := &expr.Root{
// Children: make([]expr.Node, len(ast)),
// }
// copy(astHolder.Children, ast)

// // walk ast and populate counters for unused variables and functions
// for _, node := range ast {
// o.walkAst(astHolder, node)
// }

// // unused variables
// for k, v := range o.variables {
// if v.Used {
// continue
// }
// if v.Parent == nil {
// continue
// }
// ch := v.Parent.GetChildren()
// if ch == nil {
// continue
// }
// for i, c := range ch {
// if c == v.Child {
// ch = append(ch[:i], ch[i+1:]...)
// v.Parent.SetChildren(ch)
// t := v.Child.GetToken()
// debug.Logf("removed: %T(%s) [%d:%d]\n", v.Child, k, t.Line+1, t.LinePos)
// delete(o.variables, k)
// o.didOptimise = true
// break
// }
// }
// }

// // unused functions
// for k, v := range o.functions {
// if v.Used {
// continue
// }
// if v.Parent == nil {
// continue
// }
// ch := v.Parent.GetChildren()
// if ch == nil {
// continue
// }
// for i, c := range ch {
// if c == v.Child {
// ch = append(ch[:i], ch[i+1:]...)
// v.Parent.SetChildren(ch)
// t := v.Child.GetToken()
// debug.Logf("removed: %T(%s) [%d:%d]\n", v.Child, k, t.Line+1, t.LinePos)
// delete(o.functions, k)
// o.didOptimise = true
// break
// }
// }
// }

// if o.didOptimise {
// o.didOptimise = false
// return o.Start(astHolder.Children)
// }

// return astHolder.Children
return ast
}

// postFix appends a random id of length 5 to val, returns result
Expand Down

0 comments on commit 4eaa872

Please sign in to comment.