123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- var ExprCal = {
- // gramParser :new syntax(), // 用于获取运行实例符号表的传入类
- expression :'', // 表达式字符数组
- charIndex :0, // 数组下标指针
- num :new Array(), // 运算数栈
- // 运算符栈关系运算符对应关系
- // '<' 对应 <
- // '>' 对应 >
- // 'e' 对应 ==
- // 'n' 对应 <>
- op :new Array(), // 运算符栈
- hasReturn :false,
- genCode :true,
- // 中间代码临时存放变量
- tempIndex :1,
- // 获取新temp变量名
- getTemp : function() {
- ExprCal.tempIndex++;
- return "@temp" + ExprCal.tempIndex;
- },
- IsDigit : function(c) {
- if (c >= '0' && c <= '9') {
- return true;
- }
- return false;
- },
- IsLetter : function(c) {
- if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') {
- return true;
- }
- return false;
- },
- init : function(expression)
- {
- ExprCal.expression = expression;
- ExprCal.charIndex=0;
- ExprCal.hasReturn=false;
- ExprCal.num=new Array();
- ExprCal.genCode = !Syntax.InWrite;
- tempIndex =1;
- },
- Calculate : function() {
- ExprCal.build();
- while (ExprCal.op.length > 0){
- ExprCal.calc();
- }
- if (!(ExprCal.hasReturn)) {
- ExprCal.gen("load", "_", ExprCal.num[ExprCal.num.length - 1],
- "@result");
- } else {
- ExprCal.gen("load", "_", Syntax.getLastReg(), "@result");
- }
- return 0;
- },
- gen : function(s1, s2, s3, s4) {
- if (ExprCal.genCode) {
- Syntax.gen(s1, s2, s3, s4);
- }
- },
- // 获取下一个字符
- getChar : function() {
- if (ExprCal.charIndex < ExprCal.expression.length) {
- return ExprCal.expression[ExprCal.charIndex];
- } else {
- return '\0';
- }
- },
- // 栈顶运算
- calc : function() {
- var value;
- var num1, num2;
- if (ExprCal.op.length == 0) {
- return;
- }
- switch (ExprCal.op[ExprCal.op.length - 1]) {
- case '<':
- num1 = ExprCal.num.pop();
- num2 = ExprCal.num.pop();
- value = ExprCal.getTemp();
- ExprCal.gen("lt", num2, num1, value);
- ExprCal.hasReturn = true;
- ExprCal.num.push(value);
- break;
- case '>':
- num1 = ExprCal.num.pop();
- num2 = ExprCal.num.pop();
- value = ExprCal.getTemp();
- ExprCal.gen("gt", num2, num1, value);
- ExprCal.hasReturn = true;
- ExprCal.num.push(value);
- break;
- case 'e':
- num1 = ExprCal.num.pop();
- num2 = ExprCal.num.pop();
- value = ExprCal.getTemp();
- ExprCal.gen("eq", num2, num1, value);
- ExprCal.hasReturn = true;
- ExprCal.num.push(value);
- break;
- case 'n':
- num1 = ExprCal.num.pop();
- num2 = ExprCal.num.pop();
- value = ExprCal.getTemp();
- ExprCal.gen("ne", num2, num1, value);
- ExprCal.hasReturn = true;
- ExprCal.num.push(value);
- break;
- case '+':
- num1 = ExprCal.num.pop();
- num2 = ExprCal.num.pop();
- value = ExprCal.getTemp();
- ExprCal.gen("add", num2, num1, value);
- ExprCal.hasReturn = true;
- ExprCal.num.push(value);
- break;
- case '-':
- num1 = ExprCal.num.pop();
- num2 = ExprCal.num.pop();
- value = ExprCal.getTemp();
- ExprCal.gen("sub", num2, num1, value);
- ExprCal.hasReturn = true;
- ExprCal.num.push(value);
- break;
- case '*':
- num1 = ExprCal.num.pop();
- num2 = ExprCal.num.pop();
- value = ExprCal.getTemp();
- ExprCal.gen("mul", num2, num1, value);
- ExprCal.hasReturn = true;
- ExprCal.num.push(value);
- break;
- case '/':
- num1 = ExprCal.num.pop();
- num2 = ExprCal.num.pop();
- value = ExprCal.getTemp();
- ExprCal.gen("div", num2, num1, value);
- ExprCal.hasReturn = true;
- ExprCal.num.push(value);
- break;
- }
- ExprCal.op.pop();
- },
- // 建立数栈和运算符栈
- build : function() {
- var t, tempExpr;
- var c = ExprCal.getChar();
- while (ExprCal.charIndex < ExprCal.expression.length) {
- // 处理变量
- if (ExprCal.IsLetter(c)) {
- t = "";
- tempExpr = "";
- // 标识符
- while (ExprCal.IsLetter(c) || ExprCal.IsDigit(c) || c == '_') {
- t += c;
- ExprCal.charIndex++;
- c = ExprCal.getChar();
- }
- // 数组下标
- if (c == '[') {
- var level = 1;
- ExprCal.op.push(c);
- ExprCal.charIndex++;
- c = ExprCal.getChar();
- while (c != ']' || level != 1) {
- if (c == '[') {
- level++;
- }
- if (c == ']') {
- level--;
- }
- tempExpr += c;
- ExprCal.charIndex++;
- c = ExprCal.getChar();
- }
- ExprCal.init(tempExpr);// /////////////////////////
- ExprCal.Calculate();
- tempExpr = "[@result]";
- }
- t += tempExpr;
- ExprCal.num.push(t);
- }
- // 处理数字
- if (ExprCal.IsDigit(c)) {
- t = "";
- while (ExprCal.IsDigit(c) || c == '.') {
- t += c;
- ExprCal.charIndex++;
- c = ExprCal.getChar();
- }
- ExprCal.num.push(parseFloat(t));
- }
- // 处理运算符
- switch (c) {
- case '(':
- ExprCal.op.push(c);
- break;
- case ')':
- while (ExprCal.op[ExprCal.op.length - 1] != '(') {
- ExprCal.calc();
- }
- ExprCal.op.pop();
- break;
- case '>': // > 运算
- if (ExprCal.op.length > 0
- && (ExprCal.op[ExprCal.op.length - 1] == '<'
- || ExprCal.op[ExprCal.op.length - 1] == 'e'
- || ExprCal.op[ExprCal.op.length - 1] == 'n'
- || ExprCal.op[ExprCal.op.length - 1] == '+'
- || ExprCal.op[ExprCal.op.length - 1] == '-'
- || ExprCal.op[ExprCal.op.length - 1] == '*' || ExprCal.op[ExprCal.op.length - 1] == '/')) {
- ExprCal.calc();
- }
- ExprCal.op.push('>');
- break;
- case '=': // == 运算
- ExprCal.charIndex++;
- if (ExprCal.op.length > 0
- && (ExprCal.op[ExprCal.op.length - 1] == '<'
- || ExprCal.op[ExprCal.op.length - 1] == '>'
- || ExprCal.op[ExprCal.op.length - 1] == 'n'
- || ExprCal.op[ExprCal.op.length - 1] == '+'
- || ExprCal.op[ExprCal.op.length - 1] == '-'
- || ExprCal.op[ExprCal.op.length - 1] == '*' || ExprCal.op[ExprCal.op.length - 1] == '/')) {
- ExprCal.calc();
- }
- ExprCal.op.push('e');
- break;
- case '<':
- if (ExprCal.expression[ExprCal.charIndex + 1] != '>') // > 运算
- {
- if (ExprCal.op.length > 0
- && (ExprCal.op[ExprCal.op.length - 1] == '>'
- || ExprCal.op[ExprCal.op.length - 1] == 'e'
- || ExprCal.op[ExprCal.op.length - 1] == 'n'
- || ExprCal.op[ExprCal.op.length - 1] == '+'
- || ExprCal.op[ExprCal.op.length - 1] == '-'
- || ExprCal.op[ExprCal.op.length - 1] == '*' || ExprCal.op[ExprCal.op.length - 1] == '/')) {
- ExprCal.calc();
- }
- ExprCal.op.push('<');
- } else // <> 运算
- {
- ExprCal.charIndex++;
- if (ExprCal.op.length > 0
- && (ExprCal.op[ExprCal.op.length - 1] == '<' || ExprCal.op[ExprCal.op.length - 1] == '>'
- || ExprCal.op[ExprCal.op.length - 1] == 'e'
- || ExprCal.op[ExprCal.op.length - 1] == '+'
- || ExprCal.op[ExprCal.op.length - 1] == '-'
- || ExprCal.op[ExprCal.op.length - 1] == '*' || ExprCal.op[ExprCal.op.length - 1] == '/')) {
- ExprCal.calc();
- }
- ExprCal.op.push('n');
- }
- break;
- case '+':
- while (ExprCal.op.length > 0
- && (ExprCal.op[ExprCal.op.length - 1] == '+' || ExprCal.op[ExprCal.op.length - 1] == '-'
- || ExprCal.op[ExprCal.op.length - 1] == '*' || ExprCal.op[ExprCal.op.length - 1] == '/')) {
- ExprCal.calc();
- }
- ExprCal.op.push(c);
- break;
- case '-':
- while (ExprCal.op.length > 0
- && (ExprCal.op[ExprCal.op.length - 1] == '+' || ExprCal.op[ExprCal.op.length - 1] == '-'
- || ExprCal.op[ExprCal.op.length - 1] == '*' || ExprCal.op[ExprCal.op.length - 1] == '/')) {
- ExprCal.calc();
- }
- ExprCal.op.push(c);
- break;
- case '*':
- while (ExprCal.op.length > 0
- && (ExprCal.op[ExprCal.op.length - 1] == '*' || ExprCal.op[ExprCal.op.length - 1] == '/')) {
- ExprCal.calc();
- }
- ExprCal.op.push(c);
- break;
- case '/':
- while (ExprCal.op.length > 0
- && (ExprCal.op[ExprCal.op.length - 1] == '*' || ExprCal.op[ExprCal.op.length - 1] == '/')) {
- ExprCal.calc();
- }
- ExprCal.op.push(c);
- break;
- default:
- ExprCal.charIndex++;
- c = ExprCal.getChar();
- continue;
- }
- ExprCal.charIndex++;
- c = ExprCal.getChar();
- }
- }
- }
|