Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exponentiation Operator #838

Merged
merged 8 commits into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/org/mozilla/javascript/CodeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,7 @@ private void visitExpression(Node node, int contextFlags)
case Token.MOD:
case Token.DIV:
case Token.MUL:
case Token.EXP:
case Token.EQ:
case Token.NE:
case Token.SHEQ:
Expand Down
4 changes: 4 additions & 0 deletions src/org/mozilla/javascript/Decompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,10 @@ else if (nextToken == Token.NAME) {
result.append(" % ");
break;

case Token.EXP:
result.append(" ** ");
break;

case Token.COLONCOLON:
result.append("::");
break;
Expand Down
18 changes: 14 additions & 4 deletions src/org/mozilla/javascript/IRFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.mozilla.javascript.ast.ThrowStatement;
import org.mozilla.javascript.ast.TryStatement;
import org.mozilla.javascript.ast.UnaryExpression;
import org.mozilla.javascript.ast.UpdateExpression;
import org.mozilla.javascript.ast.VariableDeclaration;
import org.mozilla.javascript.ast.VariableInitializer;
import org.mozilla.javascript.ast.WhileLoop;
Expand Down Expand Up @@ -215,6 +216,9 @@ public Node transform(AstNode node) {
if (node instanceof UnaryExpression) {
return transformUnary((UnaryExpression)node);
}
if (node instanceof UpdateExpression) {
return transformUpdate((UpdateExpression)node);
}
if (node instanceof XmlMemberGet) {
return transformXmlMemberGet((XmlMemberGet)node);
}
Expand Down Expand Up @@ -1185,17 +1189,22 @@ private Node transformUnary(UnaryExpression node) {
if (type == Token.DEFAULTNAMESPACE) {
return transformDefaultXmlNamepace(node);
}
decompiler.addToken(type);

Node child = transform(node.getOperand());
return createUnary(type, child);
}

private Node transformUpdate(UpdateExpression node) {
int type = node.getType();
if (node.isPrefix()) {
decompiler.addToken(type);
}
Node child = transform(node.getOperand());
if (node.isPostfix()) {
decompiler.addToken(type);
}
if (type == Token.INC || type == Token.DEC) {
return createIncDec(type, node.isPostfix(), child);
}
return createUnary(type, child);
return createIncDec(type, node.isPostfix(), child);
}

private Node transformVariables(VariableDeclaration node) {
Expand Down Expand Up @@ -2253,6 +2262,7 @@ private Node createAssignment(int assignType, Node left, Node right)
case Token.ASSIGN_MUL: assignOp = Token.MUL; break;
case Token.ASSIGN_DIV: assignOp = Token.DIV; break;
case Token.ASSIGN_MOD: assignOp = Token.MOD; break;
case Token.ASSIGN_EXP: assignOp = Token.EXP; break;
default: throw Kit.codeBug();
}

Expand Down
8 changes: 6 additions & 2 deletions src/org/mozilla/javascript/Interpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1405,7 +1405,8 @@ private static Object interpretLoop(Context cx, CallFrame frame,
case Token.SUB :
case Token.MUL :
case Token.DIV :
case Token.MOD : {
case Token.MOD :
case Token.EXP : {
stackTop = doArithmetic(frame, op, stack, sDbl, stackTop);
continue Loop;
}
Expand Down Expand Up @@ -3223,9 +3224,9 @@ private static void doAdd(Object[] stack, double[] sDbl, int stackTop,

private static int doArithmetic(CallFrame frame, int op, Object[] stack,
double[] sDbl, int stackTop) {
double lDbl = stack_double(frame, stackTop - 1);
double rDbl = stack_double(frame, stackTop);
--stackTop;
double lDbl = stack_double(frame, stackTop);
stack[stackTop] = DOUBLE_MARK;
switch (op) {
case Token.SUB:
Expand All @@ -3240,6 +3241,9 @@ private static int doArithmetic(CallFrame frame, int op, Object[] stack,
case Token.MOD:
lDbl %= rDbl;
break;
case Token.EXP:
lDbl = Math.pow(lDbl, rDbl);
break;
}
sDbl[stackTop] = lDbl;
return stackTop;
Expand Down
Loading