Skip to content

Commit 5fbcc93

Browse files
committed
cleanup
1 parent 9a22a9d commit 5fbcc93

File tree

2 files changed

+34
-44
lines changed

2 files changed

+34
-44
lines changed

src/commonMain/kotlin/com/github/murzagalin/evaluator/Tokenizer.kt

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -51,40 +51,32 @@ internal class Tokenizer(
5151
return when {
5252
str.startsWith("true") -> PUnit(Token.Operand.Boolean(true), 4)
5353
str.startsWith("false") -> PUnit(Token.Operand.Boolean(false), 5)
54-
str.startsWith("&&") && str.requireOp("&&") -> PUnit(Token.Operator.And, 2)
55-
str.startsWith("||") && str.requireOp("||") -> PUnit(Token.Operator.Or, 2)
56-
str.startsWith("<=") && str.requireOp("<=") -> PUnit(Token.Operator.LessEqualThan, 2)
57-
str.startsWith(">=") && str.requireOp(">=") -> PUnit(Token.Operator.GreaterEqualThan, 2)
58-
str.startsWith("==") && str.requireOp("==") -> PUnit(Token.Operator.Equal, 2)
59-
str.startsWith("!=") && str.requireOp("!=") -> PUnit(Token.Operator.NotEqual, 2)
54+
str.startsWith("&&") -> PUnit(Token.Operator.And, 2)
55+
str.startsWith("||") -> PUnit(Token.Operator.Or, 2)
56+
str.startsWith("<=") -> PUnit(Token.Operator.LessEqualThan, 2)
57+
str.startsWith(">=") -> PUnit(Token.Operator.GreaterEqualThan, 2)
58+
str.startsWith("==") -> PUnit(Token.Operator.Equal, 2)
59+
str.startsWith("!=") -> PUnit(Token.Operator.NotEqual, 2)
6060
symbol in digitChars -> str.parseStartingNumber()
6161
symbol in letterChars -> str.parseVarOrConstOrFunction()
6262
symbol == argumentsDelimiter -> PUnit(Token.FunctionCall.Delimiter, 1)
63-
symbol == '+' && str.requireOp(symbol) -> getPlus(result)
64-
symbol == '-' && str.requireOp(symbol) -> getMinus(result)
65-
symbol == '%' && str.requireOp(symbol) -> PUnit(Token.Operator.Modulo, 1)
66-
symbol == '*' && str.requireOp(symbol) -> PUnit(Token.Operator.Multiplication, 1)
67-
symbol == '/' && str.requireOp(symbol) -> PUnit(Token.Operator.Division, 1)
68-
symbol == '^' && str.requireOp(symbol) -> PUnit(Token.Operator.Power, 1)
63+
symbol == '+' -> getPlus(result)
64+
symbol == '-' -> getMinus(result)
65+
symbol == '%' -> PUnit(Token.Operator.Modulo, 1)
66+
symbol == '*' -> PUnit(Token.Operator.Multiplication, 1)
67+
symbol == '/' -> PUnit(Token.Operator.Division, 1)
68+
symbol == '^' -> PUnit(Token.Operator.Power, 1)
6969
symbol == ')' -> PUnit(Token.Bracket.Right, 1)
70-
symbol == '(' && str.requireOp(symbol) -> PUnit(Token.Bracket.Left, 1)
71-
symbol == '<' && str.requireOp(symbol) -> PUnit(Token.Operator.LessThan, 1)
72-
symbol == '>' && str.requireOp(symbol) -> PUnit(Token.Operator.GreaterThan, 1)
73-
symbol == '!' && str.requireOp(symbol) -> PUnit(Token.Operator.Not, 1)
74-
symbol == '?' && str.requireOp(symbol) -> PUnit(Token.Operator.TernaryIf, 1)
75-
symbol == ':' && str.requireOp(symbol) -> PUnit(Token.Operator.TernaryElse, 1)
70+
symbol == '(' -> PUnit(Token.Bracket.Left, 1)
71+
symbol == '<' -> PUnit(Token.Operator.LessThan, 1)
72+
symbol == '>' -> PUnit(Token.Operator.GreaterThan, 1)
73+
symbol == '!' -> PUnit(Token.Operator.Not, 1)
74+
symbol == '?' -> PUnit(Token.Operator.TernaryIf, 1)
75+
symbol == ':' -> PUnit(Token.Operator.TernaryElse, 1)
7676
else -> null
7777
}
7878
}
7979

