Skip to content
Snippets Groups Projects
Commit dbc23eb8 authored by Leo's avatar Leo Committed by Leo
Browse files

fix: object example, added asty extension and fix numeric evaluation by...

fix: object example, added asty extension and fix numeric evaluation by keeping it float, and representing based on numeric type
parent 7cfd55c8
No related branches found
No related tags found
No related merge requests found
......@@ -2,7 +2,7 @@
<configuration default="false" name="Main" type="Application" factoryName="Application" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="Main" />
<module name="styve" />
<option name="PROGRAM_PARAMETERS" value="examples/" />
<option name="PROGRAM_PARAMETERS" value="examples/unary.styve -p" />
<method v="2">
<option name="Make" enabled="true" />
</method>
......
......@@ -8,20 +8,20 @@ Arne Arne Arne obj Arne 8=
STANDUP("Object expressions:") Styve
STANDUP("First name:", obj probe firstName) Styve
STANDUP("Last name:", obj probe lastName) Styve
STANDUP("Age:", obj probe age) Styve
STANDUP("Is alive:", obj probe isAlive) Styve
STANDUP("Is dead:", obj probe isDead) Styve
STANDUP("First name:", obj Arne Styve firstName) Styve
STANDUP("Last name:", obj Arne Styve lastName) Styve
STANDUP("Age:", obj Arne Styve age) Styve
STANDUP("Is alive:", obj Arne Styve isAlive) Styve
STANDUP("Is dead:", obj Arne Styve isDead) Styve
--- Assignment
Arne Arne Styve constVal Arne obj probe firstName Styve
Arne Arne Styve constVal Arne obj Arne Styve firstName Styve
STANDUP("Assigned value:", constVal) Styve
--- Test setting a value
obj probe firstName = "Not Arne" Styve
STANDUP("Changed value:", obj probe firstName) Styve
obj Arne Styve firstName Arne "Not Arne" Styve
STANDUP("Changed value:", obj Arne Styve firstName) Styve
--- Test setting a value using unary
obj probe isAlive = ! obj probe isAlive Styve
STANDUP("Changed value using unary:", obj probe isAlive) Styve
obj Arne Styve isAlive Arne ! obj Arne Styve isAlive Styve
STANDUP("Changed value using unary:", obj Arne Styve isAlive) Styve
......@@ -20,12 +20,11 @@ import frontend.Constants.Colors;
public class Main {
public static boolean validateFileExtension(String filename) {
String[] validExtensions = {".styve", ".sty"};
String[] validExtensions = {".styve", ".sty", ".asty"};
if (!filename.contains(".")) {
return false;
}
String extension = filename.substring(filename.lastIndexOf('.'));
System.out.println("Filename: " + filename + " Extension: " + extension);
return Arrays.asList(validExtensions).contains(extension);
}
public static void main(String[] args) {
......@@ -67,6 +66,8 @@ public class Main {
if (files.isEmpty()) {
throw new RuntimeException("No files found in directory: " + filename);
}
// Sort the files by name
files.sort(Path::compareTo);
System.out.println(files.size() + " files found in directory: " + filename);
for (Path file : files) {
if (!validateFileExtension(file.toString())) {
......
......@@ -191,29 +191,30 @@ public static RTValue evalPostfixOperatorExpr(PostfixOperatorExpr postfixOp, Env
}
public static RTValue evalNumericExpr(String operator, RTNumericVal left, RTNumericVal right) {
return switch (operator) {
case "+" -> RTNumericVal.from(left.getValue() + right.getValue());
case "-" -> RTNumericVal.from(left.getValue() - right.getValue());
case "*" -> RTNumericVal.from(left.getValue() * right.getValue());
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 "/" -> {
if (right.getValue() == 0) {
if (right.getFloatValue() == 0) {
throw new RuntimeException("Evaluation error: division by zero");
}
yield RTNumericVal.from(left.getValue() / right.getValue());
yield RTNumericVal.from(left.getFloatValue() / right.getFloatValue());
}
case "%" -> RTNumericVal.from(left.getValue() % right.getValue());
case "<" -> new RTBooleanVal(left.getValue() < right.getValue());
case "<=" -> new RTBooleanVal(left.getValue() <= right.getValue());
case ">" -> new RTBooleanVal(left.getValue() > right.getValue());
case ">=" -> new RTBooleanVal(left.getValue() >= right.getValue());
case "==" -> new RTBooleanVal(left.getValue() == right.getValue());
case "!=" -> new RTBooleanVal(left.getValue() != right.getValue());
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());
case "===" -> {
boolean isEqual = left.equals(right);
yield new RTBooleanVal(isEqual);
}
default -> throw new RuntimeException("Evaluation error: unknown binary operator: " + operator);
};
return val;
}
public static RTValue evalIdentExpr(IdentifierExpr ident, Environment env, boolean debug) {
......
......@@ -6,18 +6,28 @@ import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
public class RTNumericVal extends RTValue {
public long value;
public long longValue;
public float floatValue;
private boolean isInt;
public RTNumericVal(float value) {
this.type = rtValueType.NUMBER;
this.value = (long) value;
this.floatValue = value;
this.longValue = (long) value;
this.isInt = false;
}
public RTNumericVal(int value) {
this.type = rtValueType.NUMBER;
this.value = value;
this.floatValue = value;
this.longValue = value;
this.isInt = true;
}
public RTNumericVal(long value) {
this.type = rtValueType.NUMBER;
this.floatValue = value;
this.longValue = value;
this.isInt = true;
}
......@@ -52,12 +62,16 @@ public class RTNumericVal extends RTValue {
return new RTNumericVal(value);
}
public float getValue() {
if (this.isInt) {
return (int) value;
} else {
return value;
public long getLongValue() {
return this.longValue;
}
public float getFloatValue() {
return this.floatValue;
}
public float getValue() {
return this.floatValue;
}
public boolean isInteger() {
......@@ -66,25 +80,35 @@ public class RTNumericVal extends RTValue {
public String toString() {
DecimalFormat df = new DecimalFormat("0");
df.setMaximumFractionDigits(340); // 340 = DecimalFormat.DOUBLE_FRACTION_DIGITS
if (this.isInteger()) {
df.setMaximumFractionDigits(0);
} else {
df.setMaximumFractionDigits(2);
}
return df.format(this.getValue());
}
public void setValue(float value) {
this.value = (long) value;
this.floatValue = value;
this.longValue = (long) value;
this.isInt = false;
}
public void setValue(int value) {
this.value = value;
this.floatValue = value;
this.longValue = value;
this.isInt = true;
}
public boolean equals(RTValue other) {
if (other.getType() == rtValueType.NUMBER) {
RTNumericVal otherNum = (RTNumericVal) other;
return this.value == otherNum.value;
if (this.isInteger() && otherNum.isInteger()) {
return this.longValue == otherNum.longValue;
} else {
return this.floatValue == otherNum.floatValue;
}
}
return false;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment