Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <h1>CGL (CGL Golfing Language)</h1>
<h2>Code</h2>
<textarea></textarea>
<h2>Input</h2>
<button>Add input</button>
<div id = "input">
</div>
<button id = "run">Run</button>
<h2>Output</h2>
<div id = "output"></div>
            
          
!
            
              textarea,button, input{
	display: block;
}
textarea,div:not(#input){
	width: 100%;
	resize: vertical;
	box-sizing: border-box;
	min-height: 200px;
	border: 1px solid black;
}
button, input{
	width: 20%;
	margin: 10px auto;
}
h1,h2{
	text-align: center;
}
            
          
!
            
              var copyTextToClipboard = function(text) {
  var textArea = document.createElement("textarea");

  //
  // *** This styling is an extra step which is likely not required. ***
  //
  // Why is it here? To ensure:
  // 1. the element is able to have focus and selection.
  // 2. if element was to flash render it has minimal visual impact.
  // 3. less flakyness with selection and copying which **might** occur if
  //    the textarea element is not visible.
  //
  // The likelihood is the element won't even render, not even a flash,
  // so some of these are just precautions. However in IE the element
  // is visible whilst the popup box asking the user for permission for
  // the web page to copy to the clipboard.
  //

  // Place in top-left corner of screen regardless of scroll position.
  textArea.style.position = 'fixed';
  textArea.style.top = 0;
  textArea.style.left = 0;

  // Ensure it has a small width and height. Setting to 1px / 1em
  // doesn't work as this gives a negative w/h on some browsers.
  textArea.style.width = '2em';
  textArea.style.height = '2em';

  // We don't need padding, reducing the size if it does flash render.
  textArea.style.padding = 0;

  // Clean up any borders.
  textArea.style.border = 'none';
  textArea.style.outline = 'none';
  textArea.style.boxShadow = 'none';

  // Avoid flash of white box if rendered for any reason.
  textArea.style.background = 'transparent';


  textArea.value = text;

  document.body.appendChild(textArea);

  textArea.select();

  try {
    var successful = document.execCommand('copy');
    var msg = successful ? 'successful' : 'unsuccessful';
    console.log('Copying text command was ' + msg);
  } catch (err) {
    console.log('Oops, unable to copy');
  }

  document.body.removeChild(textArea);
};

function prime(num){
 if(num == 1) return true;
 var t = num / 2;
 var k = 2;
 while(k <= t) {
   if(num % k == 0) {
      return false
   } else {
   k++;  
  }
 }
  return true;
}

if (!Math.cbrt) {
  Math.cbrt = function(x) {
    var y = Math.pow(Math.abs(x), 1/3);
    return x < 0 ? -y : y;
  };
}

if (!Math.root4) {
  Math.root4 = function(x) {
    var y = Math.pow(Math.abs(x), 1/4);
    return x < 0 ? -y : y;
  };
}

var $ = function(selector){
	return document.querySelector(selector);
};

var stacks;
var stackOn;
var isString;
var isComment;
var string;
var isNumber;
var number;
var isCompString;
var compString;

var clear = function(){
	stacks = [[]];
	stackOn = 0;
	isString = false;
	isComment = false;
	isNumber = false;
	isCompString = false;
	compString = "";
	string = "";
	number = "";
};

clear();

Array.prototype.rotate = function(n){
	this.unshift.apply(this, this.splice(n, this.length));
	return this;
};

