/// <summary> /// set loop record info /// </summary> /// <param name="symbol"></param> /// <param name="startVal"></param> /// <param name="endVal"></param> /// <param name="stepVal"></param> public void SetLoopRecord( VarSymbol symbol, BaseData endVal, BaseData stepVal ) { m_loopSymbol = symbol; m_endValue = endVal; m_stepValue = stepVal; }
/// <summary> /// constructor /// </summary> /// <param name="type"></param> /// <param name="bd"></param> public VarSymbol( string name, BaseData bd ) { m_type = Symbol.VAR; m_name = name; VALUE = bd; }
/// <summary> /// constructor /// </summary> /// <param name="bd"></param> public Expression(BaseData bd) { m_type = Expression.VALUE; m_leftExp = null; m_rightExp = null; m_funcParams = null; m_value = bd; }
/// <summary> /// number list like 1,2,3,4,5 /// </summary> /// <returns></returns> protected List<int> numberList() { List<int> result = new List<int>(); while (true) { BaseData bd; int tt = lookAhead(); Token tok = eatToken(tt); if (tt == Token.INT) bd = new BaseData(tok.m_intVal); else if (tt == Token.FLOAT) bd = new BaseData(tok.m_floatVal); else throw new ErrorCode(ErrorCode.ERROR_CODE_12); bd.Convert(BaseData.TYPE_INT); result.Add(bd.INT_VAL); if (lookAhead() == Token.COMMA) eatToken(Token.COMMA); else break; } return result; }
public static BaseData operator |(BaseData lhs, BaseData rhs) { BaseData result; if (lhs.m_type == BaseData.TYPE_FLOAT || rhs.m_type == BaseData.TYPE_FLOAT) { lhs.Convert(BaseData.TYPE_FLOAT); rhs.Convert(BaseData.TYPE_FLOAT); if (lhs.m_floatVal == 0 && rhs.m_floatVal == 0) result = new BaseData(0); else result = new BaseData(1); } else if (lhs.m_type == BaseData.TYPE_INT && rhs.m_type == BaseData.TYPE_INT) { if (lhs.m_intVal == 0 && rhs.m_intVal == 0) result = new BaseData(0); else result = new BaseData(1); } else { throw new ErrorCode(ErrorCode.ERROR_CODE_12); } return result; }
/// <summary> /// power /// </summary> /// <param name="lhs"></param> /// <param name="rhs"></param> /// <returns></returns> public static BaseData operator ^(BaseData lhs, BaseData rhs ) { BaseData result; if (lhs.m_type == BaseData.TYPE_FLOAT || rhs.m_type == BaseData.TYPE_FLOAT) { lhs.Convert(BaseData.TYPE_FLOAT); rhs.Convert(BaseData.TYPE_FLOAT); result = new BaseData( (float)Math.Pow( lhs.m_floatVal, rhs.m_floatVal )); } else if (lhs.m_type == BaseData.TYPE_INT && rhs.m_type == BaseData.TYPE_INT) { result = new BaseData((int)Math.Pow(lhs.m_floatVal, rhs.m_floatVal)); } else { throw new ErrorCode(ErrorCode.ERROR_CODE_12); } return result; }
/// <summary> /// reduce the expession /// </summary> /// <param name="exp"></param> /// <returns></returns> protected Expression calculateExp(Expression exp) { Expression result = null; Expression midExp = null; Expression expLeft = null; Expression expRight = null; switch (exp.m_type) { case Expression.VALUE: result = exp; break; case Expression.TYPE_CLOSE_TO: result = exp; result.m_value = new BaseData(BaseData.TYPE_CLOSE_TO, 0); break; case Expression.TYPE_NEXT_LINE: result = exp; result.m_value = new BaseData(BaseData.TYPE_NEXT_LINE, 0); break; case Expression.EXP_SYMBOL: VarSymbol s = m_symbolTable.ResolveVar(exp.m_symbolName); result = new Expression(s.VALUE); break; case Expression.EXP_ARRAY_SYMBOL: List <int> indexs = expressListToIndexs(exp.m_funcParams); ArraySymbol arr = m_symbolTable.ResolveArray(exp.m_symbolName, indexs); result = new Expression(arr.GetValue(indexs)); break; // 内置函数 case Expression.EXP_FUNC: List <BaseData> param = new List <BaseData>(); // convert the parameters foreach (Expression e in exp.m_funcParams) { param.Add(calculateExp(e).m_value); } // call the function BaseData returnVal = m_innerFunc.CallFunc(exp.m_symbolName, param); result = new Expression(returnVal); break; // 用户自定义函数 case Expression.EXP_USER_FUNC: string funcName = exp.m_symbolName; FunSymbol func = m_symbolTable.ResolveFunc(funcName); Expression funcParam = exp.m_funcParams[0]; BaseData paramVal = calculateExp(funcParam).m_value; m_symbolTable.Define(new VarSymbol(CommonDef.FN_PARAM_SYMBOL, paramVal)); result = calculateExp(func.EXP); break; case Expression.OP_NOT: midExp = calculateExp(exp.m_leftExp); if (midExp.m_type == Expression.VALUE) { if (midExp.m_value != BaseData.ZERO) { result = new Expression(new BaseData(0)); } else { result = new Expression(new BaseData(1)); } } else { throw new ErrorCode(ErrorCode.ERROR_CODE_12); } break; case Expression.OP_NEG: midExp = calculateExp(exp.m_leftExp); if (midExp.m_type == Expression.VALUE) { result = midExp; result.m_value.NegValue(); } else { throw new ErrorCode(ErrorCode.ERROR_CODE_12); } break; case Expression.EXP_INKEY: result = new Expression(new BaseData(m_apiCall.Inkey())); break; // 二元运算符 case Expression.OP_ADD: case Expression.OP_MINUS: case Expression.OP_MUL: case Expression.OP_DIV: case Expression.OP_POWER: case Expression.OP_AND: case Expression.OP_OR: case Expression.OP_EQUAL: case Expression.OP_GREATER: case Expression.OP_GREATER_EQU: case Expression.OP_LESS: case Expression.OP_LESS_EQ: // check the value expLeft = calculateExp(exp.m_leftExp); expRight = calculateExp(exp.m_rightExp); if (expLeft.m_type != Expression.VALUE || expRight.m_type != Expression.VALUE) { throw new ErrorCode(ErrorCode.ERROR_CODE_12); } if (exp.m_type == Expression.OP_ADD) { result = new Expression(expLeft.m_value + expRight.m_value); } else if (exp.m_type == Expression.OP_MINUS) { result = new Expression(expLeft.m_value - expRight.m_value); } else if (exp.m_type == Expression.OP_MUL) { result = new Expression(expLeft.m_value * expRight.m_value); } else if (exp.m_type == Expression.OP_DIV) { result = new Expression(expLeft.m_value / expRight.m_value); } else if (exp.m_type == Expression.OP_POWER) { result = new Expression(expLeft.m_value ^ expRight.m_value); } else if (exp.m_type == Expression.OP_AND) { result = new Expression(expLeft.m_value & expRight.m_value); } else if (exp.m_type == Expression.OP_OR) { result = new Expression(expLeft.m_value | expRight.m_value); } else if (exp.m_type == Expression.OP_EQUAL) { result = new Expression(expLeft.m_value == expRight.m_value ? 1 : 0); } else if (exp.m_type == Expression.OP_GREATER) { result = new Expression(new BaseData(expLeft.m_value > expRight.m_value ? 1 : 0)); } else if (exp.m_type == Expression.OP_GREATER_EQU) { result = new Expression(new BaseData(expLeft.m_value >= expRight.m_value ? 1 : 0)); } else if (exp.m_type == Expression.OP_LESS) { result = new Expression(new BaseData(expLeft.m_value < expRight.m_value ? 1 : 0)); } else if (exp.m_type == Expression.OP_LESS_EQ) { result = new Expression(new BaseData(expLeft.m_value <= expRight.m_value ? 1 : 0)); } break; default: throw new ErrorCode(ErrorCode.ERROR_CODE_02); } return(result); }
protected void onParamCmd(Statement s) { if (s.m_symbol == "PLAY") { if (s.m_expressList.Count != 1) { throw new ErrorCode(ErrorCode.ERROR_CODE_02); } BaseData bd = calculateExp(s.m_expressList[0]).m_value; if (bd.TYPE != BaseData.TYPE_STRING) { throw new ErrorCode(ErrorCode.ERROR_CODE_02); } m_apiCall.Play(bd.STR_VAL); } else { List <int> indexs = expressListToIndexs(s.m_expressList); if (s.m_symbol == "LOCATE") { if (indexs.Count != 2) { throw new ErrorCode(ErrorCode.ERROR_CODE_02); } m_apiCall.Locate(indexs[0], indexs[1]); } else if (s.m_symbol == "BOX") { if (indexs.Count == 4) { m_apiCall.Box(indexs[0], indexs[1], indexs[2], indexs[3], 0, 1); } else if (indexs.Count == 5) { m_apiCall.Box(indexs[0], indexs[1], indexs[2], indexs[3], indexs[4], 1); } else if (indexs.Count == 6) { m_apiCall.Box(indexs[0], indexs[1], indexs[2], indexs[3], indexs[4], indexs[5]); } else { throw new ErrorCode(ErrorCode.ERROR_CODE_02); } } else if (s.m_symbol == "DRAW") { if (indexs.Count == 2) { m_apiCall.Draw(indexs[0], indexs[1], 1); } else if (indexs.Count == 3) { m_apiCall.Draw(indexs[0], indexs[1], indexs[2]); } else { throw new ErrorCode(ErrorCode.ERROR_CODE_02); } } else if (s.m_symbol == "CIRCLE") { if (indexs.Count == 3) { m_apiCall.Circle(indexs[0], indexs[1], indexs[2], 0, 1); } else if (indexs.Count == 4) { m_apiCall.Circle(indexs[0], indexs[1], indexs[2], indexs[3], 0); } else if (indexs.Count == 5) { m_apiCall.Circle(indexs[0], indexs[1], indexs[2], indexs[3], indexs[4]); } else { throw new ErrorCode(ErrorCode.ERROR_CODE_02); } } else if (s.m_symbol == "ELLIPSE") { if (indexs.Count == 4) { m_apiCall.Ellipse(indexs[0], indexs[1], indexs[2], indexs[3], 0, 1); } else if (indexs.Count == 5) { m_apiCall.Ellipse(indexs[0], indexs[1], indexs[2], indexs[3], indexs[4], 1); } else if (indexs.Count == 6) { m_apiCall.Ellipse(indexs[0], indexs[1], indexs[2], indexs[3], indexs[4], indexs[5]); } else { throw new ErrorCode(ErrorCode.ERROR_CODE_02); } } else if (s.m_symbol == "LINE") { if (indexs.Count == 4) { m_apiCall.Line(indexs[0], indexs[1], indexs[2], indexs[3], 1); } else if (indexs.Count == 5) { m_apiCall.Line(indexs[0], indexs[1], indexs[2], indexs[3], indexs[4]); } else { throw new ErrorCode(ErrorCode.ERROR_CODE_02); } } } }
protected BaseData ABS( List<BaseData> param ) { checkMathParam(param); BaseData ret = BaseData.ZERO; BaseData p = param[0]; if (p.TYPE == BaseData.TYPE_INT) ret = new BaseData(p.INT_VAL >= 0 ? p.INT_VAL : -p.INT_VAL); else if (p.TYPE == BaseData.TYPE_FLOAT) ret = new BaseData(p.FLOAT_VAL >= 0.0f ? p.FLOAT_VAL : -p.FLOAT_VAL); return ret; }
/// <summary> /// set value /// </summary> /// <param name="indexs"></param> /// <param name="val"></param> public void SetValue( List<int> indexs, BaseData val ) { if (indexs.Count != m_dimension.Count) throw new ErrorCode(ErrorCode.ERROR_CODE_08); val.Convert(m_dataType); int index = 0; int factor = 1; for (int i = m_dimension.Count - 1; i >= 0; i-- ) { index += factor * indexs[i]; factor *= m_dimension[i]; } m_data[index] = val; }
protected BaseData TAB(List<BaseData> param) { if (param.Count != 1) throw new ErrorCode(ErrorCode.ERROR_CODE_02); if (param[0].TYPE != BaseData.TYPE_INT) throw new ErrorCode(ErrorCode.ERROR_CODE_12); BaseData bd = new BaseData(BaseData.TYPE_TAB, param[0].INT_VAL); return bd; }
protected BaseData POS( List<BaseData> param ) { if (param.Count != 1) throw new ErrorCode(ErrorCode.ERROR_CODE_02); BaseData res = new BaseData(m_iapi.CursorX()); return res; }
/// <summary> /// *产生一个(0,1)间的随机小数。如果x>0,每次产生不同的随机数;如果x<0,产生有一定序列的随机数;如果x=0,输出上次产生的随机数。 /// </summary> /// <param name="param"></param> /// <returns></returns> protected BaseData RND(List<BaseData> param) { checkMathParam(param); BaseData ret = BaseData.ZERO; BaseData p = param[0]; int randParam = 0; if (p.TYPE == BaseData.TYPE_INT) randParam = p.INT_VAL; else if (p.TYPE == BaseData.TYPE_FLOAT) randParam = (int)p.FLOAT_VAL; if( randParam > 0 ) { m_lastRandomNum = (float)m_random.NextDouble(); } else if( randParam < 0 ) { m_lastRandomNum = (float)m_random.NextDouble(); //[TEMP] } ret = new BaseData(m_lastRandomNum); return ret; }
protected BaseData INT( List<BaseData> param ) { checkMathParam(param); BaseData ret = BaseData.ZERO; BaseData p = param[0]; if (p.TYPE == BaseData.TYPE_INT) ret = new BaseData(p.INT_VAL); else if (p.TYPE == BaseData.TYPE_FLOAT) ret = new BaseData(((int)p.FLOAT_VAL)); return ret; }
protected BaseData SGN(List<BaseData> param) { checkMathParam(param); BaseData ret = BaseData.ZERO; BaseData p = param[0]; if (p.TYPE == BaseData.TYPE_INT) { ret = new BaseData(p.INT_VAL == 0 ? 0 : p.INT_VAL / Math.Abs(p.INT_VAL)); } else if (p.TYPE == BaseData.TYPE_FLOAT) { if (p.FLOAT_VAL < float.Epsilon && p.FLOAT_VAL > -float.Epsilon) ret = new BaseData(0.0f); else ret = new BaseData(p.FLOAT_VAL >= 0.0f ? 1 : -1); } return ret; }