Esempio n. 1
0
void Runtime::Debugger::EvaluateCalculation(CalculatedExpression* expression) {
  EvaluateExpression(expression->GetLeft());
  EvaluateExpression(expression->GetRight());

  Expression* left = expression->GetLeft();
  Expression* right = expression->GetRight();

  switch(expression->GetExpressionType()) {
  case AND_EXPR:
    if(left->GetFloatEval() && right->GetFloatEval()) {
      expression->SetFloatValue(left->GetFloatValue() && right->GetFloatValue());
    }
    else if(left->GetFloatEval()) {
      expression->SetFloatValue(left->GetFloatValue() && right->GetIntValue());
    }
    else if(right->GetFloatEval()) {
      expression->SetFloatValue(left->GetIntValue() && right->GetFloatValue());
    }
    else {
      expression->SetIntValue(left->GetIntValue() && right->GetIntValue());
    }
    break;

  case OR_EXPR:
    if(left->GetFloatEval() && right->GetFloatEval()) {
      expression->SetFloatValue(left->GetFloatValue() || right->GetFloatValue());
    }
    else if(left->GetFloatEval()) {
      expression->SetFloatValue(left->GetFloatValue() || right->GetIntValue());
    }
    else if(right->GetFloatEval()) {
      expression->SetFloatValue(left->GetIntValue() || right->GetFloatValue());
    }
    else {
      expression->SetIntValue(left->GetIntValue() || right->GetIntValue());
    }
    break;

  case EQL_EXPR:
    if(left->GetFloatEval() && right->GetFloatEval()) {
      expression->SetFloatValue(left->GetFloatValue() == right->GetFloatValue());
    }
    else if(left->GetFloatEval()) {
      expression->SetFloatValue(left->GetFloatValue() == right->GetIntValue());
    }
    else if(right->GetFloatEval()) {
      expression->SetFloatValue(left->GetIntValue() == right->GetFloatValue());
    }
    else {
      expression->SetIntValue(left->GetIntValue() == right->GetIntValue());
    }
    break;

  case NEQL_EXPR:
    if(left->GetFloatEval() && right->GetFloatEval()) {
      expression->SetFloatValue(left->GetFloatValue() != right->GetFloatValue());
    }
    else if(left->GetFloatEval()) {
      expression->SetFloatValue(left->GetFloatValue() != right->GetIntValue());
    }
    else if(right->GetFloatEval()) {
      expression->SetFloatValue(left->GetIntValue() != right->GetFloatValue());
    }
    else {
      expression->SetIntValue(left->GetIntValue() != right->GetIntValue());
    }
    break;

  case LES_EXPR:
    if(left->GetFloatEval() && right->GetFloatEval()) {
      expression->SetFloatValue(left->GetFloatValue() < right->GetFloatValue());
    }
    else if(left->GetFloatEval()) {
      expression->SetFloatValue(left->GetFloatValue() < right->GetIntValue());
    }
    else if(right->GetFloatEval()) {
      expression->SetFloatValue(left->GetIntValue() < right->GetFloatValue());
    }
    else {
      expression->SetIntValue(left->GetIntValue() < right->GetIntValue());
    }
    break;

  case GTR_EQL_EXPR:
    if(left->GetFloatEval() && right->GetFloatEval()) {
      expression->SetFloatValue(left->GetFloatValue() >= right->GetFloatValue());
    }
    else if(left->GetFloatEval()) {
      expression->SetFloatValue(left->GetFloatValue() >= right->GetIntValue());
    }
    else if(right->GetFloatEval()) {
      expression->SetFloatValue(left->GetIntValue() >= right->GetFloatValue());
    }
    else {
      expression->SetIntValue(left->GetIntValue() >= right->GetIntValue());
    }
    break;

  case LES_EQL_EXPR:
    if(left->GetFloatEval() && right->GetFloatEval()) {
      expression->SetFloatValue(left->GetFloatValue() <= right->GetFloatValue());
    }
    else if(left->GetFloatEval()) {
      expression->SetFloatValue(left->GetFloatValue() <= right->GetIntValue());
    }
    else if(right->GetFloatEval()) {
      expression->SetFloatValue(left->GetIntValue() <= right->GetFloatValue());
    }
    else {
      expression->SetIntValue(left->GetIntValue() <= right->GetIntValue());
    }
    break;

  case GTR_EXPR:
    if(left->GetFloatEval() && right->GetFloatEval()) {
      expression->SetFloatValue(left->GetFloatValue() > right->GetFloatValue());
    }
    else if(left->GetFloatEval()) {
      expression->SetFloatValue(left->GetFloatValue() > right->GetIntValue());
    }
    else if(right->GetFloatEval()) {
      expression->SetFloatValue(left->GetIntValue() > right->GetFloatValue());
    }
    else {
      expression->SetIntValue(left->GetIntValue() > right->GetIntValue());
    }
    break;

  case ADD_EXPR:
    if(left->GetFloatEval() && right->GetFloatEval()) {
      expression->SetFloatValue(left->GetFloatValue() + right->GetFloatValue());
    }
    else if(left->GetFloatEval()) {
      expression->SetFloatValue(left->GetFloatValue() + right->GetIntValue());
    }
    else if(right->GetFloatEval()) {
      expression->SetFloatValue(left->GetIntValue() + right->GetFloatValue());
    }
    else {
      expression->SetIntValue(left->GetIntValue() + right->GetIntValue());
    }
    break;

  case SUB_EXPR:
    if(left->GetFloatEval() && right->GetFloatEval()) {
      expression->SetFloatValue(left->GetFloatValue() - right->GetFloatValue());
    }
    else if(left->GetFloatEval()) {
      expression->SetFloatValue(left->GetFloatValue() - right->GetIntValue());
    }
    else if(right->GetFloatEval()) {
      expression->SetFloatValue(left->GetIntValue() - right->GetFloatValue());
    }
    else {
      expression->SetIntValue(left->GetIntValue() - right->GetIntValue());
    }
    break;

  case MUL_EXPR:
    if(left->GetFloatEval() && right->GetFloatEval()) {
      expression->SetFloatValue(left->GetFloatValue() * right->GetFloatValue());
    }
    else if(left->GetFloatEval()) {
      expression->SetFloatValue(left->GetFloatValue() * right->GetIntValue());
    }
    else if(right->GetFloatEval()) {
      expression->SetFloatValue(left->GetIntValue() * right->GetFloatValue());
    }
    else {
      expression->SetIntValue(left->GetIntValue() * right->GetIntValue());
    }
    break;

  case DIV_EXPR:
    if(left->GetFloatEval() && right->GetFloatEval()) {
      expression->SetFloatValue(left->GetFloatValue() / right->GetFloatValue());
    }
    else if(left->GetFloatEval()) {
      expression->SetFloatValue(left->GetFloatValue() / right->GetIntValue());
    }
    else if(right->GetFloatEval()) {
      expression->SetFloatValue(left->GetIntValue() / right->GetFloatValue());
    }
    else {
      expression->SetIntValue(left->GetIntValue() / right->GetIntValue());
    }
    break;

  case MOD_EXPR:
    if(left->GetIntValue() && right->GetIntValue()) {
      expression->SetIntValue(left->GetIntValue() % right->GetIntValue());
    }
    else {
      wcout << L"modulus operation requires integer values." << endl;
      is_error = true;
    }
    break;

  default:
    break;
  }
}
Esempio n. 2
0
void Runtime::Debugger::ProcessPrint(Print* print) {
  Expression* expression = print->GetExpression();
  EvaluateExpression(expression);

  if(!is_error) {
    switch(expression->GetExpressionType()) {
    case REF_EXPR:
      if(interpreter) {
        Reference* reference = static_cast<Reference*>(expression);
        while(reference->GetReference()) {
          reference = reference->GetReference();
        }

        const StackDclr& dclr_value =  static_cast<Reference*>(reference)->GetDeclaration();
        switch(dclr_value.type) {
        case CHAR_PARM:
          if(reference->GetIndices()) {
            wcout << L"cannot reference scalar variable" << endl;
          }
          else {
            wcout << L"print: type=Char, value=" << (wchar_t)reference->GetIntValue() << endl;
          }
          break;

        case INT_PARM:
          if(reference->GetIndices()) {
            wcout << L"cannot reference scalar variable" << endl;
          }
          else {
            wcout << L"print: type=Int, value=" << reference->GetIntValue() << endl;
          }
          break;


        case FLOAT_PARM:
          if(reference->GetIndices()) {
            wcout << L"cannot reference scalar variable" << endl;
          }
          else {
            wcout << L"print: type=Float, value=" << reference->GetFloatValue() << endl;
          }
          break;

        case BYTE_ARY_PARM:
          if(reference->GetIndices()) {
            wcout << L"print: type=Int, value=" << (unsigned char)reference->GetIntValue() << endl;
          }
          else {
            wcout << L"print: type=Byte[], value=" << reference->GetIntValue()
									<< L"(" << (void*)reference->GetIntValue() << L")";
            if(reference->GetArrayDimension()) {
              wcout << L", dimension=" << reference->GetArrayDimension() << L", size="
										<< reference->GetArraySize();
            }
            wcout << endl;
          }
          break;

        case CHAR_ARY_PARM:
          if(reference->GetIndices()) {
            wcout << L"print: type=Char, value=" << (wchar_t)reference->GetIntValue() << endl;
          }
          else {
            wcout << L"print: type=Char[], value=" << reference->GetIntValue()
									<< L"(" << (void*)reference->GetIntValue() << L")";
            if(reference->GetArrayDimension()) {
              wcout << L", dimension=" << reference->GetArrayDimension() << L", size="
										<< reference->GetArraySize();
            }
            wcout << endl;
          }
          break;

        case INT_ARY_PARM:
          if(reference->GetIndices()) {
            wcout << L"print: type=Int, value=" << reference->GetIntValue() << endl;
          }
          else {
            wcout << L"print: type=Int[], value=" << reference->GetIntValue()
									<< L"(" << (void*)reference->GetIntValue() << L")";
            if(reference->GetArrayDimension()) {
              wcout << L", dimension=" << reference->GetArrayDimension() << L", size="
										<< reference->GetArraySize();
            }
            wcout << endl;
          }
          break;

        case FLOAT_ARY_PARM:
          if(reference->GetIndices()) {
            wcout << L"print: type=Float, value=" << reference->GetFloatValue() << endl;
          }
          else {
            wcout << L"print: type=Float[], value=" << reference->GetIntValue()
									<< L"(" << (void*)reference->GetIntValue() << L")";
            if(reference->GetArrayDimension()) {
              wcout << L", dimension=" << reference->GetArrayDimension() << L", size="
										<< reference->GetArraySize();
            }
            wcout << endl;
          }
          break;

        case OBJ_PARM:
          if(ref_klass && ref_klass->GetName() == L"System.String") {
            long* instance = (long*)reference->GetIntValue();
            if(instance) {
              long* string_instance = (long*)instance[0];
              const wchar_t* char_string = (wchar_t*)(string_instance + 3);
              wcout << L"print: type=" << ref_klass->GetName() << L", value=\""
										<< char_string << L"\"" << endl;
            }
            else {
              wcout << L"print: type=" << (ref_klass ? ref_klass->GetName() : L"System.Base") << L", value="
										<< (void*)reference->GetIntValue() << endl;
            }
          }
          else {
            wcout << L"print: type=" << (ref_klass ? ref_klass->GetName() : L"System.Base") << L", value="
									<< (void*)reference->GetIntValue() << endl;
          }
          break;

        case OBJ_ARY_PARM:
          if(reference->GetIndices()) {
            StackClass* klass = MemoryManager::GetClass((long*)reference->GetIntValue());
            if(klass) {	      
              long* instance = (long*)reference->GetIntValue();
              if(instance) {
                wcout << L"print: type=" << klass->GetName() << L", value=" << (void*)reference->GetIntValue() << endl;
              }
              else {
                wcout << L"print: type=System.Base, value=" << (void*)reference->GetIntValue() << endl;
              }
            }
            else {
              wcout << L"print: type=System.Base, value=" << (void*)reference->GetIntValue() << endl;
            }
          }
          else {
            wcout << L"print: type=System.Base[], value=" << (void*)reference->GetIntValue();
            if(reference->GetArrayDimension()) {
              wcout << L", dimension=" << reference->GetArrayDimension() << L", size="
										<< reference->GetArraySize();
            }
            wcout << endl;
          }
          break;

        case FUNC_PARM: {
          StackClass* klass = cur_program->GetClass(reference->GetIntValue());
          if(klass) {
            wcout << L"print: type=Functon, class=" << klass->GetName() 
									<< L", method=" << PrintMethod(klass->GetMethod(reference->GetIntValue2())) << endl;
          }
				}
					break;
        }
      }
      else {
        wcout << L"program is not running." << endl;
        is_error = true;
      }
      break;

    case NIL_LIT_EXPR:
      wcout << L"print: type=Nil, value=Nil" << endl;
      break;

    case CHAR_LIT_EXPR:
      wcout << L"print: type=Char, value=" << (char)expression->GetIntValue() << endl;
      break;

    case INT_LIT_EXPR:
      wcout << L"print: type=Int, value=" << expression->GetIntValue() << endl;
      break;

    case FLOAT_LIT_EXPR:
      wcout << L"print: type=Float, value=" << expression->GetFloatValue() << endl;
      break;

    case BOOLEAN_LIT_EXPR:
      wcout << L"print: type=Bool, value=" << (expression->GetIntValue() ? "true" : "false" ) << endl;
      break;

    case AND_EXPR:
    case OR_EXPR:
    case EQL_EXPR:
    case NEQL_EXPR:
    case LES_EXPR:
    case GTR_EQL_EXPR:
    case LES_EQL_EXPR:
    case GTR_EXPR:
      wcout << L"print: type=Bool, value=" << (expression->GetIntValue() ? "true" : "false" ) << endl;
      break;

    case ADD_EXPR:
    case SUB_EXPR:
    case MUL_EXPR:
    case DIV_EXPR:
    case MOD_EXPR:
      if(expression->GetFloatEval()) {
        wcout << L"print: type=Float, value=" << expression->GetFloatValue() << endl;
      }
      else {
        wcout << L"print: type=Int, value=" << expression->GetIntValue() << endl;
      }
      break;

    case CHAR_STR_EXPR:
      break;
    }
  }
}