Skip to content

Commit d47bd2c

Browse files
committed
Format
1 parent 414ae0a commit d47bd2c

File tree

3 files changed

+86
-33
lines changed

3 files changed

+86
-33
lines changed

bin/sgen.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ let watch input_file timeout =
104104
let preprocess_only input_file =
105105
let expr = parse input_file in
106106
let preprocessed = Expr.preprocess expr in
107-
preprocessed |> List.map ~f:(fun e -> Expr.to_string e.Expr.content) |> String.concat ~sep:"\n"
108-
|> Stdlib.print_endline
107+
preprocessed
108+
|> List.map ~f:(fun e -> Expr.to_string e.Expr.content)
109+
|> String.concat ~sep:"\n" |> Stdlib.print_endline
109110

110111
let input_file_arg =
111112
let doc = "Input file to process." in

src/expr.ml

Lines changed: 78 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ let ( let* ) x f = Result.bind x ~f
88
type ident = string
99

1010
(* Generic type for attaching source locations *)
11-
type 'a loc = {
12-
content : 'a;
13-
loc : source_location option;
14-
}
11+
type 'a loc =
12+
{ content : 'a
13+
; loc : source_location option
14+
}
1515

1616
module Raw = struct
1717
type t =
@@ -32,13 +32,14 @@ end
3232
type expr =
3333
| Symbol of string
3434
| Var of ident
35-
| List of (expr loc) list
35+
| List of expr loc list
3636

3737
let rec equal_expr e1 e2 =
3838
match (e1, e2) with
3939
| Symbol s1, Symbol s2 -> String.equal s1 s2
4040
| Var v1, Var v2 -> String.equal v1 v2
41-
| List l1, List l2 -> List.equal (fun a b -> equal_expr a.content b.content) l1 l2
41+
| List l1, List l2 ->
42+
List.equal (fun a b -> equal_expr a.content b.content) l1 l2
4243
| _ -> false
4344

4445
let primitive = String.append "%"
@@ -69,28 +70,54 @@ let rec to_string : expr -> string = function
6970
| Symbol s -> s
7071
| Var x -> x
7172
| List es ->
72-
Printf.sprintf "(%s)" (List.map ~f:(fun e -> to_string e.content) es |> String.concat ~sep:" ")
73+
Printf.sprintf "(%s)"
74+
(List.map ~f:(fun e -> to_string e.content) es |> String.concat ~sep:" ")
7375

