School Commit Init

This commit is contained in:
2024-08-31 12:07:21 +03:00
commit 0b130ee18c
2801 changed files with 4720552 additions and 0 deletions
@@ -0,0 +1,77 @@
package Models.Expressions;
import Exceptions.BaseException;
import Models.Program.IDictionary;
import Models.Program.IHeap;
import Models.Types.IType;
import Models.Types.IntegerType;
import Models.Values.IntegerValue;
import Models.Values.IValue;
public class ArithmeticExpression implements IExpression {
private IExpression left;
private IExpression right;
private char operator;
public ArithmeticExpression(char operator,IExpression left, IExpression right) {
this.left = left;
this.right = right;
this.operator = operator;
}
public IValue evaluate(IDictionary<String, IValue> table, IHeap heap) throws BaseException {
IValue leftValue = left.evaluate(table, heap);
IValue rightValue = right.evaluate(table, heap);
if (leftValue.getType().equals(new IntegerType())) {
int leftInt = ((IntegerValue)leftValue).getValue();
if (rightValue.getType().equals(new IntegerType())) {
int rightInt = ((IntegerValue)rightValue).getValue();
switch (operator) {
case '+':
return new IntegerValue(leftInt + rightInt);
case '-':
return new IntegerValue(leftInt - rightInt);
case '*':
return new IntegerValue(leftInt * rightInt);
case '/':
if (rightInt == 0) {
throw new BaseException("Division by zero!");
}
return new IntegerValue(leftInt / rightInt);
default:
throw new BaseException("Invalid operator!");
}
} else {
throw new BaseException("Second operand is not an integer!");
}
} else {
throw new BaseException("First operand is not an integer!");
}
}
public IExpression copy() {
return new ArithmeticExpression(operator, left.copy(), right.copy());
}
public String toString() {
return left.toString() + " " + operator + " " + right.toString();
}
public IType typeCheck(IDictionary<String, IType> typeTable) throws BaseException {
IType type1, type2;
type1 = left.typeCheck(typeTable);
type2 = right.typeCheck(typeTable);
if (type1.equals(new IntegerType())) {
if (type2.equals(new IntegerType())) {
return new IntegerType();
} else {
throw new BaseException("Second operand is not an integer!");
}
} else {
throw new BaseException("First operand is not an integer!");
}
}
}
@@ -0,0 +1,13 @@
package Models.Expressions;
import Exceptions.BaseException;
import Models.Program.IDictionary;
import Models.Program.IHeap;
import Models.Types.IType;
import Models.Values.IValue;
public interface IExpression {
public IValue evaluate(IDictionary<String, IValue> table, IHeap heap) throws BaseException;
public IType typeCheck(IDictionary<String, IType> typeTable) throws BaseException;
public IExpression copy();
}
@@ -0,0 +1,70 @@
package Models.Expressions;
import Exceptions.BaseException;
import Models.Program.IDictionary;
import Models.Program.IHeap;
import Models.Types.BooleanType;
import Models.Types.IType;
import Models.Values.BooleanValue;
import Models.Values.IValue;
public class LogicExpression implements IExpression {
private IExpression left;
private IExpression right;
private String operator;
public LogicExpression(IExpression left, IExpression right, String operator) {
this.left = left;
this.right = right;
this.operator = operator;
}
public IValue evaluate(IDictionary<String, IValue> table, IHeap heap) throws BaseException {
IValue leftValue = left.evaluate(table, heap);
IValue rightValue = right.evaluate(table, heap);
if (leftValue.getType().equals(new BooleanType())) {
boolean leftBool = ((BooleanValue)leftValue).getValue();
if (rightValue.getType().equals(new BooleanType())) {
boolean rightBool = ((BooleanValue)rightValue).getValue();
switch (operator) {
case "&&":
return new BooleanValue(leftBool && rightBool);
case "||":
return new BooleanValue(leftBool || rightBool);
default:
throw new BaseException("Invalid operator!");
}
} else {
throw new BaseException("Second operand is not a boolean!");
}
} else {
throw new BaseException("First operand is not a boolean!");
}
}
public IExpression copy() {
return new LogicExpression(left.copy(), right.copy(), operator);
}
public String toString() {
return left.toString() + " " + operator + " " + right.toString();
}
public IType typeCheck(IDictionary<String, IType> typeTable) throws BaseException {
IType leftType, rightType;
leftType = left.typeCheck(typeTable);
rightType = right.typeCheck(typeTable);
if (leftType.equals(new BooleanType())) {
if (rightType.equals(new BooleanType())) {
return new BooleanType();
} else {
throw new BaseException("Second operand is not a boolean!");
}
} else {
throw new BaseException("First operand is not a boolean!");
}
}
}
@@ -0,0 +1,41 @@
package Models.Expressions;
import Exceptions.BaseException;
import Models.Program.IDictionary;
import Models.Program.IHeap;
import Models.Types.IType;
import Models.Types.ReferenceType;
import Models.Values.IValue;
import Models.Values.ReferenceValue;
public class ReadHeap implements IExpression {
private IExpression expression;
public ReadHeap(IExpression expression) {
this.expression = expression;
}
public IValue evaluate(IDictionary<String, IValue> table, IHeap heap) throws BaseException {
IValue value = this.expression.evaluate(table, heap);
if(!(value instanceof ReferenceValue)) {
throw new BaseException("Expression is not a reference value");
}
int address = ((ReferenceValue)value).getAddress();
if(!heap.contains(address)) {
throw new BaseException("Address " + address + " is not in the heap");
}
return heap.get(address);
}
public IExpression copy() {
return new ReadHeap(this.expression.copy());
}
public IType typeCheck(IDictionary<String, IType> typeTable) throws BaseException {
IType type = this.expression.typeCheck(typeTable);
if(!(type instanceof ReferenceType)) {
throw new BaseException("Expression is not a reference value");
}
return ((ReferenceType)type).getInnerType();
}
}
@@ -0,0 +1,71 @@
package Models.Expressions;
import Exceptions.BaseException;
import Models.Program.IDictionary;
import Models.Program.IHeap;
import Models.Types.BooleanType;
import Models.Types.IType;
import Models.Types.IntegerType;
import Models.Values.BooleanValue;
import Models.Values.IValue;
import Models.Values.IntegerValue;
public class RelationalExpression implements IExpression {
private IExpression leftExpression;
private IExpression rightExpression;
private String operator;
public RelationalExpression(IExpression leftExpression, IExpression rightExpression, String operator) {
this.leftExpression = leftExpression;
this.rightExpression = rightExpression;
this.operator = operator;
}
public IExpression copy() {
return new RelationalExpression(this.leftExpression.copy(), this.rightExpression.copy(), this.operator);
}
public String toString() {
return this.leftExpression.toString() + " " + this.operator + " " + this.rightExpression.toString();
}
public IValue evaluate(IDictionary<String, IValue> symbolTable, IHeap heap) throws BaseException {
var leftValue = this.leftExpression.evaluate(symbolTable, heap);
var rightValue = this.rightExpression.evaluate(symbolTable, heap);
if (leftValue.getType().equals(new IntegerType()) && rightValue.getType().equals(new IntegerType())) {
var leftIntegerValue = (IntegerValue) leftValue;
var rightIntegerValue = (IntegerValue) rightValue;
var leftInteger = leftIntegerValue.getValue();
var rightInteger = rightIntegerValue.getValue();
switch (this.operator) {
case "<":
return new BooleanValue(leftInteger < rightInteger);
case "<=":
return new BooleanValue(leftInteger <= rightInteger);
case "==":
return new BooleanValue(leftInteger == rightInteger);
case "!=":
return new BooleanValue(leftInteger != rightInteger);
case ">":
return new BooleanValue(leftInteger > rightInteger);
case ">=":
return new BooleanValue(leftInteger >= rightInteger);
default:
throw new BaseException("RelationalExpression: Invalid operator");
}
}
throw new BaseException("RelationalExpression: Operands are not of type IntegerType");
}
public IType typeCheck(IDictionary<String, IType> typeTable) throws BaseException {
var leftType = this.leftExpression.typeCheck(typeTable);
var rightType = this.rightExpression.typeCheck(typeTable);
if (leftType.equals(new IntegerType())) {
if (rightType.equals(new IntegerType())) {
return new BooleanType();
}
throw new BaseException("RelationalExpression: Right operand is not of type IntegerType");
}
throw new BaseException("RelationalExpression: Left operand is not of type IntegerType");
}
}
@@ -0,0 +1,31 @@
package Models.Expressions;
import Models.Values.IValue;
import Exceptions.BaseException;
import Models.Program.IDictionary;
import Models.Program.IHeap;
import Models.Types.IType;
public class ValueExpression implements IExpression {
private IValue value;
public ValueExpression(IValue value) {
this.value = value;
}
public IValue evaluate(IDictionary<String, IValue> table, IHeap heap) throws BaseException {
return value;
}
public IExpression copy() {
return new ValueExpression(value);
}
public String toString() {
return value.toString();
}
public IType typeCheck(IDictionary<String, IType> typeTable) throws BaseException {
return value.getType();
}
}
@@ -0,0 +1,31 @@
package Models.Expressions;
import Exceptions.BaseException;
import Models.Program.IDictionary;
import Models.Program.IHeap;
import Models.Types.IType;
import Models.Values.IValue;
public class VariableExpression implements IExpression {
private String name;
public VariableExpression(String name) {
this.name = name;
}
public IValue evaluate(IDictionary<String, IValue> table, IHeap heap) throws BaseException {
return table.get(name);
}
public IExpression copy() {
return new VariableExpression(name);
}
public String toString() {
return name;
}
public IType typeCheck(IDictionary<String, IType> typeTable) throws BaseException {
return typeTable.get(name);
}
}