Skip to content
Snippets Groups Projects
Commit 5d63a3ea authored by Leo's avatar Leo
Browse files

feat: add compound assignment expression support.

Native handling using AssignmentExpression(BinaryExpression(left, right, operator="+Arne / -Arne / *Arne / etc.."))
parent cc2503a2
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="Styve" type="Application" factoryName="Application" nameIsGenerated="true"> <configuration default="false" name="Styve" type="Application" factoryName="Application" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="Styve" /> <option name="MAIN_CLASS_NAME" value="Styve" />
<module name="styve" /> <module name="styve" />
<option name="PROGRAM_PARAMETERS" value="examples/07_objects.styve" /> <option name="PROGRAM_PARAMETERS" value="examples/09_for_in.asty -p" />
<method v="2"> <method v="2">
<option name="Make" enabled="true" /> <option name="Make" enabled="true" />
</method> </method>
......
...@@ -22,3 +22,5 @@ STANDUP("\nLooping over keys and values of object:", obj) Styve ...@@ -22,3 +22,5 @@ STANDUP("\nLooping over keys and values of object:", obj) Styve
Gjenta Arne Arne Arne x,y Nasket obj 8= Gjenta Arne Arne Arne x,y Nasket obj 8=
STANDUP("Element:", x, "and:", y) Styve STANDUP("Element:", x, "and:", y) Styve
=D Styve =D Styve
STANDUP(obj) Styve
\ No newline at end of file
...@@ -28,6 +28,15 @@ public class BinaryExpr extends ExprAbstract { ...@@ -28,6 +28,15 @@ public class BinaryExpr extends ExprAbstract {
return operator; return operator;
} }
public boolean operatorIsAny(String ... operators) {
for (String operator : operators) {
if (this.operator.equals(operator)) {
return true;
}
}
return false;
}
public String toString() { public String toString() {
return left.toString() + " " + operator + " " + right.toString(); return left.toString() + " " + operator + " " + right.toString();
} }
......
...@@ -23,6 +23,10 @@ public class Expressions { ...@@ -23,6 +23,10 @@ public class Expressions {
// Both operands are numbers // Both operands are numbers
RTValue returnData = new RTNullVal(); RTValue returnData = new RTNullVal();
Interpreter.print(debug, "Left type: ", left.getType().toString(), ", Right type: ", right.getType().toString(), "\n"); Interpreter.print(debug, "Left type: ", left.getType().toString(), ", Right type: ", right.getType().toString(), "\n");
/*if (binOp.operatorIsAny("+=", "-=", "*=", "/=", "%=", "**=")) {
return evalCompoundAssignmentExpr(binOp.getOperator(), left, right, env, debug);
}*/
if (left.getType() == rtValueType.NUMBER && right.getType() == rtValueType.NUMBER) { if (left.getType() == rtValueType.NUMBER && right.getType() == rtValueType.NUMBER) {
returnData = evalNumericExpr(binOp.getOperator(), (RTNumericVal) left, (RTNumericVal) right); returnData = evalNumericExpr(binOp.getOperator(), (RTNumericVal) left, (RTNumericVal) right);
} else { } else {
...@@ -190,16 +194,16 @@ public static RTValue evalPostfixOperatorExpr(PostfixOperatorExpr postfixOp, Env ...@@ -190,16 +194,16 @@ public static RTValue evalPostfixOperatorExpr(PostfixOperatorExpr postfixOp, Env
public static RTValue evalNumericExpr(String operator, RTNumericVal left, RTNumericVal right) { public static RTValue evalNumericExpr(String operator, RTNumericVal left, RTNumericVal right) {
RTValue val = switch (operator) { RTValue val = switch (operator) {
case "+" -> RTNumericVal.from(left.getFloatValue() + right.getFloatValue()); case "+", "+=" -> RTNumericVal.from(left.getFloatValue() + right.getFloatValue());
case "-" -> RTNumericVal.from(left.getFloatValue() - right.getFloatValue()); case "-", "-=" -> RTNumericVal.from(left.getFloatValue() - right.getFloatValue());
case "*" -> RTNumericVal.from(left.getFloatValue() * right.getFloatValue()); case "*", "*=" -> RTNumericVal.from(left.getFloatValue() * right.getFloatValue());
case "/" -> { case "/", "/=" -> {
if (right.getFloatValue() == 0) { if (right.getFloatValue() == 0) {
throw new RuntimeException("Evaluation error: division by zero"); throw new RuntimeException("Evaluation error: division by zero");
} }
yield RTNumericVal.from(left.getFloatValue() / right.getFloatValue()); yield RTNumericVal.from(left.getFloatValue() / right.getFloatValue());
} }
case "%" -> RTNumericVal.from(left.getFloatValue() % right.getFloatValue()); case "%", "%=" -> RTNumericVal.from(left.getFloatValue() % right.getFloatValue());
case "<" -> new RTBooleanVal(left.getFloatValue() < right.getFloatValue()); case "<" -> new RTBooleanVal(left.getFloatValue() < right.getFloatValue());
case "<=" -> new RTBooleanVal(left.getFloatValue() <= right.getFloatValue()); case "<=" -> new RTBooleanVal(left.getFloatValue() <= right.getFloatValue());
case ">" -> new RTBooleanVal(left.getFloatValue() > right.getFloatValue()); case ">" -> new RTBooleanVal(left.getFloatValue() > right.getFloatValue());
...@@ -216,6 +220,32 @@ public static RTValue evalPostfixOperatorExpr(PostfixOperatorExpr postfixOp, Env ...@@ -216,6 +220,32 @@ public static RTValue evalPostfixOperatorExpr(PostfixOperatorExpr postfixOp, Env
return val; return val;
} }
public static RTValue evalCompoundAssignmentExpr(String operator, RTValue left, RTValue right, Environment env, boolean debug) {
if (left.getType() != right.getType()) {
throw new RuntimeException("Evaluation error: compound assignment operands must be of the same type");
}
if (left.getType() != rtValueType.NUMBER) {
throw new RuntimeException("Evaluation error: compound assignment operands must be numbers");
}
RTNumericVal leftNum = (RTNumericVal) left;
RTNumericVal rightNum = (RTNumericVal) right;
RTNumericVal result = switch (operator) {
case "+=" -> RTNumericVal.from(leftNum.getFloatValue() + rightNum.getFloatValue());
case "-=" -> RTNumericVal.from(leftNum.getFloatValue() - rightNum.getFloatValue());
case "*=" -> RTNumericVal.from(leftNum.getFloatValue() * rightNum.getFloatValue());
case "/=" -> {
if (rightNum.getFloatValue() == 0) {
throw new RuntimeException("Evaluation error: division by zero");
}
yield RTNumericVal.from(leftNum.getFloatValue() / rightNum.getFloatValue());
}
case "%=" -> RTNumericVal.from(leftNum.getFloatValue() % rightNum.getFloatValue());
case "**=" -> RTNumericVal.from((float) Math.pow(leftNum.getFloatValue(), rightNum.getFloatValue()));
default -> throw new RuntimeException("Evaluation error: unknown compound assignment operator: " + operator);
};
return result;
}
public static RTValue evalIdentExpr(IdentifierExpr ident, Environment env, boolean debug) { public static RTValue evalIdentExpr(IdentifierExpr ident, Environment env, boolean debug) {
RTValue value = env.lookup(ident.getSymbol()); RTValue value = env.lookup(ident.getSymbol());
Interpreter.print(debug, "Evaluating identifier expression: ", ident.getSymbol(), " => ", value.toString(), "\n"); Interpreter.print(debug, "Evaluating identifier expression: ", ident.getSymbol(), " => ", value.toString(), "\n");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment