Example #1
0
int main(int argc, char **argv) {
  using std::ifstream;
  using std::string;
  Link *x = new Link();
  Link *y = new Link();

  int test = 1;

  ifstream ifp;
  ifstream tfp;

  string file = "data.txt";

  if(argc >= 2 ) {
    file = argv[argc - 1];
    test = 0;
  }

  ifp.open(file.c_str());

  int value;
  while(ifp >> value) { x->prepend(value); }

  ifp.close();
  if(test == 1) { x->display(); }

  tfp.open(file.c_str());

  while(tfp >> value) { y->append(value); }

  tfp.close();
  if(test == 1) { y->display(); }

  x->display();
  y->display();
  //add->display();

  return 0;
}
Example #2
0
void StabVM::compile(const uint8_t *bytecode) {
  size_t pc = 0;        /* program counter */
  Link *tip = nullptr;
  Link *fork_ptr = nullptr;

  uint8_t cmd;
  uint8_t arg0, arg1;
  size_t chain  = 0;    /* currently compiling chain */
  size_t output = 0;    /* used outputs counter */
  size_t fork   = 0;    /* used forks counter */
  size_t inv    = 0;    /* used inverters counter */

  pc = 0;
  while(true) {
    cmd = bytecode[pc];

    switch (cmd){
    case END:
      return;

    case INPUT:
      arg0 = bytecode[pc+1];
      vmDbgCheck(arg0 < ACS_INPUT_ENUM_END);
      vmDbgCheck(chain < TOTAL_INPUT_CNT);
      tip = input_pool[chain].compile(&acs_in.ch[arg0]);
      exec_chain[chain] = tip;
      chain += 1;
      pc += 2;
      break;

    case TERM:
      tip->append(&terminator);
      tip = nullptr;
      pc += 1;
      break;

    case NEG:
      vmDbgCheck(inv < TOTAL_INV_CNT);
      tip = tip->append(&inverter_pool[inv]);
      inv += 1;
      pc += 1;
      break;

    case PID:
      arg0 = bytecode[pc+1];
      arg1 = bytecode[pc+2];
      vmDbgCheck(arg0 < TOTAL_PID_CNT);
      vmDbgCheck(arg1 < ACS_INPUT_ENUM_END);
      tip = tip->append(pid_pool[arg0].compile(&acs_in.ch[arg1], &this->dT));
      pc += 3;
      break;

    case SUM:
      arg0 = bytecode[pc+1];
      vmDbgCheck(arg0 < TOTAL_SUM_CNT);
      tip = tip->append(&sum_pool[arg0]);
      pc += 2;
      break;

    case SCALE:
      arg0 = bytecode[pc+1];
      vmDbgCheck(arg0 < TOTAL_SCALE_CNT);
      tip = tip->append(&scale_pool[arg0]);
      pc += 2;
      break;

    case FORK:
      vmDbgCheck(nullptr == fork_ptr); /* nested forks forbidden */
      vmDbgCheck(fork < TOTAL_FORK_CNT);
      tip = tip->append(&fork_pool[fork]);
      fork_ptr = tip;
      fork += 1;
      pc += 1;
      break;

    case FORK_RET:
      vmDbgCheck(nullptr != fork_ptr); /* there is no fork return pending */
      tip = fork_ptr;
      pc += 1;
      break;

    case OUTPUT:
      arg0 = bytecode[pc+1];
      vmDbgCheck(output < TOTAL_OUT_CNT);
      vmDbgCheck(arg0 < IMPACT_ENUM_END);
      tip = tip->append(out_pool[output].compile(&impact.ch[arg0]));
      output += 1;
      pc += 2;
      break;

    default:
      vmDbgPanic("illegal instruction");
      break;
    }
  }
}