Skip to content

Commit

Permalink
refactor(pointfree): update core stack fns (re-use)
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Aug 20, 2019
1 parent 195ca08 commit a7ebb2f
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 99 deletions.
19 changes: 0 additions & 19 deletions packages/pointfree/src/math.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { StackContext } from "./api";
import { op1, op2 } from "./ops";
import { $ } from "./safe";
import { swap } from "./stack";
import { word } from "./word";

Expand Down Expand Up @@ -62,24 +61,6 @@ export const min = op2(Math.min);
*/
export const max = op2(Math.max);

/**
* ( x -- x+1 )
*
* @param ctx
*/
export const inc = (ctx: StackContext) => (
$(ctx[0], 1), ctx[0][ctx[0].length - 1]++, ctx
);

/**
* ( x -- x-1 )
*
* @param ctx
*/
export const dec = (ctx: StackContext) => (
$(ctx[0], 1), ctx[0][ctx[0].length - 1]--, ctx
);

/**
* ( x -- -x )
*
Expand Down
195 changes: 115 additions & 80 deletions packages/pointfree/src/stack.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,79 @@
import { Stack, StackContext } from "./api";
import { $, $n } from "./safe";

export const tos = (stack: Stack) => stack[stack.length - 1];
const __xsp = (id: 0 | 1) => (ctx: StackContext) => (
ctx[0].push(ctx[id].length), ctx
);

const __dup = (id: 0 | 1) => (ctx: StackContext) => (
$(ctx[id], 1), ctx[id].push(tos(ctx[id])), ctx
);

const __drop = (id: 0 | 1, n = 1) => (ctx: StackContext) => (
$(ctx[id], 1), (ctx[id].length -= n), ctx
);

const __swap = (i: number) => (ctx: StackContext) => {
const stack = ctx[i];
const n = stack.length - 2;
$n(n, 0);
const a = stack[n];
stack[n] = stack[n + 1];
stack[n + 1] = a;
return ctx;
};

const __swap2 = (i: number) => (ctx: StackContext) => {
const stack = ctx[i];
let n = stack.length - 1;
$n(n, 3);
let a = stack[n];
stack[n] = stack[n - 2];
stack[n - 2] = a;
n--;
a = stack[n];
stack[n] = stack[n - 2];
stack[n - 2] = a;
return ctx;
};

const __move = (src: 0 | 1, dest: 0 | 1) => (ctx: StackContext) => (
$(ctx[src], 1), ctx[dest].push(ctx[src].pop()), ctx
);

const __move2 = (a: 0 | 1, b: 0 | 1) => (ctx: StackContext) => {
const src = ctx[a];
$(src, 2);
const v = src.pop();
ctx[b].push(src.pop(), v);
return ctx;
};

const __copy = (src: 0 | 1, dest: 0 | 1) => (ctx: StackContext) => (
$(ctx[src], 1), ctx[dest].push(tos(ctx[src])), ctx
);

const __copy2 = (a: 0 | 1, b: 0 | 1) => (ctx: StackContext) => {
const src = ctx[a];
const n = src.length - 2;
$n(n, 0);
ctx[b].push(src[n], src[n + 1]);
return ctx;
};

const __incdec = (id: 0 | 1, n: number) => (ctx: StackContext) => (
$(ctx[id], 1), (ctx[id][ctx[id].length - 1] += n), ctx
);

//////////////////// Stack manipulation words ////////////////////

/**
* Returns top of stack value (always unsafe, no underflow checking).
*
* @param stack
*/
export const tos = (stack: Stack) => stack[stack.length - 1];

/**
* Utility word w/ no stack nor side effect.
*/
Expand All @@ -16,7 +85,7 @@ export const nop = (ctx: StackContext) => ctx;
* ( -- n )
* @param ctx
*/
export const dsp = (ctx: StackContext) => (ctx[0].push(ctx[0].length), ctx);
export const dsp = __xsp(0);

/**
* Uses TOS as index to look up a deeper d-stack value, then places it
Expand All @@ -42,7 +111,7 @@ export const pick = (ctx: StackContext) => {
*
* @param ctx
*/
export const drop = (ctx: StackContext) => ($(ctx[0], 1), ctx[0].length--, ctx);
export const drop = __drop(0);

/**
* Removes top 2 vals from d-stack.
Expand All @@ -51,9 +120,7 @@ export const drop = (ctx: StackContext) => ($(ctx[0], 1), ctx[0].length--, ctx);
*
* @param ctx
*/
export const drop2 = (ctx: StackContext) => (
$(ctx[0], 2), (ctx[0].length -= 2), ctx
);
export const drop2 = __drop(0, 2);

/**
* If TOS is truthy then drop it:
Expand Down Expand Up @@ -86,9 +153,7 @@ export const push = (...args: any[]) => (ctx: StackContext) => (
*
* @param ctx
*/
export const dup = (ctx: StackContext) => (
$(ctx[0], 1), ctx[0].push(tos(ctx[0])), ctx
);
export const dup = __dup(0);

