-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambdacalc.js
160 lines (140 loc) · 4.03 KB
/
lambdacalc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: https://codemirror.net/LICENSE
// Lambda Calculus Mode written by Keldan Chapman (Kacarott on CodeWars)
(function(mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(require("../../lib/codemirror"));
else if (typeof define == "function" && define.amd) // AMD
define(["../../lib/codemirror"], mod);
else // Plain browser env
mod(CodeMirror);
})(function(CodeMirror) {
"use strict";
CodeMirror.defineMode("lambdacalc", function(_config, modeConfig) {
// Modes for different types of symbols
const DEFNAME = "builtin";
const EQUALS = "text";
const BRACKETS = "bracket";
const LAMBDA = "atom";
const DOT = LAMBDA;
const PREDEF = "text";
const BOUND = "text";
const ARGS = "variable";
const HOLE = "variable-2";
const NUMBER = "number";
const EMPTY = "variable-3";
const UNDEF = "error";
const REDEF = "error";
const FAIL = "error";
const defName = /[a-zA-Z][a-zA-Z0-9_\-']*/
const assign = /=/
const brack = /\(|\)/
const lamArg = /[a-zA-Z_][a-zA-Z0-9_\-']*|\./
const numconst = /\d+/
function expectDef(stream, state) {
const name = (stream.match(defName)||[])[0];
state.f = expectAssign;
if (!name || !(/[=\s]/.test(stream.peek()) || stream.eol())) return null;
const res = [];
if (state.defined.includes(name)) res.push(REDEF);
state.defined.push(name);
res.push(DEFNAME);
return res.join(" ");
}
function expectAssign(stream, state) {
if (!stream.match(assign)) return null;
state.f = expectTerm;
return EQUALS;
}
function expectTerm(stream, state) {
return brackets(stream, state)
|| lambda(stream, state)
|| namedTerm(stream, state)
|| number(stream, state);
}
function brackets(stream, state) {
const v = stream.eat(brack);
if (!v) return null;
if (v == '(' && stream.peek() == ')') {
stream.next();
return EMPTY;
}
if (v == '(') {
state.depth.push(stream.column() + stream.indentation());
state.bound.push([]);
}
else {
state.depth.pop();
state.bound.pop();
}
return BRACKETS;
}
function lambda(stream, state) {
if (!stream.eat("\\")) return null;
state.f = expectArg;
return LAMBDA;
}
function namedTerm(stream, state) {
const res = (stream.match(defName)||[])[0];
if (!res) return null;
if (state.bound.some(v=>v.includes(res))) return BOUND;
if (state.defined.includes(res)) return PREDEF;
return UNDEF;
}
function number(stream, state) {
const num = (stream.match(numconst)||[])[0];
return num && (/\s|\)/.test(stream.peek()) || stream.eol()) ? NUMBER : null;
}
function expectArg(stream, state) {
const arg = (stream.match(lamArg)||[])[0];
if (!arg) return null;
if (arg === '.') {
state.f = expectTerm;
return DOT;
}
if (arg[0] === '_') return HOLE;
state.bound[state.bound.length-1].push(arg);
return ARGS;
}
function onFail(stream, state) {
stream.match(/[^\s]*/);
return FAIL
}
return {
startState: function () { return {
f: expectDef,
depth: [],
defined: [],
bound: [[]]
}; },
copyState: function (s) { return {
f: s.f,
depth: [...s.depth],
defined: [...s.defined],
bound: s.bound.map(v=>[...v])
}; },
token: function(stream, state) {
if (/\s/.test(stream.peek())) {
stream.eatSpace();
return;
}
if (stream.peek() === '#') {
stream.skipToEnd();
return "comment"
}
if (stream.sol() && state.depth.length === 0) {
state.bound = [[]];
state.f = expectDef;
}
return state.f(stream, state) || onFail(stream, state);
},
indent: function(state, textAfter) {
console.log(state.depth);
if (!state.depth.length) return 0;
return state.depth[state.depth.length-1] + 2;
},
lineComment: "#",
};
});
CodeMirror.defineMIME("text/x-lambdacalc", "lambdacalculus");
});