7476
let rec expand_macro : Raw.t -> expr loc = function
7577
| Raw.Symbol s -> { content = Symbol s; loc = None }
7678
| Raw.Var x -> { content = Var x; loc = None }
77-
| Raw.String s -> { content = List [ { content = Symbol string_op; loc = None }; { content = Symbol s; loc = None } ]; loc = None }
79+
| Raw.String s ->
80+
{ content =
81+
List
82+
[ { content = Symbol string_op; loc = None }
83+
; { content = Symbol s; loc = None }
84+
]
85+
; loc = None
86+
}
7887
| Raw.Call e' ->
7988
let e = expand_macro e' in
80-
{ content = List [ { content = Symbol call_op; loc = None }; e ]; loc = None }
89+
{ content = List [ { content = Symbol call_op; loc = None }; e ]
90+
; loc = None
91+
}
8192
| Raw.Focus e' ->
8293
let e = expand_macro e' in
83-
{ content = List [ { content = Symbol focus_op; loc = None }; e ]; loc = None }
94+
{ content = List [ { content = Symbol focus_op; loc = None }; e ]
95+
; loc = None
96+
}
8497
| Raw.Group es ->
85-
{ content = List ({ content = Symbol group_op; loc = None } :: List.map ~f:expand_macro es); loc = None }
86-
| Raw.List es ->
87-
{ content = List (List.map ~f:expand_macro es); loc = None }
98+
{ content =
99+
List
100+
( { content = Symbol group_op; loc = None }
101+
:: List.map ~f:expand_macro es )
102+
; loc = None
103+
}
104+
| Raw.List es -> { content = List (List.map ~f:expand_macro es); loc = None }
88105
| Raw.Cons es -> expand_macro (Raw.ConsWithBase (es, Symbol nil_op))
89106
| Raw.ConsWithBase (es, base) ->
90107
List.fold_left es ~init:(expand_macro base) ~f:(fun acc e ->
91-
{ content = List [ { content = Symbol cons_op; loc = None }; expand_macro e; acc ]; loc = None } )
108+
{ content =
109+
List [ { content = Symbol cons_op; loc = None }; expand_macro e; acc ]
110+
; loc = None
111+
} )
92112
| Raw.ConsWithParams (es, ps) ->
93-
{ content = List [ { content = Symbol params_op; loc = None }; expand_macro (Cons es); expand_macro (List ps) ]; loc = None }
113+
{ content =
114+
List
115+
[ { content = Symbol params_op; loc = None }
116+
; expand_macro (Cons es)
117+
; expand_macro (List ps)
118+
]
119+
; loc = None
120+
}
94121
| Raw.Stack [] -> { content = List []; loc = None }
95122
| Raw.Stack (h :: t) ->
96123
List.fold_left t ~init:(expand_macro h) ~f:(fun acc e ->
@@ -109,16 +136,22 @@ let rec replace_id (var_from : ident) replacement (expr : expr loc) : expr loc =
109136
match expr.content with
110137
| Var x when String.equal x var_from -> { replacement with loc = expr.loc }
111138
| Symbol _ | Var _ -> expr
112-
| List exprs -> { content = List (List.map exprs ~f:(replace_id var_from replacement)); loc = expr.loc }
139+
| List exprs ->
140+
{ content = List (List.map exprs ~f:(replace_id var_from replacement))
141+
; loc = expr.loc
142+
}
113143

114-
let unfold_decl_def (macro_env : (string * (string list * (expr loc) list)) list)
144+
let unfold_decl_def (macro_env : (string * (string list * expr loc list)) list)
115145
exprs =
116146
let rec process_expr (env, acc) (expr : expr loc) =
117147
match expr.content with
118-
| List ({ content = Symbol "new-declaration"; _ } :: { content = List ({ content = Symbol macro_name; _ } :: args); _ } :: body)
119-
->
148+
| List
149+
( { content = Symbol "new-declaration"; _ }
150+
:: { content = List ({ content = Symbol macro_name; _ } :: args); _ }
151+
:: body ) ->
120152
let var_args =
121-
List.map args ~f:(fun arg -> match arg.content with
153+
List.map args ~f:(fun arg ->
154+
match arg.content with
122155
| Var x -> x
123156
| _ -> failwith "error: syntax declaration must contain variables" )
124157
in
@@ -142,7 +175,9 @@ let unfold_decl_def (macro_env : (string * (string list * (expr loc) list)) list
142175
| None -> (env, expr :: acc) )
143176
| _ -> (env, expr :: acc)
144177
in
145-
List.fold_left exprs ~init:(macro_env, []) ~f:(fun (env, acc) e -> process_expr (env, acc) e) |> snd |> List.rev
178+
List.fold_left exprs ~init:(macro_env, []) ~f:(fun (env, acc) e ->
179+
process_expr (env, acc) e )
180+
|> snd |> List.rev
146181

147182
(* ---------------------------------------
148183
Constellation of Expr
@@ -166,11 +201,13 @@ let rec ray_of_expr : expr -> (ray, expr_err) Result.t = function
166201

167202
let bans_of_expr ban_exprs : (ban list, expr_err) Result.t =
168203
let rec ban_of_expr = function
169-
| List [ { content = Symbol op; _ }; expr1; expr2 ] when String.equal op ineq_op ->
204+
| List [ { content = Symbol op; _ }; expr1; expr2 ]
205+
when String.equal op ineq_op ->
170206
let* ray1 = ray_of_expr expr1.content in
171207
let* ray2 = ray_of_expr expr2.content in
172208
Ineq (ray1, ray2) |> Result.return
173-
| List [ { content = Symbol op; _ }; expr1; expr2 ] when String.equal op incomp_op ->
209+
| List [ { content = Symbol op; _ }; expr1; expr2 ]
210+
when String.equal op incomp_op ->
174211
let* ray1 = ray_of_expr expr1.content in
175212
let* ray2 = ray_of_expr expr2.content in
176213
Incomp (ray1, ray2) |> Result.return
@@ -184,7 +221,8 @@ let rec raylist_of_expr expr : (ray list, expr_err) Result.t =
184221
| Symbol _ | Var _ ->
185222
let* ray = ray_of_expr expr in
186223
Ok [ ray ]
187-
| List [ { content = Symbol op; _ }; head; tail ] when String.equal op cons_op ->
224+
| List [ { content = Symbol op; _ }; head; tail ] when String.equal op cons_op
225+
->
188226
let* head_ray = ray_of_expr head.content in
189227
let* tail_rays = raylist_of_expr tail.content in
190228
Ok (head_ray :: tail_rays)
@@ -194,7 +232,8 @@ let rec star_of_expr : expr -> (Marked.star, expr_err) Result.t = function
194232
| List [ { content = Symbol k; _ }; s ] when equal_string k focus_op ->
195233
let* ss = star_of_expr s.content in
196234
ss |> Marked.remove |> Marked.make_state |> Result.return
197-
| List [ { content = Symbol k; _ }; s; { content = List ps; _ } ] when equal_string k params_op ->
235+
| List [ { content = Symbol k; _ }; s; { content = List ps; _ } ]
236+
when equal_string k params_op ->
198237
let* content = raylist_of_expr s.content in
199238
let* bans = bans_of_expr ps in
200239
Marked.Action { content; bans } |> Result.return
@@ -242,16 +281,24 @@ let rec sgen_expr_of_expr expr : (sgen_expr, expr_err) Result.t =
242281
let* sgen_expr = sgen_expr_of_expr arg.content in
243282
Focus sgen_expr |> Result.return
244283
| List ({ content = Symbol op; _ } :: args) when String.equal op group_op ->
245-
let* sgen_exprs = List.map args ~f:(fun e -> sgen_expr_of_expr e.content) |> Result.all in
284+
let* sgen_exprs =
285+
List.map args ~f:(fun e -> sgen_expr_of_expr e.content) |> Result.all
286+
in
246287
Group sgen_exprs |> Result.return
247288
| List ({ content = Symbol "process"; _ } :: args) ->
248-
let* sgen_exprs = List.map args ~f:(fun e -> sgen_expr_of_expr e.content) |> Result.all in
289+
let* sgen_exprs =
290+
List.map args ~f:(fun e -> sgen_expr_of_expr e.content) |> Result.all
291+
in
249292
Process sgen_exprs |> Result.return
250293
| List ({ content = Symbol "interact"; _ } :: args) ->
251-
let* sgen_exprs = List.map args ~f:(fun e -> sgen_expr_of_expr e.content) |> Result.all in
294+
let* sgen_exprs =
295+
List.map args ~f:(fun e -> sgen_expr_of_expr e.content) |> Result.all
296+
in
252297
Exec (false, Group sgen_exprs) |> Result.return
253298
| List ({ content = Symbol "fire"; _ } :: args) ->
254-
let* sgen_exprs = List.map args ~f:(fun e -> sgen_expr_of_expr e.content) |> Result.all in
299+
let* sgen_exprs =
300+
List.map args ~f:(fun e -> sgen_expr_of_expr e.content) |> Result.all
301+
in
255302
Exec (true, Group sgen_exprs) |> Result.return
256303
| List [ { content = Symbol "eval"; _ }; arg ] ->
257304
let* sgen_expr = sgen_expr_of_expr arg.content in
@@ -277,7 +324,8 @@ let rec decl_of_expr (expr : expr loc) : (declaration, expr_err) Result.t =
277324
let* sgen_expr2 = sgen_expr_of_expr expr2.content in
278325
let* message_ray = ray_of_expr message.content in
279326
Expect (sgen_expr1, sgen_expr2, message_ray, expr.loc) |> Result.return
280-
| List [ { content = Symbol op; _ }; identifier; value ] when String.equal op def_op ->
327+
| List [ { content = Symbol op; _ }; identifier; value ]
328+
when String.equal op def_op ->
281329
let* id_ray = ray_of_expr identifier.content in
282330
let* value_expr = sgen_expr_of_expr value.content in
283331
Def (id_ray, value_expr) |> Result.return

src/sgen_eval.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,11 @@ and expr_of_ray : ray -> Expr.expr = function
227227
| Var (x, Some i) -> Expr.Var (x ^ Int.to_string i)
228228
| Func (pf, []) -> Symbol (string_of_polsym pf)
229229
| Func (pf, args) ->
230-
Expr.List ({ Expr.content = Symbol (string_of_polsym pf); loc = None } :: List.map ~f:(fun r -> { Expr.content = expr_of_ray r; loc = None }) args)
230+
Expr.List
231+
( { Expr.content = Symbol (string_of_polsym pf); loc = None }
232+
:: List.map
233+
~f:(fun r -> { Expr.content = expr_of_ray r; loc = None })
234+
args )
231235

232236
let rec eval_decl env : declaration -> (env, err) Result.t = function
233237
| Def (identifier, expr) -> Ok { objs = add_obj env identifier expr }

0 commit comments

Comments
 (0)