-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4bf4f8d
commit 5c20201
Showing
12 changed files
with
721 additions
and
278 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import OperandPkg.LinkedList; | ||
import OperandPkg.Operand; | ||
import OperandPkg.OperandUtility; | ||
import SymbolPkg.*; | ||
|
||
|
||
import java.io.*; | ||
|
||
|
||
// NAME : Quazi Irfan | ||
// CLASS : CSc 354 | ||
// ASSIGNMENT : 1 | ||
// DUE DATE : 9/14/16 | ||
// INSTRUCTOR : Dr. Hamer | ||
// DESCRIPTION : Assignment 1 : Symbol Table | ||
|
||
|
||
/** | ||
* Main class of Assignment 1. This class is within SumbolTable package. | ||
* | ||
* Currently everything is contained within static Main method. | ||
* Three stages are labeled as Stage X. This code mostly read the file and | ||
* delegates responsibilities to other classes. | ||
* | ||
*/ | ||
public class Main { | ||
public static void main(String[] args) throws IOException{ | ||
SymbolTable symbolTable = new SymbolTable(); | ||
LinkedList<Operand> literalLnkdLst = new LinkedList<>(); | ||
|
||
if(args.length < 2){ | ||
System.out.println("Please use command \"javac Main labels.txt operands.txt"); | ||
return; | ||
} | ||
|
||
String symbolFile = args[0]; | ||
SymbolUtility.populateSymbolTable(symbolTable, symbolFile); | ||
|
||
String operandFile = args[1]; | ||
OperandUtility.evaluateOperand(symbolTable, literalLnkdLst, operandFile); | ||
|
||
// print the literal linked list | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package OperandPkg; | ||
|
||
public class LinkedList<T> { | ||
public T parent; | ||
public Operand operand; | ||
public T child; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package OperandPkg; | ||
|
||
public class Operand { | ||
public String expression; | ||
public int value; | ||
public boolean relocability, nbit, ibit, xbit; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package OperandPkg; | ||
|
||
import SymbolPkg.Node; | ||
import SymbolPkg.SymbolTable; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.util.StringTokenizer; | ||
|
||
public class OperandUtility{ | ||
public static void evaluateOperand(SymbolTable symbolTable, LinkedList<Operand> 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) != '=') | ||
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.length() >= 3 && expression.substring(expression.length()-2, expression.length()).equals(",X")){ | ||
if(operand.nbit & operand.ibit == false){ | ||
System.out.println("@ or # can't be mixed with ,X"); | ||
return; | ||
} | ||
operand.xbit = true; | ||
expression = expression.substring(0, expression.length()-2); | ||
} | ||
|
||
validateExp(expression, operand, symbolTable); | ||
|
||
print(operand); | ||
|
||
// go to the next line | ||
expression = reader.readLine(); | ||
} | ||
} | ||
|
||
private static void validateExp(String expression, Operand operand, SymbolTable symbolTable){ | ||
if(expression.indexOf('+') >= 0 || expression.indexOf('-') >= 0){ | ||
int token1Value =0, token2Value=0; | ||
boolean token1rflag=false, token2rflag=false; | ||
|
||
StringTokenizer tokenizer = new StringTokenizer(expression, "+-"); | ||
|
||
String temp = tokenizer.nextToken(); | ||
token1Value = Token.getTokenValue(temp, symbolTable); | ||
token1rflag = Token.getTokenRflag(temp, symbolTable); | ||
|
||
temp = tokenizer.nextToken(); | ||
token2Value = Token.getTokenValue(temp, symbolTable); | ||
token2rflag = Token.getTokenRflag(temp, symbolTable); | ||
|
||
// set operands value, and flag | ||
if(expression.indexOf('+') >= 0) | ||
operand.value = token1Value + token2Value; | ||
else | ||
operand.value = token1Value - token2Value; | ||
|
||
if(evaluateRelocability(token1rflag, token2rflag, expression).equals("true")) | ||
operand.relocability = true; | ||
else | ||
operand.relocability = false; | ||
|
||
} else { | ||
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 == false & token2rflag == false) | ||
return "false"; | ||
if(token1rflag == false & token2rflag == true) | ||
return "true"; | ||
if(token1rflag == true & token2rflag == false) | ||
return "true"; | ||
if(token1rflag == true & token2rflag == true) | ||
return "error"; | ||
} else if(expression.indexOf('-') >= 0){ | ||
if(token1rflag == false & token2rflag == false) | ||
return "false"; | ||
if(token1rflag == false & token2rflag == true) | ||
return "error"; | ||
if(token1rflag == true & token2rflag == false) | ||
return "true"; | ||
if(token1rflag == true & token2rflag == true) | ||
return "false"; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package OperandPkg; | ||
|
||
import SymbolPkg.Node; | ||
import SymbolPkg.SymbolTable; | ||
|
||
class Token{ | ||
public static boolean getTokenRflag(String token, SymbolTable symbolTable){ | ||
boolean operantRflag = false; | ||
|
||
Integer tokenValue = null; | ||
try { | ||
tokenValue = Integer.valueOf(token); | ||
} catch (NumberFormatException e){ | ||
} | ||
|
||
Node node = new Node(); | ||
|
||
if(tokenValue == null){ | ||
// Symbol is not a number | ||
node = symbolTable.search(token); | ||
if(node != null){ | ||
// symbol found in the symbol table. Return the rflag | ||
operantRflag = node.getRflag(); | ||
} | ||
} else { | ||
// Symbol is a number, return false | ||
operantRflag = false; | ||
} | ||
|
||
return operantRflag; | ||
} | ||
|
||
public static Integer getTokenValue(String token, SymbolTable symbolTable){ | ||
Integer operandValue = null; | ||
|
||
Integer tokenValue = null; | ||
try { | ||
tokenValue = Integer.valueOf(token); | ||
} catch (NumberFormatException e){ | ||
} | ||
|
||
Node node = new Node(); | ||
|
||
if(tokenValue == null){ | ||
// Symbol is not a number | ||
node = symbolTable.search(token); | ||
if(node != null){ | ||
operandValue = node.getValue(); | ||
} | ||
} else { | ||
// Symbol is a number | ||
operandValue = tokenValue; | ||
} | ||
|
||
return operandValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.