Skip to content

Commit

Permalink
core/optimizer: fix bugs, enable by default
Browse files Browse the repository at this point in the history
  • Loading branch information
xNaCly committed Nov 17, 2023
1 parent 900cef7 commit dfba2f5
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 23 deletions.
9 changes: 4 additions & 5 deletions core/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ var TARGETS = map[string]struct{}{
}

type Config struct {
Debug bool // enable debug logs
AllErrors bool
Target string // target to compile sophia to
EnableOptimizer bool
Ast bool // print ast
Debug bool // enable debug logs
AllErrors bool
Target string // target to compile sophia to
Ast bool // print ast
}

var CONF = Config{
Expand Down
28 changes: 28 additions & 0 deletions core/expr/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package expr

import (
"sophia/core/token"
"strings"
)

type Root struct {
Children []Node
}

func (r *Root) GetChildren() []Node {
return r.Children
}

func (r *Root) SetChildren(c []Node) {
r.Children = c
}

func (r *Root) GetToken() *token.Token {
return nil
}

func (r *Root) Eval() any {
return nil
}

func (r *Root) CompileJs(b *strings.Builder) {}
6 changes: 3 additions & 3 deletions core/optimizer/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ func New() *Optimiser {

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

// walk ast and populate counters for unused variables and functions
for _, node := range ast {
Expand Down Expand Up @@ -178,13 +179,12 @@ func (o *Optimiser) walkAst(parent, node expr.Node) {
}
case *expr.Ident:
// if a variable with a matching name is subject to removal we want to remove its uses as well
if o.containsNode(o.emptyNodes, v.Name) {
if o.containsNode(o.emptyNodes, v.Name) || o.containsNode(o.nodes, v.Name) {
o.emptyNodes = append(o.emptyNodes, NodeTuple{Name: v.Name, Parent: parent, Child: v})
}

// detects a variable usage, removes the item from the tracker
o.removeNodeByName(o.nodes, v.Name)

}

children := node.GetChildren()
Expand Down
3 changes: 2 additions & 1 deletion core/optimizer/opt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ func TestOptimizer(t *testing.T) {
"(match)",
"(for (_ i) 20)",
"(fun dummy (_))(put (dummy))",
// "(fun dummy (_))(let b (dummy))(put b)",
"(fun dummy (_))(let b (dummy))(put b)",
"(let b 12)(fun dummy (_))(let b (dummy))(put b)",
}
for _, test := range tests {
tokens := lexer.New(test).Lex()
Expand Down
14 changes: 6 additions & 8 deletions core/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,12 @@ func run(input string, filename string) (s []string, e error) {
}

if filename != "repl" {
if core.CONF.EnableOptimizer {
debug.Log("done parsing - starting optimizer")
opt := optimizer.New()
ast = opt.Start(ast)
if core.CONF.Ast {
out, _ := json.MarshalIndent(ast, "", " ")
debug.Log(string(out))
}
debug.Log("done parsing - starting optimizer")
opt := optimizer.New()
ast = opt.Start(ast)
if core.CONF.Ast {
out, _ := json.MarshalIndent(ast, "", " ")
debug.Log(string(out))
}
} else {
debug.Log("done parsing - starting eval")
Expand Down
10 changes: 4 additions & 6 deletions core/run/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ func Start() {
dbg := flag.Bool("dbg", false, "enable debug logs")
allErrors := flag.Bool("all-errors", false, "display all found errors")
ast := flag.Bool("ast", false, "display the ast")
enableOptimizer := flag.Bool("enable-opt", false, "enable the experimental optimizer")
flag.Parse()
core.CONF = core.Config{
Debug: *dbg,
Target: *target,
AllErrors: *allErrors,
EnableOptimizer: *enableOptimizer,
Ast: *ast,
Debug: *dbg,
Target: *target,
AllErrors: *allErrors,
Ast: *ast,
}

if *dbg {
Expand Down

0 comments on commit dfba2f5

Please sign in to comment.