diff --git a/index.html b/index.html
index c54128c..40b85af 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
@@ -104,7 +116,7 @@
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 . is-z a (is-z b) (is-z 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)
diff --git a/lambdacalc.js b/lambdacalc.js
index 09b7f05..47c00fe 100644
--- a/lambdacalc.js
+++ b/lambdacalc.js
@@ -14,23 +14,24 @@ 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_\-']*/
+ const defName = /[a-zA-Z][-'\w]*\??/
const assign = /=/
const brack = /\(|\)/
- const lamArg = /[a-zA-Z_][a-zA-Z0-9_\-']*|\./
- const numconst = /\d+/
+ const lamArg = /[_a-zA-Z][-'\w]*\??|\./
+ 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);
@@ -63,6 +64,7 @@ CodeMirror.defineMode("lambdacalc", function(_config, modeConfig) {
state.bound.push([]);
}
else {
+ if (!(state.depth.length && state.bound.length)) return FAIL;
state.depth.pop();
state.bound.pop();
}
@@ -81,7 +83,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) {
@@ -102,13 +104,13 @@ CodeMirror.defineMode("lambdacalc", function(_config, modeConfig) {
}
function onFail(stream, state) {
- stream.match(/[^\s]*/);
- return FAIL
+ stream.match(/[^\s#]*/);
+ return FAIL ;
}
return {
startState: function () { return {
- f: expectDefOrTerm,
+ f: expectDef,
depth: [],
defined: [],
bound: [[]],
@@ -136,13 +138,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;
},
diff --git a/package-lock.json b/package-lock.json
index e31df3d..5f0a862 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "@codewars/codemirror-lambda-calculus",
- "version": "0.2.0",
+ "version": "0.3.4",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@codewars/codemirror-lambda-calculus",
- "version": "0.2.0",
+ "version": "0.3.4",
"license": "MIT",
"devDependencies": {
"postcss-nesting": "^10.1.2",
diff --git a/package.json b/package.json
index d60b3d9..b6463e5 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "@codewars/codemirror-lambda-calculus",
- "version": "0.2.0",
+ "version": "0.3.4",
"type": "module",
"description": "Lambda Calculus mode for CodeMirror",
"main": "lambdacalc.js",