experiments/little-smalltalk/run.cc
author Tony Garnock-Jones <tonygarnockjones@gmail.com>
Sat, 04 Aug 2018 11:56:53 +0100
changeset 431 97c12bf829d8
child 432 527bdc1042a9
permissions -rw-r--r--
WIP C++ simple interpreter, product of yesterday evening's work
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
431
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
     1
#include <cstring>
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
     2
#include <cstdio>
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
     3
#include <cstdlib>
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
     4
#include <cstdint>
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
     5
#include <ctime>
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
     6
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
     7
#include <string>
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
     8
#include <iostream>
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
     9
#include <iomanip>
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    10
#include <fstream>
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    11
#include <vector>
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    12
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    13
#include <netinet/in.h>
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    14
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    15
using namespace std;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    16
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    17
typedef void *obj;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    18
typedef intptr_t smi;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    19
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    20
static inline int isSmi(obj o) { return ((intptr_t) o) & 1; }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    21
static inline obj mkSmi(intptr_t v) { return (obj) ((v << 1) | 1); }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    22
static inline smi unSmi(obj o) { return ((smi) o) >> 1; }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    23
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    24
static inline intptr_t rawCount(obj o) { return ((intptr_t *) o)[0]; }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    25
static inline int isBytes(obj o) { return rawCount(o) & 1; }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    26
static inline unsigned slotCount(obj o) { return rawCount(o) >> 1; }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    27
static inline unsigned bytesCount(obj o) { return rawCount(o) >> 1; }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    28
static inline obj slotAt(obj o, unsigned i) { return ((obj *) o)[i + 2]; }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    29
static inline void slotAtPut(obj o, unsigned i, obj v) { ((obj *) o)[i + 2] = v; }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    30
static inline uint8_t *bvBytes(obj o) { return (uint8_t *) &(((obj *) o)[2]); }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    31
static inline string bvString(obj o) { return string((char *) bvBytes(o), bytesCount(o)); }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    32
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    33
static inline intptr_t rawCount(unsigned count, int isBs) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    34
  return (intptr_t) count << 1 | (isBs & 1);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    35
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    36
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    37
static inline void _fillWith(obj o, unsigned n, obj v) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    38
  for (unsigned i = 0; i < n; i++) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    39
    slotAtPut(o, i, v);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    40
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    41
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    42
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    43
static int32_t nextInt(istream &f) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    44
  int32_t i;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    45
  f.read((char *) &i, sizeof(int32_t));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    46
  return ntohl(i);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    47
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    48
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    49
struct VM;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    50
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    51
typedef obj (*primitive_handler_t)(unsigned, VM &, obj);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    52
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    53
static obj unhandledPrimitive(unsigned, VM &, obj);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    54
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    55
struct VM {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    56
  obj *allocBase;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    57
  obj *allocPtr;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    58
  obj *allocLimit;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    59
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    60
  obj _nil;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    61
  obj _true;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    62
  obj _false;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    63
  obj _Array;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    64
  obj _Block;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    65
  obj _Context;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    66
  obj _Integer;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    67
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    68
  primitive_handler_t primitiveTable[256];
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    69
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    70
  inline obj objClass(obj o) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    71
    return isSmi(o) ? _Integer : ((obj *) o)[1];
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    72
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    73
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    74
  string className(obj c) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    75
    if (c == _nil) return "(nil)";
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    76
    return bvString(slotAt(c, 0));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    77
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    78
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    79
  string objClassName(obj o) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    80
    return className(objClass(o));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    81
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    82
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    83
  VM() {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    84
    unsigned heapSize = 1048576000;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    85
    allocBase = allocPtr = new obj[heapSize];
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    86
    allocLimit = &allocPtr[heapSize];
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    87
    for (int i = 0; i < 256; i++) primitiveTable[i] = unhandledPrimitive;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    88
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    89
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    90
  void gc() {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    91
    cerr << "GC! urk" << endl;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    92
    exit(1);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    93
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    94
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    95
  obj allocObj(unsigned nSlots, obj klass) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    96
  retry:
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    97
    obj result = (obj) allocPtr;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    98
    *allocPtr++ = (obj) rawCount(nSlots, 0);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    99
    *allocPtr++ = klass;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   100
    allocPtr += nSlots;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   101
    if (allocPtr >= allocLimit) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   102
      allocPtr = (obj *) result;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   103
      gc();
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   104
      goto retry;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   105
    }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   106
    return result;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   107
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   108
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   109
  obj allocBytes(unsigned nBytes, obj klass) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   110
  retry:
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   111
    obj result = (obj) allocPtr;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   112
    *allocPtr++ = (obj) rawCount(nBytes, 1);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   113
    *allocPtr++ = klass;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   114
    nBytes = (nBytes + sizeof(obj) - 1) & ~(sizeof(obj) - 1);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   115
    allocPtr = (obj *) (((uint8_t *) allocPtr) + nBytes);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   116
    if (allocPtr >= allocLimit) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   117
      allocPtr = (obj *) result;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   118
      gc();
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   119
      goto retry;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   120
    }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   121
    return result;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   122
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   123
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   124
  obj allocBytes(string const &s, obj klass) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   125
    obj o = allocBytes(s.length(), klass);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   126
    memcpy(bvBytes(o), s.c_str(), s.length());
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   127
    return o;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   128
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   129
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   130
  obj allocRawArray(unsigned n) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   131
    return allocObj(n, _Array);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   132
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   133
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   134
  obj allocArray(unsigned n, obj v) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   135
    obj o = allocRawArray(n);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   136
    _fillWith(o, n, v);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   137
    return o;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   138
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   139
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   140
  obj allocArray(unsigned n) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   141
    return allocArray(n, _nil);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   142
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   143
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   144
  void print(ostream &f, obj o) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   145
    if (isSmi(o)) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   146
      f << unSmi(o);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   147
      return;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   148
    }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   149
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   150
    string className = objClassName(o);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   151
    int isString = className.compare("String") == 0;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   152
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   153
    if (!isString) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   154
      f << className;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   155
    }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   156
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   157
    if (isBytes(o)) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   158
      f << (isString ? '\"' : '{');
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   159
      ios_base::fmtflags saved(f.flags());
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   160
      char fill = f.fill('0');
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   161
      f << hex;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   162
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   163
      for (int i = 0; i < bytesCount(o); i++) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   164
        char c = bvBytes(o)[i];
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   165
        if (isString) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   166
          switch (c) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   167
            case '\"': f << "\\\""; break;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   168
            case '\n': f << "\\n"; break;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   169
            default: f << c; break;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   170
          }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   171
        } else {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   172
          f << setw(2) << ((unsigned) c & 0xff);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   173
        }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   174
      }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   175
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   176
      f.flags(saved);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   177
      f.fill(fill);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   178
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   179
      f << (isString ? '\"' : '}');
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   180
    } else {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   181
    }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   182
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   183
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   184
  int loadImage(string const &filename) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   185
    ifstream f(filename);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   186
    vector<obj> table;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   187
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   188
    if (!f.is_open()) return 0;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   189
    if (nextInt(f) != 1 || !f) return 0; // wrong image version
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   190
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   191
    while (true) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   192
      int32_t len = nextInt(f);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   193
      if (!f) break;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   194
      int32_t code = nextInt(f);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   195
      int32_t classIndex = nextInt(f);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   196
      int32_t oopCount = nextInt(f);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   197
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   198
      switch (code) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   199
        case 0:
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   200
          if (len != 5) return 0; // weird SmallInt object length
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   201
          if (oopCount != 0) return 0; // SmallInt doesn't get to have slots
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   202
          table.push_back(mkSmi(nextInt(f)));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   203
          break;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   204
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   205
        case 1: {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   206
          int32_t byteCount = len - oopCount - 4;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   207
          obj bv = allocBytes(byteCount, mkSmi(classIndex));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   208
          f.read((char *) bvBytes(bv), byteCount);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   209
          table.push_back(bv);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   210
          break;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   211
        }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   212
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   213
        case 2: {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   214
          obj o = allocObj(oopCount, mkSmi(classIndex));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   215
          for (int i = 0; i < oopCount; i++) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   216
            slotAtPut(o, i, mkSmi(nextInt(f)));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   217
          }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   218
          table.push_back(o);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   219
          break;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   220
        }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   221
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   222
        default:
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   223
          return 0; // unhandled code
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   224
      }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   225
    }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   226
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   227
    for (vector<obj>::iterator it = table.begin(); it != table.end(); ++it) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   228
      obj o = *it;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   229
      if (!isSmi(o)) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   230
        unsigned count = 1 + (isBytes(o) ? 0 : slotCount(o));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   231
        for (unsigned i = 0; i < count; i++) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   232
          (((obj *) o)[i + 1]) = table[unSmi(((obj *) o)[i + 1])];
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   233
        }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   234
      }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   235
    }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   236
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   237
    _nil = table[0];
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   238
    _true = table[1];
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   239
    _false = table[2];
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   240
    _Array = table[3];
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   241
    _Block = table[4];
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   242
    _Context = table[5];
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   243
    _Integer = table[6];
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   244
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   245
    return 1;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   246
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   247
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   248
  obj searchClassMethodDictionary(obj c, obj selector) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   249
    obj methods = slotAt(c, 2);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   250
    unsigned selectorLen = bytesCount(selector);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   251
    char const *selectorBytes = (char const *) bvBytes(selector);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   252
    for (unsigned i = 0; i < slotCount(methods); i++) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   253
      obj m = slotAt(methods, i);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   254
      obj mname = slotAt(m, 0);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   255
      // cerr << "  " << selectorBytes << " =?= " << bvString(mname) << endl;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   256
      if ((bytesCount(mname) == selectorLen) &&
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   257
          (memcmp(bvBytes(mname), selectorBytes, selectorLen) == 0)) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   258
        return m;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   259
      }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   260
    }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   261
    return 0;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   262
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   263
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   264
  obj buildContext(obj prevCtx, obj args, obj method) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   265
    unsigned tempCount = (unsigned) unSmi(slotAt(method, 4));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   266
    unsigned maxStack = (unsigned) unSmi(slotAt(method, 3));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   267
    obj ctx = allocObj(7, _Context);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   268
    slotAtPut(ctx, 0, method);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   269
    slotAtPut(ctx, 1, args);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   270
    slotAtPut(ctx, 2, allocArray(tempCount));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   271
    slotAtPut(ctx, 3, allocArray(maxStack));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   272
    slotAtPut(ctx, 4, mkSmi(0));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   273
    slotAtPut(ctx, 5, mkSmi(0));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   274
    slotAtPut(ctx, 6, prevCtx);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   275
    return ctx;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   276
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   277
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   278
  obj __ctx, __method, __args, __temps, __stack, __prevCtx, __receiver, __literals;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   279
  unsigned __ip, __stackTop;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   280
  uint8_t *__bytecode;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   281
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   282
  void push(obj v) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   283
    slotAtPut(__stack, __stackTop++, v);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   284
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   285
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   286
  obj pop() {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   287
    return slotAt(__stack, --__stackTop);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   288
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   289
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   290
  obj peek() {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   291
    return slotAt(__stack, __stackTop - 1);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   292
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   293
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   294
  uint8_t nextByte() {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   295
    return __bytecode[__ip++];
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   296
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   297
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   298
  obj popArray(uint8_t count) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   299
    obj a = allocRawArray(count);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   300
    __stackTop -= count;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   301
    for (int i = 0; i < count; i++) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   302
      slotAtPut(a, i, slotAt(__stack, __stackTop + i));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   303
    }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   304
    return a;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   305
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   306
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   307
  void loadContext(obj ctx);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   308
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   309
  void loadContextAndPush(obj ctx, obj v) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   310
    loadContext(ctx);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   311
    push(v);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   312
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   313
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   314
  void storeRegisters() {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   315
    slotAtPut(__ctx, 4, mkSmi(__ip));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   316
    slotAtPut(__ctx, 5, mkSmi(__stackTop));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   317
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   318
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   319
  obj lookupMethod(obj c, obj selector) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   320
    while (c != _nil) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   321
      // cerr << "Searching in " << className(c) << " for "; print(cerr, selector); cerr << endl;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   322
      obj m = searchClassMethodDictionary(c, selector);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   323
      if (m != 0) return m;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   324
      c = slotAt(c, 1);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   325
    }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   326
    return 0;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   327
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   328
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   329
  void sendMessage(obj c, obj newArgs, obj selector) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   330
    storeRegisters();
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   331
    // cerr << "Sending " << bvString(selector) << " via " << className(c) << " to ";
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   332
    // print(cerr, slotAt(newArgs, 0));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   333
    // cerr << endl;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   334
    obj method = lookupMethod(c, selector);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   335
    if (method == 0) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   336
      cerr << "DNU ";
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   337
      print(cerr, slotAt(newArgs, 0));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   338
      cerr << ' ';
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   339
      print(cerr, selector);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   340
      cerr << endl;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   341
      exit(2);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   342
    }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   343
    loadContext(buildContext(__ctx, newArgs, method));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   344
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   345
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   346
  void interpret() {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   347
    while (__ctx != _nil) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   348
      uint8_t opcode, arg;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   349
      opcode = nextByte();
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   350
      arg = opcode & 0xf;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   351
      opcode = opcode >> 4;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   352
      if (opcode == 0) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   353
        opcode = arg;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   354
        arg = nextByte();
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   355
      }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   356
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   357
      // cout << (int) opcode << ' ' << (int) arg;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   358
      // for (int i = 0; i < __stackTop; i++) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   359
      //   cout << ' ';
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   360
      //   print(cout, slotAt(__stack, i));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   361
      // }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   362
      // cout << endl;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   363
      // cout << flush;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   364
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   365
      switch (opcode) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   366
        case 1: push(slotAt(__receiver, arg)); continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   367
        case 2: push(slotAt(__args, arg)); continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   368
        case 3: push(slotAt(__temps, arg)); continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   369
        case 4: push(slotAt(__literals, arg)); continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   370
        case 5:
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   371
          if (arg < 10) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   372
            push(mkSmi(arg));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   373
          } else {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   374
            switch (arg) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   375
              case 10: push(_nil); break;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   376
              case 11: push(_true); break;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   377
              case 12: push(_false); break;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   378
            }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   379
          }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   380
          continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   381
        case 6: slotAtPut(__receiver, arg, peek()); continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   382
        case 7: slotAtPut(__temps, arg, peek()); continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   383
        case 8: push(popArray(arg)); continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   384
        case 9: {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   385
          obj newArgs = pop();
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   386
          sendMessage(objClass(slotAt(newArgs, 0)), newArgs, slotAt(__literals, arg));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   387
          continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   388
        }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   389
        case 10:
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   390
          switch (arg) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   391
            case 0: push((pop() == _nil) ? _true : _false); continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   392
            case 1: push((pop() != _nil) ? _true : _false); continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   393
          }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   394
        case 11: {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   395
          obj j = pop();
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   396
          obj i = pop();
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   397
          if (isSmi(i) && isSmi(j)) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   398
            switch (arg) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   399
              case 0: push((unSmi(i) < unSmi(j)) ? _true : _false); continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   400
              case 1: push((unSmi(i) <= unSmi(j)) ? _true : _false); continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   401
              case 2: push(mkSmi(unSmi(i) + unSmi(j))); continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   402
            }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   403
          } else {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   404
            obj newArgs = allocRawArray(2);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   405
            slotAtPut(newArgs, 0, i);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   406
            slotAtPut(newArgs, 1, j);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   407
            obj selector;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   408
            switch (arg) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   409
              case 0: selector = allocBytes("<", _nil); break;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   410
              case 1: selector = allocBytes("<=", _nil); break;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   411
              case 2: selector = allocBytes("+", _nil); break;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   412
            }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   413
            sendMessage(objClass(i), newArgs, selector);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   414
            continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   415
          }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   416
        }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   417
        case 12: {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   418
          uint8_t target = nextByte();
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   419
          obj block = allocObj(10, _Block);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   420
          slotAtPut(block, 0, __method);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   421
          slotAtPut(block, 1, __args);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   422
          slotAtPut(block, 2, __temps);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   423
          slotAtPut(block, 3, __stack);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   424
          slotAtPut(block, 4, mkSmi(__ip));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   425
          slotAtPut(block, 5, mkSmi(0));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   426
          slotAtPut(block, 6, __prevCtx);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   427
          slotAtPut(block, 7, mkSmi(arg));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   428
          slotAtPut(block, 8, __ctx);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   429
          slotAtPut(block, 9, mkSmi(__ip));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   430
          push(block);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   431
          __ip = target;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   432
          continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   433
        }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   434
        case 13: {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   435
          uint8_t primNumber = nextByte();
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   436
          // cerr << "  primNumber = " << (int) primNumber << endl;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   437
          switch (primNumber) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   438
            case 6: {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   439
              loadContext(pop());
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   440
              continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   441
            }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   442
            case 8: {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   443
              obj block = pop();
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   444
              unsigned argloc = unSmi(slotAt(block, 7));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   445
              unsigned argcount = arg - 1;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   446
              for (unsigned i = 0; i < argcount; i++) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   447
                slotAtPut(slotAt(block, 2),
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   448
                          argloc + i,
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   449
                          slotAt(__stack, __stackTop - argcount + i));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   450
              }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   451
              __stackTop = __stackTop - argcount;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   452
              storeRegisters();
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   453
              obj blockCtx = allocObj(10, _Block);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   454
              slotAtPut(blockCtx, 0, slotAt(block, 0));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   455
              slotAtPut(blockCtx, 1, slotAt(block, 1));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   456
              slotAtPut(blockCtx, 2, slotAt(block, 2));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   457
              slotAtPut(blockCtx, 3, allocArray(slotCount(slotAt(block, 3))));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   458
              slotAtPut(blockCtx, 4, slotAt(block, 9));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   459
              slotAtPut(blockCtx, 5, mkSmi(0));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   460
              slotAtPut(blockCtx, 6, slotAt(__ctx, 6));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   461
              slotAtPut(blockCtx, 7, slotAt(block, 7));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   462
              slotAtPut(blockCtx, 8, slotAt(block, 8));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   463
              slotAtPut(blockCtx, 9, slotAt(block, 9));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   464
              loadContext(blockCtx);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   465
              continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   466
            }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   467
            case 34: return;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   468
            case 35: push(__ctx); continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   469
            default:
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   470
              push(primitiveTable[primNumber](primNumber, *this, popArray(arg)));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   471
              continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   472
          }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   473
        }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   474
        case 14: push(slotAt(objClass(__receiver), arg + 5)); continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   475
        case 15:
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   476
          switch (arg) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   477
            case 1: loadContextAndPush(__prevCtx, __receiver); continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   478
            case 2: loadContextAndPush(__prevCtx, pop()); continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   479
            case 3: loadContextAndPush(slotAt(slotAt(__ctx, 8), 6), pop()); continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   480
            case 4: push(peek()); continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   481
            case 5: pop(); continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   482
            case 6: __ip = nextByte(); continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   483
            case 7: {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   484
              unsigned target = nextByte();
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   485
              if (pop() == _true) __ip = target;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   486
              continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   487
            }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   488
            case 8: {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   489
              unsigned target = nextByte();
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   490
              if (pop() == _false) __ip = target;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   491
              continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   492
            }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   493
            case 11: {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   494
              obj selector = slotAt(__literals, nextByte());
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   495
              obj newArgs = pop();
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   496
              obj definingClass = slotAt(__method, 5);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   497
              obj super = slotAt(definingClass, 1);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   498
              sendMessage(super, newArgs, selector);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   499
              continue;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   500
            }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   501
            default:
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   502
              cerr << "Unhandled opcode 15 arg " << (int) arg << endl;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   503
              exit(1);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   504
          }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   505
        default:
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   506
          cerr << "Unhandled opcode " << (int) opcode << ", " << (int) arg << endl;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   507
          exit(1);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   508
      }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   509
    }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   510
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   511
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   512
  void installPrimitive(unsigned primNumber, primitive_handler_t handler) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   513
    primitiveTable[primNumber] = handler;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   514
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   515
};
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   516
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   517
void VM::loadContext(obj ctx) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   518
  __ctx = ctx;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   519
  if (ctx != _nil) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   520
    __method = slotAt(ctx, 0);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   521
    __args = slotAt(ctx, 1);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   522
    __temps = slotAt(ctx, 2);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   523
    __stack = slotAt(ctx, 3);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   524
    __ip = unSmi(slotAt(ctx, 4));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   525
    __stackTop = unSmi(slotAt(ctx, 5));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   526
    __prevCtx = slotAt(ctx, 6);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   527
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   528
    __receiver = slotAt(__args, 0);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   529
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   530
    __bytecode = bvBytes(slotAt(__method, 1));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   531
    __literals = slotAt(__method, 2);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   532
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   533
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   534
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   535
static obj unhandledPrimitive(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   536
  cerr << "Primitive " << primNumber << " is unhandled" << endl;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   537
  exit(1);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   538
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   539
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   540
static obj prim_eq(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   541
  return (slotAt(args, 0) == slotAt(args, 1)) ? vm._true : vm._false;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   542
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   543
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   544
static obj prim_getClass(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   545
  return vm.objClass(slotAt(args, 0));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   546
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   547
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   548
static obj prim_objectSize(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   549
  obj o = slotAt(args, 0);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   550
  if (isSmi(o)) return mkSmi(0);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   551
  if (isBytes(o)) return mkSmi(bytesCount(o));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   552
  return mkSmi(slotCount(o));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   553
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   554
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   555
static obj prim_slotAtPut(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   556
  slotAtPut(slotAt(args, 1), unSmi(slotAt(args, 2)) - 1, slotAt(args, 0));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   557
  return slotAt(args, 1);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   558
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   559
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   560
static obj prim_mkObject(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   561
  obj r = vm.allocObj(unSmi(slotAt(args, 1)), slotAt(args, 0));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   562
  _fillWith(r, slotCount(r), vm._nil);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   563
  return r;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   564
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   565
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   566
static obj prim_numericQuotient(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   567
  return mkSmi(unSmi(slotAt(args, 0)) / unSmi(slotAt(args, 1)));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   568
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   569
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   570
static obj prim_numericModulo(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   571
  return mkSmi(unSmi(slotAt(args, 0)) % unSmi(slotAt(args, 1)));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   572
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   573
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   574
static obj prim_numericEq(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   575
  return (slotAt(args, 0) == slotAt(args, 1)) ? vm._true : vm._false;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   576
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   577
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   578
static obj prim_numericSub(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   579
  return mkSmi(unSmi(slotAt(args, 0)) - unSmi(slotAt(args, 1)));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   580
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   581
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   582
static obj prim_numericMul(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   583
  // TODO overflow checks
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   584
  return mkSmi(unSmi(slotAt(args, 0)) * unSmi(slotAt(args, 1)));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   585
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   586
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   587
static obj prim_mkBytes(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   588
  return vm.allocBytes(unSmi(slotAt(args, 1)), slotAt(args, 0));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   589
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   590
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   591
static obj prim_bytesRef(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   592
  return mkSmi(bvBytes(slotAt(args, 0))[unSmi(slotAt(args, 1)) - 1]);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   593
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   594
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   595
static obj prim_bytesSet(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   596
  bvBytes(slotAt(args, 1))[unSmi(slotAt(args, 2)) - 1] = unSmi(slotAt(args, 0));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   597
  return slotAt(args, 1);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   598
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   599
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   600
static obj prim_bytesAppend(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   601
  obj b = slotAt(args, 0);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   602
  obj a = slotAt(args, 1);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   603
  obj r = vm.allocBytes(bytesCount(a) + bytesCount(b), vm.objClass(a));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   604
  memcpy(bvBytes(r), bvBytes(a), bytesCount(a));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   605
  memcpy(bvBytes(r) + bytesCount(a), bvBytes(b), bytesCount(b));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   606
  return r;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   607
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   608
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   609
static obj prim_bytesCmp(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   610
  obj a = slotAt(args, 0);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   611
  obj b = slotAt(args, 1);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   612
  int alen = bytesCount(a);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   613
  int blen = bytesCount(a);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   614
  int minlen = alen < blen ? alen : blen;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   615
  int result = memcmp(bvBytes(a), bvBytes(b), minlen);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   616
  if (result == 0) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   617
    result = alen - blen;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   618
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   619
  if (result < 0) return mkSmi(-1);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   620
  if (result > 0) return mkSmi(1);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   621
  return mkSmi(0);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   622
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   623
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   624
static obj prim_slotAt(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   625
  return slotAt(slotAt(args, 0), unSmi(slotAt(args, 1)) - 1);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   626
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   627
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   628
static obj prim_extendObj(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   629
  obj v = slotAt(args, 0);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   630
  obj o = slotAt(args, 1);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   631
  obj r = vm.allocObj(slotCount(o) + 1, vm.objClass(o));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   632
  for (int i = 0; i < slotCount(o); i++) slotAtPut(r, i, slotAt(o, i));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   633
  slotAtPut(r, slotCount(o), v);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   634
  return r;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   635
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   636
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   637
static obj prim_createWindow(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   638
  cerr << "Creating window ";
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   639
  vm.print(cerr, slotAt(args, 0));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   640
  cerr << endl;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   641
  return vm.allocObj(0, slotAt(args, 0));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   642
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   643
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   644
static obj prim_showWindow(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   645
  cerr << "Show/hide window " << slotAt(args, 0) << " " << (slotAt(args, 1) == vm._true) << endl;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   646
  return slotAt(args, 1);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   647
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   648
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   649
static obj prim_setContentPane(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   650
  cerr << "Setting content pane " << slotAt(args, 0) << endl;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   651
  return slotAt(args, 0);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   652
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   653
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   654
static obj prim_setWindowSize(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   655
  cout << slotAt(args, 0) << "prim_setWindowSize:"
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   656
       << " height " << unSmi(slotAt(args, 1))
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   657
       << " width " << unSmi(slotAt(args, 2)) << endl;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   658
  return slotAt(args, 0);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   659
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   660
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   661
static obj prim_setWindowMenu(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   662
  cout << slotAt(args, 0) << "prim_setWindowMenu" << endl;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   663
  return slotAt(args, 0);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   664
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   665
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   666
static obj prim_setWindowTitle(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   667
  cout << slotAt(args, 0) << "prim_setWindowTitle: " << bvString(slotAt(args, 1)) << endl;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   668
  return slotAt(args, 0);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   669
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   670
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   671
static obj prim_repaintWindow(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   672
  cout << slotAt(args, 0) << "prim_repaintWindow" << endl;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   673
  return slotAt(args, 0);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   674
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   675
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   676
static obj prim_createButton(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   677
  cerr << "Creating button ";
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   678
  vm.print(cerr, slotAt(args, 0));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   679
  cerr << " with label " << bvString(slotAt(args, 1)) << endl;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   680
  return vm.allocObj(0, slotAt(args, 0));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   681
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   682
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   683
static obj prim_createTextArea(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   684
  cerr << "Creating textarea ";
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   685
  vm.print(cerr, slotAt(args, 0));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   686
  cerr << endl;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   687
  return vm.allocObj(0, slotAt(args, 0));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   688
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   689
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   690
static obj prim_updateTextArea(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   691
  cout << slotAt(args, 0) << "prim_updateTextArea: " << bvString(slotAt(args, 1)) << endl;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   692
  return slotAt(args, 1);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   693
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   694
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   695
static obj prim_createMenu(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   696
  cerr << "Creating menu ";
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   697
  vm.print(cerr, slotAt(args, 0));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   698
  cerr << " with title " << bvString(slotAt(args, 1)) << endl;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   699
  return vm.allocObj(0, slotAt(args, 0));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   700
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   701
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   702
static obj prim_createMenuItem(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   703
  cerr << "Creating menu item ";
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   704
  vm.print(cerr, slotAt(args, 0));
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   705
  cerr << " with title " << bvString(slotAt(args, 1)) << endl;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   706
  return slotAt(args, 0);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   707
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   708
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   709
static obj prim_closeWindowHandler(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   710
  cerr << "Ignoring prim_closeWindowHandler" << endl;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   711
  return slotAt(args, 0);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   712
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   713
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   714
static struct timespec ts_start;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   715
static obj prim_getMilliseconds(unsigned primNumber, VM &vm, obj args) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   716
  struct timespec ts_end;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   717
  clock_gettime(CLOCK_MONOTONIC, &ts_end);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   718
  intptr_t delta = (ts_end.tv_sec - ts_start.tv_sec) * 1000;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   719
  delta += (ts_end.tv_nsec / 1000000) - (ts_start.tv_nsec / 1000000);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   720
  return mkSmi(delta);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   721
}
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   722
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   723
int main(int argc, char *argv[]) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   724
  VM vm;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   725
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   726
  clock_gettime(CLOCK_MONOTONIC, &ts_start);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   727
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   728
  vm.installPrimitive(1, prim_eq);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   729
  vm.installPrimitive(2, prim_getClass);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   730
  vm.installPrimitive(4, prim_objectSize);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   731
  vm.installPrimitive(5, prim_slotAtPut);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   732
  vm.installPrimitive(7, prim_mkObject);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   733
  vm.installPrimitive(11, prim_numericQuotient);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   734
  vm.installPrimitive(12, prim_numericModulo);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   735
  vm.installPrimitive(14, prim_numericEq);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   736
  vm.installPrimitive(15, prim_numericMul);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   737
  vm.installPrimitive(16, prim_numericSub);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   738
  vm.installPrimitive(20, prim_mkBytes);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   739
  vm.installPrimitive(21, prim_bytesRef);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   740
  vm.installPrimitive(22, prim_bytesSet);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   741
  vm.installPrimitive(24, prim_bytesAppend);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   742
  vm.installPrimitive(26, prim_bytesCmp);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   743
  vm.installPrimitive(30, prim_slotAt);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   744
  vm.installPrimitive(31, prim_extendObj);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   745
  vm.installPrimitive(60, prim_createWindow);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   746
  vm.installPrimitive(61, prim_showWindow);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   747
  vm.installPrimitive(62, prim_setContentPane);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   748
  vm.installPrimitive(63, prim_setWindowSize);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   749
  vm.installPrimitive(64, prim_setWindowMenu);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   750
  vm.installPrimitive(65, prim_setWindowTitle);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   751
  vm.installPrimitive(66, prim_repaintWindow);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   752
  vm.installPrimitive(71, prim_createButton);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   753
  vm.installPrimitive(73, prim_createTextArea);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   754
  vm.installPrimitive(82, prim_updateTextArea);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   755
  vm.installPrimitive(90, prim_createMenu);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   756
  vm.installPrimitive(91, prim_createMenuItem);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   757
  vm.installPrimitive(118, prim_closeWindowHandler);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   758
  vm.installPrimitive(119, prim_getMilliseconds);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   759
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   760
  if (!vm.loadImage("SmallWorld/src/image")) {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   761
    cerr << "Could not load image" << endl;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   762
    return 1;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   763
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   764
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   765
  cout << "Loaded!" << endl;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   766
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   767
  {
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   768
    string bootCode = "[SmallWorld startUp. \
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   769
Transcript show: 0 tinyBenchmarks.] value";
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   770
    // "SmallWorld startUp";
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   771
    obj _String = vm.objClass(slotAt(vm.objClass(vm._true), 0)); // class String
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   772
    obj code = vm.allocBytes(bootCode, _String);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   773
    obj selector = vm.allocBytes("doIt", _String);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   774
    obj args = vm.allocRawArray(1);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   775
    slotAtPut(args, 0, code);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   776
    obj doIt = vm.searchClassMethodDictionary(vm.objClass(code), selector);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   777
    obj ctx = vm.buildContext(vm._nil, args, doIt);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   778
    vm.loadContext(ctx);
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   779
    vm.interpret();
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   780
    cout << "Boot complete" << endl;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   781
    cout << "Used: " << vm.allocPtr - vm.allocBase << endl;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   782
    cout << flush;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   783
  }
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   784
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   785
  return 0;
97c12bf829d8 WIP C++ simple interpreter, product of yesterday evening's work
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
   786
}