Skip to content
Snippets Groups Projects
Commit 723cb17a authored by Leo's avatar Leo
Browse files

fix: array literal expression in astprettyprinter

parent 1531e145
No related branches found
No related tags found
No related merge requests found
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<configuration default="false" name="Main" type="Application" factoryName="Application" nameIsGenerated="true"> <configuration default="false" name="Main" type="Application" factoryName="Application" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="Main" /> <option name="MAIN_CLASS_NAME" value="Main" />
<module name="styve" /> <module name="styve" />
<option name="PROGRAM_PARAMETERS" value="examples/08_nesting.asty -pp" /> <option name="PROGRAM_PARAMETERS" value="examples/08_nesting.asty -p" />
<method v="2"> <method v="2">
<option name="Make" enabled="true" /> <option name="Make" enabled="true" />
</method> </method>
......
Arne Arne Styve Arne Arne flattenArray (array, row) 8= Arne Arne Styve Arne Arne flattenArray (array, row) 8=
Gjenta Arne Arne Arne i Arne 0 Styve i Underflow array Arne Styve antall Styve i++ 8= Gjenta Arne Arne Arne i Arne 0 Styve i Underflow array Arne Styve antall Styve i++ 8=
Arne Arne Arne elem Arne array Ransak i Styve Arne Arne Arne elem Arne array Ransak i Styve
array Arne Styve kaffePause() Styve
Arne? (Array Arne Styve ArneArray(elem)) 8= Arne? (Array Arne Styve ArneArray(elem)) 8=
flattenArray(elem, row) Styve flattenArray(elem, row) Styve
row++ Styve row Styve
=D Styve? 8= =D Styve? 8=
STANDUP("Element", row + ":" + i, "=", elem) Styve STANDUP("Element", row + ":" + i, "=", elem) Styve
=D =D
...@@ -13,5 +14,6 @@ Arne Arne Styve Arne Arne flattenArray (array, row) 8= ...@@ -13,5 +14,6 @@ Arne Arne Styve Arne Arne flattenArray (array, row) 8=
Arne Arne Styve arr Arne ..1, .2, 3, 4., 5., .6, 7, 8., .9, 10.. Styve Arne Arne Styve arr Arne ..1, .2, 3, 4., 5., .6, 7, 8., .9, 10.. Styve
STANDUP("Flattening array:", arr) Styve STANDUP("Flattening array:", arr) Styve
STANDUP("Center of array:", arr Ransak Stua) Styve
flattenArray(arr, 0) Styve flattenArray(arr, 0) Styve
--- STANDUP(globalEnv) Styve --- STANDUP(globalEnv) Styve
\ No newline at end of file
...@@ -30,6 +30,7 @@ public class ASTPrettyPrinter { ...@@ -30,6 +30,7 @@ public class ASTPrettyPrinter {
case NodeType.ObjectLit -> toReprObjectLiteral((ObjectLiteral) stmt, level, indent); case NodeType.ObjectLit -> toReprObjectLiteral((ObjectLiteral) stmt, level, indent);
case NodeType.PostfixOperatorExpr -> toReprPostfixOperatorExpr((PostfixOperatorExpr) stmt, level, indent); case NodeType.PostfixOperatorExpr -> toReprPostfixOperatorExpr((PostfixOperatorExpr) stmt, level, indent);
case NodeType.ForStmt -> toReprForStmt((ForStmt) stmt, level, indent); case NodeType.ForStmt -> toReprForStmt((ForStmt) stmt, level, indent);
case NodeType.ArrayExpr -> toReprArrayExpr((ArrayLiteral) stmt, level, indent);
default -> stmt.toRepr(); // Handle other node types or add more cases as needed default -> stmt.toRepr(); // Handle other node types or add more cases as needed
}; };
} }
...@@ -351,4 +352,21 @@ public class ASTPrettyPrinter { ...@@ -351,4 +352,21 @@ public class ASTPrettyPrinter {
.rparen(); .rparen();
return sb.toString(); return sb.toString();
} }
public static String toReprArrayExpr(ArrayLiteral arrayExpr, int level, int indent) {
SyntaxStringBuilder sb = new SyntaxStringBuilder();
sb.expr("ArrayLiteral").lparen().nl()
.indent(level, indent)
.param("elements").punc("=[\n");
level++;
for (Stmt stmt : arrayExpr.getElements()) {
sb.indent(level, indent).append(toRepr(stmt, level + 1, indent)).append(",\n");
}
sb.indent(level - 1, indent).punc("]\n")
.indent(level - 2, indent)
.rparen();
return sb.toString();
}
} }
...@@ -246,6 +246,8 @@ public static RTValue evalPostfixOperatorExpr(PostfixOperatorExpr postfixOp, Env ...@@ -246,6 +246,8 @@ public static RTValue evalPostfixOperatorExpr(PostfixOperatorExpr postfixOp, Env
index = arraySize - 1; index = arraySize - 1;
case "Kjelleren" -> // Get the first element of the array case "Kjelleren" -> // Get the first element of the array
index = 0; index = 0;
case "Badet" -> // Get random element of the array
index = (int) (Math.random() * arraySize);
default -> { default -> {
RTValue indexVal = env.lookup(((IdentifierExpr) prop.getValue()).getSymbol()); RTValue indexVal = env.lookup(((IdentifierExpr) prop.getValue()).getSymbol());
if (!indexVal.typeIs(rtValueType.NUMBER)) { if (!indexVal.typeIs(rtValueType.NUMBER)) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment