📄 javasourcegenerator.java
字号:
monitor_procs.put(name.toUpperCase(), new Procedure(name));
previous_entity = MONITOR;
monitor = false;
}
// is the locals table empty ? symbol_tables.addFirst(new Hashtable());
if (!local_table.isEmpty()) { // if not then remove all its elements
local_table.clear();
}
}
public void outAArgumentsProcedureHeading(AArgumentsProcedureHeading node) {
out.println(") {");
}
/**
* {arguments_throws} procedure identifier ...;
*/
public void caseAArgumentsThrowsProcedureHeading(AArgumentsThrowsProcedureHeading node) {
String name = node.getIdentifier().getText();
// is the locals table empty ? symbol_tables.addFirst(new Hashtable());
if (!local_table.isEmpty()) { // if not then remove all its elements
local_table.clear();
}
// empty the idlist
if (!idlist.isEmpty()) {
idlist.removeAllElements();
}
// is this a program?
if (program) {
out.print(" static void " + name + "(");
global_table.put(name.toUpperCase(), new Procedure(name));
previous_entity = PROGRAM;
program = false;
}
else { // indent twice
System.out.println(" public synchronized void " + name + "(");
monitor_table.put(name.toUpperCase(), new Procedure(name));
monitor_procs.put(name.toUpperCase(), new Procedure(name));
previous_entity = MONITOR;
monitor = false;
}
if (node.getParameterList() != null) {
node.getParameterList().apply(this);
}
out.print(") throws ");
if (!idlist.isEmpty()) {
idlist.removeAllElements();
}
if (node.getIdentifierList() != null) {
node.getIdentifierList().apply(this);
}
Enumeration e = idlist.elements();
String key = ((String) e.nextElement()).toUpperCase();
String id = ((Variable) global_table.get(key)).getImage();
out.print(id);
for (; e.hasMoreElements();) {
key = ((String) e.nextElement()).toUpperCase();
id = ((Variable) global_table.get(key)).getImage();
out.print(", " + id);
}
out.println(" {");
}
public void outAProcedureDeclaration(AProcedureDeclaration node) {
if (previous_entity == PROGRAM) {
program = true;
out.println(" }");
}
else {
monitor = true;
out.println(" }");
}
}
/**
* clear idlist
*/
public void inAFormalParameter(AFormalParameter node) {
if (!idlist.isEmpty()) {
idlist.removeAllElements();
}
}
/**
* formal_parameter =
* identifier_list colon P.type ;
*/
public void outAFormalParameter(AFormalParameter node) {
String id_name;
Enumeration e = idlist.elements();
id_name = (String) e.nextElement();
out.print(type + " " + id_name);
local_table.put(id_name.toUpperCase(), new Variable(id_name, type));
for (; e.hasMoreElements();) {
id_name = (String) e.nextElement();
out.print(", " + type + " " + id_name);
local_table.put(id_name.toUpperCase(), new Variable(id_name, type));
}
}
/**
* function_heading =
* {simple} function identifier colon P.type semicolon |
*/
public void outASimpleFunctionHeading(ASimpleFunctionHeading node) {
String name = node.getIdentifier().getText();
out.println(" static " + type + " " + name + "() {");
global_table.put(name.toUpperCase(), new Function(name, type));
program = false;
if (!local_table.isEmpty()) { // if not then remove all its elements
local_table.clear();
}
}
public void caseASimpleThrowsFunctionHeading(ASimpleThrowsFunctionHeading node) {
String name = node.getIdentifier().getText();
if (node.getType() != null) {
node.getType().apply(this);
}
global_table.put(name.toUpperCase(), new Function(name, type));
out.print(" static " + type + " " + name + "() throws ");
if (!idlist.isEmpty()) {
idlist.removeAllElements();
}
if (node.getIdentifierList() != null) {
node.getIdentifierList().apply(this);
}
String id_name;
Enumeration e = idlist.elements();
id_name = (String) e.nextElement();
out.print(id_name);
for (; e.hasMoreElements();) {
id_name = (String) e.nextElement();
out.print(", " + id_name);
}
out.println(" {");
program = false;
if (!local_table.isEmpty()) { // if not then remove all its elements
local_table.clear();
}
}
/**
* {arguments} function identifier l_paren parameter_list r_paren colon P.type;
*/
public void caseAArgumentsFunctionHeading(AArgumentsFunctionHeading node) {
String name = node.getIdentifier().getText();
// is the locals table empty ?
if (!local_table.isEmpty()) { // if not then remove all its elements
local_table.clear();
}
out.print(" static " + type + " " + name + "(");
global_table.put(name.toUpperCase(), new Function(name, type));
node.getParameterList().apply(this);
out.println(") {");
}
/**
* {arguments} function identifier l_paren parameter_list r_paren colon P.type;
*/
public void caseAArgumentsThrowsFunctionHeading(AArgumentsThrowsFunctionHeading node) {
String name = node.getIdentifier().getText();
// is the locals table empty ? symbol_tables.addFirst(new Hashtable());
if (!local_table.isEmpty()) { // if not then remove all its elements
local_table.clear();
}
// empty the idlist
if (!idlist.isEmpty()) {
idlist.removeAllElements();
}
if (node.getType() != null) {
node.getType().apply(this);
}
out.print("static " + type + " " + name + "(");
global_table.put(name.toUpperCase(), new Function(name, type));
if (node.getParameterList() != null) {
node.getParameterList().apply(this);
}
out.println(") throws ");
if (!idlist.isEmpty()) {
idlist.removeAllElements();
}
if (node.getIdentifierList() != null) {
node.getIdentifierList().apply(this);
}
Enumeration e = idlist.elements();
String key = ((String) e.nextElement()).toUpperCase();
String id = ((Variable) global_table.get(key)).getImage();
out.print(id);
for(; e.hasMoreElements();) {
key = ((String) e.nextElement()).toUpperCase();
id = ((Variable) global_table.get(key)).getImage();
out.print(", " + id);
}
out.println(" {");
}
public void outAFunctionDeclaration(AFunctionDeclaration node) {
program = true;
out.println(" }");
}
/**
* monitor_heading =
* monitor identifier semicolon ;
*/
public void outAMonitorHeading(AMonitorHeading node) {
program = false;
monitor = true;
String name = node.getIdentifier().getText();
monitor_name = name;
out.println(" static " + name + " " + name + " = " +
"new " + name + "();");
out.println(" static class " + name + " {");
global_table.put(name.toUpperCase(), new Monitor(name));
// is there any identifier in the monitor table?
if (!monitor_table.isEmpty()) {
monitor_table.clear(); // then remove all the elements
}
}
/**
* monitor body generates the constructor for the
* the monitor class
*/
public void inAMonitorBody(AMonitorBody node) {
out.println(" public " + monitor_name + "() {");
}
public void outAMonitorBody(AMonitorBody node) {
out.println(" }");
}
public void outAMonitorDeclaration(AMonitorDeclaration node) {
out.println(" }");
program = true;
monitor = false;
}
/**
* process_heading =
* process identifier semicolon;
*/
public void outAProcessHeading(AProcessHeading node) {
String name = node.getIdentifier().getText();
out.println(" static " + name + " " + name +
" = " + "new " + name + "();");
out.println(" static class " + name + " extends " + "java.lang.Thread {");
global_table.put(name.toUpperCase(), new Process(name));
program = false;
process = true;
// is the local table full?;
if (!local_table.isEmpty()) {
local_table.clear(); // then remove all the elements
}
}
public void inAProcessBody(AProcessBody node) {
out.println(" public void run() {");
}
public void outAProcessBody(AProcessBody node) {
out.println(" }");
}
public void outAProcessDeclaration(AProcessDeclaration node) {
program = true;
process = false;
out.println(" }");
}
// Types
/**
* type = // standard types
* {integer} integer |
*/
public void outAIntegerType(AIntegerType node) {
type = "int";
}
/**
* {real} real |
*/
public void outARealType(ARealType node) {
type = "double";
}
/**
* {char} char |
*/
public void outACharType(ACharType node) {
type = "char";
}
/**
* {boolean} boolean |
*/
public void outABooleanType(ABooleanType node) {
type = "boolean";
}
/**
* {array} array l_bracket index_type_list r_bracket of P.type |
*/
public void outAArrayType(AArrayType node) {
type = type + "[]";
}
/**
* {channel} channel of P.type |
*/
public void outAChannelType(AChannelType node) {
if (type.equals("long")) {
type = "uk.edu.sbu.seeie.jpascal.type.ChannelOfInteger";
}
else if (type.equals("char")) {
type = "uk.edu.sbu.seeie.jpascal.type.ChannelOfChar";
}
else if (type.equals("double")) {
type = "uk.edu.sbu.seeie.jpascal.type.ChannelOfReal";
}
else if (type.equals("java.lang.String")) {
type = "uk.edu.sbu.seeie.jpascal.type.ChannelOfString";
}
else if (type.equals("boolean")) {
type = "uk.edu.sbu.seeie.jpascal.type.ChannelOfBoolean";
}
}
/**
* {semaphore} semaphore |
*/
public void outASemaphoreType(ASemaphoreType node) {
type = "uk.edu.sbu.seeie.jpascal.type.Semaphore";
}
/**
* {condition} condition |
*/
public void outAConditionType(AConditionType node) {
type = "uk.edu.sbu.seeie.jpascal.type.Condition";
}
/**
* {exception} exception ;
*/
public void outAExceptionType(AExceptionType node) {
type = "exception";
}
/**
* index =
* {integer} integer_literal |
*/
public void outAIntegerIndex(AIntegerIndex node) {
index = node.getIntegerLiteral().getText();
}
/** {identifier} identifier ;
*/
public void outAIdentifierIndex(AIdentifierIndex node) {
String key = node.getIdentifier().getText().toUpperCase();
Constant constant;
if (program) {
constant =(Constant) global_table.get(key);
}
else if (monitor) {
if (monitor_table.containsKey(key)) {
constant = (Constant) monitor_table.get(key);
}
else {
constant = (Constant) global_table.get(key);
}
}
else {
if (local_table.containsKey(key)) {
constant = (Constant) local_table.get(key);
}
else {
constant = (Constant) global_table.get(key);
}
}
index = constant.getImage();
}
/**
* type =
* {string} string |
*/
public void outAStringType(AStringType node) {
type = "java.lang.String";
string_size = "";
}
/**
* process_activation_list =
* {identifier} identifier |
*/
public void outAIdentifierProcessActivationList(AIdentifierProcessActivationList node) {
String key = node.getIdentifier().getText().toUpperCase();
String image = ((Process) global_table.get(key)).getImage();
out.println(" " + image + ".start();");
}
/**
* {sequence} process_activation_list semicolon identifier ;
*/
public void outASequenceProcessActivationList(ASequenceProcessActivationList node) {
String key = node.getIdentifier().getText().toUpperCase();
String image = ((Process) global_table.get(key)).getImage();
out.println(" " + image + ".start();");
}
/**
* statement =
* {assign} variable assignop expression |
*/
public void caseAAssignStatement(AAssignStatement node) {
if (node.getVariable() != null) {
node.getVariable().apply(this);
}
if (function_assign) {
out.print("return ");
}
else {
out.print(" = ");
}
if (node.getExpression() != null) {
node.getExpression().apply(this);
}
out.println(";");
if (function_assign) {
function_assign = false;
}
}
/**
* {while} while expression do statement |
*/
public void caseAWhileStatement(AWhileStatement node) {
out.print(" while (");
if (node.getExpression() != null) {
node.getExpression().apply(this);
}
out.println(") {");
if (node.getStatement() != null) {
node.getStatement().apply(this);
}
out.println(" }");
}
/**
* {if_then} if expression then statement |
*/
public void caseAIfThenStatement(AIfThenStatement node) {
out.print(" if (");
if (node.getExpression() != null) {
node.getExpression().apply(this);
}
out.println(") {");
if (node.getStatement() != null) {
node.getStatement().apply(this);
}
out.println(" }");
}
/**
* in compound statement
*/
public void inACompoundStatement(ACompoundStatement node) {
out.println(" {");
}
/**
* {compound} begin statement_sequence end |
*/
public void outACompoundStatement(ACompoundStatement node) {
if (program) {
out.println(" }");
}
else {
out.println(" }");
}
}
/**
* {try_except} try statement_sequence handle exception_block |
*/
public void caseATryExceptStatement(ATryExceptStatement node) {
out.println(" try {");
if (node.getStatementSequence() != null) {
node.getStatementSequence().apply(this);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -