Skip to content

Commit 80b0362

Browse files
committed
Add prototype of evaluation
1 parent 68c4dd3 commit 80b0362

File tree

4 files changed

+45
-17
lines changed

4 files changed

+45
-17
lines changed

examples/syntax.sg

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,24 @@
3535
'interactive debugging of execution
3636
'(trace #ineq)
3737

38-
'''
3938
'dynamic definition of constellation
40-
(def c (process
41-
(const (star (+n0 0))) 'base constellation
42-
(const (star (-n0 X) (+n1 (s X)))) 'interacts with previous
43-
(const (star (-n1 X) (+n2 (s X)))))) 'interacts with previous
44-
45-
'galaxy definition
46-
(def g (galaxy
47-
(test1 (const (star (+f a) ok)))
48-
(test2 (const (star (+f b) ok)))))
49-
39+
(:= c (process
40+
(+n0 0) 'base constellation
41+
[(-n0 X) (+n1 (s X))] 'interacts with previous
42+
[(-n1 X) (+n2 (s X))])) 'interacts with previous
43+
(show #c)
44+
45+
'constellation with fields
46+
(:= g [
47+
[+test1 [(+f a) ok]]
48+
[+test2 [(+f b) ok]]])
5049
(show #g)
5150

51+
'field access and evaluation
52+
<show eval exec (union #g @[-test1])>
53+
<show eval exec (union #g @[-test2])>
54+
55+
'''
5256
'reactive effects
5357
(run (const
5458
(star (+&print X))

src/expr.ml

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ let symbol_of_str (s : string) : idfunc =
8282
let rec ray_of_expr : expr -> ray = function
8383
| Symbol s -> to_func ((Muted, symbol_of_str s), [])
8484
| Var s -> to_var s
85-
| Unquote _ -> failwith "error: cannot unquote ray"
85+
| Unquote e -> failwith ("error: cannot unquote ray " ^ to_string e)
8686
| List [] -> failwith "error: ray cannot be empty"
8787
| List (Symbol h :: t) ->
8888
to_func ((Muted, symbol_of_str h), List.map ~f:ray_of_expr t)
@@ -102,7 +102,7 @@ let rec raylist_of_expr (e : expr) : ray list =
102102
match e with
103103
| Symbol k when equal_string k nil_op -> []
104104
| Symbol _ | Var _ -> [ ray_of_expr e ]
105-
| Unquote _ -> failwith "error: cannot unquote star"
105+
| Unquote e -> failwith ("error: cannot unquote star " ^ to_string e)
106106
| List [ Symbol s; h; t ] when equal_string s cons_op ->
107107
ray_of_expr h :: raylist_of_expr t
108108
| e -> failwith ("error: unhandled star " ^ to_string e)
@@ -118,7 +118,7 @@ let rec constellation_of_expr : expr -> marked_constellation = function
118118
| Symbol k when equal_string k nil_op -> []
119119
| Symbol s -> [ Unmarked { content = [ var (s, None) ]; bans = [] } ]
120120
| Var x -> [ Unmarked { content = [ var (x, None) ]; bans = [] } ]
121-
| Unquote _ -> failwith "error: can't unquote constellation"
121+
| Unquote e -> failwith ("error: can't unquote constellation" ^ to_string e)
122122
| List [ Symbol s; h; t ] when equal_string s cons_op ->
123123
star_of_expr h :: constellation_of_expr t
124124
| List g -> [ Unmarked { content = [ ray_of_expr (List g) ]; bans = [] } ]
@@ -154,20 +154,32 @@ let rec galaxy_expr_of_expr (e : expr) : galaxy_expr =
154154
(* union *)
155155
| List (Symbol k :: gs) when equal_string k "union" ->
156156
Union (List.map ~f:galaxy_expr_of_expr gs)
157+
(* process *)
158+
| List (Symbol k :: gs) when equal_string k "process" ->
159+
Process (List.map ~f:galaxy_expr_of_expr gs)
160+
(* kill *)
161+
| List [ Symbol k; g ] when equal_string k "kill" ->
162+
Kill (galaxy_expr_of_expr g)
163+
(* clean *)
164+
| List [ Symbol k; g ] when equal_string k "clean" ->
165+
Clean (galaxy_expr_of_expr g)
157166
(* exec *)
158167
| List [ Symbol k; g ] when equal_string k "exec" ->
159168
Exec (galaxy_expr_of_expr g)
160169
(* linear exec *)
161170
| List [ Symbol k; g ] when equal_string k "linexec" ->
162171
LinExec (galaxy_expr_of_expr g)
163-
(* raw constellation *)
172+
(* linear exec *)
173+
| List [ Symbol k; g ] when equal_string k "eval" ->
174+
Eval (ray_of_expr g)
175+
(* KEEP LAST -- raw constellation *)
164176
| List g -> Raw (Const (constellation_of_expr (List g)))
165177

166178
(* ---------------------------------------
167179
Stellogen program of Expr
168180
--------------------------------------- *)
169181

170-
let rec decl_of_expr : expr -> declaration = function
182+
let decl_of_expr : expr -> declaration = function
171183
(* definition := *)
172184
| List [ Symbol k; x; g ] when equal_string k def_op ->
173185
Def (ray_of_expr x, galaxy_expr_of_expr g)

src/sgen_ast.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ and galaxy_expr =
3434
| Clean of galaxy_expr
3535
| Kill of galaxy_expr
3636
| Process of galaxy_expr list
37+
| Eval of ray
3738

3839
and substitution =
3940
| Extend of ray_prefix

src/sgen_eval.ml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ and map_galaxy_expr env ~f : galaxy_expr -> (galaxy_expr, err) Result.t =
8181
| Process gs ->
8282
let* procs = List.map ~f:(map_galaxy_expr env ~f) gs |> Result.all in
8383
Process procs |> Result.return
84+
| Eval e -> Eval e |> Result.return
8485

8586
let rec replace_id env (_from : ident) (_to : galaxy_expr) e :
8687
(galaxy_expr, err) Result.t =
@@ -114,7 +115,7 @@ let rec replace_id env (_from : ident) (_to : galaxy_expr) e :
114115
| Process gs ->
115116
let* procs = List.map ~f:(replace_id env _from _to) gs |> Result.all in
116117
Process procs |> Result.return
117-
| Raw _ | Id _ -> e |> Result.return
118+
| Raw _ | Id _ | Eval _ -> e |> Result.return
118119

119120
let subst_vars env _from _to =
120121
map_galaxy_expr env ~f:(subst_all_vars [ (_from, _to) ])
@@ -296,6 +297,16 @@ and eval_galaxy_expr ~notyping (env : env) :
296297
| Subst (e, SGal (x, _to)) ->
297298
let* fill = replace_id env x _to e in
298299
eval_galaxy_expr ~notyping env fill
300+
| Eval e ->
301+
let* eval_e = Expr.galaxy_expr_of_expr (expr_of_ray e)
302+
|> eval_galaxy_expr ~notyping env in
303+
eval_galaxy_expr ~notyping env (Raw eval_e)
304+
305+
and expr_of_ray = function
306+
| Var (x, None) -> Expr.Var x
307+
| Var (x, Some i) -> Expr.Var (x ^ Int.to_string i)
308+
| Func (pf, args) ->
309+
Expr.List (Symbol (Lsc_ast.string_of_polsym pf) :: List.map ~f:expr_of_ray args)
299310

300311
and galaxy_to_constellation ~notyping env :
301312
galaxy -> (marked_constellation, err) Result.t = function

0 commit comments

Comments
 (0)