diff --git a/examples/yolol/unoptimized.yolol b/examples/yolol/unoptimized.yolol index b65c4cf..4cd1257 100644 --- a/examples/yolol/unoptimized.yolol +++ b/examples/yolol/unoptimized.yolol @@ -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 \ No newline at end of file +: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 \ No newline at end of file diff --git a/pkg/optimizers/comments.go b/pkg/optimizers/comments.go index 16b9f3e..df46117 100644 --- a/pkg/optimizers/comments.go +++ b/pkg/optimizers/comments.go @@ -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