cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              var grammar = `
{    
    function strip(a) {
    	if (a == "a") return;
    	if (!Array.isArray(a)) return a;
    	a = a.filter(function(n){ return n != undefined && n != ""; })
        	 .map(function(n) { return strip(n); });
        while (a && Array.isArray(a) && a.length <= 1) { a = a[0]; }
        return a;
    }
    
    // remove undefined etc., but ensure result is a list
    function clean(a) {
   		if (!Array.isArray(a)) return a;
    	a = a.filter(function(n){ return n != undefined && n != ""; })
        	 .map(function(n) { return clean(n); });
        return a;
    }
    
    function simplify(a) {
   		if (!Array.isArray(a)) return a;
    	a = a.filter(function(n){ return n != undefined && n != ""; })
        	 .map(function(n) { return simplify(n); });
		while (a && Array.isArray(a) && a.length <= 1) { a = a[0]; }
        return a;
    }
    
    function stringify(a) {
    	if (!a) return "()";
    	if (!Array.isArray(a)) return a;
        a = a.map(function(n) { return stringify(n); });
        return "(" + a.join("") + ")";
    }
}

start = l:list { return stringify(simplify(l)); }

list = "(" items:item* ")" { return clean(items); }
item = quote / mod / slash / eq / bar / star
     / first / first_rest / last / last_rest
	   / "a" / list 
     / !")" . { return; }
     
first = "+" l:list { return l[0]; }
first_rest = "-" l:list { return (l.slice(1)); }
last = ">" l:list { return l[l.length-1]; }
last_rest = "<" l:list { return (l.slice(0, -1)); }

// "*" (2 arguments): if the second arg is a list, 
// then return a copy of this list with the first argument 
// inserted at its head. Otherwise just return the first argument.
star = "*" a:list b:list { return (a.concat(b)); }
	/ "*" a:item b:list { var c = b.slice(); c.unshift(a); return clean(c); }
    / "*" a:item b:item { return a; }
    
// "|" (2 arguments): the longer argument is shortened 
// by the length of the shorter argument, and is then returned.
bar = "|" a:item b:item {
	var al = a ? a.length : 0;
    var bl = b ? b.length : 0;
    if (al > bl) {
    	return a ? (a.slice(0, bl)) : a;
    } else {
    	return b ? (b.slice(0, al)) : b;
    }
}

// "=" (3 arguments): if first two arguments are identical strings, 
// evaluate & return the third.
eq = "=" a:$item b:$item c:item { if (a == b) return c; }

//"/" (3 arguments): if the length of the first argument is odd, 
// return the second, else the third argument.
slash = "/" a:item b:item c:item { return (a && a.length % 2) ? b : c; }

// "%" returns the argument evaluated 
// with the input being null (so long as the depth limit is not reached)
mod = "%" l:item {
	// duplicate l with all 'a' symbols stripped out
    // (but this doesn't evaluate yet)
    return strip(l);
}

// "'" the quote operator returns the argument without evaluating it
// i.e., it skips parsing
quote = "'" l:quoted_item { return l; }
quoted_item = quoted_list / !")" . 
quoted_list = "(" items:quoted_item* ")" { return items; }
`;
var parser = pegjs.generate(grammar);

function parse(a, b) {
  try {
    // replace all "a" characters in a with quoted b:
    var c = a.replace(/a/g, "'"+b);
    //console.log("c", c);
    // now use grammar to reduce this:
    return parser.parse(c);
  } catch (e) {
    console.log(e);
  }
}

var lexicon = "aaaaaa+-<>*|=/%'".split("");
function make_list(depth) {
  var out = "(";
  for (var i=0; i<1+random(5); i++) {
    if (random(depth) > 0) {
      out += make_list(depth-1);
    } else {
      out += lexicon[random(lexicon.length)];
    }
  }
  return out + ")";
}

function viable(c) {
  return c 
      && c != "()"
      && c.length > 1
      && c.length < 256
      && c.indexOf("a") >= 0;
}

var pop = [];
while(pop.length < 64) {
  var a = make_list(3);
  if (viable(a)) pop.push(a);
}

var successes = 0;

function simulate() {
  // pick two at random:
  var a = pop[random(pop.length)];
  var b = pop[random(pop.length)];
  // react them to get a product:
  var c = parse(a, b);
  
  // viability conditions:
  if (viable(c)) {
    successes++;
    //console.log(successes, c);
  } else {
    successes--;
    // re-seed:
    c = make_list(3);
  }
  // insert:
  pop[random(pop.length)] = c;
}

function update() {
  
  for (var i=0; i<10; i++) { 
    simulate(); 
  }
  
  for (var i in pop) {
    write(i, pop[i]);
  }
}
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console