Browse Source

Added Rollup

feature/distfiles
Brian Lindner 11 months ago
parent
commit
2a7ff2d1bc
5 changed files with 471 additions and 2 deletions
  1. +325
    -0
      dist/memeforth.dist.js
  2. +8
    -0
      index.html
  3. +117
    -0
      package-lock.json
  4. +6
    -2
      package.json
  5. +15
    -0
      rollup.config.js

+ 325
- 0
dist/memeforth.dist.js View File

@@ -0,0 +1,325 @@
var memeforth = (function (exports) {
'use strict';

/**
* Interprets a Forth token list.
* @param {} tokens The tokens to interpret, in order.
* @param {} stack The initial Forth stack to use. Empty by default.
* @param {} words The initial set of words to use. Defaults to `dictionary`.
*/
const interpret = (tokens, stack = [], words = dictionary) => {
let compile = false;

let currentWord = "";

let output = "";

while (tokens.length > 0) {
const token = tokens.shift();

if (compile) {
if (token.trim() === ";") compile = false;
else words[currentWord].push(token);
continue;
}

if (token.trim() === ":") {
compile = true;
currentWord = tokens.shift();
words[currentWord] = [];
} else {
if (words[token])
if (typeof words[token] === "function") {
const outputToAdd = words[token](stack, tokens);
if (typeof outputToAdd !== "undefined") {
if (output.length > 0) {
output += " ";
}
output += outputToAdd;
}
} else {
interpret(words[token], stack);
}
else if (token.length > 0) {
if (token.match(/\d+/)) stack.push(parseInt(token));
else stack.push(token);
}
}
}

return {
output: output,
stack: stack,
words: words
};
};

/**
* @summary Adds "lmao gottem" to the top of the stack.
* @param {Array<string>} stack The Forth stack
*/
const gotem = stack => {
stack.push("lmao gottem");
};

const bit = stack => {
stack.push(`${stack.pop()}, as a bit`);
};

/**
* @summary Converts the top of the stack to a "jorts" meme.
* @param {Array<string>} stack The Forth stack.
*/
const jorts = stack => {
const subject = stack.pop();
const jubject = "j" + subject.substring(1);
stack.push(`${jubject} (jean ${subject})`);
};

/**
* @summary Converts the top of the stack to an arbitrary "jorts" meme based on a root and a subject
* @param {Array<string>} stack The Forth stack.
*/
const xorts = stack => {
const subject = stack.pop();
const root = stack.pop();
const xubject = root[0] + subject.substring(1);

stack.push(`${xubject} (${root} ${subject})`);
};

/**
* @summary Converts the top of the stack to an "another hard day's work" meme.
* @param {Array<string>} stack The Forth stack.
*/
const hardwork = stack => {
stack.push(`Just another hard day's work at the '${stack.pop()}' factory.`);
};

/**
* @summary Converts the top of the stack to an "X? I sure hope it does!" meme.
* @param {Array<string>} stack The Forth stack.
*/
const surehope = stack => {
stack.push(`${stack.pop()}? I sure hope it does!`);
};

/**
* @summary Converts the top of the stack to an "OK Google, X" meme.
* @param {Array<string>} stack The Forth stack.
*/
const okgoog = stack => {
stack.push(`OK Google, ${stack.pop()}`);
};

/**
* @summary Converts the top of the stack to :hacker_X: emojis.
* @param {Array<string>} stack The Forth stack.
*/
const hacker = stack => {
const message = stack.pop();
let newStr = "";

for (let i = 0; i < message.length; i++) {
const char = message[i];
if (/[A-Za-z]/.test(char)) newStr += ":hacker_" + char.toLowerCase() + ": ";
else newStr += char;
}

stack.push(newStr.trim());
};

/**
* @summary Converts the top of the stack to :sm64_X: emojis.
* @param {Array<string>} stack The Forth stack.
*/
const sm64 = stack => {
const message = stack.pop();
let newStr = "";

for (var i = 0; i < message.length; i++) {
const char = message[i];
if (/[A-Za-z0-9]/.test(char))
newStr += ":sm64_" + char.toLowerCase() + ": ";
else if (char === "'") newStr += ":sm64_quote: ";
else if (char === '"') newStr += ":sm64_dblquote: ";
else newStr += char;
}

stack.push(newStr.trim());
};

/**
* @summary Converts the top of the stack into a "DNS over X" meme.
* @param {Array<string>} stack The Forth stack.
*/
const dns = stack => {
stack.push(`DNS over ${stack.pop()}`);
};

/**
* @summary Concatenates the top two elements on the stack.
* @param {Array<string>} stack The Forth stack.
*/
const concat = stack => {
const b = stack.pop();
const a = stack.pop();
stack.push(a + b);
};

/**
* @summary Drops the top element of the stack.
* @param {Array<string>} stack The Forth stack.
*/
const drop = stack => {
stack.pop();
};

/**
* @summary Duplicate the top two elements of the stack.
* @param {Array<string>} stack The Forth stack.
*/
const dup = stack => {
const a = stack.pop();
stack.push(a, a);
};

/**
* @summary Swap the top two elements of the stack
* @param {Array<string>} stack The Forth stack.
*/
const swap = stack => {
const b = stack.pop();
const a = stack.pop();

stack.push(b, a);
};

/**
* @summary Take the second element of the stack and duplicate it "over" the first.
* @param {Array<string>} stack The Forth stack.
*/
const over = stack => {
const b = stack.pop();
const a = stack.pop();

stack.push(a, b, a);
};

/**
* @summary Rotate the top three elements of the stack
* @param {Array<string>} stack The Forth stack.
*/
const rot = stack => {
const a = stack.pop();
const b = stack.pop();
const c = stack.pop();

stack.push(b, a, c);
};

/**
* @summary Parse a literal string until we find a string ending with "
* @param {Array<string>} stack The Forth stack.
* @param {Array<string} program The tokenized program.
*/
const litstring = (stack, program) => {
let newStr = "";

do {
newStr += program.shift() + " ";
} while (newStr.slice(-2, -1) !== '"');

stack.push(newStr.slice(0, -2));
};

const add = stack => {
const b = stack.pop();
const a = stack.pop();
stack.push(a + b);
};

const sub = stack => {
const b = stack.pop();
const a = stack.pop();
stack.push(a - b);
};

const mul = stack => {
const b = stack.pop();
const a = stack.pop();

stack.push(a * b);
};

const divmod = stack => {
const b = stack.pop();
const a = stack.pop();

stack.push(a % b);
stack.push(Math.round(a / b));
};

/**
* @summary Return the first element in the stack, staging it for output.
* @param {Array<string>} stack The Forth stack.
*/
const print = stack => stack.pop();

const printStack = stack => stack.toString();

/**
* The base dictionary of supported MemeForth tokens.
*/
const dictionary = {
HARDWORK: hardwork,
SUREHOPE: surehope,
HACKER: hacker,
SM64: sm64,
DNS: dns,
OKGOOG: okgoog,
CONCAT: concat,
DROP: drop,
DUP: dup,
SWAP: swap,
OVER: over,
ROT: rot,
BIT: bit,
"-ROT": ["ROT", "ROT"],
's"': litstring,
GOTEM: gotem,
JORTS: jorts,
".": print,
TYPE: print,
".s": printStack,
XORTS: xorts,
"+": add,
"-": sub,
"*": mul,
"/MOD": divmod,
"/": ["/MOD", "SWAP", "DROP"],
MOD: ["/MOD", "DROP"]
};

/**
* @summary Runs a Forth program and returns the stack.
* @param {string} program A Forth program, in string form.
* @returns {Array<string>} The stack of the Forth program.
*/
const runMemeForth = (program, stack = []) =>
interpret(program.split(/\s/), stack);

var memeforth = {
run: runMemeForth,
dictionary: dictionary
};
var memeforth_1 = memeforth.run;
var memeforth_2 = memeforth.dictionary;

exports.default = memeforth;
exports.dictionary = memeforth_2;
exports.run = memeforth_1;

return exports;

}({}));

