public bool Proc() { TokenData temp = new TokenData(); if (m_bin == null) { Error("CInterpreter::Proc m_bin == null"); return(false); } if (m_pos == m_size) { return(false); } if (m_pos < 0 || m_pos > m_size) { Error("CInterpreter::Proc %x pos error [0~%x] size= %d", m_pos, m_size); return(false); } int code = 0; if (!ReadCode(ref code)) { Error("CInterpreter::Proc read code error pos%d", m_pos); return(false); } switch ((CODE)code) { case CODE.CODE_FUNC: if (!CodeFunc(ref temp)) { return(false); } break; case CODE.CODE_GOTO: if (!CodeGoto()) { return(false); } break; case CODE.CODE_MOV: if (!CodeMov()) { return(false); } break; case CODE.CODE_CMP: if (!CodeCmp()) { return(false); } break; case CODE.CODE_ADD: if (!CodeAdd()) { return(false); } break; case CODE.CODE_SUB: if (!CodeSub()) { return(false); } break; case CODE.CODE_MUL: if (!CodeMul()) { return(false); } break; case CODE.CODE_DIV: if (!CodeDiv()) { return(false); } break; case CODE.CODE_MOD: if (!CodeMod()) { return(false); } break; case CODE.CODE_INC: if (!CodeInc()) { return(false); } break; case CODE.CODE_DEC: if (!CodeDec()) { return(false); } break; case CODE.CODE_PUSH: if (!CodePush()) { return(false); } break; case CODE.CODE_POP: if (!CodePop()) { return(false); } break; case CODE.CODE_CASE: if (!CodeCase()) { return(false); } break; case CODE.CODE_END: return(false); default: Error("CInterpreter::Proc code error pos{0} code[{1}]", m_pos, code); return(false); } return(true); }
public bool ReadValue(ref TokenData ret) { LinkedList <TokenData> dataQ = new LinkedList <TokenData>(); List <byte> opQ = new List <byte>(); byte type = 0; byte op = 0; string str = new string(new char[Globals.STR_MAXLEN]); long num = 0; while (true) { TokenData data = new TokenData(); if (!PeekByte(ref type)) { Error("ReadValue: !PeekByte(type)"); return(false); } // Debug.Print(type.ToString()); switch (Convert.ToChar(type)) { case 's': if (!ReadStr(ref str)) { Error("ReadValue: !ReadStr(str)"); return(false); } data.Set(str); //Debug.Print(Convert.ToChar(type).ToString() + ": " + str); break; case 'n': if (!ReadNum(ref num)) { Error("ReadValue: !ReadNum(num)"); return(false); } data.Set(num); //Debug.Print(Convert.ToChar(type).ToString() + ": " + num); break; case 'v': if (!ReadVar(ref str)) { Error("ReadValue: !ReadVar(str)"); return(false); } if (!m_varMap.Get(str, ref data)) { data = new TokenData(); } break; case 'f': GetByte(ref type); var n = 0; if (!ReadCode(ref n)) { Error("ReadValue: !ReadCode(num)"); return(false); } num = n; if (num != (int)CODE.CODE_FUNC) { Error("ReadValue: num != CODE_FUNC %d", num); return(false); } if (!CodeFunc(ref data)) { Error("ReadValue: !CodeFunc(data)"); return(false); } break; default: Error("ReadValue: not found c[%c] d[%d] h[%x] ", type, type, type); return(false); } dataQ.AddLast(data); if (!GetByte(ref op)) { Error("ReadValue: !GetByte(op) %c", type); return(false); } if (op == (byte)OP.OP_END) { break; } opQ.Add(op); } int count = opQ.Count; for (int i = 0; i < count; i++) { switch (opQ.ElementAt(i)) { case (byte)OP.OP_MUL: { var first = dataQ.ElementAt(i); var second = dataQ.ElementAt(i + 1); first.Set(first.GetNum() * second.GetNum()); second.Set(0); opQ.RemoveAt(i); opQ.Insert(i, (byte)OP.OP_ADD); } break; case (byte)OP.OP_DIV: { var first = dataQ.ElementAt(i); var second = dataQ.ElementAt(i + 1); first.Set(first.GetNum() / second.GetNum()); second.Set(0); opQ.RemoveAt(i); opQ.Insert(i, (byte)OP.OP_ADD); } break; } } while (opQ.Any()) { TokenData d1 = dataQ.First(); dataQ.RemoveFirst(); TokenData d2 = dataQ.First(); dataQ.RemoveFirst(); TokenData result = new TokenData(); result.Set(0); switch (opQ.First()) { case (byte)OP.OP_ADD: result = d1 + d2; break; case (byte)OP.OP_SUB: result = d1 - d2; break; case (byte)OP.OP_MOD: result = d1 % d2; break; case (byte)OP.OP_EQUAL: result.Set(d1 == d2); break; case (byte)OP.OP_NOTEQUAL: result.Set(d1 != d2); break; case (byte)OP.OP_LARGE: result.Set(d1 > d2); break; case (byte)OP.OP_SMALL: result.Set(d1 < d2); break; case (byte)OP.OP_LARGE_OR_EQUAL: result.Set(d1 >= d2); break; case (byte)OP.OP_SMALL_OR_EQUAL: result.Set(d1 <= d2); break; //case (byte)OP.OP_AND: result.Set(d1 && d2); break; //case (byte)OP.OP_OR: result.Set(d1 || d2); break; default: break; } dataQ.AddFirst(result); opQ.RemoveAt(0); } ret = dataQ.First.Value; return(true); }
public void Set(string str, TokenData t) { data[str] = t; }