Skip to content

Commit c004133

Browse files
authored
Merge pull request #2 from Kacarott/dev
Fix issues and improve
2 parents 6a2085f + b89d14f commit c004133

File tree

2 files changed

+50
-18
lines changed

2 files changed

+50
-18
lines changed

index.html

+31-10
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ <h1>
3131

3232
<form>
3333
<textarea id="code">
34-
# Some code (with ignored arguments)
34+
# Ignored arguments
3535
false = \ _a b . b
3636
true = \ a _b . a
3737
not = \ b . b false true
@@ -43,16 +43,37 @@ <h1>
4343
# Invalid names
4444
%value = ()
4545

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

49-
# Invalid args - scoped
50-
otherFunc = \ x . const (\ scopedArg . x ()) scopedArg x
49+
# Bare term
50+
(\ f x . f (x x))
51+
52+
# Unbound
53+
some-func = \ local . true non-existent local
54+
55+
# Out of scope args
56+
other-func = \ x . const (\ scoped-arg . x ()) scoped-arg x
57+
58+
# Debug mode on
59+
#debug
60+
61+
# Bare term - Debug
62+
(\ f x . f (x x))
63+
64+
# Unbound - Debug
65+
some-func = \ local . true non-existent local
66+
67+
# Out of scope args - Debug
68+
other-func = \ x . const (\ scoped-arg . x ()) scoped-arg x
69+
70+
# Debug mode off
71+
#debug
5172

5273
# More code
5374
zero = false
5475
succ = \ n f x . f (n f x)
55-
isZ = \ n . n (const false) true
76+
is-z = \ n . n (const false) true
5677
add = \ a b f x . a f (b f x)
5778
mul = \ a b f . a (b f)
5879
three = succ (succ (succ zero))
@@ -75,15 +96,15 @@ <h1>
7596
map = \ f xs . null xs nil (cons (f (head xs)) (map f (tail xs)))
7697
sum = foldr add zero
7798
drop = \ n . n tail
78-
take = \ n xs . isZ n nil (cons (head xs) (take (pred n) (tail xs)))
99+
take = \ n xs . is-z n nil (cons (head xs) (take (pred n) (tail xs)))
79100
col = \ n xs . head (drop n xs)
80101
colS = \ n xs . cons (col n xs) (cons (col (succ n) xs) (cons (col (succ (succ n)) xs) (nil)))
81102
row = \ n xs . map (col n) xs
82-
rowS = \ n xs . cons (row n xs) (cons (row (succ n) xs) (cons (row (succ (succ n)) xs) (nil)))
83-
chunk = \ a b xs . rowS a (colS b xs)
103+
row-s = \ n xs . cons (row n xs) (cons (row (succ n) xs) (cons (row (succ (succ n)) xs) (nil)))
104+
chunk = \ a b xs . row-s a (colS b xs)
84105
append = \ as bs . null as bs (cons (head as) (append (tail as) bs))
85106
concat = foldr append nil
86-
eq = \ a b . isZ a (isZ b) (isZ b false (eq (pred a) (pred b)))
107+
eq = \ a b . is-z a (is-z b) (is-z b false (eq (pred a) (pred b)))
87108
all = foldr (\ a b . a b false) true
88109
allf = \ f xs . all (map f xs)
89110
</textarea>

lambdacalc.js

+19-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ CodeMirror.defineMode("lambdacalc", function(_config, modeConfig) {
66
const BRACKETS = "bracket";
77
const LAMBDA = "keyword";
88
const DOT = LAMBDA;
9-
const PREDEF = "variable";
9+
const PREDEF = "text";
1010
const BOUND = "text";
1111
const ARGS = "def";
1212
const HOLE = "atom";
@@ -22,6 +22,11 @@ CodeMirror.defineMode("lambdacalc", function(_config, modeConfig) {
2222
const lamArg = /[a-zA-Z_][a-zA-Z0-9_\-']*|\./
2323
const numconst = /\d+/
2424

25+
function expectDefOrTerm(stream, state) {
26+
return expectDef(stream, state)
27+
|| (state.debug ? null : expectTerm(stream, state));
28+
}
29+
2530
function expectDef(stream, state) {
2631
const name = (stream.match(defName)||[])[0];
2732
state.f = expectAssign;
@@ -61,6 +66,7 @@ CodeMirror.defineMode("lambdacalc", function(_config, modeConfig) {
6166
state.depth.pop();
6267
state.bound.pop();
6368
}
69+
state.f = expectTerm;
6470
return BRACKETS;
6571
}
6672

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

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

103109
return {
104110
startState: function () { return {
105-
f: expectDef,
111+
f: expectDefOrTerm,
106112
depth: [],
107113
defined: [],
108-
bound: [[]]
114+
bound: [[]],
115+
debug: false
109116
}; },
110117
copyState: function (s) { return {
111118
f: s.f,
112119
depth: [...s.depth],
113120
defined: [...s.defined],
114-
bound: s.bound.map(v=>[...v])
121+
bound: s.bound.map(v=>[...v]),
122+
debug: s.debug
115123
}; },
116124

117125
token: function(stream, state) {
118-
if (/\s/.test(stream.peek())) {
119-
stream.eatSpace();
126+
if (stream.eat(/\t/)) return FAIL;
127+
if (/[ \n]/.test(stream.peek())) {
128+
stream.eatWhile(/[ \n]/);
120129
return;
121130
}
122131
if (stream.peek() === '#') {
132+
if (stream.match(/^#debug/))
133+
state.debug = !state.debug;
123134
stream.skipToEnd();
124135
return "comment"
125136
}
126137
if (stream.sol() && state.depth.length === 0) {
127138
state.bound = [[]];
128-
state.f = expectDef;
139+
state.f = expectDefOrTerm;
129140
}
130141
return state.f(stream, state) || onFail(stream, state);
131142
},

0 commit comments

Comments
 (0)