Skip to content

Fix issues and improve #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ <h1>

<form>
<textarea id="code">
# Some code (with ignored arguments)
# Ignored arguments
false = \ _a b . b
true = \ a _b . a
not = \ b . b false true
Expand All @@ -43,16 +43,37 @@ <h1>
# Invalid names
%value = ()

# Invalid args - unbound
someFunc = \ local . true nonexistant local
# Invalid whitespace (tabs)
whitespace = ()

# Invalid args - scoped
otherFunc = \ x . const (\ scopedArg . x ()) scopedArg x
# Bare term
(\ f x . f (x x))

# Unbound
some-func = \ local . true non-existent local

# Out of scope args
other-func = \ x . const (\ scoped-arg . x ()) scoped-arg x

# Debug mode on
#debug

# Bare term - Debug
(\ f x . f (x x))

# Unbound - Debug
some-func = \ local . true non-existent local

# Out of scope args - Debug
other-func = \ x . const (\ scoped-arg . x ()) scoped-arg x

# Debug mode off
#debug

# More code
zero = false
succ = \ n f x . f (n f x)
isZ = \ n . n (const false) true
is-z = \ n . n (const false) true
add = \ a b f x . a f (b f x)
mul = \ a b f . a (b f)
three = succ (succ (succ zero))
Expand All @@ -75,15 +96,15 @@ <h1>
map = \ f xs . null xs nil (cons (f (head xs)) (map f (tail xs)))
sum = foldr add zero
drop = \ n . n tail
take = \ n xs . isZ n nil (cons (head xs) (take (pred n) (tail xs)))
take = \ n xs . is-z n nil (cons (head xs) (take (pred n) (tail xs)))
col = \ n xs . head (drop n xs)
colS = \ n xs . cons (col n xs) (cons (col (succ n) xs) (cons (col (succ (succ n)) xs) (nil)))
row = \ n xs . map (col n) xs
rowS = \ n xs . cons (row n xs) (cons (row (succ n) xs) (cons (row (succ (succ n)) xs) (nil)))
chunk = \ a b xs . rowS a (colS b xs)
row-s = \ n xs . cons (row n xs) (cons (row (succ n) xs) (cons (row (succ (succ n)) xs) (nil)))
chunk = \ a b xs . row-s a (colS b xs)
append = \ as bs . null as bs (cons (head as) (append (tail as) bs))
concat = foldr append nil
eq = \ a b . isZ a (isZ b) (isZ b false (eq (pred a) (pred b)))
eq = \ a b . is-z a (is-z b) (is-z b false (eq (pred a) (pred b)))
all = foldr (\ a b . a b false) true
allf = \ f xs . all (map f xs)
</textarea>
Expand Down
27 changes: 19 additions & 8 deletions lambdacalc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CodeMirror.defineMode("lambdacalc", function(_config, modeConfig) {
const BRACKETS = "bracket";
const LAMBDA = "keyword";
const DOT = LAMBDA;
const PREDEF = "variable";
const PREDEF = "text";
const BOUND = "text";
const ARGS = "def";
const HOLE = "atom";
Expand All @@ -22,6 +22,11 @@ CodeMirror.defineMode("lambdacalc", function(_config, modeConfig) {
const lamArg = /[a-zA-Z_][a-zA-Z0-9_\-']*|\./
const numconst = /\d+/

function expectDefOrTerm(stream, state) {
return expectDef(stream, state)
|| (state.debug ? null : expectTerm(stream, state));
}

function expectDef(stream, state) {
const name = (stream.match(defName)||[])[0];
state.f = expectAssign;
Expand Down Expand Up @@ -61,6 +66,7 @@ CodeMirror.defineMode("lambdacalc", function(_config, modeConfig) {
state.depth.pop();
state.bound.pop();
}
state.f = expectTerm;
return BRACKETS;
}

Expand All @@ -75,7 +81,7 @@ CodeMirror.defineMode("lambdacalc", function(_config, modeConfig) {
if (!res) return null;
if (state.bound.some(v=>v.includes(res))) return BOUND;
if (state.defined.includes(res)) return PREDEF;
return UNDEF;
return state.debug ? UNDEF : "text";
}

function number(stream, state) {
Expand All @@ -102,30 +108,35 @@ CodeMirror.defineMode("lambdacalc", function(_config, modeConfig) {

return {
startState: function () { return {
f: expectDef,
f: expectDefOrTerm,
depth: [],
defined: [],
bound: [[]]
bound: [[]],
debug: false
}; },
copyState: function (s) { return {
f: s.f,
depth: [...s.depth],
defined: [...s.defined],
bound: s.bound.map(v=>[...v])
bound: s.bound.map(v=>[...v]),
debug: s.debug
}; },

token: function(stream, state) {
if (/\s/.test(stream.peek())) {
stream.eatSpace();
if (stream.eat(/\t/)) return FAIL;
if (/[ \n]/.test(stream.peek())) {
stream.eatWhile(/[ \n]/);
return;
}
if (stream.peek() === '#') {
if (stream.match(/^#debug/))
state.debug = !state.debug;
stream.skipToEnd();
return "comment"
}
if (stream.sol() && state.depth.length === 0) {
state.bound = [[]];
state.f = expectDef;
state.f = expectDefOrTerm;
}
return state.f(stream, state) || onFail(stream, state);
},
Expand Down