Skip to content

Commit

Permalink
updated ring-methods to NodeJS parser
Browse files Browse the repository at this point in the history
  • Loading branch information
tmhglnd committed Feb 25, 2020
1 parent 55f241f commit 6553483
Show file tree
Hide file tree
Showing 10 changed files with 1,373 additions and 643 deletions.
122 changes: 85 additions & 37 deletions mercury_ide_0.9.9/code/mercury.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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) => {
Expand Down Expand Up @@ -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 };
}
29 changes: 29 additions & 0 deletions mercury_ide_0.9.9/code/node_modules/moo/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6553483

Please sign in to comment.