Skip to content

Commit

Permalink
Fixed small bug in factorial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaumgarten committed Dec 4, 2020
1 parent 00b787b commit f3e3245
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
10 changes: 7 additions & 3 deletions pkg/optimizers/static_expressions.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package optimizers

import (
"fmt"

"github.com/dbaumgarten/yodk/pkg/number"
"github.com/dbaumgarten/yodk/pkg/parser/ast"
"github.com/dbaumgarten/yodk/pkg/vm"
Expand Down Expand Up @@ -52,9 +50,15 @@ func (o *StaticExpressionOptimizer) OptimizeExpressionNonRecursive(exp ast.Expre

res, err := vm.RunUnaryOperation(constToVar(n.Exp), n.Operator)
if err != nil {
fmt.Println(err)
break
}

// Most of the times the results of factorials are far longer then the original expression
// Only pre-evaluate if the result is relatively short
if n.Operator == "!" && res.IsNumber() && len(res.Number().String()) > 2 {
break
}

return varToConst(res, n.Position)
case *ast.BinaryOperation:
if !isConstant(n.Exp1) || !isConstant(n.Exp2) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/parser/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,12 @@ func (p *Printer) printUnaryOperation(o *ast.UnaryOperation, visitType int) {
}
}
if visitType == ast.PostVisit {
if o.Operator == "!" {
p.Write(o.Operator)
}
if childPrio < thisPrio {
p.Write(")")
}
if o.Operator == "!" {
p.Write(o.Operator)
}
}
}

Expand Down

0 comments on commit f3e3245

Please sign in to comment.