Nary

From Esolang
Jump to navigation Jump to search

Nary (pronounced "enn-airy") is a programming language "skin" that was created by User:Conor_O'Brien. It's purpose is to be very successful at programs that have a source restriction (e.g. do not use any of character X). Nary is based off of the term n-ary, referring to something whose arity is n.

Concept

Nary allows the user to use an arbitrary set of symbols for programming, of which the program only uses. Then, after the set is decided upon, a destination language and source is chosen and the source is encoded (from ASCII) into base N, where N = cardinality of symbol set. After, each number in base N is mapped to the symbol set. For example, a symbol set {g, h, q} would map each of "Hello" into base three padded ([02200, 10202, 11000, 11000, 11010]) then mapped to the symbol set ([gqqgg, hgqgq, hhggg, hhggg, hhghg]).

Syntax

The syntax of Nary is quite simple, and is divided into two parts:

  1. The preamble
  2. The body

The preamble is defined to be the first "line"; the line can be defined in various ways, depending on the structure of the preamble. The most common is a space-delineated string containing the set, followed by a newline. This newline can be replaced by a duplicate of the last command. If there are no spaces, each command is assumed to be a single character.

The body is then mapped in the process described above.

Generator and Implementation

RegExp.escape=function(s){return s.replace(/[-\/\\^$*+?.()|[\]{}]/g,'\\$&')};function pad(e,q,h=" "){return e.length>q?(e.concat(["."])).slice(-q-1,-1):q==e.length?e:pad([h].concat(e),q,h)}function nary(src,set){max="~".charCodeAt().toString(set.length).length;set=typeof set=="string"?set.split(""):set;return src.split("").map(function(chr){return pad(chr.charCodeAt().toString(set.length).split("").map(function(byt){return set[+byt]}),max,set[0]).join("")})}function toNary(program){set=[];b="";for(i=0;i<program.length;i++){g=program[i];if(g==" "){set.push(b);b=""}else if(g=="\n"||set[set.length-1]==b){set.push(b);break}else{b+=g}}if(set.length==1)set=set[0].split("");program2=program.slice(i+1,program.length);return program.slice(0,i+1)+nary(program2,set).join("");return nary(program,set).join("")}function fromNary(output){set=[];b="";for(i=0;i<output.length;i++){g=output[i];if(g==" "){set.push(b);b=""}else if(g=="\n"||set[set.length-1]==b){set.push(b);break}else{b+=g}}output=output.slice(i+1,output.length);max="~".charCodeAt().toString(set.length).length;for(var i=0;i<set.length;i++){output=output.replace(new RegExp(RegExp.escape(set[i]),"g"),i)}return output.match(new RegExp(".{0,"+max+"}","g")).map(function(e){return String.fromCharCode(parseInt(e,set.length))}).join("").slice(0,-1)}

To create an Nary program, one may opt for any easy way of doing this. In this case, you simply need to create your preamble, then type your program as normal. Set this result into a string (call it s), then call toNary(s). The result is your program.

To interpret an Nary program (call that p), simply call fromNary(p). Call the language that p was written in L. Then, the final result is written in Nary L.