var functions = {
	"\n": function(){
		isComment = false;
	},
	
	"!": function(){
		stacks[stackOn].forEach((item, idx)=>{
			var currentStack = stacks[stackOn];
			switch(typeof item){
				case "number":
					//invert sign (from -2 to 2, and 3 to -3)
					currentStack[idx] = item * -1;
				break;
				case "boolean":
					currentStack[idx] = !currentStack[idx];
				break;
				case "string":
					//something like "1"
					if(parseInt(item)){
						//invert sign (from -2 to 2, and 3 to -3)
						currentStack[idx] = item * -1;
					}
				break;
			}
		});
	},
	
	"\"": function(){
		if(isString){
			stacks[stackOn].push(string);
			string = "";
		}
		isString = !isString;
	},
	
	//numbers are stored as strings internally
	"#": function(){
		if(isNumber){
			console.log(number);
			stacks[stackOn].push(parseInt(number));
			number = "";
		}
		isNumber = !isNumber;
	},
	
	"$": function(){
		//our rotate function is backwards
		stacks[stackOn].rotate(1);
	},
	
	"%": function(){
		//our rotate function is backwards
		stacks[stackOn].rotate(-1);
	},
	
	"&": function(){
		stacks[stackOn].forEach((item, idx)=>{
			var currentStack = stacks[stackOn];
			currentStack[idx] = LZString.compress(item);
		});
	},
	
	
	"'": function(){
		if(isCompString){
			stacks[stackOn].push(LZString.decompress(compString));
			compString = "";
		}
		isCompString = !isCompString;
	},
	
	//add 2 because a stack shift of 0 is a no-op and 1 has its own char
	"(": function(){
		stackOn -= parseInt(stacks[stackOn].find(element=>{
			return !isNaN(parseInt(element));
		})) + 2;
		stacks[stackOn] = stacks[stackOn] || [];
	},
	
	//add 2 because a stack shift of 0 is a no-op and 1 has its own char
	")": function(){
		stackOn += parseInt(stacks[stackOn].find(element=>{
			return !isNaN(parseInt(element));
		})) + 2;
		stacks[stackOn] = stacks[stackOn] || [];
	},
	
	
	//increment current stack
	"+": function(){
		stackOn ++;
		stacks[stackOn] = stacks[stackOn] || [];
	},
	
	//decrement current stack
	"-": function(){
		stackOn --;
		stacks[stackOn] = stacks[stackOn] || [];
	},
	
	//delibrate no-op
	"/": function(){},
	
	":": function(){
		var temp = stacks[stackOn].toString().split("");
		stackOn++;
		stacks[stackOn] = temp;
	},
	
	"L": function(){
		var gcd=(a,b)=>b?gcd(b, a % b):a;
		lcm = (a,b)=>(a*b)/gcd(a,b);
		stacks[stackOn].push(
			lcm(parseInt(stacks[stackOn][0]),parseInt(stacks[stackOn][1]))
		);
	},
	
	"S": function(){
		var copy = [...stacks[stackOn]].sort();
		stackOn++;
		stacks[stackOn] = copy;
	},
	
	"c": function(){
		var same = JSON.stringify(stacks[stackOn]) === JSON.stringify(stacks[stackOn - 1]);
		stackOn ++;
		stacks[stackOn] = stacks[stackOn] || [];
		stacks[stackOn].push(same);
	},
	
	"h": function(){
		stacks[stackOn].push("Hello, World!");
	},
	
	//non-breaking space starts comment - perfect for polyglots
	"\u00a0": function(){
		isComment = true;
	},
	
	"²": function(){
		stacks[stackOn].forEach((item, idx)=>{
			stacks[stackOn][idx] = item ** 2;
		});
	},
	
	
	"³": function(){
		stacks[stackOn].forEach((item, idx)=>{
			stacks[stackOn][idx] = item ** 3;
		});
	},
	
	
	"⁴": function(){
		stacks[stackOn].forEach((item, idx)=>{
			stacks[stackOn][idx] = item ** 4;
		});
	},
	
	"➕": function(){
		stacks[stackOn][0] = parseInt(stacks[stackOn][0]);
		stacks[stackOn][0] += stacks[stackOn][1] ? parseInt(stacks[stackOn][1]) : 1;
	},
	
	"₂": function(){
		stacks[stackOn].forEach((item, idx)=>{
			stacks[stackOn][idx] = Math.sqrt(item);
		});
	},
	
	
	"₃": function(){
		stacks[stackOn].forEach((item, idx)=>{
			stacks[stackOn][idx] = Math.cbrt(item);
		});
	},
	
	
	"÷": function(){
		if(stacks[stackOn][1] !== undefined){
			stacks[stackOn].push(stacks[stackOn][0] / stacks[stackOn][1]);
		}else{
			stacks[stackOn].push(1/stacks[stackOn][0]);
		}
	},
	
	"₄": function(){
		stacks[stackOn].forEach((item, idx)=>{
			stacks[stackOn][idx] = Math.root4(item);
		});
	},
	
	"ⓟ": function(){
		stacks[stackOn].forEach((item, idx)=>{
			stacks[stackOn][idx] = prime(item);
		});
	},
	
	"Ⓧ": function(output){
		stacks[stackOn] = [stacks[stackOn].pop()];
	},
	
	"ⓧ": function(output){
		stacks[stackOn] = [stacks[stackOn][0]];
	},
	
	"☒": function(){
		stacks[stacksOn].pop();
	},
	
	"☑": function(){
		stacks[stacksOn] = [stacks[stacksOn].pop()];
	},
	
	//not a "normal +"
	"+": function(){
		stacks[stackOn].push(parseInt(stacks[stackOn][0]) + parseInt(stacks[stackOn][1]));
	},
	
	//not a "normal letter x"
	"✕": function(){
		stacks[stackOn].push(parseInt(stacks[stackOn][0]) + parseInt(stacks[stackOn][0]));
	},
	
	
	"⨥": function(){
		var temp = JSON.parse(JSON.stringify(stacks[stackOn])).reduce((accum, item)=>parseInt(accum)+parseInt(item));
		console.log(temp);
		stackOn ++;
		stacks[stackOn].push(temp);
	},
	
	/*
	
	"!": function(){
	},
	*/
	
};

