Skip to content

Commit

Permalink
Added basic syntax highlighting to wiki
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinborner committed Nov 11, 2023
1 parent ff53e25 commit bd1b669
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 117 deletions.
36 changes: 36 additions & 0 deletions docs/code.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
.com {
color: #ff64bd;
}

.def {
color: #13dbee;
}

.comment {
color: #999999;
}

.type {
color: #ffa120;
}

.left-abs, .right-abs {
color: #6b82ff;
}

.left-app, .right-app {
color: #ff8750;
}

.index {
color: #ff5050;
}

.number {
color: #b1ee13;
}

/* lol */
.string, .string * {
color: #bb73f0 !important;
}
56 changes: 56 additions & 0 deletions docs/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// high-quality syntax highlighter
// TODO: Implement actual highlighter (or fix many regex bugs)

const term = (t) =>
t
.replaceAll(/'(.)'/g, "<span class='string'>'$1'</span>")
.replaceAll(/"([^\"]*)"/g, "<span class='string'>\"$1\"</span>")
.replaceAll(/(\([+-][0-9]+[ubt]?\))/g, "<span class='number'>$1</span>")
.replaceAll(/(?<!\>)(\()/g, "<span class='left-app'>(</span>")
.replaceAll(/(\))(?!\<)/g, "<span class='right-app'>)</span>")
.replaceAll("[", "<span class='left-abs'>[</span>")
.replaceAll("]", "<span class='right-abs'>]</span>")
.replaceAll(/(?<![+-\d])([0-9])/g, "<span class='index'>$1</span>");

const highlightTerm = (elem) => {
elem.innerHTML = term(elem.innerHTML);
};

