Skip to content

Commit

Permalink
Adds optimizations to tokenizer and parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
mtresnik committed Jun 7, 2024
1 parent 0320c9b commit 9cbc8a8
Show file tree
Hide file tree
Showing 23 changed files with 115 additions and 49 deletions.
2 changes: 1 addition & 1 deletion main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func main() {

func TestParse() {
var inputString = "a * b * c + 123 + sin(x)"
var parsed, err = parser.Parse(inputString)
var parsed, err = parser.ParseOperation(inputString)
if err != nil {
fmt.Println((*err).Error())
return
Expand Down
4 changes: 2 additions & 2 deletions pkg/operations/constants/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package constants

import (
"goast/pkg/operations"
"strconv"
"goast/pkg/utils"
)

type Constant struct {
Expand All @@ -22,7 +22,7 @@ func (c Constant) ToString() string {
if c.StringRepresentation != nil {
return *(c.StringRepresentation)
}
return strconv.FormatComplex(c.Representation, 'G', 5, 64)
return utils.SmartComplexString(c.Representation)
}

func (c Constant) ToNumber() complex128 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/operations/functions/abs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package functions

import (
"goast/pkg/operations"
"goast/pkg/utils"
"math/cmplx"
"strconv"
)

type Abs struct {
Expand All @@ -21,7 +21,7 @@ func (a Abs) IsConstant() bool {
func (a Abs) ToString() string {
if a.Inner.IsConstant() {
c := a.ToNumber()
return strconv.FormatComplex(c, 'f', 5, 64)
return utils.SmartComplexString(c)
}
retString := "abs("
retString += a.Inner.ToString()
Expand Down
4 changes: 2 additions & 2 deletions pkg/operations/functions/addition.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package functions

import (
"goast/pkg/operations"
"goast/pkg/utils"
"math/cmplx"
"strconv"
)

type Addition struct {
Expand All @@ -27,7 +27,7 @@ func (a Addition) IsConstant() bool {
func (a Addition) ToString() string {
if a.IsConstant() {
c := a.ToNumber()
return strconv.FormatComplex(c, 'f', 5, 64)
return utils.SmartComplexString(c)
}
retString := ""
var values = a.GetValues()
Expand Down
4 changes: 2 additions & 2 deletions pkg/operations/functions/arccos.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package functions

import (
"goast/pkg/operations"
"goast/pkg/utils"
"math/cmplx"
"strconv"
)

type ArcCos struct {
Expand All @@ -21,7 +21,7 @@ func (a ArcCos) IsConstant() bool {
func (a ArcCos) ToString() string {
if a.Inner.IsConstant() {
c := a.ToNumber()
return strconv.FormatComplex(c, 'f', 5, 64)
return utils.SmartComplexString(c)
}
retString := "arccos("
retString += a.Inner.ToString()
Expand Down
4 changes: 2 additions & 2 deletions pkg/operations/functions/arcsin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package functions

import (
"goast/pkg/operations"
"goast/pkg/utils"
"math/cmplx"
"strconv"
)

type ArcSin struct {
Expand All @@ -21,7 +21,7 @@ func (a ArcSin) IsConstant() bool {
func (a ArcSin) ToString() string {
if a.Inner.IsConstant() {
c := a.ToNumber()
return strconv.FormatComplex(c, 'f', 5, 64)
return utils.SmartComplexString(c)
}
retString := "arcsin("
retString += a.Inner.ToString()
Expand Down
4 changes: 2 additions & 2 deletions pkg/operations/functions/arctan.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package functions

import (
"goast/pkg/operations"
"goast/pkg/utils"
"math/cmplx"
"strconv"
)

type ArcTan struct {
Expand All @@ -21,7 +21,7 @@ func (a ArcTan) IsConstant() bool {
func (a ArcTan) ToString() string {
if a.Inner.IsConstant() {
c := a.ToNumber()
return strconv.FormatComplex(c, 'f', 5, 64)
return utils.SmartComplexString(c)
}
retString := "arctan("
retString += a.Inner.ToString()
Expand Down
4 changes: 2 additions & 2 deletions pkg/operations/functions/cos.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package functions

import (
"goast/pkg/operations"
"goast/pkg/utils"
"math/cmplx"
"strconv"
)

type Cos struct {
Expand All @@ -21,7 +21,7 @@ func (c Cos) IsConstant() bool {
func (c Cos) ToString() string {
if c.Inner.IsConstant() {
c := c.ToNumber()
return strconv.FormatComplex(c, 'f', 5, 64)
return utils.SmartComplexString(c)
}
retString := "cos("
retString += c.Inner.ToString()
Expand Down
4 changes: 2 additions & 2 deletions pkg/operations/functions/division.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package functions

import (
"goast/pkg/operations"
"goast/pkg/utils"
"math/cmplx"
"strconv"
)

type Division struct {
Expand All @@ -28,7 +28,7 @@ func (d Division) IsConstant() bool {
func (d Division) ToString() string {
if d.IsConstant() {
c := d.ToNumber()
return strconv.FormatComplex(c, 'f', 5, 64)
return utils.SmartComplexString(c)
}
retString := ""
retString += d.Numerator.ToString()
Expand Down
6 changes: 3 additions & 3 deletions pkg/operations/functions/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package functions

import (
"goast/pkg/operations"
"goast/pkg/utils"
"math/cmplx"
"strconv"
)

type Log struct {
Expand All @@ -22,9 +22,9 @@ func (l Log) IsConstant() bool {
func (l Log) ToString() string {
if l.Inner.IsConstant() {
c := l.ToNumber()
return strconv.FormatComplex(c, 'f', 5, 64)
return utils.SmartComplexString(c)
}
retString := "log("
retString := "log_("
retString += l.Base.ToString() + ", "
retString += l.Inner.ToString()
retString += ")"
Expand Down
4 changes: 2 additions & 2 deletions pkg/operations/functions/multiplication.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package functions

import (
"goast/pkg/operations"
"goast/pkg/utils"
"math/cmplx"
"strconv"
)

type Multiplication struct {
Expand All @@ -27,7 +27,7 @@ func (m Multiplication) IsConstant() bool {
func (m Multiplication) ToString() string {
if m.IsConstant() {
c := m.ToNumber()
return strconv.FormatComplex(c, 'f', 5, 64)
return utils.SmartComplexString(c)
}
retString := ""
var values = m.GetValues()
Expand Down
4 changes: 2 additions & 2 deletions pkg/operations/functions/negation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package functions

import (
"goast/pkg/operations"
"goast/pkg/utils"
"math/cmplx"
"strconv"
)

type Negation struct {
Expand All @@ -21,7 +21,7 @@ func (n Negation) IsConstant() bool {
func (n Negation) ToString() string {
if n.Inner.IsConstant() {
c := n.ToNumber()
return strconv.FormatComplex(c, 'f', 5, 64)
return utils.SmartComplexString(c)
}
retString := "-"
retString += n.Inner.ToString()
Expand Down
4 changes: 2 additions & 2 deletions pkg/operations/functions/parentheses.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package functions

import (
"goast/pkg/operations"
"goast/pkg/utils"
"math/cmplx"
"strconv"
)

type Parentheses struct {
Expand All @@ -21,7 +21,7 @@ func (p Parentheses) IsConstant() bool {
func (p Parentheses) ToString() string {
if p.Inner.IsConstant() {
c := p.Inner.ToNumber()
return strconv.FormatComplex(c, 'f', 5, 64)
return utils.SmartComplexString(c)
}
retString := "("
retString += p.Inner.ToString()
Expand Down
4 changes: 2 additions & 2 deletions pkg/operations/functions/power.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package functions

import (
"goast/pkg/operations"
"goast/pkg/utils"
"math/cmplx"
"strconv"
)

type Power struct {
Expand All @@ -25,7 +25,7 @@ func (p Power) IsConstant() bool {
func (p Power) ToString() string {
if p.IsConstant() {
c := p.ToNumber()
return strconv.FormatComplex(c, 'f', 5, 64)
return utils.SmartComplexString(c)
}
retString := p.Base.ToString()
retString += " ^ "
Expand Down
4 changes: 2 additions & 2 deletions pkg/operations/functions/sin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package functions

import (
"goast/pkg/operations"
"goast/pkg/utils"
"math/cmplx"
"strconv"
)

type Sin struct {
Expand All @@ -21,7 +21,7 @@ func (s Sin) IsConstant() bool {
func (s Sin) ToString() string {
if s.Inner.IsConstant() {
c := s.ToNumber()
return strconv.FormatComplex(c, 'f', 5, 64)
return utils.SmartComplexString(c)
}
retString := "sin("
retString += s.Inner.ToString()
Expand Down
4 changes: 2 additions & 2 deletions pkg/operations/functions/subtraction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package functions

import (
"goast/pkg/operations"
"goast/pkg/utils"
"math/cmplx"
"strconv"
)

type Subtraction struct {
Expand All @@ -27,7 +27,7 @@ func (s Subtraction) IsConstant() bool {
func (s Subtraction) ToString() string {
if s.IsConstant() {
c := s.ToNumber()
return strconv.FormatComplex(c, 'f', 5, 64)
return utils.SmartComplexString(c)
}
retString := ""
for i := 0; i < len(s.Values); i++ {
Expand Down
4 changes: 2 additions & 2 deletions pkg/operations/functions/tan.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package functions

import (
"goast/pkg/operations"
"goast/pkg/utils"
"math/cmplx"
"strconv"
)

type Tan struct {
Expand All @@ -21,7 +21,7 @@ func (t Tan) IsConstant() bool {
func (t Tan) ToString() string {
if t.Inner.IsConstant() {
c := t.ToNumber()
return strconv.FormatComplex(c, 'f', 5, 64)
return utils.SmartComplexString(c)
}
retString := "tan("
retString += t.Inner.ToString()
Expand Down
18 changes: 9 additions & 9 deletions pkg/operations/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ func Remove(intermediates []Intermediate, other Intermediate) []Intermediate {
return intermediates
}

func Parse(inputString string) (operations.Operation, *error) {
func ParseOperation(inputString string) (operations.Operation, *error) {
err := validateString(inputString)
if err != nil {
return constants.NaN, err
Expand Down Expand Up @@ -730,7 +730,7 @@ func generateOperators(current []Intermediate, inputList []tokens.Token) []Inter
}

func generateIdentities(current []Intermediate, inputList []tokens.Token) []Intermediate {
var clone = slices.Clone(current)
var clone = current
for i, token := range inputList {
if indexProcessedOperation(i, clone) == false {
if token.TokenType == tokens.Operator {
Expand Down Expand Up @@ -762,13 +762,13 @@ func generateIdentities(current []Intermediate, inputList []tokens.Token) []Inte
}
}
}
var retList = slices.Clone(clone)
var retList = clone
SortByStartIndex(retList)
return retList
}

func generatePowers(current []Intermediate, inputList []tokens.Token) []Intermediate {
var clone = slices.Clone(current)
var clone = current
for i, token := range inputList {
if indexProcessedOperation(i, clone) == false {
if token.TokenType == tokens.Operator {
Expand All @@ -790,14 +790,14 @@ func generatePowers(current []Intermediate, inputList []tokens.Token) []Intermed
}
}
}
var retList = slices.Clone(clone)
var retList = clone
SortByStartIndex(retList)
return retList
}

func generateMultiplicationAndDivision(current []Intermediate, inputList []tokens.Token) []Intermediate {
// TODO Require left and right checks for mult and div
var clone = slices.Clone(current)
var clone = current
for i, token := range inputList {
if indexProcessedOperation(i, clone) == false {
if token.TokenType == tokens.Operator {
Expand Down Expand Up @@ -831,14 +831,14 @@ func generateMultiplicationAndDivision(current []Intermediate, inputList []token
}
}
}
var retList = slices.Clone(clone)
var retList = clone
SortByStartIndex(retList)
return retList
}

func generateAdditionAndSubtraction(current []Intermediate, inputList []tokens.Token) []Intermediate {
// TODO Require left and right checks for add and sub
var clone = slices.Clone(current)
var clone = current
for i, token := range inputList {
if indexProcessedOperation(i, clone) == false {
if token.TokenType == tokens.Operator {
Expand Down Expand Up @@ -873,7 +873,7 @@ func generateAdditionAndSubtraction(current []Intermediate, inputList []tokens.T
}
}

var retList = slices.Clone(clone)
var retList = clone
SortByStartIndex(retList)
return retList
}
Expand Down
Loading

0 comments on commit 9cbc8a8

Please sign in to comment.