+ 8
- 0
index.html View File

@@ -0,0 +1,8 @@
<html>
<head>
<script src="./dist/memeforth.dist.js"></script>
</head>
<body>

</body>
</html>

+ 117
- 0
package-lock.json View File

@@ -426,6 +426,12 @@
"@babel/types": "^7.3.0"
}
},
"@types/estree": {
"version": "0.0.39",
"resolved": "https://registry.npmjs.org/@types/estree/-/estree-0.0.39.tgz",
"integrity": "sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==",
"dev": true
},
"@types/istanbul-lib-coverage": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.1.tgz",
@@ -451,6 +457,21 @@
"@types/istanbul-lib-report": "*"
}
},
"@types/node": {
"version": "12.7.5",
"resolved": "https://registry.npmjs.org/@types/node/-/node-12.7.5.tgz",
"integrity": "sha512-9fq4jZVhPNW8r+UYKnxF1e2HkDWOWKM5bC2/7c9wPV835I0aOrVbS/Hw/pWPk2uKrNXQqg9Z959Kz+IYDd5p3w==",
"dev": true
},
"@types/resolve": {
"version": "0.0.8",
"resolved": "https://registry.npmjs.org/@types/resolve/-/resolve-0.0.8.tgz",
"integrity": "sha512-auApPaJf3NPfe18hSoJkp8EbZzer2ISk7o8mCC3M9he/a04+gbMF97NkpD2S8riMGvm4BMRI59/SZQSaLTKpsQ==",
"dev": true,
"requires": {
"@types/node": "*"
}
},
"@types/stack-utils": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/@types/stack-utils/-/stack-utils-1.0.1.tgz",
@@ -831,6 +852,12 @@
"integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==",
"dev": true
},
"builtin-modules": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/builtin-modules/-/builtin-modules-3.1.0.tgz",
"integrity": "sha512-k0KL0aWZuBt2lrxrcASWDfwOLMnodeQjodT/1SxEQAXsHANgo6ZC/VEaSEHCXt7aSTZ4/4H5LKa+tBXmW7Vtvw==",
"dev": true
},
"cache-base": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/cache-base/-/cache-base-1.0.1.tgz",
@@ -1265,6 +1292,12 @@
"integrity": "sha512-39nnKffWz8xN1BU/2c79n9nB9HDzo0niYUqx6xyqUnyoAnQyyWpOTdZEeiCch8BBu515t4wp9ZmgVfVhn9EBpw==",
"dev": true
},
"estree-walker": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-0.6.1.tgz",
"integrity": "sha512-SqmZANLWS0mnatqbSfRP5g8OXZC12Fgg1IwNtLsyHDzJizORW4khDfjPqJZsemPWBB2uqykUah5YpQ6epsqC/w==",
"dev": true
},
"esutils": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/esutils/-/esutils-2.0.3.tgz",
@@ -2425,6 +2458,12 @@
"integrity": "sha512-cTIB4yPYL/Grw0EaSzASzg6bBy9gqCofvWN8okThAYIxKJZC+udlRAmGbM0XLeniEJSs8uEgHPGuHSe1XsOLSQ==",
"dev": true
},
"is-module": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/is-module/-/is-module-1.0.0.tgz",
"integrity": "sha1-Mlj7afeMFNW4FdZkM2tM/7ZEFZE=",
"dev": true
},
"is-number": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/is-number/-/is-number-3.0.0.tgz",
@@ -2454,6 +2493,15 @@
"isobject": "^3.0.1"
}
},
"is-reference": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/is-reference/-/is-reference-1.1.3.tgz",
"integrity": "sha512-W1iHHv/oyBb2pPxkBxtaewxa1BC58Pn5J0hogyCdefwUIvb6R+TGbAcIa4qPNYLqLhb3EnOgUf2MQkkF76BcKw==",
"dev": true,
"requires": {
"@types/estree": "0.0.39"
}
},
"is-regex": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.0.4.tgz",
@@ -3210,6 +3258,15 @@
"js-tokens": "^3.0.0 || ^4.0.0"
}
},
"magic-string": {
"version": "0.25.3",
"resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.25.3.tgz",
"integrity": "sha512-6QK0OpF/phMz0Q2AxILkX2mFhi7m+WMwTRg0LQKq/WBB0cDP4rYH3Wp4/d3OTXlrPLVJT/RFqj8tFeAR4nk8AA==",
"dev": true,
"requires": {
"sourcemap-codec": "^1.4.4"
}
},
"make-dir": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/make-dir/-/make-dir-2.1.0.tgz",
@@ -3969,6 +4026,60 @@
"glob": "^7.1.3"
}
},
"rollup": {
"version": "1.21.2",
"resolved": "https://registry.npmjs.org/rollup/-/rollup-1.21.2.tgz",
"integrity": "sha512-sCAHlcQ/PExU5t/kRwkEWHdhGmQrZ2IgdQzbjPVNfhWbKHMMZGYqkASVTpQqRPLtQKg15xzEscc+BnIK/TE7/Q==",
"dev": true,
"requires": {
"@types/estree": "0.0.39",
"@types/node": "^12.7.4",
"acorn": "^7.0.0"
},
"dependencies": {
"acorn": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/acorn/-/acorn-7.0.0.tgz",
"integrity": "sha512-PaF/MduxijYYt7unVGRuds1vBC9bFxbNf+VWqhOClfdgy7RlVkQqt610ig1/yxTgsDIfW1cWDel5EBbOy3jdtQ==",
"dev": true
}
}
},
"rollup-plugin-commonjs": {
"version": "10.1.0",
"resolved": "https://registry.npmjs.org/rollup-plugin-commonjs/-/rollup-plugin-commonjs-10.1.0.tgz",
"integrity": "sha512-jlXbjZSQg8EIeAAvepNwhJj++qJWNJw1Cl0YnOqKtP5Djx+fFGkp3WRh+W0ASCaFG5w1jhmzDxgu3SJuVxPF4Q==",
"dev": true,
"requires": {
"estree-walker": "^0.6.1",
"is-reference": "^1.1.2",
"magic-string": "^0.25.2",
"resolve": "^1.11.0",
"rollup-pluginutils": "^2.8.1"
}
},
"rollup-plugin-node-resolve": {
"version": "5.2.0",
"resolved": "https://registry.npmjs.org/rollup-plugin-node-resolve/-/rollup-plugin-node-resolve-5.2.0.tgz",
"integrity": "sha512-jUlyaDXts7TW2CqQ4GaO5VJ4PwwaV8VUGA7+km3n6k6xtOEacf61u0VXwN80phY/evMcaS+9eIeJ9MOyDxt5Zw==",
"dev": true,
"requires": {
"@types/resolve": "0.0.8",
"builtin-modules": "^3.1.0",
"is-module": "^1.0.0",
"resolve": "^1.11.1",
"rollup-pluginutils": "^2.8.1"
}
},
"rollup-pluginutils": {
"version": "2.8.1",
"resolved": "https://registry.npmjs.org/rollup-pluginutils/-/rollup-pluginutils-2.8.1.tgz",
"integrity": "sha512-J5oAoysWar6GuZo0s+3bZ6sVZAC0pfqKz68De7ZgDi5z63jOVZn1uJL/+z1jeKHNbGII8kAyHF5q8LnxSX5lQg==",
"dev": true,
"requires": {
"estree-walker": "^0.6.1"
}
},
"rsvp": {
"version": "4.8.5",
"resolved": "https://registry.npmjs.org/rsvp/-/rsvp-4.8.5.tgz",
@@ -4241,6 +4352,12 @@
"integrity": "sha1-PpNdfd1zYxuXZZlW1VEo6HtQhKM=",
"dev": true
},
"sourcemap-codec": {
"version": "1.4.6",
"resolved": "https://registry.npmjs.org/sourcemap-codec/-/sourcemap-codec-1.4.6.tgz",
"integrity": "sha512-1ZooVLYFxC448piVLBbtOxFcXwnymH9oUF8nRd3CuYDVvkRBxRl6pB4Mtas5a4drtL+E8LDgFkQNcgIw6tc8Hg==",
"dev": true
},
"spdx-correct": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/spdx-correct/-/spdx-correct-3.1.0.tgz",


+ 6
- 2
package.json View File

@@ -4,7 +4,8 @@
"description": "A Forth interpreter for memes.",
"main": "memeforth.js",
"scripts": {
"test": "jest"
"test": "jest",
"build-dist": "rollup -c"
},
"repository": {
"type": "git",
@@ -17,6 +18,9 @@
},
"homepage": "https://gitlab.com/mdszy/memeforth#readme",
"devDependencies": {
"jest": "^24.9.0"
"jest": "^24.9.0",
"rollup": "^1.21.2",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-node-resolve": "^5.2.0"
}
}

+ 15
- 0
rollup.config.js View File

@@ -0,0 +1,15 @@
const resolve = require('rollup-plugin-node-resolve')
const commonjs = require('rollup-plugin-commonjs')

module.exports = {
input: "memeforth.js",
output: {
file: 'dist/memeforth.dist.js',
format: 'iife',
name: 'memeforth'
},
plugins: [
resolve(),
commonjs()
]
}

Loading…
Cancel
Save