80-
private fun String.requireOp(str: String): Boolean {
81-
require(length > str.length) { "Malformed expression. '$str' requires operand after it" }
82-
83-
return true
84-
}
85-
86-
private fun String.requireOp(c: Char) = requireOp(c.toString())
87-
8880
private fun getPlus(result: List<Token>) = PUnit(
8981
if (supposedToBeUnaryOperator(result)) Token.Operator.UnaryPlus else Token.Operator.Plus,
9082
1

src/commonMain/kotlin/com/github/murzagalin/evaluator/ast/Parser.kt

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ package com.github.murzagalin.evaluator.ast
33
import com.github.murzagalin.evaluator.Token
44

55
/*
6-
expression -> ternary_if
7-
ternary_if -> logic_or ( "?" ternary_if ":" ternary_if )?
8-
logic_or -> logic_and ( ( "or" | "||" ) logic_and )*
9-
logic_and -> equality ( ( "and" | "&&" ) equality )*
6+
expression -> logic_or ( "?" expression ":" expression )?
7+
logic_or -> logic_and ( "||" logic_and )*
8+
logic_and -> equality ( "&&" equality )*
109
equality -> comparison ( ( "!=" | "==" ) comparison )*
1110
comparison -> sum ( ( ">" | ">=" | "<" | "<=" ) sum )*
1211
sum -> factor ( ( "-" | "+" ) factor )*
1312
factor -> unary ( ( "/" | "*" | "%") unary )*
1413
unary -> ( "!" | "-" | "+" ) unary | exponent
15-
exponent -> identifier ( "^" unary )*
16-
identifier -> function | variable | number | boolean
14+
exponent -> terminal ( "^" unary )*
15+
terminal -> function | variable | number | boolean
1716
1817
function -> name "(" ( arguments )* ")"
1918
arguments -> expression ( ',' expression )*
@@ -44,21 +43,20 @@ internal class Parser {
4443

4544
fun parse(tokens: List<Token>): Expression {
4645
ix = 0
47-
return expression(tokens)
48-
}
46+
val expression = expression(tokens)
47+
require(ix == tokens.size) { "malformed expression" }
4948

50-
fun expression(tokens: List<Token>): Expression {
51-
return ternaryIf(tokens)
49+
return expression
5250
}
5351

54-
private fun ternaryIf(tokens: List<Token>): Expression {
52+
private fun expression(tokens: List<Token>): Expression {
5553
val first = logicOr(tokens)
5654

5755
if (ix < tokens.size && tokens[ix] is Token.Operator.TernaryIf) {
5856
ix++
59-
val second = ternaryIf(tokens)
57+
val second = expression(tokens)
6058
require(tokens[ix++] is Token.Operator.TernaryElse) { "':' expected in ternary-if-else expression" }
61-
val third = ternaryIf(tokens)
59+
val third = expression(tokens)
6260

6361
return Expression.Ternary(Token.Operator.TernaryIfElse, first, second, third)
6462
}
@@ -149,17 +147,17 @@ internal class Parser {
149147
}
150148

151149
private fun exponent(tokens: List<Token>): Expression {
152-
var identifier = identifier(tokens)
150+
var terminal = terminal(tokens)
153151

154152
if (ix < tokens.size && tokens[ix] is Token.Operator.Power) {
155153
ix++
156-
identifier = Expression.Binary(Token.Operator.Power, identifier, unary(tokens))
154+
terminal = Expression.Binary(Token.Operator.Power, terminal, unary(tokens))
157155
}
158156

159-
return identifier
157+
return terminal
160158
}
161159

162-
private fun identifier(tokens: List<Token>): Expression {
160+
private fun terminal(tokens: List<Token>): Expression {
163161
require(tokens.size > ix) { "Expression expected" }
164162

165163
return when (val token = tokens[ix++]) {
@@ -173,7 +171,7 @@ internal class Parser {
173171
}
174172
require(tokens[ix++] is Token.Bracket.Right) { "expected ')' after a function call" }
175173

176-
return Expression.FunctionCall(token, arguments)
174+
Expression.FunctionCall(token, arguments)
177175
}
178176
is Token.Bracket.Left -> {
179177
val result = expression(tokens)

0 commit comments

Comments
 (0)