📄 scriptedit.java
字号:
/* Generated By:JavaCC: Do not edit this line. ScriptEdit.java */
package scriptedit;
import java.io.*;
import java.util.*;
import scriptedit.data.*;
public class ScriptEdit implements ScriptEditConstants {
static final String VERSION = "2.0";
static Script script;
static Expression exp;
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("ScriptEditor Useage: scriptFile, binFile");
return;
}
else {
ScriptEdit script;
System.out.println("======================================");
System.out.println(" Welcom To Use ScriptEditor " + VERSION);
System.out.println(" Authors: CoCoMo");
System.out.println(" CoCoMo Copyright (c) 2007");
System.out.println(" http://j2medev.spaces.live.com");
System.out.println("======================================");
System.out.println("");
System.out.println("Reading From File " + args[0]);
try {
script = new ScriptEdit(new FileInputStream(args[0]));
System.out.println("Parseing...");
script.Parse();
// DataStore.printScrList(); System.out.println("Parse Successfully");
System.out.println("Checking Rule...");
if(Check.check()) {
System.out.println("Check Rule Successfully");
System.out.println("Writeing Data To File " + args[1] + "...");
LocalFile.writeData(args[1]);
System.out.println("Write Data Successfully");
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
/***************************************** * THE CoCoMo LANGUAGE GRAMMAR STARTS HERE * *****************************************/
static final public void Parse() throws ParseException {
jj_consume_token(COCOMO);
jj_consume_token(LBRACE);
ScriptBodyDeclaration();
jj_consume_token(RBRACE);
jj_consume_token(0);
}
static final public void ScriptBodyDeclaration() throws ParseException {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case CONSTANT:
jj_consume_token(CONSTANT);
jj_consume_token(LBRACE);
label_1:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case IDENTIFIER:
;
break;
default:
jj_la1[0] = jj_gen;
break label_1;
}
ConstantDeclaration();
}
jj_consume_token(RBRACE);
break;
default:
jj_la1[1] = jj_gen;
;
}
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case VARIABLE:
jj_consume_token(VARIABLE);
jj_consume_token(LBRACE);
label_2:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case IDENTIFIER:
;
break;
default:
jj_la1[2] = jj_gen;
break label_2;
}
VariableDeclaration();
}
jj_consume_token(RBRACE);
break;
default:
jj_la1[3] = jj_gen;
;
}
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case FUNCTION:
jj_consume_token(FUNCTION);
jj_consume_token(LBRACE);
label_3:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case INTEGER_LITERAL:
;
break;
default:
jj_la1[4] = jj_gen;
break label_3;
}
FunctionDeclaration();
}
jj_consume_token(RBRACE);
break;
default:
jj_la1[5] = jj_gen;
;
}
ScriptStart();
label_4:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case SCRIPT:
;
break;
default:
jj_la1[6] = jj_gen;
break label_4;
}
ScriptDeclaration();
}
}
static final public void ConstantDeclaration() throws ParseException {
Token name, value, symbol;
String strName, strValue;
symbol = new Token();
symbol.image = "";
name = jj_consume_token(IDENTIFIER);
jj_consume_token(ASSIGN);
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case PLUS:
case MINUS:
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case PLUS:
symbol = jj_consume_token(PLUS);
break;
case MINUS:
symbol = jj_consume_token(MINUS);
break;
default:
jj_la1[7] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
break;
default:
jj_la1[8] = jj_gen;
;
}
value = jj_consume_token(INTEGER_LITERAL);
jj_consume_token(SEMICOLON);
strName = name.image;
strValue = symbol.image + value.image;
Constant con = new Constant(strName, strValue);
DataStore.add(con);
}
static final public void VariableDeclaration() throws ParseException {
Token name, value, symbol;
String strName, strValue;
symbol = new Token();
symbol.image = "";
name = jj_consume_token(IDENTIFIER);
jj_consume_token(ASSIGN);
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case PLUS:
case MINUS:
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case PLUS:
symbol = jj_consume_token(PLUS);
break;
case MINUS:
symbol = jj_consume_token(MINUS);
break;
default:
jj_la1[9] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
break;
default:
jj_la1[10] = jj_gen;
;
}
value = jj_consume_token(INTEGER_LITERAL);
jj_consume_token(SEMICOLON);
strName = name.image;
strValue = symbol.image + value.image;
Variable var = new Variable(strName, strValue);
DataStore.add(var);
}
static final public void FunctionDeclaration() throws ParseException {
Token name, id;
String strName, strID;
id = jj_consume_token(INTEGER_LITERAL);
jj_consume_token(ASSIGN);
name = jj_consume_token(IDENTIFIER);
jj_consume_token(SEMICOLON);
strName = name.image;
strID = id.image;
Function fun = new Function(strName, strID);
DataStore.add(fun);
}
static final public void ScriptStart() throws ParseException {
script = new Script();
Control.setPC(0);
jj_consume_token(SCRIPT);
jj_consume_token(START);
jj_consume_token(LBRACE);
label_5:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case IF:
case WHILE:
case BREAK:
case CONTINUE:
case STOP:
case RETURN:
case IDENTIFIER:
;
break;
default:
jj_la1[11] = jj_gen;
break label_5;
}
Statement();
}
jj_consume_token(RBRACE);
Instruction ins = new Instruction();
ins.setName(Instruction.RETURN);
script.add(ins);
Control.nextPC();
script.setName("Start");
DataStore.add(script);
}
static final public void ScriptDeclaration() throws ParseException {
Token name;
String strName;
script = new Script();
Control.setPC(0);
jj_consume_token(SCRIPT);
name = jj_consume_token(IDENTIFIER);
jj_consume_token(LBRACE);
label_6:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case IF:
case WHILE:
case BREAK:
case CONTINUE:
case STOP:
case RETURN:
case IDENTIFIER:
;
break;
default:
jj_la1[12] = jj_gen;
break label_6;
}
Statement();
}
jj_consume_token(RBRACE);
Instruction ins = new Instruction();
ins.setName(Instruction.RETURN);
script.add(ins);
Control.nextPC();
strName = name.image;
script.setName(strName);
DataStore.add(script);
}
static final public void Statement() throws ParseException {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case BREAK:
case CONTINUE:
case STOP:
case RETURN:
case IDENTIFIER:
InstructionStatement();
break;
case IF:
IfStatement();
break;
case WHILE:
WhileStatement();
break;
default:
jj_la1[13] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
}
static final public void InstructionStatement() throws ParseException {
Token name, token;
String strName;
Teminal t;
Instruction ins;
int prePC = Control.getPC();
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case IDENTIFIER:
name = jj_consume_token(IDENTIFIER);
jj_consume_token(LPAREN);
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case SCRIPT:
case TRUE:
case FALSE:
case INTEGER_LITERAL:
case STRING_LITERAL:
case IDENTIFIER:
case PLUS:
case MINUS:
ArgumentList();
break;
default:
jj_la1[14] = jj_gen;
;
}
jj_consume_token(RPAREN);
jj_consume_token(SEMICOLON);
int count = Control.getPC() - prePC;
strName = name.image;
ins = new Instruction();
ins.setName(Instruction.PCALL);
t = new Teminal();
t.setType(Teminal.IDENTIFER);
t.setValue(strName);
//附加调试信息
token = getToken(0);
t.setLine(token.beginLine);
ins.setNum1(t);
//参数个数
t = new Teminal();
t.setType(Teminal.INTEGER);
t.setValue(count);
ins.setNum2(t);
script.add(ins);
Control.nextPC();
break;
case BREAK:
BreakStatement();
if(Control.getInsStatus() == Control.STATUS_IF_STM ||
Control.getInsStatus() == Control.STATUS_WHILE_STM) {
ins = new Instruction();
ins.setName(Instruction.BREAK);
script.add(ins);
Control.pushJC(ins);
Control.nextPC();
}
else {
token = getToken(0);
{if (true) throw new ParseException("Instruction break at line:" + token.beginLine + " place error!");}
}
break;
case RETURN:
ReturnStatement();
ins = new Instruction();
ins.setName(Instruction.RETURN);
script.add(ins);
Control.nextPC();
break;
case CONTINUE:
ContinueStatement();
if(Control.getInsStatus() == Control.STATUS_IF_STM ||
Control.getInsStatus() == Control.STATUS_WHILE_STM) {
ins = new Instruction();
ins.setName(Instruction.CONTINUE);
script.add(ins);
Control.pushJC(ins);
Control.nextPC();
}
else {
token = getToken(0);
{if (true) throw new ParseException("Instruction continue at line:" + token.beginLine + " place error!");}
}
break;
case STOP:
StopStatement();
ins = new Instruction();
ins.setName(Instruction.STOP);
script.add(ins);
Control.nextPC();
break;
default:
jj_la1[15] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
}
static final public void BreakStatement() throws ParseException {
jj_consume_token(BREAK);
jj_consume_token(SEMICOLON);
}
static final public void ReturnStatement() throws ParseException {
jj_consume_token(RETURN);
jj_consume_token(SEMICOLON);
}
static final public void ContinueStatement() throws ParseException {
jj_consume_token(CONTINUE);
jj_consume_token(SEMICOLON);
}
static final public void StopStatement() throws ParseException {
jj_consume_token(STOP);
jj_consume_token(SEMICOLON);
}
static final public void IfStatement() throws ParseException {
Teminal t;
Instruction ins;
int pc, preJC;
boolean flag;
jj_consume_token(IF);
jj_consume_token(LPAREN);
exp = new Expression();
t = new Teminal();
t.setType(Teminal.MSYMBOL);
t.setValue(Expression.LPAREN);
exp.pushO(t);
//是否有else
flag = false;
//进入前的JC栈数
preJC = Control.getJCSize();
//设置指令状态
Control.setInsStatus(Control.STATUS_IF_EXP);
Expression();
jj_consume_token(RPAREN);
exp.popO();
if(exp.oStackSize() != 0 || exp.tStackSize() != 0)
{if (true) throw new ParseException("If Expression Error!");}
ins = new Instruction();
ins.setName(Instruction.IF);
script.add(ins);
Control.nextPC();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -