-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDomain.fs
86 lines (77 loc) · 2.32 KB
/
Domain.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
namespace ParseExample
[<RequireQualifiedAccess>]
type Expr =
| Plus of Expr * Expr
| Times of Expr * Expr
| UnaryMinus of Expr
| Minus of Expr * Expr
| Equal of Expr * Expr
| Int of int
| FunctionCall of Expr * Expr
| Var of string
| Factorial of Expr
| Paren of Expr
| IfThenElse of Expr * Expr * Expr
| IfThen of Expr * Expr
| ArrayIndex of Expr * Expr
[<RequireQualifiedAccess>]
module Expr =
let plus a b = Expr.Plus (a, b)
let equal a b = Expr.Equal (a, b)
let times a b = Expr.Times (a, b)
let unaryMinus a = Expr.UnaryMinus a
let minus a b = Expr.Minus (a, b)
let constInt a = Expr.Int a
let functionCall f x = Expr.FunctionCall (f, x)
let var name = Expr.Var name
let factorial a = Expr.Factorial a
let paren a = Expr.Paren a
let arrayIndex a b = Expr.ArrayIndex (a, b)
let ifThenElse ifClause thenClause elseClause =
Expr.IfThenElse (ifClause, thenClause, elseClause)
let ifThen ifClause thenClause = Expr.IfThen (ifClause, thenClause)
[<RequireQualifiedAccess>]
type TokenType =
| Plus
| Minus
| Times
| Equal
| ConstInt
| LeftBracket
| RightBracket
| ArrayIndex
| RightSquareBracket
| Var
| Factorial
| If
| Then
| Else
type Token =
{
Type : TokenType
/// The token is represented in the string as s.[left .. left + len], i.e. inclusive.
Trivia : int * int
}
[<RequireQualifiedAccess>]
module Token =
let standalone (ty : TokenType) (left : int) (len : int) =
{
Type = ty
Trivia = (left, len)
}
let standalone' (ty : TokenType) (singleCharPos : int) =
{
Type = ty
Trivia = (singleCharPos, 1)
}
let (|SingleChar|_|) (i : int, c : char) : Token option =
match c with
| '(' -> standalone' TokenType.LeftBracket i |> Some
| ')' -> standalone' TokenType.RightBracket i |> Some
| '*' -> standalone' TokenType.Times i |> Some
| '+' -> standalone' TokenType.Plus i |> Some
| '-' -> standalone' TokenType.Minus i |> Some
| '!' -> standalone' TokenType.Factorial i |> Some
| '=' -> standalone' TokenType.Equal i |> Some
| ']' -> standalone' TokenType.RightSquareBracket i |> Some
| _ -> None