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,72 @@
package Models.Program;
import Exceptions.BaseException;
import java.util.Collection;
import java.util.Hashtable;
public class GenericDictionary<K, V> implements IDictionary<K, V> {
private Hashtable<K, V> dictionary;
public GenericDictionary() {
dictionary = new Hashtable<K, V>();
}
public void add(K key, V value) throws BaseException {
if (dictionary.containsKey(key)) {
throw new BaseException("Key already exists in dictionary.");
}
dictionary.put(key, value);
}
public void update(K key, V value) throws BaseException {
if (!dictionary.containsKey(key)) {
throw new BaseException("Key does not exist in dictionary.");
}
dictionary.put(key, value);
}
public void remove(K key) throws BaseException {
if (!dictionary.containsKey(key)) {
throw new BaseException("Key does not exist in dictionary.");
}
dictionary.remove(key);
}
public V get(K key) throws BaseException {
if (!dictionary.containsKey(key)) {
throw new BaseException("Key does not exist in dictionary.");
}
return dictionary.get(key);
}
public boolean contains(K key) {
return dictionary.containsKey(key);
}
public String toString() {
var sb = new StringBuilder();
for (var key : dictionary.keySet()) {
sb.append(key);
sb.append(" --> ");
sb.append(dictionary.get(key));
sb.append("\n");
}
return sb.toString();
}
public Collection<V> getContent() {
return dictionary.values();
}
public IDictionary<K, V> copy() {
var newDictionary = new GenericDictionary<K, V>();
for (var key : dictionary.keySet()) {
newDictionary.dictionary.put(key, dictionary.get(key));
}
return newDictionary;
}
}
@@ -0,0 +1,30 @@
package Models.Program;
import java.util.Queue;
import java.util.LinkedList;
public class GenericQueue<T> implements IQueue<T> {
private Queue<T> queue;
public GenericQueue() {
this.queue = new LinkedList<T>();
}
public T dequeue() {
return queue.poll();
}
public void enqueue(T value) {
queue.add(value);
}
public String toString() {
var sb = new StringBuilder();
for (var item : queue) {
sb.append(item);
sb.append("\n");
}
return sb.toString();
}
}
@@ -0,0 +1,34 @@
package Models.Program;
import java.util.ArrayDeque;
import java.util.Deque;
public class GenericStack<T> implements IStack<T> {
private Deque<T> stack;
public GenericStack() {
stack = new ArrayDeque<T>();
}
public T pop() {
return stack.pop();
}
public void push(T value) {
stack.push(value);
}
public boolean isEmpty() {
return stack.isEmpty();
}
public String toString() {
var sb = new StringBuilder();
for (var item : stack) {
sb.append(item);
sb.append("\n");
}
return sb.toString();
}
}
@@ -0,0 +1,74 @@
package Models.Program;
import java.util.Hashtable;
import Exceptions.BaseException;
import Models.Values.IValue;
public class Heap implements IHeap{
private Integer freeAddress;
private Hashtable<Integer, IValue> heap;
public Heap() {
this.freeAddress = 1;
this.heap = new Hashtable<Integer, IValue>();
}
public Integer getFreeAddress() {
return this.freeAddress;
}
public Boolean contains(Integer address) {
return this.heap.containsKey(address);
}
public Integer add(IValue value) {
this.heap.put(this.freeAddress, value);
this.freeAddress += 1;
return this.freeAddress - 1;
}
public void update(Integer address, IValue value) throws BaseException {
if (!this.heap.containsKey(address)) {
throw new BaseException("Address does not exist in heap.");
}
this.heap.put(address, value);
}
public IValue get(Integer address) throws BaseException {
if (!this.heap.containsKey(address)) {
throw new BaseException("Address does not exist in heap.");
}
return this.heap.get(address);
}
public void remove(Integer address) throws BaseException {
if (!this.heap.containsKey(address)) {
throw new BaseException("Address does not exist in heap.");
}
this.heap.remove(address);
}
public String toString() {
var sb = new StringBuilder();
for (var key : this.heap.keySet()) {
sb.append(key);
sb.append(" --> ");
sb.append(this.heap.get(key));
sb.append("\n");
}
return sb.toString();
}
public Hashtable<Integer, IValue> getContent() {
return this.heap;
}
public void setContent(Hashtable<Integer, IValue> content) {
this.heap = content;
}
}
@@ -0,0 +1,15 @@
package Models.Program;
import java.util.Collection;
import Exceptions.BaseException;
public interface IDictionary<K, V> {
public void add(K key, V value) throws BaseException;
public void update(K key, V value) throws BaseException;
public void remove(K key) throws BaseException;
public V get(K key) throws BaseException;
public boolean contains(K key);
public Collection<V> getContent();
public IDictionary<K, V> copy();
}
@@ -0,0 +1,17 @@
package Models.Program;
import java.util.Hashtable;
import Exceptions.BaseException;
import Models.Values.IValue;
public interface IHeap {
public Integer getFreeAddress();
public Integer add(IValue value);
public void update(Integer address, IValue value) throws BaseException;
public IValue get(Integer address) throws BaseException;
public void remove(Integer address) throws BaseException;
public Boolean contains(Integer address);
public Hashtable<Integer, IValue> getContent();
public void setContent(Hashtable<Integer, IValue> content);
}
@@ -0,0 +1,8 @@
package Models.Program;
import Exceptions.BaseException;
public interface IQueue<T> {
public T dequeue() throws BaseException;
public void enqueue(T value);
}
@@ -0,0 +1,7 @@
package Models.Program;
public interface IStack<T> {
public T pop();
public void push(T value);
public boolean isEmpty();
}
@@ -0,0 +1,92 @@
package Models.Program;
import java.io.BufferedReader;
import java.util.Set;
import Exceptions.BaseException;
import Models.Statements.IStatement;
import Models.Types.IType;
import Models.Values.IValue;
import Models.Values.StringValue;;
public class ProgramState{
//region Fields
private IStack<IStatement> stack;
private IDictionary<String, IValue> symbolTable;
private IQueue<IValue> output;
private IDictionary<StringValue, BufferedReader> fileTable;
private IStatement originalProgram;
private IHeap heap;
private Integer id;
private static Set<Integer> ids = new java.util.HashSet<>();
//endregion
//region Exposed Methods
public ProgramState(IStack<IStatement> stack, IDictionary<String, IValue> symbolTable, IQueue<IValue> output, IDictionary<StringValue, BufferedReader> fileTable, IHeap heap,IStatement program) throws BaseException{
this.stack = stack;
this.symbolTable = symbolTable;
this.output = output;
this.fileTable = fileTable;
this.originalProgram = program.copy();
this.stack.push(program);
this.heap = heap;
this.id = getNewId();
}
private static Integer getNewId(){
Integer newId = 1;
synchronized (ids){
while(ids.contains(newId)){
newId++;
}
ids.add(newId);
}
return newId;
}
public IStack<IStatement> getStack(){
return this.stack;
}
public IDictionary<String, IValue> getSymbolTable(){
return this.symbolTable;
}
public IQueue<IValue> getOutput(){
return this.output;
}
public IDictionary<StringValue, BufferedReader> getFileTable(){
return this.fileTable;
}
public IStatement getOriginalProgram(){
return this.originalProgram;
}
public IHeap getHeap(){
return this.heap;
}
public boolean isNotCompleted(){
return !this.stack.isEmpty();
}
public ProgramState oneStep() throws BaseException{
IStack<IStatement> stack = this.getStack();
if (stack.isEmpty()) {
throw new RuntimeException("Execution stack is empty!");
}
IStatement currentStatement = stack.pop();
return currentStatement.execute(this);
}
public String toString(){
return "Id: " + id + "\n" + "Stack:\n" + this.stack.toString() + "\nSymbol Table:\n" + this.symbolTable.toString() + "\nOutput:\n" + this.output.toString() + "\nFile Table:\n" + this.fileTable.toString() + "\n" + "Heap:" + "\n" + this.heap.toString() + "\n";
}
public void typeCheck() throws BaseException{
this.originalProgram.typeCheck(new GenericDictionary<String, IType>());
}
//#endregion
}