-
Notifications
You must be signed in to change notification settings - Fork 0
/
RuntimeFunctions.fs
65 lines (63 loc) · 3.91 KB
/
RuntimeFunctions.fs
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
module RuntimeFunctions
open System.IO
open AST
let Functions: Map<string, Memory<MemoryValue> -> MemoryValue -> Memory<MemoryValue> * MemoryValue> = Map.ofArray [|
("read", fun mem p -> mem, System.Console.ReadLine() |> String)
("string", fun mem p -> mem, match p with
| String s -> p
| Int i -> string i |> String
| Bool b -> if b then "true" |> String else "false" |> String
| _ -> string p |> String)
("bool", fun mem p -> mem, match p with
| Bool b -> p
| Int i -> if i = 1 then Bool true elif i = 0 then Bool false else Unit ()
| String s -> if s = "true" then Bool true elif s = "false" then Bool false else Unit ()
| _ -> Unit ())
("int", fun mem p -> mem, match p with
| Bool b -> if b then Int 1 else Int 0
| Int i -> p
| String s -> int s |> Int
| _ -> Unit ())
("count", fun mem p -> mem, match p with
| String s -> s.Length |> Int
| _ -> Unit ())
("type", fun mem p -> mem, match p with
| Array _ -> String "array"
| String _ -> String "string"
| Int _ -> String "int"
| LambdaExpr _ -> String "lambda"
| Object _ -> String "record"
| Bool _ -> String "bool"
| Unit _ -> String "unit")
("has_key", fun mem p -> mem, match p with
| Array a -> match (a.[0], a.[1]) with
| (Object r, String s) -> Map.containsKey s r |> Bool
| _ -> Bool false
| _ -> Bool false)
("array_count", fun mem p -> mem, match p with
| Array a -> Int a.Length
| _ -> Unit ())
("file_read", fun mem p -> mem, match p with
| String s -> File.ReadAllText s |> String
| _ -> Unit ())
("file_write", fun mem p -> mem, match p with
| Array a -> let path = match a.[0] with
| String s -> s
| _ -> System.Exception "no path in file_write" |> raise
let text = match a.[1] with
| String s -> s
| _ -> System.Exception "no text in file_write" |> raise
File.WriteAllText(path, text)
Bool true
| _ -> Bool false)
("file_exists", fun mem p -> mem, match p with
| String s -> File.Exists(s) |> Bool
| _ -> Unit ())
("dir_exists", fun mem p -> mem, match p with
| String s -> System.IO.Directory.Exists(s) |> Bool
| _ -> Unit ())
("dir_create", fun mem p -> mem, match p with
| String s -> System.IO.Directory.CreateDirectory(s) |> ignore
Bool true
| _ -> Unit ())
|]