<commit_before>package GrafischeRam;

import java.util.Hashtable;

public class Ram{
	
	String program;
	String[] lines;
	Hashtable<String, Integer> labels;						//name of the labels (e.g. L1) and the corresponding line
	int currentLine;
	int[] Registers;
	private int ucmOrder;
	private int ucmMemory;
	private int ucmOrderTotal;
	private int ucmMemoryTotal;
	private double lcmOrder;
	private double lcmOrderTotal;
	
	public Ram(){
		this.Registers = new int[0];
		this.program = "";
		this.lines = new String[0];
		this.currentLine = 0;
		this.ucmOrder = 0;
		this.ucmOrderTotal = 0;
		this.ucmMemory = 0;
		this.ucmMemoryTotal = 0;
		this.lcmOrder = 0;
		this.lcmOrderTotal = 0;
	}
	//initiates all things necessary to compute the given program
	//p - program
	//r - registers
	public Ram(String p, String r){
		this.program = p;
		this.lines = partProgrammLines(this.program);
		labels = new Hashtable<String, Integer>();
		this.currentLine = 0;
		this.getLabels();
		this.fillRegisters(r);
		this.ucmOrder = 0;
		this.ucmOrderTotal = 0;
		this.ucmMemory = 0;
		this.ucmMemoryTotal = 0;
		this.lcmOrder = 0;
		this.lcmOrderTotal = 0;
	}
	
	//reads the contents of the given registers and transforms them into integer
	private void fillRegisters(String registers){
		//read the contents of the registers...
		String[] temp = registers.split(";");
		this.Registers = new int[temp.length];
		//and make each one an integer
		for(int i = 0; i < temp.length; i++){
			this.Registers[i] = Integer.parseInt(temp[i].trim());
		}
	}
	
	void addNewRegister(){
		int[] newRegister = new int[this.Registers.length + 1];
		for(int i = 0; i < this.Registers.length; i++){
			newRegister[i] = this.Registers[i];
		}
		newRegister[this.Registers.length] = 0;
		this.Registers = newRegister;
	}
	
	void addNewRegister(int content){
		int[] newRegister = new int[this.Registers.length + 1];
		for(int i = 0; i < this.Registers.length; i++){
			newRegister[i] = this.Registers[i];
		}
		newRegister[this.Registers.length] = content;
		this.Registers = newRegister;
	}
	
	//splits the lines of the given program to make each line easy accessible and eliminates the newlines
	private String[] partProgrammLines(String p){
		//String[] tmp = p.split(System.getProperty("line.separator"));
		//for(int i = 0; i < tmp.length; i++){
		//	System.out.println(i + ": " + tmp[i]);
		//}
		return p.split(System.getProperty("line.separator"));
	}
	
	//search for labels and save which line has which label
	//spaces at the beginning are not allowed!
	private void getLabels(){
		for(int i = 0; i < this.lines.length; i++){
			String label = this.lines[i].split(" ")[0];
			if(label.matches("[L]\\d+")){
				this.labels.put(label, i);
				String[] labelAndLine = this.lines[i].split("^[L]\\d+");
				if(labelAndLine.length > 1){
					this.lines[i] = labelAndLine[1].trim();
				}
				else{
					this.lines[i] = "";
				}
			}
		}
	}
	
	//set a new program and registers
	private void setProgramAndRegisters(String p, String r){
		this.program = p;
		this.lines = partProgrammLines(this.program);
		/*for(int i = 0; i < this.lines.length; i++){
			System.out.println(i + ": " + this.lines[i]);
		}*/
		this.labels = new Hashtable<String, Integer>();
		this.currentLine = 0;
		this.getLabels();
		this.fillRegisters(r);
		this.ucmOrder = 0;
		this.ucmOrderTotal = 0;
		this.ucmMemory = 0;
		this.ucmMemoryTotal = 0;
		this.lcmOrder = 0;
		this.lcmOrderTotal = 0;
	}
	
