package varTable;
import errors.Errors;
import errors.Messages;
import parser.Reader;
public class VarTabs {
private static VarTab[] tables = new VarTab[Reader.getFuncCount()];
private static VarTabLine[] lines = new VarTabLine[Reader.getFuncCount() * 100];
private static int actualTab = 0;
private static int tabCount = 0;
private static int lineCount = 0;
private static int maxBitAddr = 47;
private static int maxByteAddr = 123;
private static int bitMemoryPtr = 32;
private static int bitNumber = 0;
private static int byteMemoryPtr = 48;
private static void incBitMemoryPtr(int bits) { bitNumber = bitNumber + bits;
if (bitNumber > 7) { bitMemoryPtr++;
bitNumber -= 8;
} }
public static String[] getFreeAddress(int size) { String[] addr = new String[1];
int size1 = Math.abs(size);
if (size1 == 1) addr = new String[1];
else if (size1 == 2) addr = new String[2];
else if (size1 == 4) addr = new String[4];
else if (size1 == 8) addr = new String[1];
else if (size1 == 16) addr = new String[2];
else if (size1 == 32) addr = new String[4];
else Errors.msg(Reader.getIndex(), "Neplatna velikost " + size1);
if (size1 < 8) {
if ((bitMemoryPtr == maxBitAddr) && ((bitNumber + size1) > 7)) { Errors.msg(Reader.getIndex(), "Prekrocen bitovy adresni prostor");
}
for (int i = 0; i < addr.length; i++) {
addr[i] = Integer.toHexString(bitMemoryPtr) + "h." + bitNumber;
Messages.msg(bitMemoryPtr + " / " + bitNumber);
incBitMemoryPtr(1);
} }else {
if ((byteMemoryPtr + size1 - 1) > maxByteAddr) {
Errors.msg(Reader.getIndex(), "Prekrocen bytovy adresni prostor");
}
for (int i = 0; i < addr.length; i++) { Messages.msg("bajt " + (byteMemoryPtr));
addr[i] = Integer.toHexString(byteMemoryPtr++) + "h";
} }
return addr;
}
public static String getActualTab() {
if (tabCount > 0) return tables[actualTab].getFuncName();
else return "";
}
public static void addTab(String name) {
Messages.creating("tabulku promennych funkce " + name);
actualTab = tabCount;
tables[tabCount++] = new VarTab(name);
}
public static VarTab getTab(String name) { for (int i = 0; i < tabCount; i++) {
if (name.equals(tables[i].getFuncName())) return tables[i];
}
return null;
}
public static void declare(String caption, String size, String type) { if (type == "PIN") {
Messages.creating("promennou " + caption + " typu " + type + " na adrese " + size);
} else
Messages.creating("promennou " + caption + " typu " + type + " a velikosti " + size);
tables[actualTab].declare(caption, size, type);
}
public static void use(String caption) {
if (!tables[actualTab].isDeclared(caption))
Messages.msg("Pridavam nedeklarovanou promennou " + caption + " do tabulky promennych");
tables[actualTab].use(caption);
}
public static void joinTabs() {
for (int j = 0; j < tabCount; j++)
for (int k = 0; k < tables[j].getVarCount(); k++) lines[lineCount++] = tables[j].getLine(k);
}
public static boolean existsTab(String name) { for (int i = 0; i < tabCount; i++) {
if (name.equals(tables[i].getFuncName())) return true;
}
return false;
}
public static int getDeclaredCount() { int j = 0;
for (int i = 0; i < lineCount; i++) if (lines[i].isDeclared()) j++;
return j;
}
public static void report() {
System.out.println("---");
Messages.msg("Nalezeno celkem " + lineCount + " promennych");
for (int i = 0; i < tabCount; i++) { System.out.println();
System.out.println("Ve funkci " + tables[i].getFuncName() + ":");
VarTabLine line = new VarTabLine();
int j = 0;
while ((line = tables[i].getLine(j++)) != null) { if (!line.isDeclared()) System.out.print("!!! ");
System.out.print(line.getCaption());
if (line.getType() != "")
System.out.print(" typu " + line.getType() + " na adrese " + line.getAddress(0));
if (Math.abs(line.getSize()) > 0)
System.out.print(", rozsah: " + line.getMin() + ".." + line.getMax());
if (!line.isUsed()) System.out.print(" - nepouzita!");
if (!line.isDeclared()) System.out.print(" - nedeklarovana!");
System.out.println();
} } }
public static VarTabLine getLine(int ptr) { if (ptr < lineCount) return lines[ptr];
else return null;
}
public static int getLineCount() { return lineCount; }
}
package varTable;
import errors.Errors;
import parser.Reader;
public class VarTab {
public VarTab(String name) { funcName = name; } private String funcName = "";
public String getFuncName() { return funcName; } private VarTabLine[] lines = new VarTabLine[1000];
private int varCount = 0, inCount = 0;
private String[] inputs = new String[4];
public int getVarCount() { return varCount; }
public VarTabLine getLine(int index) { return lines[index]; } public void declare(String caption, String size, String type) {
if (!isDeclared(caption) & (varCount < 1000)) { lines[varCount] = new VarTabLine();
lines[varCount].setCaption(caption);
lines[varCount].setLongCaption(funcName + "_" + caption);
lines[varCount].setSize(size);
lines[varCount].setType(type);
if (type == "IN") {
inputs[inCount++] = lines[varCount].getCaption();
}lines[varCount++].declare();
}
else if (isDeclared(caption)) {
Errors.msg(Reader.getIndex(), "Promenna " + caption + " je jiz deklarovana");
}
else Errors.msg(Reader.getIndex(), "Promenna " + caption + " se nevejde do tabulky ");
}
private int whereIs(String find) { int i = 0;
while (lines[i] != null)
if (find.equals(lines[i++].getCaption())) return i-1;
return -1;
}
public void use(String caption) { int place = whereIs(caption);
if (place != -1) lines[place].use();
else if (varCount < 1000) {
lines[varCount] = new VarTabLine();
lines[varCount].setCaption(caption);
lines[varCount].setLongCaption(funcName + "_" + caption);
lines[varCount++].use();
Errors.msg(Reader.getIndex(), "Promenna " + caption + " je pouzita ale neni deklarovana");
}
else Errors.msg(Reader.getIndex(), "Promenna " + caption + " se nevejde do tabulky ");
}
public boolean isDeclared(String find) { int place = whereIs(find);
if (place != -1) return lines[place].isDeclared();
else return false;
}
public boolean isUsed(String find) { int place = whereIs(find);;
if (place != -1) return lines[place].isUsed();
else return false;
}
public VarTabLine getVar(String var) { int place = whereIs(var);
if (place != -1) return lines[place];
else return null;
}
public boolean existsVar(String caption) { if (whereIs(caption) == -1) return false;
else return true;
}
public int getInCount() { return inCount; }
public String getInput(int index) { return inputs[index]; }
}
package varTable;
import errors.Messages;
public class VarTabLine {
private String caption = "", longCaption = "";
private String[] address;
private int size = 0;
private long min = 0, max = 0;
private boolean declared = false, used = false, io = false;
private String type = "";
private int decodeSize(String s) { String value = "";
int ret = 0;
boolean signed = false;
if (s != null) {
if (s.charAt(0) == 'p') { address = new String[1];
size = 1;
min = 0;
max = 1;
address[0] = s;
io = true;
ret = 1;
} else {
io = false;
for (int i = 0; i < s.length(); i++) {
if ((s.charAt(i) >= '1') && (s.charAt(i) <= '8')) value += s.charAt(i);
else if (s.charAt(i) == '-') signed = true;
}if (signed) {
ret = -Integer.parseInt(value);
max = (long) (Math.pow(2, Math.floor(-ret))/2 - 1);
min = (long) -Math.pow(2, Math.floor(-ret))/2;
}
else if (!signed) {
ret = Integer.parseInt(value);
max = (long) Math.pow(2, ret) - 1;
min = 0;
}
}Messages.msg("Rozsah hodnot promenne " + caption + ": "
+ min + ".." + max);
}
Messages.msg(" Delka " + ret);
return (int) Math.floor(ret);
}
public void setCaption(String s) { caption = s; }
public void setLongCaption(String s) { longCaption = s; } public void setSize(String s) { size = decodeSize(s); } public void declare() {
declared = true;
if (type != "PIN") {
address = VarTabs.getFreeAddress(size);
Messages.msg("Promenne " + caption + " prirazuji adresu " + address[0]);
} }
public void use() { used = true; }
public void setType(String s) { type = s; } public String getCaption() { return caption; }
public String getLongCaption() { return longCaption; } public int getSize() { return size; }
public String getAddress(int i) { return address[i]; } public String[] getFullAddress() {
int len = address.length;
String[] names = new String[len];
for (int i = 0; i < len; i++) { names[i] = getLongCaption() + i;
}return names;
}
public boolean isDeclared() { return declared; } public boolean isUsed() { return used; }
public boolean isIO() { return io; } public String getType() { return type; } public long getMin() { return min; } public long getMax() { return max; } }