Skip to content

Commit

Permalink
JavaScript generator: Fixed assignment statements (#250)
Browse files Browse the repository at this point in the history
Co-authored-by: yulong.guo <[email protected]>
  • Loading branch information
anfreshman and yulong.guo authored May 18, 2024
1 parent c0c07b9 commit 6915825
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 6915825

Please sign in to comment.