	//compute one line of the ram and give the output
	String computeLine(){
		if(this.currentLine >= this.lines.length){
			return "Error. End of program reached, but HALT is missing.";
		}
		this.ucmMemory = 0;
		this.ucmOrder = 0;
		this.lcmOrder = 0;
		//ignore all whitespace characters
		String computingLine = this.lines[this.currentLine].replaceAll("\\s", "");
		//System.out.println("computing: " + computingLine);
		//if a HALT comes, end the program
		if(computingLine.matches("HALT")){
			return "End of program reached";
		}
		//if the current line is empty or contains only white spaces
		if(computingLine.matches("") || computingLine.matches("\\s*")){
			this.currentLine++;
			return "go one line further";
		}
		if(computingLine.matches("((R[0-9]+)|([(]R[0-9]+[)])):=((R[0-9]+)|([(]R[0-9]+[)])|(\\-[0-9]+)|[0-9]+)((\\+|\\-|\\*|\\/)((R[0-9]+)|([(]R[0-9]+[)])|(\\-[0-9]+)|([0-9]+)))?")){
			this.ucmOrder++;
			this.ucmOrderTotal++;
			String[] currentContent = computingLine.split(":=");
			//check the left part
			int leftElement = 0;
			if(currentContent[0].trim().matches("(\\-)?[0-9]+")){
				return "Syntax not allowed: Left element of an assignment may not be a constant.";
			}
			else{
				if(currentContent[0].matches("^R\\d+")){
					leftElement = Integer.parseInt(currentContent[0].replace("R", "").replace(" ", ""));
					this.ucmMemory++;
					this.ucmMemoryTotal++;
					this.lcmOrder += this.calculateLogarithmicCost(leftElement);
				}
				else{
					if(currentContent[0].matches("^[(]R\\d+[)]")){
						leftElement = this.Registers[Integer.parseInt(currentContent[0].replace("(", "").replace(")", "").replace("R", "").replace(" ", ""))];
						this.ucmMemory++;
						this.ucmMemoryTotal++;
						this.lcmOrder += this.calculateLogarithmicCost(leftElement);
						this.lcmOrder += this.calculateLogarithmicCost(this.Registers[leftElement]);
					}
					
				}
			}
			if(leftElement < 0){
				return "Registers cannot be negative";
			}
			//check the right part
			String rightElement = currentContent[1];
			//if it is a constant
			if(rightElement.matches("(\\-)?[0-9]+")){
				//go to next line
				this.currentLine++;
				int number = Integer.parseInt(rightElement);
				this.Registers[leftElement] = number;
				this.lcmOrder += this.calculateLogarithmicCost(number);
				this.lcmOrderTotal += this.lcmOrder;
				return "Set Register " + leftElement + " to " + Integer.parseInt(rightElement);
			}
			//TODO: define more
			if(currentContent[1].matches(".*\\+.*")){
				String[] rightHalfs = currentContent[1].split("\\+");
				//check what the left part of the right element is
				int rightLeftHalf = getElement(rightHalfs[0]);<commit_msg>fixed a bug that assignments from just registers or indirect registers did not work<commit_after>package GrafischeRam;

import java.util.Hashtable;

public class Ram{
	
	String program;
	String[] lines;
	Hashtable<String, Integer> labels;						//name of the labels (e.g. L1) and the corresponding line
	int currentLine;
	int[] Registers;
	private int ucmOrder;
	private int ucmMemory;
	private int ucmOrderTotal;
	private int ucmMemoryTotal;
	private double lcmOrder;
	private double lcmOrderTotal;
	
	public Ram(){
		this.Registers = new int[0];
		this.program = "";
		this.lines = new String[0];
		this.currentLine = 0;
		this.ucmOrder = 0;
		this.ucmOrderTotal = 0;
		this.ucmMemory = 0;
		this.ucmMemoryTotal = 0;
		this.lcmOrder = 0;
		this.lcmOrderTotal = 0;
	}
	//initiates all things necessary to compute the given program
	//p - program
	//r - registers
	public Ram(String p, String r){
		this.program = p;
		this.lines = partProgrammLines(this.program);
		labels = new Hashtable<String, Integer>();
		this.currentLine = 0;
		this.getLabels();
		this.fillRegisters(r);
		this.ucmOrder = 0;
		this.ucmOrderTotal = 0;
		this.ucmMemory = 0;
		this.ucmMemoryTotal = 0;
		this.lcmOrder = 0;
		this.lcmOrderTotal = 0;
	}
	
	//reads the contents of the given registers and transforms them into integer
	private void fillRegisters(String registers){
		//read the contents of the registers...
		String[] temp = registers.split(";");
		this.Registers = new int[temp.length];
		//and make each one an integer
		for(int i = 0; i < temp.length; i++){
			this.Registers[i] = Integer.parseInt(temp[i].trim());
		}
	}
	
	void addNewRegister(){
		int[] newRegister = new int[this.Registers.length + 1];
		for(int i = 0; i < this.Registers.length; i++){
			newRegister[i] = this.Registers[i];
		}
		newRegister[this.Registers.length] = 0;
		this.Registers = newRegister;
	}
	
	void addNewRegister(int content){
		int[] newRegister = new int[this.Registers.length + 1];
		for(int i = 0; i < this.Registers.length; i++){
			newRegister[i] = this.Registers[i];
		}
		newRegister[this.Registers.length] = content;
		this.Registers = newRegister;
	}
	
	//splits the lines of the given program to make each line easy accessible and eliminates the newlines
	private String[] partProgrammLines(String p){
		//String[] tmp = p.split(System.getProperty("line.separator"));
		//for(int i = 0; i < tmp.length; i++){
		//	System.out.println(i + ": " + tmp[i]);
		//}
		return p.split(System.getProperty("line.separator"));
	}
	
	//search for labels and save which line has which label
	//spaces at the beginning are not allowed!
	private void getLabels(){
		for(int i = 0; i < this.lines.length; i++){
			String label = this.lines[i].split(" ")[0];
			if(label.matches("[L]\\d+")){
				this.labels.put(label, i);
				String[] labelAndLine = this.lines[i].split("^[L]\\d+");
				if(labelAndLine.length > 1){
					this.lines[i] = labelAndLine[1].trim();
				}
				else{
					this.lines[i] = "";
				}
			}
		}
	}
	
	//set a new program and registers
	private void setProgramAndRegisters(String p, String r){
		this.program = p;
		this.lines = partProgrammLines(this.program);
		/*for(int i = 0; i < this.lines.length; i++){
			System.out.println(i + ": " + this.lines[i]);
		}*/
		this.labels = new Hashtable<String, Integer>();
		this.currentLine = 0;
		this.getLabels();
		this.fillRegisters(r);
		this.ucmOrder = 0;
		this.ucmOrderTotal = 0;
		this.ucmMemory = 0;
		this.ucmMemoryTotal = 0;
		this.lcmOrder = 0;
		this.lcmOrderTotal = 0;
	}
	
	//compute one line of the ram and give the output
	String computeLine(){
		if(this.currentLine >= this.lines.length){
			return "Error. End of program reached, but HALT is missing.";
		}
		this.ucmMemory = 0;
		this.ucmOrder = 0;
		this.lcmOrder = 0;
		//ignore all whitespace characters
		String computingLine = this.lines[this.currentLine].replaceAll("\\s", "");
		//System.out.println("computing: " + computingLine);
		//if a HALT comes, end the program
		if(computingLine.matches("HALT")){
			return "End of program reached";
		}
		//if the current line is empty or contains only white spaces
		if(computingLine.matches("") || computingLine.matches("\\s*")){
			this.currentLine++;
			return "go one line further";
		}
		System.out.println("computingLine: " + computingLine);
		if(computingLine.matches("((R[0-9]+)|([(]R[0-9]+[)])):=((R[0-9]+)|([(]R[0-9]+[)])|(\\-[0-9]+)|[0-9]+)((\\+|\\-|\\*|\\/)((R[0-9]+)|([(]R[0-9]+[)])|(\\-[0-9]+)|([0-9]+)))?")){
			this.ucmOrder++;
			this.ucmOrderTotal++;
			String[] currentContent = computingLine.split(":=");
			//check the left part
			int leftElement = 0;
			if(currentContent[0].trim().matches("(\\-)?[0-9]+")){
				return "Syntax not allowed: Left element of an assignment may not be a constant.";
			}
			else{
				if(currentContent[0].matches("^R\\d+")){
					leftElement = Integer.parseInt(currentContent[0].replace("R", "").replace(" ", ""));
					this.ucmMemory++;
					this.ucmMemoryTotal++;
					this.lcmOrder += this.calculateLogarithmicCost(leftElement);
				}
				else{
					if(currentContent[0].matches("^[(]R\\d+[)]")){
						leftElement = this.Registers[Integer.parseInt(currentContent[0].replace("(", "").replace(")", "").replace("R", "").replace(" ", ""))];
						this.ucmMemory++;
						this.ucmMemoryTotal++;
						this.lcmOrder += this.calculateLogarithmicCost(leftElement);
						this.lcmOrder += this.calculateLogarithmicCost(this.Registers[leftElement]);
					}
					
				}
			}
			if(leftElement < 0){
				return "Registers cannot be negative";
			}
			//check the right part
			String rightElement = currentContent[1];
			System.out.println("rightElement: " + rightElement);
			//if it is a constant
			if(rightElement.matches("(\\-)?[0-9]+")){
				//go to next line
				this.currentLine++;
				int number = Integer.parseInt(rightElement);
				this.Registers[leftElement] = number;
				this.lcmOrder += this.calculateLogarithmicCost(number);
				this.lcmOrderTotal += this.lcmOrder;
				return "Set Register " + leftElement + " to " + Integer.parseInt(rightElement);
			}
			//if the right element is just a register
			if(rightElement.matches("R[0-9]+")){
				int result = this.getElement(rightElement);
				this.Registers[leftElement] = result;
				//go to next line
				this.currentLine++;
				return "Set Register " + leftElement + " to " + result;
			}
			//if the right element is an indirect register
			if(rightElement.matches("[(]R[0-9]+[)]")){
				int result = this.getElement(rightElement);
				this.Registers[leftElement] = result;
				//go to next line
				this.currentLine++;
				return "Set Register " + leftElement + " to " + result;
			}
			//TODO: define more
			if(currentContent[1].matches(".*\\+.*")){
				String[] rightHalfs = currentContent[1].split("\\+");
				//check what the left part of the right element is
				int rightLeftHalf = getElement(rightHalfs[0]);
{'fraction_non_alphanumeric': 0.1687432702661129, 'fraction_numerical': 0.011690509152438086, 'mean_word_length': 3.5385689354275742, 'pattern_counts': {'":': 6, '<': 23, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1749038', 'n_tokens_mistral': 5110, 'n_tokens_neox': 4424, 'n_words': 1054}