Skip to content

Commit

Permalink
Merge pull request #2 from Kacarott/dev
Browse files Browse the repository at this point in the history
Fix issues and improve
  • Loading branch information
Kacarott authored Feb 19, 2022
2 parents 6a2085f + b89d14f commit c004133
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 18 deletions.
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

0 comments on commit c004133

Please sign in to comment.