Example #1
0
File: osvm.c Project: osandov/osos
int main(int argc, const char *argv[])
{
    int ret = 0;
    if (argc > 2) {
        fprintf(stderr, "USAGE: %s [FILE]\n", argv[0]);
        return 1;
    }

    vm = VM_create(buffer_size);

    if (argc == 2) {
        FILE *file = fopen(argv[1], "r");
        if (!file) {
            fprintf(stderr, "%s: %s: No such file or directory\n",
                    argv[0], argv[1]);
            ret = 1;
            goto done;
        }
        read_bytecode(file, VM_get_instructions(vm));
        fclose(file);
    } else if (argc == 1) {
        read_bytecode(stdin, VM_get_instructions(vm));
    }

    if (!vm) {
        fprintf(stderr, "%s: Out of memory\n", argv[0]);
        ret = 1;
        goto done;
    }

    ret = VM_execute(vm);
    VM_print_stack(vm);

done:
    VM_destroy(vm);
    return ret;
}
Example #2
0
File: reader.cpp Project: AmkG/hl
// read a sequence of bytecodes in a list left on the stack
void read_sequence(Process & proc, std::istream & in) {
  char c;
  proc.stack.push(Object::nil()); // head
  proc.stack.push(Object::nil()); // tail
  while (1) {
    skip_seps(in);
    c = in.peek();
    if (!in.eof() && c == bc_start) {
      read_bytecode(proc, in);
      Object::ref c2 = Object::to_ref(proc.create<Cons>());
      if (proc.stack.top(3)==Object::nil()) // test the head
        proc.stack.top(3) = c2; // new head
      else
        scdr(proc.stack.top(2), c2); // cdr of the tail
      scar(c2, proc.stack.top()); proc.stack.pop();
      proc.stack.top() = c2; // new tail
    }
    else 
      break;
  }
  proc.stack.pop(); // remove tail and leave head
}