;; data structure: board
(primitive square)
(address square-address (square)) ; pointer. verbose but sadly necessary for now
(array file (square)) ; ranks and files are arrays of squares
(address file-address (file))
(address file-address-address (file-address)) ; pointer to a pointer
(array board (file-address))
(address board-address (board))
(function init-board [
(default-space:space-address <- new space:literal 30:literal)
(initial-position:pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */<html lang="en"><head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript Playground</title>
<meta name="description" content="A JavaScript jungle-gym for doing experiments and sharing scrappy fiddles.">
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
background-color: #ddd;
padding: 10px;
height: 100vh;
margin: 0;
}
textarea {
width: 100%;
height: 64%;
font-family: monospace;
background-color: #FFFEEC;
border: 2px solid #000;
scrollbar-width: none;
font-size: 1rem;
margin-bottom: 10px;
padding: 10px;
box-sizing: border-box;
resize: none;
border-bottom: 12px solid teal;
-webkit-user-modify: read-write-plaintext-only;
}
textarea::-webkit-scrollbar {
display: none;
}
textarea::selection {
background-color: #EFECA7;
}
textarea:focus {
outline: none;
}
#console {
width: 100%;
height: 22%;
background-color: #000;
color: #0fc;
font-family: monospace;
font-size: 1rem;
overflow-y: auto;
padding: 10px;
box-sizing: border-box;
white-space: pre-wrap;
}
.button-container {
width: 100%;
display: flex;
justify-content: flex-end;
margin-bottom: 10px;
}
button {
padding: 10px 20px;
font-size: 1rem;
margin-left: 10px;
cursor: pointer;
border: none;
transition: background-color 0.3s ease;
}
button:hover, button:focus {
outline: none;
}
button.run {
background-color: teal;
color: #FFFFFF;
}
</style>
</head>
<body>
<div class="playground" id="playground">
<div class="seesaw" id="seesaw"></div>
<div class="slide" id="slide"></div>
</div>
<div class="button-container">
<button onclick="clearEverything()">Clear</button>
<button onclick="downloadCodeAndEditor()">Download</button>
<button onclick="shareCode()">Share</button>
<button onclick="evaluateCode()" class="run">Run Code</button>
</div>
<textarea id="codeInput">function tokenizeScheme(input) {
const tokens = [];
let current = 0;
const isWhitespace = (char) => /\s/.test(char);
const isDigit = (char) => /[0-9]/.test(char);
const isParen = (char) => char === '(' || char === ')';
// Symbols can include letters, numbers, and some punctuation like - _ ! ?
const isSymbolChar = (char) => /[a-zA-Z0-9\+\-\*\/\=\?\!\_]/.test(char);
while (current < input.length) {
let char = input[current];
if (isWhitespace(char)) {
current++;
continue;
}
if (isParen(char)) {
tokens.push({ type: 'paren', value: char });
current++;
continue;
}
if (isDigit(char) || (char === '-' && isDigit(input[current + 1]))) {
let number = '';
while (isDigit(char) || char === '-') {
number += char;
char = input[++current];
}
tokens.push({ type: 'number', value: number });
continue;
}
// Handle symbols, including letters, numbers, punctuation
if (isSymbolChar(char)) {
let symbol = '';
while (isSymbolChar(char)) {
symbol += char;
char = input[++current];
}
tokens.push({ type: 'symbol', value: symbol });
continue;
}
throw new Error(`Unexpected character: ${char}`);
}
return tokens;
}
function parseScheme(tokens) {
let current = 0;
function walk() {
let token = tokens[current];
if (token.type === 'number') {
current++;
return { type: 'NumberLiteral', value: Number(token.value) };
}
if (token.type === 'symbol') {
current++;
return { type: 'Symbol', value: token.value };
}
if (token.type === 'paren' && token.value === '(') {
current++;
const node = { type: 'List', value: [] };
while (!(tokens[current].type === 'paren' && tokens[current].value === ')')) {
node.value.push(walk());
}
current++; // Skip closing ')'
return node;
}
throw new Error(`Unexpected token: ${token.type}`);
}
return walk();
}
const globalEnv = {
'+': (...args) => args.reduce((acc, curr) => acc + curr),
'-': (...args) => args.reduce((acc, curr) => acc - curr),
'*': (...args) => args.reduce((acc, curr) => acc * curr),
'/': (a, b) => a / b, // Only two arguments for division
'eq?': (...args) => args.every((val, i, arr) => val === arr[0]),
'car': (list) => {
if (list.type !== 'List' || list.value.length === 0) {
throw new Error('car expects a non-empty list');
}
return list.value[0];
},
'cdr': (list) => {
if (list.type !== 'List' || list.value.length === 0) {
throw new Error('cdr expects a non-empty list');
}
return { type: 'List', value: list.value.slice(1) };
},
'cons': (a, b) => {
if (b.type !== 'List') {
throw new Error('cons expects second argument to be a list');
}
return { type: 'List', value: [a].concat(b.value) };
},
'null?': (list) => list.type === 'List' && list.value.length === 0,
'zero?': (n) => n === 0,
'atom?': (x) => typeof x !== 'object' || x === null,
'number?': (x) => typeof x === 'number',
'add1': (n) => n + 1,
'sub1': (n) => n - 1,
'quote': (x) => x, // Simply return the quoted expression
'and': (...args) => args.every(Boolean),
'or': (...args) => args.some(Boolean),
'true': true,
'false': false
};
function evaluate(node, env = globalEnv) {
if (node.type === 'NumberLiteral') {
return node.value;
}
if (node.type === 'Symbol') {
if (env[node.value] !== undefined) {
return env[node.value];
}
throw new Error(`Undefined symbol: ${node.value}`);
}
if (node.type === 'List') {
const [first, ...rest] = node.value;
// Is the first element a symbol, like an operator or function name?
if (first.type === 'Symbol') {
const operator = first.value;
// Special case for define
if (operator === 'define') {
const [symbol, expr] = rest;
env[symbol.value] = evaluate(expr, env);
return;
}
// Special case for lambda
if (operator === 'lambda') {
const [params, body] = rest;
// Create a closure to return
return function (...args) {
const lambdaEnv = { ...env };
// Bind each argument to the corresponding parameter...
params.value.forEach((param, i) => {
lambdaEnv[param.value] = args[i];
});
// ...and then evaluate the body with the environment
return evaluate(body, lambdaEnv);
};
}
// Special case for if
if (operator === 'if') {
const [test, consequent, alternate] = rest;
const condition = evaluate(test, env);
return condition ? evaluate(consequent, env) : evaluate(alternate, env);
}
// Special case for quote
if (operator === 'quote') {
return rest[0]; // Return the quoted expression without evaluating it
}
// Special case for cond
if (operator === 'cond') {
for (let clause of rest) {
const [test, expr] = clause.value;
if (evaluate(test, env)) {
return evaluate(expr, env);
}
}
return null; // No matching condition
}
// Special case for letrec (recursive let)
if (operator === 'letrec') {
const [bindings, body] = rest;
const letEnv = { ...env };
// Loop through bindings and evaluate each
bindings.value.forEach(binding => {
const [name, expr] = binding.value;
letEnv[name.value] = evaluate(expr, letEnv);
});
return evaluate(body, letEnv);
}
}
// Evaluate the first element
const func = evaluate(first, env);
if (typeof func !== 'function') {
throw new Error(`Expected a function but got: ${func}`);
}
const args = rest.map(arg => evaluate(arg, env));
return func(...args);
}
throw new Error(`Unexpected node type: ${node.type}`);
}
function evalScheme(input) {
const tokens = tokenizeScheme(input);
const ast = parseScheme(tokens);
return evaluate(ast);
}
function mountRepl(playground) {
// Create a REPL container
const replContainer = document.createElement('div');
replContainer.style.display = 'flex';
replContainer.style.flexDirection = 'column';
replContainer.style.width = '100%';
// Create an input field for the Scheme expressions
const input = document.createElement('textarea');
input.placeholder = "Scheme here...";
input.style.width = '100%';
input.style.height = '100px';
input.style.marginBottom = '10px';
input.style.fontFamily = 'monospace';
// Create a button to evaluate the expression
const evalButton = document.createElement('button');
evalButton.textContent = 'Evaluate';
// Create a container to display the results
const output = document.createElement('pre');
output.style.width = '100%';
output.style.height = '200px';
output.style.overflowY = 'auto';
output.style.backgroundColor = '#f0f0f0';
output.style.padding = '10px';
output.style.fontFamily = 'monospace';
// Add the input, button, and output to the REPL container
replContainer.appendChild(input);
replContainer.appendChild(evalButton);
replContainer.appendChild(output);
// Add the REPL container to the playground div
playground.appendChild(replContainer);
evalButton.addEventListener('click', () => {
const expression = input.value.trim();
if (expression) {
try {
// Evaluate the expression
const result = evalScheme(expression);
// Append the result to the output area
output.textContent += `> ${expression}\n${result}\n\n`;
} catch (error) {
// Error if the expression is invalid
output.textContent += `> ${expression}\nError: ${error.message}\n\n`;
}
}
// Clear input after evaluation
input.value = '';
});
}
mount(mountRepl);</textarea>
<div id="console"></div>
<script>
function evaluateCode() {
const code = document.getElementById('codeInput').value;
const consoleDiv = document.getElementById('console');
consoleDiv.innerHTML = '';
// Custom console.log function to output to the console div
const originalConsoleLog = console.log;
console.log = function(...args) {
args.forEach(arg => {
const output = document.createElement('div');
output.textContent = typeof arg === 'object' ? JSON.stringify(arg, null, 2) : arg;
consoleDiv.appendChild(output);
});
originalConsoleLog.apply(console, args);
};
try {
eval(code);
} catch (error) {
const errorOutput = document.createElement('div');
errorOutput.textContent = error;
errorOutput.style.color = 'red';
consoleDiv.appendChild(errorOutput);
}
// Restore browser's console.log
console.log = originalConsoleLog;
}
function downloadCodeAndEditor() {
const codeInput = document.getElementById('codeInput').value;
const htmlContent = document.documentElement.outerHTML.replace(
/<textarea id="codeInput"[^>]*>.*<\/textarea>/,
`<textarea id="codeInput">${codeInput}</textarea>`
);
const blob = new Blob([htmlContent], { type: 'text/html' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'code_editor.html';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
function shareCode() {
const code = document.getElementById('codeInput').value;
const encodedCode = btoa(encodeURIComponent(code));
window.location.hash = encodedCode;
window.prompt("Copy the URL to share.\nBe warned! Very long URLs don't share wicked well, sometimes.", window.location.href);
}
function clearEverything() {
if (!confirm('Are you sure you want to reset the playground?')) {
return;
} else {
window.location.hash = '';
window.location.reload();
}
}
function loadCodeFromHash() {
const hash = window.location.hash.substring(1);
if (hash) {
try {
const decodedCode = decodeURIComponent(atob(hash));
document.getElementById('codeInput').value = decodedCode;
} catch (error) {
console.error('Failed to decode the URL hash:', error);
}
}
}
function help() {
const helpText = `
Welcome to the JavaScript Playground! Here are some tips to get you started:
1. Enter your JavaScript code in the textarea.
2. Click the "Run Code" button to execute your code.
3. The console output will be displayed below the textarea.
4. Click the "Clear" button to reset the playground.
5. Click the "Download" button to save your code and editor as an HTML file.
6. Click the "Share" button to generate a URL to share your code with others.
7. You can also press "Cmd + Enter" to run your code.
8. There's an empty div above the buttons with the id "playground"
9. You can mount stuff to it using the "mount" function, for more info run "mountHelp()"
10. You can use the "clear()" function to clear the content's of the console.
Go nuts! Share scrappy fiddles!
`;
console.log(helpText);
}
function clear() {
document.getElementById('console').innerHTML = '';
}
function mountHelp() {
console.log(`
The mount function is used to mount stuff to the playground div.
It takes a function as an argument, which in turn receives the playground div as an argument.
Before mounting, it clears the playground div.
Here's an example of how to use the mount function:
mount(playground => {
const h1 = document.createElement('h1');
h1.textContent = 'Hell is empty and all the devils are here.';
playground.appendChild(h1);
});
This will add an h1 element to the playground div.
`);
}
function mount(mountFunction) {
const playground = document.getElementById('playground');
if (!playground) {
console.error("Couldn't find a div with the id 'playground'! You may need to reload the page.");
return;
}
if (playground.innerHTML.trim() !== "") {
playground.innerHTML = "";
}
mountFunction(playground);
}
document.getElementById('codeInput').addEventListener('keydown', function(event) {
if (event.metaKey && event.key === 'Enter') {
event.preventDefault();
evaluateCode();
}
});
window.onload = loadCodeFromHash;
</script>
</body></html>