Skip to content

Commit

Permalink
Add Literal operand handling
Browse files Browse the repository at this point in the history
  • Loading branch information
quazi-irfan committed Oct 4, 2016
1 parent aeb18bc commit 875b007
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 54 deletions.
11 changes: 7 additions & 4 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import OperandPkg.LinkedList;
import OperandPkg.Literal;
import OperandPkg.Operand;
import OperandPkg.OperandUtility;
import SymbolPkg.*;


import java.io.*;
import java.util.Arrays;
import java.util.LinkedList;


// NAME : Quazi Irfan
Expand All @@ -16,7 +18,7 @@


/**
* Main class of Assignment 1. This class is within SumbolTable package.
* Main class of Assignment 1. This class is within SymbolTable package.
*
* Currently everything is contained within static Main method.
* Three stages are labeled as Stage X. This code mostly read the file and
Expand All @@ -26,10 +28,10 @@
public class Main {
public static void main(String[] args) throws IOException{
SymbolTable symbolTable = new SymbolTable();
LinkedList<Operand> literalLnkdLst = new LinkedList<>();
LinkedList<Literal> literalLnkdLst = new LinkedList<>();

if(args.length < 2){
System.out.println("Please use command \"javac Main labels.txt operands.txt");
System.out.println("Please use command \"javac Main labels.txt operands.txt\"");
return;
}

Expand All @@ -40,6 +42,7 @@ public static void main(String[] args) throws IOException{
OperandUtility.evaluateOperand(symbolTable, literalLnkdLst, operandFile);

// print the literal linked list
// System.out.println(literalLnkdLst);
}

}
7 changes: 0 additions & 7 deletions src/OperandPkg/LinkedList.java

This file was deleted.

13 changes: 13 additions & 0 deletions src/OperandPkg/Literal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package OperandPkg;

public class Literal {
public static int staticAddress = 1;

String name, value;
int length, address;

public String toString() {
return name + " " + value + " " + length + " " + address;
}
}

9 changes: 9 additions & 0 deletions src/OperandPkg/Operand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,13 @@ public class Operand {
public String expression;
public int value;
public boolean relocability, nbit, ibit, xbit;

public String toString() {
return expression + " " +
value + " " +
(relocability ? "Relative":"Absolute") + " " +
(nbit? "1":"0") + " " +
(ibit? "1":"0") + " " +
(xbit? "1":"0");
}
}
130 changes: 87 additions & 43 deletions src/OperandPkg/OperandUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,113 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.StringTokenizer;

public class OperandUtility{
public static void evaluateOperand(SymbolTable symbolTable, LinkedList<Operand> literalLnkdLst, String operandFile) throws IOException{
public static void evaluateOperand(SymbolTable symbolTable, LinkedList<Literal> literalLnkdLst, String operandFile) throws IOException{
BufferedReader reader = new BufferedReader(new FileReader(operandFile));

String expression = reader.readLine();
while(expression != null){
Operand operand = new Operand();
operand.expression = expression;

if(expression.charAt(0) == '=') {
expression = reader.readLine();
continue;
}
if(expression.charAt(0) != '=') {
// Handle non-Literal
Operand operand = new Operand();
operand.expression = expression;

if(expression.charAt(0) != '=')
expression = expression.toUpperCase();

if(expression.charAt(0) == '#'){
operand.ibit = true;
expression = expression.substring(1);
}
else if(expression.charAt(0) == '@'){
operand.nbit = true;
expression = expression.substring(1);
} else if(Character.isDigit(expression.charAt(0))){
operand.nbit = false;
operand.ibit = true;
}
else {
operand.nbit = true;
operand.ibit = true;
}
if (expression.charAt(0) == '#') {
operand.ibit = true;
expression = expression.substring(1);
} else if (expression.charAt(0) == '@') {
operand.nbit = true;
expression = expression.substring(1);
} else if (Character.isDigit(expression.charAt(0))) {
operand.nbit = false;
operand.ibit = true;
} else {
operand.nbit = true;
operand.ibit = true;
}

if(expression.length() >= 3 && expression.substring(expression.length()-2, expression.length()).equals(",X")){
if(operand.nbit & !operand.ibit){
System.out.println("@ or # can't be mixed with ,X");
return;
if (expression.length() >= 3 && expression.substring(expression.length() - 2, expression.length()).equals(",X")) {
if (operand.nbit & !operand.ibit) {
System.out.println("@ or # can't be mixed with ,X");
return;
}
operand.xbit = true;
expression = expression.substring(0, expression.length() - 2);
}
operand.xbit = true;
expression = expression.substring(0, expression.length()-2);
}

validateExp(expression, operand, symbolTable);
validateExp(expression, operand, symbolTable);

// print(operand);
System.out.println(operand);
expression = reader.readLine();

} else {
// Handle Literal
Literal literal = new Literal();
literal.name = expression;
String tempValue = "";

expression = expression.substring(1);

if(expression.charAt(0) == 'C' | expression.charAt(0) == 'c'){
// Handle Character Literal
expression = expression.substring(2, expression.length()-1);

for(int i =0;i<expression.length(); i++){
char ch = expression.charAt(i);
tempValue = tempValue.concat(Integer.toString((int)ch));
}

literal.length = expression.length();
literal.value = tempValue;
literal.address = Literal.staticAddress;

} else if(expression.charAt(0) == 'X' | expression.charAt(0) == 'x'){
// Handle Hex Literal
expression = expression.substring(2, expression.length()-1);
if(expression.length() % 2 != 0){
// Hex literal length must divisible by 2
}

literal.length = expression.length()/2;
literal.value = expression;
literal.address = Literal.staticAddress;
}

print(operand);
// Check duplicate, and insert to Linked List
if(literalLnkdLst.isEmpty()){
literalLnkdLst.add(literal);
Literal.staticAddress++;
System.out.println(literal);
} else {
for(int i =0; i<literalLnkdLst.size(); i++){
if(literal.name.equals(literalLnkdLst.get(i).name)){
break;
}

if(!literal.name.equals(literalLnkdLst.get(i).name) && i+1 == literalLnkdLst.size()){
literalLnkdLst.add(literal);
Literal.staticAddress++;
System.out.println(literal);
break;
}
}
}

// go to the next line
expression = reader.readLine();
expression = reader.readLine();
}
}
}

private static void validateExp(String expression, Operand operand, SymbolTable symbolTable){
if(expression.indexOf('+') >= 0 || expression.indexOf('-') >= 0){

// Symbol+Symbol
StringTokenizer tokenizer = new StringTokenizer(expression, "+-");

String temp = tokenizer.nextToken();
Expand All @@ -83,19 +134,12 @@ private static void validateExp(String expression, Operand operand, SymbolTable
operand.relocability = false;

} else {
// Symbol
operand.value = Token.getTokenValue(expression, symbolTable);
operand.relocability = Token.getTokenRflag(expression, symbolTable);
}
}

public static void print(Operand operand){
System.out.print( "\n" + operand.expression + "\t\t" + operand.value + "\t");
System.out.print( operand.relocability? "Relative" : "Absolute" );
System.out.print( operand.nbit? " 1 ":" 0 ");
System.out.print( operand.ibit? " 1 ":" 0 ");
System.out.print( operand.xbit? " 1 ":" 0 ");
}

private static String evaluateRelocability(boolean token1rflag, boolean token2rflag, String expression){
if(expression.indexOf('+') >= 0){
if(!token1rflag & !token2rflag)
Expand Down

0 comments on commit 875b007

Please sign in to comment.