Skip to content

Commit

Permalink
Remove trailing empty lines when optimizing #96
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaumgarten committed Aug 28, 2021
1 parent baf36a0 commit 8a20e19
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
5 changes: 4 additions & 1 deletion examples/yolol/unoptimized.yolol
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ myFavouriteVariable="hello world" // variable names are shortened
myFavouriteVariable+=:aglobal+anothervar // global variables are not renamed (for obvious reasons)
:x=(100*2+10/5)*10 // equations only containing constant values are evaluated at compile time
:answ=(not :a) and not :b and not :c and not :d
:answ=not(not(not :answ)) // boolean expressions are converted to shorter and equivalent expressions if possible
:answ=not(not(not :answ)) // boolean expressions are converted to shorter and equivalent expressions if possible
// If removing comments results in trailing empty lines
// these emty lines are removed (because removing them can not mess up line-numberings)
// This enables long comment-blocks at the end of a file
19 changes: 18 additions & 1 deletion pkg/optimizers/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,24 @@ type CommentOptimizer struct {

// Optimize is needed to implement Optimizer
func (o *CommentOptimizer) Optimize(prog ast.Node) error {
return prog.Accept(o)
err := prog.Accept(o)
if err != nil {
return err
}

// remove trailing empty lines
if prog, is := prog.(*ast.Program); is {
for i := len(prog.Lines) - 1; i >= 0; i-- {
hasStatements := len(prog.Lines[i].Statements) > 0
if !hasStatements {
prog.Lines = prog.Lines[:i]
} else {
break
}
}
}

return nil
}

// Visit is needed to implement Visitor
Expand Down

0 comments on commit 8a20e19

Please sign in to comment.