@@ -3,6 +3,8 @@ package core
3
3
package vm
4
4
5
5
import java .io .PrintStream
6
+ import scala .util .matching as regex
7
+ import scala .util .matching .Regex
6
8
7
9
trait Runtime {
8
10
def out : PrintStream
@@ -193,12 +195,56 @@ lazy val strings: Builtins = Map(
193
195
},
194
196
195
197
builtin(" effekt::inspect(Any)" ) {
196
- case any :: Nil => Value .String (inspect(any))
198
+ case any :: Nil =>
199
+ Runtime .out.println(inspect(any))
200
+ Value .Unit ()
197
201
},
198
202
199
203
builtin(" effekt::infixEq(String, String)" ) {
200
204
case As .String (x) :: As .String (y) :: Nil => Value .Bool (x == y)
201
205
},
206
+
207
+ builtin(" effekt::length(String)" ) {
208
+ case As .String (x) :: Nil => Value .Int (x.length)
209
+ },
210
+
211
+ builtin(" effekt::substring(String, Int, Int)" ) {
212
+ case As .String (x) :: As .Int (from) :: As .Int (to) :: Nil => Value .String (x.substring(from.toInt, to.toInt))
213
+ },
214
+
215
+ builtin(" string::unsafeCharAt(String, Int)" ) {
216
+ case As .String (x) :: As .Int (at) :: Nil => Value .Int (x.charAt(at.toInt).toLong)
217
+ },
218
+
219
+ builtin(" string::toInt(Char)" ) {
220
+ case As .Int (n) :: Nil => Value .Int (n)
221
+ },
222
+
223
+ builtin(" string::toChar(Int)" ) {
224
+ case As .Int (n) :: Nil => Value .Int (n)
225
+ },
226
+
227
+ builtin(" string::infixLte(Char, Char)" ) {
228
+ case As .Int (x) :: As .Int (y) :: Nil => Value .Bool (x <= y)
229
+ },
230
+
231
+ builtin(" string::infixLt(Char, Char)" ) {
232
+ case As .Int (x) :: As .Int (y) :: Nil => Value .Bool (x < y)
233
+ },
234
+
235
+ builtin(" string::infixGt(Char, Char)" ) {
236
+ case As .Int (x) :: As .Int (y) :: Nil => Value .Bool (x > y)
237
+ },
238
+
239
+ builtin(" string::infixGte(Char, Char)" ) {
240
+ case As .Int (x) :: As .Int (y) :: Nil => Value .Bool (x >= y)
241
+ },
242
+ )
243
+
244
+ lazy val chars : Builtins = Map (
245
+ builtin(" effekt::infixEq(Char, Char)" ) {
246
+ case As .Int (x) :: As .Int (y) :: Nil => Value .Bool (x == y)
247
+ },
202
248
)
203
249
204
250
lazy val arrays : Builtins = Map (
@@ -216,6 +262,12 @@ lazy val arrays: Builtins = Map(
216
262
},
217
263
)
218
264
265
+ lazy val undefined : Builtins = Map (
266
+ builtin(" effekt::isUndefined[A](A)" ) {
267
+ case Value .Literal (m) :: Nil => Value .Bool (m == null )
268
+ },
269
+ )
270
+
219
271
lazy val refs : Builtins = Map (
220
272
builtin(" ref::ref[T](T)" ) {
221
273
case init :: Nil => Value .Ref (Reference (init))
@@ -228,7 +280,22 @@ lazy val refs: Builtins = Map(
228
280
},
229
281
)
230
282
231
- lazy val builtins : Builtins = printing ++ integers ++ doubles ++ booleans ++ strings ++ arrays ++ refs
283
+ lazy val regexes : Builtins = Map (
284
+ builtin(" regex::regex(String)" ) {
285
+ case As .String (str) :: Nil => Value .Literal (new Regex (str))
286
+ },
287
+ builtin(" regex::exec(Regex, String)" ) {
288
+ case As .Regex (r) :: As .String (str) :: Nil => Value .Literal (r.findFirstMatchIn(str).orNull)
289
+ },
290
+ builtin(" regex::matched(RegexMatch)" ) {
291
+ case As .RegexMatch (m) :: Nil => Value .String (m.matched)
292
+ },
293
+ builtin(" regex::index(RegexMatch)" ) {
294
+ case As .RegexMatch (m) :: Nil => Value .Int (m.start)
295
+ },
296
+ )
297
+
298
+ lazy val builtins : Builtins = printing ++ integers ++ doubles ++ booleans ++ strings ++ arrays ++ refs ++ chars ++ regexes ++ undefined
232
299
233
300
protected object As {
234
301
object String {
@@ -240,6 +307,8 @@ protected object As {
240
307
object Int {
241
308
def unapply (v : Value ): Option [scala.Long ] = v match {
242
309
case Value .Literal (value : scala.Long ) => Some (value)
310
+ case Value .Literal (value : scala.Int ) => Some (value.toLong)
311
+ case Value .Literal (value : java.lang.Integer ) => Some (value.toLong)
243
312
case _ => None
244
313
}
245
314
}
@@ -267,4 +336,17 @@ protected object As {
267
336
case _ => None
268
337
}
269
338
}
339
+ object Regex {
340
+ def unapply (v : Value ): Option [regex.Regex ] = v match {
341
+ case Value .Literal (v : regex.Regex ) => Some (v)
342
+ case _ => None
343
+ }
344
+ }
345
+ object RegexMatch {
346
+ def unapply (v : Value ): Option [regex.Regex .Match | Null ] = v match {
347
+ case Value .Literal (null ) => Some (null )
348
+ case Value .Literal (v : regex.Regex .Match ) => Some (v)
349
+ case _ => None
350
+ }
351
+ }
270
352
}
0 commit comments