~kylep/visual-cell-lang

b74de456824aa3d75931bb57431dedd57be20b90 — Kyle Perik 1 year, 8 months ago 662ce80
Add more opcodes and examples
2 files changed, 78 insertions(+), 0 deletions(-)

M src/lang.js
M src/main.js
M src/lang.js => src/lang.js +40 -0
@@ 102,6 102,26 @@ export function interpret (expression, initial = null) {
      const b = r.stack[1];
      return { push: [a.add(b)], pop: 2 }
    }
    else if (code === 'add') {
      const a = r.stack[0];
      const b = r.stack[1];
      return { push: [a + b], pop: 2 }
    }
    else if (code === 'mul') {
      const a = r.stack[0];
      const b = r.stack[1];
      return { push: [a * b], pop: 2 }
    }
    else if (code === 'sub') {
      const a = r.stack[0];
      const b = r.stack[1];
      return { push: [b - a], pop: 2 }
    }
    else if (code === 'mod') {
      const limit = r.stack[0];
      const value = r.stack[1];
      return { push: [value.mod(limit)], pop: 2 }
    }
    else if (code === 'jcn') {
      const condition = r.stack[1];
      const branch = r.stack[0];


@@ 117,10 137,27 @@ export function interpret (expression, initial = null) {
      const key = r.stack[0];
      return {push: [r.data[key]], pop: 1};
    }
    else if (code === 'set') {
      const value = r.stack[2];
      const key = r.stack[1];
      const index = r.stack[0];
      r.data[key][index] = value;
      return {pop: 3};
    }
    else if (code === 'at') {
      const key = r.stack[1];
      const index = r.stack[0];
      return {push: [r.data[key][index]], pop: 2};
    }
    else if (code === 'dup') {
      const value = r.stack[0];
      return {push: [value, value], pop: 1};
    }
    else if (code === 'swp') {
      const a = r.stack[0];
      const b = r.stack[1];
      return {push: [b, a], pop: 2};
    }
    else if (code === 'que') {
      const key = r.stack[0];
      const value = r.stack[1];


@@ 149,6 186,9 @@ export function interpret (expression, initial = null) {
      const value = r.data[key].shift();
      return {push: [value], pop: 1};
    }
    else if (code === 'kil') {
      return {push: [], pop: 1};
    }
    else {
      throw Error(`unknown code: ${code}`);
    }

M src/main.js => src/main.js +38 -0
@@ 205,6 205,44 @@ def pop:
`)
    }
  });
  loadCodeBox({
    name: 'pulse',
    data: {
      value: 0,
      last: [],
      step: 0
    },
    outputs: ['p', 'c'],
    inputs: {
      v: parse(`val int #value kep`),
      i: parse(`
#step get #1 int add #100 int mod dup #step kep
dup #last swp at dup #clear jcn kil
#50 int sub #value get vec dup #p out
#last #step get set
def clear:
  dup #c out
`)
    }
  });
  loadCodeBox({
    name: 'wave',
    data: {
      last: range(80).map(i => Math.sin(i * Math.PI * 2 / 80)),
      step: 0,
    },
    outputs: ['v'],
    inputs: {
      i: parse(`
#step get #1 int add #last len mod dup #step kep
#last swp at

#5 int mul

#v out
`)
    }
  });

  initUI();
  app.view.tabIndex = 0