Skip to content

Commit

Permalink
code reads like sentence
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlogic committed Jan 28, 2024
1 parent 103c46d commit fe8d49d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
40 changes: 20 additions & 20 deletions src/main/java/arb/expressions/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ public ClassVisitor generateEvaluationMethod(ClassVisitor classVisitor) throws E
methodVisitor.visitCode();
methodVisitor.visitLabel(startLabel);

Node<D, R, F> rootNode = parseRootNode();
Node<D, R, F> rootNode = evaluateRootNode();

if (position < expression.length())
{
Expand Down Expand Up @@ -687,11 +687,11 @@ public Node<D, R, F> evaluate(int depth) throws ExpressionCompilerException

int startPos = position;

if (evaluate(depth + 1, '('))
if (nextCharacterIs(depth + 1, '('))
{
node = exponentiateMultiplyAndDivideAddAndSubtract(depth + 1);

if (!evaluate(depth + 1, ')'))
if (!nextCharacterIs(depth + 1, ')'))
{
throw new ExpressionCompilerException(format("expected closing parenthesis, instead got %c at position %s in "
+ "expression '%s'", ch, startPos, expression));
Expand All @@ -714,7 +714,7 @@ else if (Parser.isLatinOrGreek(ch, false))

public int previousCharacter;

public boolean evaluate(int depth, char... expectedCharacters)
public boolean nextCharacterIs(int depth, char... expectedCharacters)
{
skipSpaces();
for (int expectedCharacter : expectedCharacters)
Expand Down Expand Up @@ -748,14 +748,14 @@ public Node<D, R, F> addAndSubtract(int depth, Node<D, R, F> node)
depth);
}

if (evaluate(depth, '+'))
if (nextCharacterIs(depth, '+'))
{
node = new Addition<>(this,
node,
exponentiateMultiplyAndDivide(depth),
depth);
}
else if (evaluate(depth, '-'))
else if (nextCharacterIs(depth, '-'))
{
node = new Subtraction<>(this,
node,
Expand All @@ -782,11 +782,11 @@ public Reference parseName(int depth, int startPos)
}
String identifier = expression.substring(startPos, position).trim();
String index = null;
if (evaluate(depth, '['))
if (nextCharacterIs(depth, '['))
{
int indexPosition = position;

while (!evaluate(depth, ']') && position < expression.length())
while (!nextCharacterIs(depth, ']') && position < expression.length())
{
nextCharacter();
}
Expand All @@ -809,7 +809,7 @@ public Node<D, R, F> parseNumber(int depth, int startPos)
depth);
}

public void parseOptionalIndependentVariableSpecification()
public void evaluateOptionalIndependentVariableSpecification()
{
expression = expression.replace("->", "➔");
int rightArrowIndex = expression.indexOf('➔');
Expand All @@ -828,10 +828,10 @@ public void parseOptionalIndependentVariableSpecification()

private Node<D, R, F> exponentiate(int depth, Node<D, R, F> node) throws ExpressionCompilerException
{
if (evaluate(depth, '^'))
if (nextCharacterIs(depth, '^'))
{
boolean parenthetical = false;
if (evaluate(depth, '('))
if (nextCharacterIs(depth, '('))
{
parenthetical = true;
}
Expand All @@ -841,7 +841,7 @@ private Node<D, R, F> exponentiate(int depth, Node<D, R, F> node) throws Express
depth + 1);
if (parenthetical)
{
if (!evaluate(depth, ')'))
if (!nextCharacterIs(depth, ')'))
{
throw new ExpressionCompilerException(String.format("parseExponent expected closing parenthesis at: position=%d, ch='%c'\n",
position,
Expand All @@ -857,12 +857,12 @@ private Node<D, R, F> exponentiate(int depth, Node<D, R, F> node) throws Express
}
}

public Node<D, R, F> parseRootNode() throws ExpressionCompilerException
public Node<D, R, F> evaluateRootNode() throws ExpressionCompilerException
{
parseOptionalIndependentVariableSpecification();
evaluateOptionalIndependentVariableSpecification();
nextCharacter();
rootNode = exponentiateMultiplyAndDivideAddAndSubtract(0);
assert rootNode != null : "parseRootNode: parseFirst() returned null, expression='" + expression + "'";
assert rootNode != null : "evaluateRootNode: parseFirst() returned null, expression='" + expression + "'";
rootNode.isResult = true;
return rootNode;
}
Expand All @@ -878,15 +878,15 @@ public Node<D, R, F> multiplyAndDivide(int depth, Node<D, R, F> node)
{
while (true)
{
if (evaluate(depth, '*', '×'))
if (nextCharacterIs(depth, '*', '×'))
{
node = new Multiplication<>(this,
node,
exponentiate(depth),
depth);

}
else if (evaluate(depth, '/', '÷'))
else if (nextCharacterIs(depth, '/', '÷'))
{
node = new Division<>(this,
node,
Expand All @@ -902,7 +902,7 @@ else if (evaluate(depth, '/', '÷'))

public Node<D, R, F> parseSuperscript(int depth, Node<D, R, F> node, char superscript, String digit)
{
if (evaluate(depth + 1, superscript))
if (nextCharacterIs(depth + 1, superscript))
{
node = new Exponentiation<>(this,
node,
Expand Down Expand Up @@ -940,7 +940,7 @@ public Node<D, R, F> resolveFunctionInvocationOrVariableReference(int depth,
int startPos) throws ExpressionCompilerException
{
Reference reference = parseName(depth, startPos);
boolean isFunction = evaluate(depth, '(');
boolean isFunction = nextCharacterIs(depth, '(');

if (isFunction)
{
Expand All @@ -951,7 +951,7 @@ public Node<D, R, F> resolveFunctionInvocationOrVariableReference(int depth,
else
{
Node<D, R, F> arg = exponentiateMultiplyAndDivideAddAndSubtract(depth + 1);
if (evaluate(depth + 1, ')'))
if (nextCharacterIs(depth + 1, ')'))
{
return new FunctionCall<>(this,
reference.name,
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/arb/expressions/nodes/unary/When.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ public static <D, R, F extends Function<D, R>> Node<D, R, F> evaluate(Expression

do
{
defaultValue = evaluateCondition(expression, depth, cases);
defaultValue = evaluateValue(expression, depth, cases);
}
while (expression.evaluate(depth + 1, ','));
if (!expression.evaluate(depth + 1, ')'))
while (expression.nextCharacterIs(depth + 1, ','));
if (!expression.nextCharacterIs(depth + 1, ')'))
{
throw new ExpressionCompilerException("Closing parenthesis expected at position=" + expression.position
+ " of expression=" + expression);
Expand All @@ -162,13 +162,13 @@ public static <D, R, F extends Function<D, R>> Node<D, R, F> evaluate(Expression
depth + 1);
}

public static <D, R, F extends Function<D, R>>
Node<D, R, F>
evaluateCondition(Expression<D, R, F> expression, int depth, TreeMap<Integer, Node<D, R, F>> cases)
public static <D, R, F extends Function<D, R>> Node<D, R, F> evaluateValue(Expression<D, R, F> expression,
int depth,
TreeMap<Integer, Node<D, R, F>> cases)
{
Node<D, R, F> value = null;

Node<D, R, F> node = expression.evaluate(depth + 1);
Node<D, R, F> node = expression.evaluate(depth + 1);
if (!(node instanceof Variable))
{
throw new ExpressionCompilerException("condition of when statement must be the equality of the input variable, but got "
Expand Down Expand Up @@ -201,7 +201,7 @@ public static <D, R, F extends Function<D, R>> Node<D, R, F> evaluate(Expression
+ expression.independentVariableNode + " not " + variable);
}

if (!expression.evaluate(depth + 1, '='))
if (!expression.nextCharacterIs(depth + 1, '='))
{
throw new ExpressionCompilerException(format("= expected in condition of when function at pos=%d expression=%s but got ch=%c and lastCh=%c",
expression.position,
Expand All @@ -217,7 +217,7 @@ public static <D, R, F extends Function<D, R>> Node<D, R, F> evaluate(Expression
+ "Integer LiteralConstant type, but got " + condition);
}
LiteralConstant<D, R, F> constant = (LiteralConstant<D, R, F>) condition;
if (!expression.evaluate(depth + 1, ','))
if (!expression.nextCharacterIs(depth + 1, ','))
{
throw new ExpressionCompilerException(", expected after condition of when function at pos="
+ expression.position);
Expand All @@ -230,7 +230,7 @@ private static <D, R, F extends Function<D, R>> Node<D, R, F> evaluateDefaultVal
int depth)
{
Node<D, R, F> defaultValue;
if (!expression.evaluate(depth + 1, ','))
if (!expression.nextCharacterIs(depth + 1, ','))
{
throw new ExpressionCompilerException(", expected after else condition");
}
Expand Down

0 comments on commit fe8d49d

Please sign in to comment.