/**
* Duplicates top 2 vals on d-stack.
Expand Down Expand Up @@ -138,38 +203,14 @@ export const dupif = (ctx: StackContext) => {
return ctx;
};

const _swap = (i: number) => (ctx: StackContext) => {
const stack = ctx[i];
const n = stack.length - 2;
$n(n, 0);
const a = stack[n];
stack[n] = stack[n + 1];
stack[n + 1] = a;
return ctx;
};

const _swap2 = (i: number) => (ctx: StackContext) => {
const stack = ctx[i];
let n = stack.length - 1;
$n(n, 3);
let a = stack[n];
stack[n] = stack[n - 2];
stack[n - 2] = a;
n--;
a = stack[n];
stack[n] = stack[n - 2];
stack[n - 2] = a;
return ctx;
};

/**
* Swaps the two topmost d-stack items.
*
* ( x y -- y x )
*
* @param ctx
*/
export const swap = _swap(0);
export const swap = __swap(0);

/**
* Swaps the two topmost d-stack pairs.
Expand All @@ -178,7 +219,7 @@ export const swap = _swap(0);
*
* @param ctx
*/
export const swap2 = _swap2(0);
export const swap2 = __swap2(0);

/**
* Removes second topmost item from d-stack.
Expand Down Expand Up @@ -261,6 +302,20 @@ export const over = (ctx: StackContext) => {
return ctx;
};

/**
* ( x -- x+1 )
*
* @param ctx
*/
export const inc = __incdec(0, 1);

/**
* ( x -- x-1 )
*
* @param ctx
*/
export const dec = __incdec(0, -1);

//////////////////// R-Stack ops ////////////////////

/**
Expand All @@ -270,7 +325,16 @@ export const over = (ctx: StackContext) => {
*
* @param ctx
*/
export const rsp = (ctx: StackContext) => (ctx[0].push(ctx[1].length), ctx);
export const rsp = __xsp(1);

/**
* Duplicates TOS on r-stack.
*
* ( x -- x x )
*
* @param ctx
*/
export const rdup = __dup(1);

/**
* Removes TOS from r-stack.
Expand All @@ -279,9 +343,7 @@ export const rsp = (ctx: StackContext) => (ctx[0].push(ctx[1].length), ctx);
*
* @param ctx
*/
export const rdrop = (ctx: StackContext) => (
$(ctx[1], 1), ctx[1].length--, ctx
);
export const rdrop = __drop(1);

/**
* Removes top 2 vals from r-stack.
Expand All @@ -290,46 +352,23 @@ export const rdrop = (ctx: StackContext) => (
*
* @param ctx
*/
export const rdrop2 = (ctx: StackContext) => (
$(ctx[1], 2), (ctx[1].length -= 2), ctx
);
export const rdrop2 = __drop(1, 2);

export const movdr = (ctx: StackContext) => (
$(ctx[0], 1), ctx[1].push(ctx[0].pop()), ctx
);
export const movdr = __move(0, 1);

export const movrd = (ctx: StackContext) => (
$(ctx[1], 1), ctx[0].push(ctx[1].pop()), ctx
);
export const movrd = __move(1, 0);

export const cpdr = (ctx: StackContext) => (
$(ctx[0], 1), ctx[1].push(tos(ctx[0])), ctx
);
export const cpdr = __copy(0, 1);

export const cprd = (ctx: StackContext) => (
$(ctx[1], 1), ctx[0].push(tos(ctx[1])), ctx
);
export const cprd = __copy(1, 0);

const mov2 = (a: number, b: number) => (ctx: StackContext) => {
const src = ctx[a];
$(src, 2);
const v = src.pop();
ctx[b].push(src.pop(), v);
return ctx;
};
export const movdr2 = __move2(0, 1);

const cp2 = (a: number, b: number) => (ctx: StackContext) => {
const src = ctx[a];
const n = src.length - 2;
$n(n, 0);
ctx[b].push(src[n], src[n + 1]);
return ctx;
};
export const movrd2 = __move2(1, 0);

export const movdr2 = mov2(0, 1);
export const movrd2 = mov2(1, 0);
export const cpdr2 = cp2(0, 1);
export const cprd2 = cp2(1, 0);
export const cpdr2 = __copy2(0, 1);

export const cprd2 = __copy2(1, 0);

/**
* Swaps the two topmost r-stack items.
Expand All @@ -338,7 +377,7 @@ export const cprd2 = cp2(1, 0);
*
* @param ctx
*/
export const rswap = _swap(1);
export const rswap = __swap(1);

/**
* Swaps the two topmost d-stack pairs.
Expand All @@ -347,22 +386,18 @@ export const rswap = _swap(1);
*
* @param ctx
*/
export const rswap2 = _swap2(1);
export const rswap2 = __swap2(1);

/**
* Like `inc`, but applies to r-stack TOS.
*
* @param ctx
*/
export const rinc = (ctx: StackContext) => (
$(ctx[1], 1), ctx[1][ctx[1].length - 1]++, ctx
);
export const rinc = __incdec(1, 1);

/**
* Like `dec`, but applies to r-stack TOS.
*
* @param ctx
*/
export const rdec = (ctx: StackContext) => (
$(ctx[1], 1), ctx[1][ctx[1].length - 1]--, ctx
);
export const rdec = __incdec(1, -1);

0 comments on commit a7ebb2f

Please sign in to comment.