var run = function(code, inputArr, output){
	stacks[-1] = inputArr;
	var hasReturned = code.split("").some(char=>{
		
		//add string chars
		if(isString && char !== "\""){
			string += char;
		//add compressed string chars
		}else if(isCompString && char !== "'"){
			compString += char;
		//add number chars
		}else if(isNumber && char !== "#"){
			//using a non-number char with a current number results in ending that number and parsing normally
			if(char === "0" || parseInt(char) || char === "." || (char === "-" && number === "")){
				number += char;
			}else{
				console.log("Ending #");
				
				//call the # function to end this number
				functions["#"]();
				
				if(!isComment || char === "\n"){
					//check if function exists
					if(functions[char]){
						return functions[char]();
					}
				}
			}
		}else if(!isComment || char === "\n"){
			//check if function exists
			if(functions[char]){
				return functions[char]();
			}
		}
	});
	
	//"adds" a " for you if you leave one out
	if(isString){
		functions["\""]();
	}
	
	//"adds" a ' for you if you leave one out
	if(isCompString){
		functions["'"]();
	}
	
	//"adds" a # for you if you leave one out
	if(isNumber){
		functions["#"]();
	}
	
	if(!hasReturned){
		output(stacks[stackOn][0] === undefined ? "" : stacks[stackOn][0]);
	}
};
$("#run").onclick = function(){
	$("#output").innerText = "";
	clear();
	var inputs = Array.from(document.querySelectorAll("input"));
	var inputValues = [];
	inputs.forEach(input=>inputValues.push(input.value));
	console.log(inputValues, inputs);
	run($("textarea").value, inputValues, function(output){
		$("#output").innerText += output;
	});
};

$("button").onclick = function(){
	$("#input").appendChild(document.createElement("input"));
};

var isUnicodeShortcut = false;
var unicode = "";

//unicode shortcut - press Control + Shift + U and enter in the hex of a unicode char, press enter and it will copy to your clipboard.
$("textarea").onkeydown = function(e){

	if(isUnicodeShortcut){
		e.preventDefault();
	}
	
	if(e.key === "Enter" && isUnicodeShortcut){
		var char = String.fromCharCode(parseInt(unicode, 16));
		console.log("Ending shortcut with " + char);
		isUnicodeShortcut = false;
		if(!copyTextToClipboard(char)){
			prompt("Control + C, Enter", char);
		}
		
	}else if(isUnicodeShortcut){
		
		console.log("Unicode shortcut adding " + e.key);
		unicode += e.key;
		
	}else if(e.ctrlKey && e.key === "U"){
		console.log("Ctrl + shift + u")
		isUnicodeShortcut = true;
	}
};
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console