diff --git a/mercury_ide_0.9.9/code/mercury.js b/mercury_ide_0.9.9/code/mercury.js index d72d60dc..ba7961f5 100644 --- a/mercury_ide_0.9.9/code/mercury.js +++ b/mercury_ide_0.9.9/code/mercury.js @@ -9,6 +9,22 @@ const Rand = require('total-serialism').Stochastic; const Util = require('total-serialism').Utility; const Dict = require('./dictionary.js'); +// const moo = require('moo'); +// let lexer = moo.compile({ +// number: /-?(?:[0-9]|[0-9]+)(?:\.[0-9]+)?(?:[eE][-+]?[0-9]+)?\b/, +// lParen: '(', +// rParen: ')', +// lArray: '[', +// rArray: ']', +// seperator: /[\,\;]/, +// string: { +// match: /["|'|\`](?:\\["\\]|[^\n"\\])*["|'|\`]/, +// value: x => x.slice(1, x.length-1) +// }, +// identifier: /[a-zA-Z\_][a-zA-Z0-9\_\-]*/, +// ws: /[ \t]+/, +// }); + var dict = new Dict(); const handlers = { @@ -26,13 +42,13 @@ const handlers = { var expr = args.join(' '); var parsed = parseString(expr); var eval = evaluateParse(parsed); - - // dict.set(name, { - // "@type" : "1d", - // "@value" : eval - // }); - - dict.set(name, eval); + // console.log("@eval", eval); + + var arr = []; + for (i in eval){ + arr.push({ 'array' : eval[i] }); + } + dict.set(name, arr); max.outlet(dict.items); }, 'spread' : (...v) => { @@ -101,88 +117,120 @@ const handlers = { } max.addHandlers(handlers); -function evaluateParse(params){ +// evaluate the parsed items if it is a function +// +function evaluateParse(parse){ + var params = parse.value; var f = params[0]; + if (!hasFunc(f)){ return params.map(x => parseNumber(x)); } else { params.shift(); params = params.map(x => parseParam(x)); - console.log("@func", f, ": @params", ...params); + // console.log("@func", f, ": @params", ...params); return mainFunc.call(handlers, f, ...params); } } +// check if the function is part of the function handlers +// function hasFunc(f){ return f in handlers; } +// apply the function and return the array result +// function mainFunc(func){ return this[func].apply(this, Array.prototype.slice.call(arguments, 1)); } +// if string to number is a number output that +// else output the string instead +// check for entire array if array is provided +// function parseNumber(v){ + if (typeof v === 'object'){ + for (i in v){ + v[i] = parseNumber(v[i]); + } + } return (isNaN(Number(v))) ? v : Number(v); } +// parse list of parameters and check for array +// already stored in variable list +// function parseParam(v){ v = parseNumber(v); if (isNaN(v)){ if (dict.has(v)){ - v = dict.get(v); + v = dict.get(v).slice(); + for (i in v){ + v[i] = v[i].array; + } } } return v; } -// parse the input string to an array of values -// and possible function name +// parse the input string to an array of values and +// possible function name. excepts multi-dimensional arrays +// arrays of 3 dimension or higher will be stripped down to 2d +// function parseString(str){ - var isFunction = false; var depth = 0; + var type = '1d'; var items = []; // array for ascii storage + var items2D = []; // array for items array var arg = ""; // string of arguments // iterate through all the characters in a codeline + // and set items and tokens based on character for (var i in str){ var char = str[i]; - if (char == "("){ - if (!isFunction){ - isFunction = true; - if(arg != ""){ items.push(arg); } // add function name - arg = ""; - } - } - else if (char == ")"){ - isFunction = false; - items.push(arg); - arg = ""; - } - else if (char == "["){ - if (!depth){ - if(arg != ""){ items.push(arg) }; // add array items + if (char === "[" || char === "("){ + if(arg != ""){ + items.push(arg); arg = ""; - } else { - arg += char; } depth++; } - else if (char == "]"){ + else if (char === "]" || char === ")"){ depth--; - if (depth <= 0){ - items.push(arg); // add array items as a string - arg = ""; + if (!depth){ + if (arg != ""){ + items.push(arg); + arg = ""; + } + } else if (depth == 1){ + if (arg != ""){ + items2D.push(arg); + arg = ""; + } + items.push(items2D); + items2D = []; } else { - arg += char; + if (arg != ""){ + items2D.push(arg); + arg = ""; + } } } else if (char == " "){ - if (arg != ""){ items.push(arg); } // add item to array + if (arg != ""){ + if (depth > 1){ + type = '2d'; + items2D.push(arg); + } else { + items.push(arg); + } + } arg = ""; } else { arg += char; } } - return items; + return { 'type' : type, 'value' : items }; } \ No newline at end of file diff --git a/mercury_ide_0.9.9/code/node_modules/moo/LICENSE b/mercury_ide_0.9.9/code/node_modules/moo/LICENSE new file mode 100644 index 00000000..3810d823 --- /dev/null +++ b/mercury_ide_0.9.9/code/node_modules/moo/LICENSE @@ -0,0 +1,29 @@ +BSD 3-Clause License + +Copyright (c) 2017, Tim Radvan (tjvr) +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +* Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/mercury_ide_0.9.9/code/node_modules/moo/README.md b/mercury_ide_0.9.9/code/node_modules/moo/README.md new file mode 100644 index 00000000..367c0120 --- /dev/null +++ b/mercury_ide_0.9.9/code/node_modules/moo/README.md @@ -0,0 +1,385 @@ +![](cow.png) + +Moo! +==== + +Moo is a highly-optimised tokenizer/lexer generator. Use it to tokenize your strings, before parsing 'em with a parser like [nearley](https://github.com/hardmath123/nearley) or whatever else you're into. + +* [Fast](#is-it-fast) +* [Convenient](#usage) +* uses [Regular Expressions](#on-regular-expressions) +* tracks [Line Numbers](#line-numbers) +* handles [Keywords](#keywords) +* supports [States](#states) +* custom [Errors](#errors) +* is even [Iterable](#iteration) +* has no dependencies +* 4KB minified + gzipped +* Moo! + +Is it fast? +----------- + +Yup! Flying-cows-and-singed-steak fast. + +Moo is the fastest JS tokenizer around. It's **~2–10x** faster than most other tokenizers; it's a **couple orders of magnitude** faster than some of the slower ones. + +Define your tokens **using regular expressions**. Moo will compile 'em down to a **single RegExp for performance**. It uses the new ES6 **sticky flag** where possible to make things faster; otherwise it falls back to an almost-as-efficient workaround. (For more than you ever wanted to know about this, read [adventures in the land of substrings and RegExps](http://mrale.ph/blog/2016/11/23/making-less-dart-faster.html).) + +You _might_ be able to go faster still by writing your lexer by hand rather than using RegExps, but that's icky. + +Oh, and it [avoids parsing RegExps by itself](https://hackernoon.com/the-madness-of-parsing-real-world-javascript-regexps-d9ee336df983#.2l8qu3l76). Because that would be horrible. + + +Usage +----- + +First, you need to do the needful: `$ npm install moo`, or whatever will ship this code to your computer. Alternatively, grab the `moo.js` file by itself and slap it into your web page via a `