📄 symtablemode.java
字号:
/*
* @(#)SymTableMode.java 1.0.0 05/13/2007
*
* Copyright 2007 Xuxt@buaa, No.34060520 All rights reserved.
*/
package com.king4solomon.homework.compiler.core;
import javax.swing.table.*;
@SuppressWarnings("serial")
public final class SymTableMode extends DefaultTableModel {
public SymTableMode() {
super();
}
public int getColumnCount() {
return cNames.length;
}
public String getColumnName(int idx) {
return cNames[idx];
}
public void addRow(SymTableItem item) {
rCount++;
item.obj[0] = rCount;
super.addRow(item.obj);
}
public void removeRow() {
super.removeRow(--rCount);
}
/**
* @param nm
* 要查找的元素名称
* @param lv
* 要查找的元素引用层次
* @return 如果在lv层找到 返回ConstSet.FINDINCLV;如果在lv外层找到 返回ConstSet.FINDINOLV
* 否则返回ConstSet.NOTFOUND
*/
public int search(String nm, int lv) {
for (int i = getRowCount() - 1; i >= 0; i--) {
// 跳过函数
if (getValueAt(i, 2).equals("func"))
continue;
// 找到
if (((String) (getValueAt(i, 1))).equals(nm)) {
int id = Integer.parseInt(getValueAt(i, 0).toString());
String tmpLv = getValueAt(i, 5).toString();
String tmpType = getValueAt(i, 3).toString();
String tmpKind = getValueAt(i, 2).toString();
String tmpAdd = getValueAt(i, 6).toString();
String tmpVal = getValueAt(i, 4).toString();
int tp;
if (tmpType.equals("int")) {
tp = ConstSet.INTEGER;
} else if (tmpType.equals("char")) {
tp = ConstSet.CHARACTER;
} else {
tp = ConstSet.REAL;
}
// 在本层
if (Integer.parseInt(tmpLv) == lv) {
lastFoundEle = new ItemInfo(id, tp, tmpAdd, 0, tmpKind, tmpVal);
return ConstSet.FINDINCLV;
}
// 在外层
else if (Integer.parseInt(tmpLv) < lv) {
lastFoundEle = new ItemInfo(id, tp, tmpAdd, 1, tmpKind, tmpVal);
return ConstSet.FINDINOLV;
}
}
}
// 未找到
return ConstSet.NOTFOUND;
}
public int searchFunc(String nm) {
for (int i = 0; i < getRowCount(); i++) {
if (!getValueAt(i, 2).equals("func"))
continue;
// 找到
if (((String) (getValueAt(i, 1))).equalsIgnoreCase(nm)) {
int id = Integer.parseInt(getValueAt(i, 0).toString());
String tmpPc = getValueAt(i, 5).toString();// 存放参数个数
String tmpType = getValueAt(i, 3).toString();
String tmpVal = getValueAt(i, 4).toString();
int tp;
if (tmpType.equals("int")) {
tp = ConstSet.INTEGER;
} else if (tmpType.equals("char")) {
tp = ConstSet.CHARACTER;
} else if(tmpType.equals("float")){
tp = ConstSet.REAL;
}else{
tp = ConstSet.VOIDTYPE;
}
String tmpAdd = getValueAt(i, 6).toString();
lastFoundFunc = new ItemInfo(id, tp, tmpAdd, Integer
.parseInt(tmpPc), "func", tmpVal);
// 无返回值函数
if (((String) getValueAt(i, 3)).equals("void")) {
return ConstSet.NRFRETURN;
}
// 带返回值函数
else {
return ConstSet.RFRETURN;
}
}
}
// 未找到
lastFoundEle = null;
return ConstSet.NOTFOUND;
}
public void quit() {
for (int i = getRowCount() - 1; i >= 0; i--) {
if (((String) this.getValueAt(i, 2)).equals("func")) {
int paraCount = Integer.parseInt(getValueAt(i, 5).toString());
for (int j = i + 1; j <= i + paraCount; j++)
setValueAt("", j, 1);
int tmp = getRowCount();
for (int j = i + paraCount + 1; j < tmp; j++)
removeRow();
return;
}
}
}
// 取得最后一次查询的元素信息
public ItemInfo getEleInfo() {
return lastFoundEle;
}
public ItemInfo getFuncInfo() {
return lastFoundFunc;
}
public int levBreak(int lv) {
return 0;
}
public int getRCount() {
return rCount;
}
public void setRCount(int r) {
rCount = r;
}
private int rCount = 0;
private ItemInfo lastFoundEle;
private ItemInfo lastFoundFunc;
private String[] cNames = { "ID", "Name", "kind", "Type", "Value", "Level",
"Adress" };
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -