-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStarCombinator.Examples.fst
125 lines (103 loc) · 3.65 KB
/
StarCombinator.Examples.fst
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
module StarCombinator.Examples
open StarCombinator
open MyIO
open FStar.Mul
open StarCombinator.Examples.While
let op_add = (fun x y -> x + y)
let op_minus = (fun x y -> x - y)
let calculator_parser: parser int =
let rec h (_:unit): parser int = (
//let g = number in
let g = delayMe (admitP (() << ()); h) in
(ptry
(
(fun ((a, op), b) -> a `op` b) @<<
(number <*> (
(op_add *<< ((keyword "+" <|> keyword "plus") <?> "expected + or plus"))
<|> (op_minus *<< ((keyword "minus" <|> keyword "-") <?> "expected - or minus"))
) <*> g)
)
)<|> number
// fp (fun (a, b) -> a + b) (number <<*> (keyword "plus" <|> ckwd '+') <*> g)
//<|> fp (fun (a, b) -> a - b) (number <<*> (keyword "minus"<|> keyword "-") <*> g)
//<|> fp (fun (a, b) -> a * b) (number <<*> (keywords ["hey";"times"] <|> keyword "*") <*> g)
//<|> number
//and no_lrec () =
// number
// <|> (keyword "Bonjour" <*>> number <<*> keywords ["X";"Y";"Z"])
) in (h ()) <<*> eof
let calculator source = match (make calculator_parser) source with
| Inl intvalue -> "Got some result: " ^ string_of_int intvalue
| Inr error -> "Got some error: " ^ error
// module All = FStar.All
// module S = FStar.String
// let mi_input_lines (): All.ML string =
// let rec h (): All.ML (list string) =
// let line = mi_input_line () in
// if line = "" then [line]
// else (h ())@[line]
// in let r = h () in
// S.concat "\n" r
// let x = make lFakeInstr_parser
//let delayMe #a (p: unit -> parser a): parser a = {description = (fun _ -> "x"); parser_fun = fun sd st0 -> let x = p () in x.parser_fun sd st0}
let rec tiny' () = (
fp (fun (l, num) -> string_of_int num ^ " " ^ String.concat " " (FStar.List.Tot.Base.map (fun (a,b) -> String.string_of_list [a;b]) l))
(
(many (exact_char 'c' <*> (exact_char '!' <|> exact_char 'a'))) <*> number// <<*> (admitP (() << ()); delayMe tiny')
)
)
let tiny = make (tiny' ())
val mi_line_non_empty : unit -> FStar.All.ML (option (s : string{String.length s > 0}))
let mi_line_non_empty () =
let prog = mi_input_line () in
if String.length prog > 0
then Some prog
else None
let main0 () =
let prog = mi_line_non_empty () in
match prog with
| None -> ()
| Some prog ->
mi_print_string (match tiny prog with
| Inl x -> "Youpi:" ^ x
| Inr x -> x)
let main1 () =
let prog = mi_line_non_empty () in
match prog with
| None -> ()
| Some prog ->
mi_print_string (match calculator prog with
| r -> r)
let main2 () =
let prog = mi_line_non_empty () in
match prog with
| None -> ()
| Some prog ->
mi_print_string (match (make (match_list "("
")"
(exact_char ',')
aexp_parser <<*> eof)) prog with
| Inl r -> String.concat ", " (List.map lAExpToString r)
| Inr r -> r)
let main3 () =
let prog = mi_line_non_empty () in
match prog with
| None -> ()
| Some prog ->
mi_print_string (match (make ((
hFunction @<< (
(keyword "function" <*>> word)
<*> (match_list "(" ")" (exact_char ',') word <<*> keyword "{")
<*> ((LFakeInstrSkip *<< number) <<*> keyword "}")
)) <<*> eof)) prog with
| Inl r -> lFakeInstrToString r
| Inr r -> r)
let main4 () =
let prog = mi_line_non_empty () in
match prog with
| None -> ()
| Some prog ->
mi_print_string (match (make (lFakeInstr_parser <<*> eof)) prog with
| Inl r -> lFakeInstrToString r
| Inr r -> r)
let main = main4 ()