Public
Edited
Apr 23, 2023
Importers
Insert cell
Insert cell
error = ({reason, input, cursor}) => {

let inputPreview = "";
if(cursor) {
inputPreview = input?.slice(cursor, 10) + (input?.length > 10 ? "…" : "");
}
return {error: true, reason: reason, input: inputPreview, cursor};
}
Insert cell
success = ({input, cursor}) => ({ast: [], input, cursor});
Insert cell
char = (c) => ({input, cursor}) => {

if(input == "") return error({reason: "Input stream is empty", input: "", cursor: null});

if(c == input[cursor]) return {ast: c, input, cursor: cursor + 1, desc: c};

else return error({reason: "Expected " + c + " but got " + input[cursor], stream: input});
}
Insert cell
or = (p1, p2) => ({input, cursor}) => {
const {ast, cursor: cursor1, error: error1} = p1({input, cursor});
if(!error1) return {ast, input, cursor: cursor1};
else {
let {ast: ast2, cursor: cursor2, error: error2} = p2({input, cursor});
if(!error2) return {ast: ast2, input, cursor: cursor2};
else return error({reason: "Couldn’t pass the OR test. " + error1 + ", " + error2, input, cursor});
}
}
Insert cell
and = (p1, p2) => ({input, cursor}) => {
const {ast, input: input1, cursor: cursor1, error, reason} = p1({input, cursor});
if(!error) {
const {ast: ast2, cursor: cursor2, error, reason} = p2({input, cursor: cursor1});
if(!error) return {ast: [].concat(ast).concat(ast2), input, cursor: cursor2};
else return {error, reason, input, cursor};
} else return {error, reason, input, cursor};
}
Insert cell
parse = (predicate, input) => predicate({input, cursor: 0});
Insert cell
parse(char("a"), "");
Insert cell
input1 = "1";
Insert cell
input2 = "0"
Insert cell
input3 = "1010101"
Insert cell
input4 = "00110011"
Insert cell
parse(char("1"), input1);
Insert cell
parse(char("0"),input1);
Insert cell
zero = char("0");
Insert cell
one = char("1");
Insert cell
parse(zero, input2);
Insert cell
parse(one, input2);
Insert cell
bit = or(zero, one)
Insert cell
Insert cell
parse(bit, input1)
Insert cell
parse(bit, input2);
Insert cell
parse(bit, input3)
Insert cell
parse(bit, input4);
Insert cell
oneThenZero = and(zero, one);
Insert cell
zeroThenOne = and(one, zero);
Insert cell
parse(oneThenZero, input1);
Insert cell
parse(oneThenZero, input2);
Insert cell
parse(oneThenZero, input3);
Insert cell
parse(zeroThenOne, input1);
Insert cell
parse(zeroThenOne, input2);
Insert cell
parse(zeroThenOne, input3);
Insert cell
parse(zeroThenOne, input4);
Insert cell
{ let result = parse(and(bit, bit), "10").ast;
let formatBit = x => x == "0" ? "⚪️" : "⚫️";
let out = result.map(formatBit);
let evaluation = result.some(x => x == "0") ? 0 : 1;
let evaluationOr = result.some(x => x == "1") ? 1 : 0;
return out.join(" & ") + " = " + formatBit(evaluation) + " *|*|* " + out.join(" | ") + " = " + formatBit(evaluationOr);
};
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more