From 6c52a9c5e77aa8d9a76bbc73791b61ddebf51de1 Mon Sep 17 00:00:00 2001 From: Mika <44454249+anfreshman@users.noreply.github.com> Date: Sat, 18 May 2024 11:27:08 +0800 Subject: [PATCH] JavaScript generator: Fixed assignment statements (#250) Co-authored-by: yulong.guo --- .../examples/js/JavaScriptCodeGenerator.java | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/examples/src/main/java/edu/berkeley/cs/jqf/examples/js/JavaScriptCodeGenerator.java b/examples/src/main/java/edu/berkeley/cs/jqf/examples/js/JavaScriptCodeGenerator.java index f5f7df5ca..835ac25ee 100644 --- a/examples/src/main/java/edu/berkeley/cs/jqf/examples/js/JavaScriptCodeGenerator.java +++ b/examples/src/main/java/edu/berkeley/cs/jqf/examples/js/JavaScriptCodeGenerator.java @@ -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; @@ -117,7 +123,8 @@ private String generateExpression(SourceOfRandomness random) { this::generateFunctionNode, this::generatePropertyNode, this::generateIndexNode, - this::generateArrowFunctionNode + this::generateArrowFunctionNode, + this::generateAssignmentNode )).apply(random); } expressionDepth--; @@ -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.>asList( + this::generateIdentNode, + this::generateIndexNode, + this::generatePropertyNode + )).apply(random); + String rhs = generateExpression(random); + return lhs + " " + token + " " + rhs + ";"; + } }