From 574af611c9b4098384c17b69017b2392f26e8dbf Mon Sep 17 00:00:00 2001 From: Kacarott Date: Fri, 25 Feb 2022 17:30:20 +0100 Subject: [PATCH 1/2] Fix and improve --- lambdacalc.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/lambdacalc.js b/lambdacalc.js index 09b7f05..8f5bfce 100644 --- a/lambdacalc.js +++ b/lambdacalc.js @@ -14,6 +14,7 @@ CodeMirror.defineMode("lambdacalc", function(_config, modeConfig) { const EMPTY = "text"; const UNDEF = "error"; const REDEF = "variable-3"; + const SUPPRESS = "text"; const FAIL = "error"; const defName = /[a-zA-Z][a-zA-Z0-9_\-']*/ @@ -23,14 +24,14 @@ CodeMirror.defineMode("lambdacalc", function(_config, modeConfig) { const numconst = /\d+/ function expectDefOrTerm(stream, state) { - return expectDef(stream, state) - || (state.debug ? null : expectTerm(stream, state)); + if (stream.match(/.*=/, false)) return expectDef(stream, state); + else return expectTerm(stream, state); } function expectDef(stream, state) { const name = (stream.match(defName)||[])[0]; state.f = expectAssign; - if (!name || !(/[=\s]/.test(stream.peek()) || stream.eol())) return null; + if (!name || !(stream.match(/\s*=/, false))) return null; const res = []; if (state.defined.includes(name)) res.push(REDEF); state.defined.push(name); @@ -81,7 +82,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 state.debug ? UNDEF : "text"; + return UNDEF; } function number(stream, state) { @@ -103,12 +104,12 @@ CodeMirror.defineMode("lambdacalc", function(_config, modeConfig) { function onFail(stream, state) { stream.match(/[^\s]*/); - return FAIL + return FAIL ; } return { startState: function () { return { - f: expectDefOrTerm, + f: expectDef, depth: [], defined: [], bound: [[]], @@ -136,13 +137,15 @@ CodeMirror.defineMode("lambdacalc", function(_config, modeConfig) { } if (stream.sol() && state.depth.length === 0) { state.bound = [[]]; - state.f = expectDefOrTerm; + state.f = expectDef; } - return state.f(stream, state) || onFail(stream, state); + const res = state.f(stream, state) + || (state.debug ? null : expectDefOrTerm(stream, state)) + || onFail(stream, state); + return !state.debug && res == FAIL ? SUPPRESS : res ; }, indent: function(state, textAfter) { - console.log(state.depth); if (!state.depth.length) return 0; return state.depth[state.depth.length-1] + 2; }, From 320dae9220c336fc1949333a1db6be1580ac0686 Mon Sep 17 00:00:00 2001 From: Kacarott Date: Fri, 25 Feb 2022 17:30:36 +0100 Subject: [PATCH 2/2] Update showcase code --- index.html | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/index.html b/index.html index c54128c..3b8e469 100644 --- a/index.html +++ b/index.html @@ -40,15 +40,18 @@

# Multiple definition true = not false -# Invalid names -%value = () - # Invalid whitespace (tabs) whitespace = () -# Bare term +# Bare lambda (\ f x . f (x x)) +# Bare term +const f x + +# Symbols +< a b c > => < a a a > + # Unbound some-func = \ local . true non-existent local @@ -58,9 +61,18 @@

# Debug mode on #debug -# Bare term - Debug +# Invalid names - Debug +%value = () + +# Bare lambda - Debug (\ f x . f (x x)) +# Bare term - Debug +const f x + +# Symbols - Debug +< a b c > => < a a a > + # Unbound - Debug some-func = \ local . true non-existent local