Skip to content

Commit

Permalink
#12 add reverse polish notation, refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
thegodenage committed Mar 26, 2024
1 parent cf9bdc8 commit cf3a37a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 13 deletions.
32 changes: 31 additions & 1 deletion internal/rule/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type expressionTreeFactory struct {
var _ ExpressionTreeFactory = (*expressionTreeFactory)(nil)

func (e *expressionTreeFactory) CreateExpressionTree(tokens []Token) (expressionTree, error) {

return nil, nil
}

// 1. While there are tokens to be read:
Expand Down Expand Up @@ -87,8 +87,38 @@ func reversePolishNotationSort(tokens []Token) []Token {
operatorStack.Push(token)
}
}

continue
}

if token.Name == tokenLParen {
operatorStack.Push(token)

continue
}

if token.Name == tokenRParen {
var lParen *Token
for !operatorStack.Empty() || lParen == nil {
v, _ := operatorStack.Pop()

vt := v.(Token)

if vt.Name == tokenLParen {
lParen = &vt
continue
}

outputTokens = append(outputTokens, vt)
}

continue
}

tokens = append(tokens, token)
}

return tokens
}

func isOperator(tkn Token) bool {
Expand Down
24 changes: 14 additions & 10 deletions internal/rule/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,38 @@ package rule

import "testing"

func Test_isAdjustableNode(t *testing.T) {
func Test_isOperator(t *testing.T) {
type args struct {
nd node
tkn Token
}
tests := []struct {
name string
args args
want bool
}{
{
name: "type isn't adjustable, false returned",
name: "is an operator, true returned",
args: args{
nd: and{},
tkn: Token{
Name: tokenMoreThan,
},
},
want: false,
want: true,
},
{
name: "type is adjustable, true returned",
name: "isn't an operator, false returned",
args: args{
nd: gt{},
tkn: Token{
Name: tokenLParen,
},
},
want: true,
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isAdjustableNode(tt.args.nd); got != tt.want {
t.Errorf("isAdjustableNode() = %v, want %v", got, tt.want)
if got := isOperator(tt.args.tkn); got != tt.want {
t.Errorf("isOperator() = %v, want %v", got, tt.want)
}
})
}
Expand Down
22 changes: 20 additions & 2 deletions internal/rule/tokenizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ var (
tokenEqual = "=="
tokenNotEqual = "!="
tokensOperators = []string{
tokenLParen,
tokenRParen,
tokenDot,
tokenDoubleAmpersand,
tokenMoreThan,
Expand Down Expand Up @@ -131,6 +129,26 @@ func (t *tokenizer) BuildTokens(variable string, expression string) ([]Token, er
match = []rune{}
continue
}

if string(match[0]) == tokenLParen {
tokens = append(tokens, Token{
Name: tokenLParen,
Value: tokenLParen,
})

match = []rune{}
continue
}

if string(match[0]) == tokenRParen {
tokens = append(tokens, Token{
Name: tokenRParen,
Value: tokenRParen,
})

match = []rune{}
continue
}
}

if len(match) > 0 {
Expand Down

0 comments on commit cf3a37a

Please sign in to comment.