Skip to content

Commit e56eccd

Browse files
authored
Merge pull request #3 from Kacarott/dev
Fixes and Improvements
2 parents 1ccad77 + 320dae9 commit e56eccd

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

index.html

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,18 @@ <h1>
4040
# Multiple definition
4141
true = not false
4242

43-
# Invalid names
44-
%value = ()
45-
4643
# Invalid whitespace (tabs)
4744
whitespace = ()
4845

49-
# Bare term
46+
# Bare lambda
5047
(\ f x . f (x x))
5148

49+
# Bare term
50+
const f x
51+
52+
# Symbols
53+
< a b c > => < a a a >
54+
5255
# Unbound
5356
some-func = \ local . true non-existent local
5457

@@ -58,9 +61,18 @@ <h1>
5861
# Debug mode on
5962
#debug
6063

61-
# Bare term - Debug
64+
# Invalid names - Debug
65+
%value = ()
66+
67+
# Bare lambda - Debug
6268
(\ f x . f (x x))
6369

70+
# Bare term - Debug
71+
const f x
72+
73+
# Symbols - Debug
74+
< a b c > => < a a a >
75+
6476
# Unbound - Debug
6577
some-func = \ local . true non-existent local
6678

lambdacalc.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ CodeMirror.defineMode("lambdacalc", function(_config, modeConfig) {
1414
const EMPTY = "text";
1515
const UNDEF = "error";
1616
const REDEF = "variable-3";
17+
const SUPPRESS = "text";
1718
const FAIL = "error";
1819

1920
const defName = /[a-zA-Z][a-zA-Z0-9_\-']*/
@@ -23,14 +24,14 @@ CodeMirror.defineMode("lambdacalc", function(_config, modeConfig) {
2324
const numconst = /\d+/
2425

2526
function expectDefOrTerm(stream, state) {
26-
return expectDef(stream, state)
27-
|| (state.debug ? null : expectTerm(stream, state));
27+
if (stream.match(/.*=/, false)) return expectDef(stream, state);
28+
else return expectTerm(stream, state);
2829
}
2930

3031
function expectDef(stream, state) {
3132
const name = (stream.match(defName)||[])[0];
3233
state.f = expectAssign;
33-
if (!name || !(/[=\s]/.test(stream.peek()) || stream.eol())) return null;
34+
if (!name || !(stream.match(/\s*=/, false))) return null;
3435
const res = [];
3536
if (state.defined.includes(name)) res.push(REDEF);
3637
state.defined.push(name);
@@ -81,7 +82,7 @@ CodeMirror.defineMode("lambdacalc", function(_config, modeConfig) {
8182
if (!res) return null;
8283
if (state.bound.some(v=>v.includes(res))) return BOUND;
8384
if (state.defined.includes(res)) return PREDEF;
84-
return state.debug ? UNDEF : "text";
85+
return UNDEF;
8586
}
8687

8788
function number(stream, state) {
@@ -103,12 +104,12 @@ CodeMirror.defineMode("lambdacalc", function(_config, modeConfig) {
103104

104105
function onFail(stream, state) {
105106
stream.match(/[^\s]*/);
106-
return FAIL
107+
return FAIL ;
107108
}
108109

109110
return {
110111
startState: function () { return {
111-
f: expectDefOrTerm,
112+
f: expectDef,
112113
depth: [],
113114
defined: [],
114115
bound: [[]],
@@ -136,13 +137,15 @@ CodeMirror.defineMode("lambdacalc", function(_config, modeConfig) {
136137
}
137138
if (stream.sol() && state.depth.length === 0) {
138139
state.bound = [[]];
139-
state.f = expectDefOrTerm;
140+
state.f = expectDef;
140141
}
141-
return state.f(stream, state) || onFail(stream, state);
142+
const res = state.f(stream, state)
143+
|| (state.debug ? null : expectDefOrTerm(stream, state))
144+
|| onFail(stream, state);
145+
return !state.debug && res == FAIL ? SUPPRESS : res ;
142146
},
143147

144148
indent: function(state, textAfter) {
145-
console.log(state.depth);
146149
if (!state.depth.length) return 0;
147150
return state.depth[state.depth.length-1] + 2;
148151
},

0 commit comments

Comments
 (0)