Skip to content

Commit

Permalink
JavaScript generator: Fixed assignment statements (rohanpadhye#250)
Browse files Browse the repository at this point in the history
Co-authored-by: yulong.guo <yulong.guo@nio.com>
  • Loading branch information
2 people authored and hoanglam-nguyen committed Jul 29, 2024
1 parent e035d7b commit 6c52a9c
Showing 1 changed file with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,18 @@ public JavaScriptCodeGenerator() {
};

private static final String[] BINARY_TOKENS = {
"!=", "!==", "%", "%=", "&", "&&", "&=", "*", "*=", "+", "+=", ",",
"-", "-=", "/", "/=", "<", "<<", ">>=", "<=", "=", "==", "===",
">", ">=", ">>", ">>=", ">>>", ">>>=", "^", "^=", "|", "|=", "||",
"!=", "!==", "%", "&", "&&", "*", "+", ",",
"-", "/", "<", "<<", "<=", "=", "==", "===",
">", ">=", ">>", ">>>", "^", "|", "||",
"in", "instanceof"
};

private static final String[] ASSIGNMENT_TOKENS = {
"=", "+=", "-=", "*=", "/=", "%=", "**=", "<<=", ">>=", ">>>=", "&=", "^=", "|=", "&&=",
"||=", "??=",
};


@Override
public String generate(SourceOfRandomness random, GenerationStatus status) {
this.status = status;
Expand Down Expand Up @@ -117,7 +123,8 @@ private String generateExpression(SourceOfRandomness random) {
this::generateFunctionNode,
this::generatePropertyNode,
this::generateIndexNode,
this::generateArrowFunctionNode
this::generateArrowFunctionNode,
this::generateAssignmentNode
)).apply(random);
}
expressionDepth--;
Expand Down Expand Up @@ -330,4 +337,15 @@ private String generateVarNode(SourceOfRandomness random) {
private String generateWhileNode(SourceOfRandomness random) {
return "while (" + generateExpression(random) + ")" + generateBlock(random);
}

private String generateAssignmentNode(SourceOfRandomness random) {
String token = random.choose(ASSIGNMENT_TOKENS);
String lhs = random.choose(Arrays.<Function<SourceOfRandomness, String>>asList(
this::generateIdentNode,
this::generateIndexNode,
this::generatePropertyNode
)).apply(random);
String rhs = generateExpression(random);
return lhs + " " + token + " " + rhs + ";";
}
}

0 comments on commit 6c52a9c

Please sign in to comment.