10
10
11
11
< style >
12
12
.CodeMirror { border-top : 1px solid black; border-bottom : 1px solid black; }
13
- </ style >
14
- < style >
15
- .text-center { text-align : center; }
13
+ [v-cloak ] { display : none !important ; }
16
14
</ style >
17
15
</ head >
18
16
19
17
< body class ="dark ">
20
18
21
- < header >
19
+ < header class =" !mb-0 " >
22
20
< h1 >
23
21
< a href ="https://github.com/codewars/lambda-calculus " title ="Lambda Calculus ">
24
22
< img alt ="Lambda Calculus " src ="https://github.com/codewars/lambda-calculus/main/logo/logo-white.svg " width ="64 " height ="64 ">
25
23
</ a >
24
+ < p class ="mt-4 text-2xl text-center "> Lambda Calculus mode for CodeMirror</ p >
26
25
</ h1 >
27
- < h3 class ="text-center "> Lambda Calculus mode for CodeMirror</ h3 >
26
+ < p class ="text-center "> < code > text/x-lambdacalc</ code > </ p >
27
+ < p class ="text-center "> < a href ="https://github.com/codewars/codemirror-lambda-calculus "> @codewars/codemirror-lambda-calculus</ a > </ p >
28
28
</ header >
29
29
30
- < main >
31
- < p class =" text-center " > < a href =" https://github.com/codewars/codemirror-lambda-calculus " > @codewars/codemirror-lambda-calculus </ a > </ p >
30
+ < main class =" mt-6 " v-scope >
31
+
32
32
< form >
33
- < textarea id ="code " name =" code " >
33
+ < textarea id ="code ">
34
34
# Some code (with ignored arguments)
35
35
false = \ _a b . b
36
36
true = \ a _b . a
@@ -87,16 +87,55 @@ <h3 class="text-center">Lambda Calculus mode for CodeMirror</h3>
87
87
all = foldr (\ a b . a b false) true
88
88
allf = \ f xs . all (map f xs)
89
89
</ textarea >
90
+
90
91
</ form >
91
- < p class ="text-center "> < strong > MIME type:</ strong > < code > text/x-lambdacalc</ code > </ p >
92
+
93
+ < div class ="mx-auto mt-8 w-96 grid grid-flow-row grid-cols-4 grid-rows-2 gap-1 ">
94
+ < input type ="text "
95
+ v-model ="evaluation "
96
+ @keyup.enter ="evaluate() "
97
+ class ="col-span-3 "
98
+ />
99
+ < button @click ="evaluate() " class ="col-span-1 "> Evaluate</ button >
100
+
101
+ < code v-cloak class ="px-2 py-1 mt-1 col-span-4 bg-white/10 " v-show ="result "> {{ result }}</ code >
102
+ < code v-cloak class ="px-2 py-1 mt-1 col-span-4 bg-red-800/60 " v-show ="error "> {{ error }}</ code >
103
+ </ div >
104
+
105
+ < div class ="mx-auto mt-4 w-80 ">
106
+ < fieldset >
107
+ < legend > Options</ legend >
108
+ < div class ="grid grid-cols-2 grid-rows-3 gap-1 ">
109
+ < label class ="!m-0 " for ="purity "> < code > purity</ code > </ label >
110
+ < select id ="purity " v-model ="purity ">
111
+ < option v-for ="opt in purityOptions " :value ="opt " :selected ="opt === purity "> {{ opt }}</ option >
112
+ </ select >
113
+
114
+ < label class ="!m-0 " for ="num-encoding "> < code > numEncoding</ code > </ label >
115
+ < select id ="num-encoding " v-model ="numEncoding ">
116
+ < option v-for ="opt in numEncodingOptions " :value ="opt " :selected ="opt === numEncoding "> {{ opt }}</ option >
117
+ </ select >
118
+
119
+ < label class ="!m-0 " for ="verbosity "> < code > verbosity</ code > </ label >
120
+ < select id ="verbosity " v-model ="verbosity ">
121
+ < option v-for ="opt in verbosityOptions " :value ="opt " :selected ="opt === verbosity "> {{ opt }}</ option >
122
+ </ select >
123
+ </ div >
124
+ </ fieldset >
125
+ </ div >
92
126
</ main >
93
127
94
128
< script src ="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.1/codemirror.min.js "> </ script >
95
129
< script src ="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.1/addon/edit/matchbrackets.min.js "> </ script >
96
130
< script type ="module ">
131
+ import "virtual:windi.css" ;
132
+
133
+ import { createApp } from "https://unpkg.com/petite-vue@0.4.1?module" ;
134
+ import * as LC from "https://unpkg.com/@codewars/lambda-calculus" ;
97
135
import { defineMode } from "./lambdacalc.js" ;
98
136
defineMode ( CodeMirror ) ;
99
- var editor = CodeMirror . fromTextArea ( document . getElementById ( "code" ) , {
137
+ const editor = CodeMirror . fromTextArea ( document . getElementById ( "code" ) , {
138
+ mode : "lambdacalc" ,
100
139
lineNumbers : true ,
101
140
matchBrackets : true ,
102
141
theme : "codewars" ,
@@ -108,7 +147,39 @@ <h3 class="text-center">Lambda Calculus mode for CodeMirror</h3>
108
147
return elem ;
109
148
}
110
149
} ) ;
111
- editor . setSize ( 500 , 500 ) ;
150
+ editor . setSize ( 500 , 400 ) ;
151
+
152
+ const toLambda = ( s ) => s . replace ( / \\ / g, "λ" ) ;
153
+ createApp ( {
154
+ purity : "Let" ,
155
+ purityOptions : [ "Let" , "LetRec" , "PureLC" ] ,
156
+ verbosity : "Calm" ,
157
+ verbosityOptions : [ "Calm" , "Concise" , "Loquacious" , "Verbose" ] ,
158
+ numEncoding : "Church" ,
159
+ numEncodingOptions : [ "None" , "Church" , "Scott" , "BinaryScott" ] ,
160
+
161
+ evaluation : "not false" ,
162
+ result : "" ,
163
+ error : "" ,
164
+ evaluate ( ) {
165
+ this . result = "" ;
166
+ this . error = "" ;
167
+
168
+ const code = editor . getValue ( ) ;
169
+ const val = this . evaluation ;
170
+ const compile = LC . compileWith ( {
171
+ purity : this . purity ,
172
+ verbosity : this . verbosity ,
173
+ numEncoding : this . numEncoding ,
174
+ } ) ;
175
+ try {
176
+ const { result } = compile ( `${ code } \n\nresult = ${ val } ` ) ;
177
+ this . result = toLambda ( result . term + "" ) ;
178
+ } catch ( e ) {
179
+ this . error = e . message || e . name ;
180
+ }
181
+ } ,
182
+ } ) . mount ( ) ;
112
183
</ script >
113
184
114
185
</ body >
0 commit comments