const highlight = (elem) => {
const fixPath = (p) => p.replace("/", "_");

elem.innerHTML = elem.innerHTML
.replaceAll(
/^:import std\/(.*) (.*)$/gm,
(_, p, s) =>
`<span class="com">:import</span> <a href='${fixPath(
p,
)}.bruijn.html'>std/${p}</a> ${s}`,
)
.replaceAll(
/^:input std\/(.*)$/gm,
(_, p) =>
`<span class="com">:input</span> <a href='${fixPath(
p,
)}.bruijn.html'>std/${p}</a>`,
)
.replaceAll(
/^:test (\(.*\)) (\(.*\))$/gm,
(_, t1, t2) => `<span class='com'>:test</span> ${term(t1)} ${term(t2)}`,
)
.replaceAll(
/^:time (.*)$/gm,
(_, t) => `<span class='com'>:time</span> ${term(t)}`,
)
.replaceAll(
/^([^:\n<#][^ ]*) (.*)$/gm,
(_, d, t) => `<span class='def'>${d}</span> ${term(t)}`,
)
.replaceAll(/^# (.*)$/gm, "<span class='comment'># $1</span>")
.replaceAll(/ ⧗ (.*)\n/g, " ⧗ <span class='type'>$1</span>\n");
};

window.onload = () => {
[...document.querySelectorAll("code.language-bruijn")].forEach(highlight);
[...document.querySelectorAll("code.bruijn")].forEach(highlightTerm);
};
54 changes: 2 additions & 52 deletions docs/content.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,56 +15,6 @@ a {
}

pre {
margin: 0 0 1em;
padding: .5em 1em;
}

pre .line {
display: block;
float: left;
margin: 0 1em 0 -1em;
border-right: 1px solid;
text-align: right;
}

pre .line span {
display: block;
padding: 0 .5em 0 1em;
}

pre .cl {
display: block;
clear: both;
}

.com {
color: #ff64bd;
}

.def {
color: #13dbee;
}

.comment {
color: #999999;
}

.type {
color: #ffa120;
}

.left-abs, .right-abs {
color: #6b82ff;
}

.left-app, .right-app {
color: #ff8750;
}

.index {
color: #ff5050;
}

.number {
color: #b1ee13;
margin: 0;
padding: .5em;
}
26 changes: 0 additions & 26 deletions docs/content.js

This file was deleted.

5 changes: 4 additions & 1 deletion docs/content.template
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<link rel="stylesheet" href="content.css" type="text/css" media="all">
<link rel="stylesheet" href="code.css" type="text/css" media="all">
<title>bruijn std/NAME</title>
</head>
<body>
<h1>NAME</h1>
<pre>
<code class="language-bruijn">
CONTENT
</code>
</pre>

<script src="content.js" charset="utf-8"></script>
<script src="code.js" charset="utf-8"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion docs/gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ done

sed -e "s@LINKS@$links@g" index.template >std/index.html

cp res/* content.js content.css index.css std/
cp res/* code.js content.css index.css code.css std/

echo "std done"

Expand Down
3 changes: 2 additions & 1 deletion docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ site_name: Bruijn Wiki
docs_dir: wiki_src/
site_dir: wiki/

extra_css: [custom.css]
extra_css: [custom.css, ../code.css]
extra_javascript: [../code.js]

theme:
name: terminal
Expand Down
23 changes: 12 additions & 11 deletions docs/wiki_src/coding/compilation.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Compilation

Bruijn can be compiled to John Tromps binary lambda calculus (BLC).
Bruijn can be compiled to John Tromp's binary lambda calculus (BLC).

BLC uses the following encoding:

Expand All @@ -12,26 +12,27 @@ BLC uses the following encoding:

There are two modes of compilation:

- **Bitwise** compiles to BLC and encodes every bit as 1 bit and pads
the last remaining byte: `bruijn -b path`
- **ASCII** compiles to BLC and encodes every bit as 1 ASCII character
(`'0'`/`'1'`): `bruijn -B path`
- **Bitwise** compiles to BLC and encodes every bit as 1 bit and pads
the last remaining byte: `bruijn -b path`
- **ASCII** compiles to BLC and encodes every bit as 1 ASCII character
(`'0'`/`'1'`): `bruijn -B path`

## Compilation overhead

Typical compilation to BLC results in much redundant code, since every
used function gets substituted and translated separately. In
`((+3) + (+4) + (+3))`, for example, `add` gets compiled to BLC two
times, resulting in a redundant overhead of around 3500 bits.
`((+3) + (+4) + (+3))`{.bruijn}, for example, `add`{.bruijn} gets
compiled to BLC two times, resulting in a redundant overhead of around
3500 bits.

This is because BLC was never intended for compilation of normal
programs, but mainly as an academic encoding model. This also means that
its quite good for writing very expressive and minimal programs
(i.e. obfuscated code golfing, see [John Tromps
it's quite good for writing very expressive and minimal programs
(i.e. obfuscated code golfing, see [John Tromp's
IOCCC](https://ioccc.org/2012/tromp/hint.html)).

Most programs, however, wont be golfed and can result in rather large
compiled programs. While theres not really any practical need for
Most programs, however, won't be golfed and can result in rather large
compiled programs. While there's not really any practical need for
compilation aside from golfing, you could still use the
[BLoC](https://github.com/marvinborner/bloc) project to optimize
redundant terms.
Expand Down
2 changes: 1 addition & 1 deletion docs/wiki_src/coding/data-structures.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ syntactic sugar.

- default syntactic sugar for numbers (optional suffix `t`),
e.g. `(+3)`{.bruijn}
- encoding: `(+4)`{.bruijn}=`[[[[(1 (1 3))]]]]`,
- encoding: `(+4)`{.bruijn}=`[[[[(1 (1 3))]]]]`{.bruijn},
`(-4)`{.bruijn}=`[[[[(2 (2 3))]]]]`{.bruijn}
- faster and more compact than binary[^1]
- positive and negative numbers
Expand Down
10 changes: 4 additions & 6 deletions docs/wiki_src/coding/meta-programming.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Meta programming
# Metaprogramming

Bruijn has a homoiconic meta encoding inspired by Lisp's quoting
feature.
Expand All @@ -8,11 +8,9 @@ self-interpretation](https://text.marvinborner.de/2023-09-03-21.html).

## Encoding

``` bruijn
`X ⤳ [[[2 (+Xu)]]]
`(M N) ⤳ [[[1 `M `N]]]
`[M] ⤳ [[[0 `M]]]
```
`X ⤳ [[[2 (+Xu)]]]
`(M N) ⤳ [[[1 `M `N]]]
`[M] ⤳ [[[0 `M]]]

Any quoted term gets converted to this encoding:

Expand Down
11 changes: 10 additions & 1 deletion docs/wiki_src/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ pre {
width: 100%;
white-space: pre;
overflow-x: scroll;
display: inline-grid;
display: grid;
}

/* to match std */
pre, pre > code, code.bruijn {
background-color: #222222;
}

pre a {
color: var(--font-color);
}

.headerlink {
Expand Down
26 changes: 12 additions & 14 deletions docs/wiki_src/introduction/lambda-calculus.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ When we talk about reduction in bruijn, we typically mean "reduction
until normal form" -- we reduce until the term can't be reduced any
further (there does not exist any `((λx.E) A)`).

``` bruijn
((λx.x) (λx.x)) ⤳ (λx.x)
((λx.x) (λx.λy.x)) ⤳ (λx.λy.x)
((λx.λy.x) (λx.x)) ⤳ (λy.λx.x)
```
((λx.x) (λx.x)) ⤳ (λx.x)
((λx.x) (λx.λy.x)) ⤳ (λx.λy.x)
((λx.λy.x) (λx.x)) ⤳ (λy.λx.x)

## De Bruijn indices

Expand All @@ -49,9 +47,9 @@ De Bruijn replace the concept of variables by using numeric references
to the abstraction layer. Let me explain using an example: The
expression `λx.x` becomes `λ0` -- the `0` refers to the first parameter
of the abstraction. Subsequently, the expression `λx.λy.x y` becomes
`λλ1 0`{.bruijn}. Basically, if you're reading from left to right
starting at the abstraction you want to bind, you increment on every
occurring `λ` until you arrive at the index.
`λλ1 0`. Basically, if you're reading from left to right starting at the
abstraction you want to bind, you increment on every occurring `λ` until
you arrive at the index.

While confusing at first, programs written with De Bruijn indices can
actually be way easier to understand than the equivalent program with
Expand All @@ -72,10 +70,10 @@ The application of `λ0` and `λλ1 0` becomes `[0] [[1 0]]`{.bruijn}.
Random example reductions:

``` bruijn
[0] [[1]] ⤳ [[1]]
[[0]] [[1]] ⤳ [0]
[[1]] [[1]] ⤳ [[[1]]]
[[0]] [0] [[1]] ⤳ [[1]]
[[0 1]] [0] ⤳ [0 [0]]
[[1 0]] [0] ⤳ [0]
a [0] [[1]] ⤳ [[1]]
b [[0]] [[1]] ⤳ [0]
c [[1]] [[1]] ⤳ [[[1]]]
d [[0]] [0] [[1]] ⤳ [[1]]
e [[0 1]] [0] ⤳ [0 [0]]
f [[1 0]] [0] ⤳ [0]
```
6 changes: 3 additions & 3 deletions docs/wiki_src/introduction/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ structures](../coding/data-structures.md).

- *Numbers*: `(SXB)`{.bruijn} where `S` is `+`/`-`, `X` is a number
and `B` is the *base* of the encoded number (or `t` by default)
- `u` for unary base (postive, Church): `(+42u)`
- `b` for binary base (positive): `(+42b)`
- `t` for balanced ternary (positive/negative): `(-42t)`
- `u` for unary base (postive, Church): `(+42u)`{.bruijn}
- `b` for binary base (positive): `(+42b)`{.bruijn}
- `t` for balanced ternary (positive/negative): `(-42t)`{.bruijn}
- *Characters*: `'C'`{.bruijn} where `C` is any ASCII character
- *Strings*: `"X1..XN"`{.bruijn} where `X1...XN` are any ASCII
characters
Expand Down

0 comments on commit bd1b669

Please sign in to comment.