Skip to content

Commit

Permalink
Get the boilerplate for rendering Hazelnut expressions ready
Browse files Browse the repository at this point in the history
  • Loading branch information
yottalogical committed Aug 29, 2022
1 parent 9ebba4c commit fd27b9e
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 24 deletions.
2 changes: 1 addition & 1 deletion bin/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(executables
(names main)
(libraries incr_dom hazelnut_lib)
(libraries incr_dom app_lib)
(preprocess
(pps js_of_ocaml-ppx ppx_jane)))
2 changes: 1 addition & 1 deletion bin/main.re
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
open! Core;
open! Incr_dom;
open! Js_of_ocaml;
module App = Hazelnut_lib.App;
module App = App_lib.App;

Start_app.start(
(module App),
Expand Down
5 changes: 5 additions & 0 deletions hazelnut/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(library
(name hazelnut_lib)
(libraries core incr_dom)
(preprocess
(pps ppx_jane)))
34 changes: 34 additions & 0 deletions hazelnut/hazelnut.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[@deriving (sexp, compare)]
type htyp =
| Arrow(htyp, htyp)
| Num
| Hole;

[@deriving (sexp, compare)]
type hexp =
| Var(string)
| Lam(string, hexp)
| Ap(hexp, hexp)
| Num(int)
| Plus(hexp, hexp)
| Asc(hexp, htyp)
| EHole
| NEHole(hexp);

[@deriving (sexp, compare)]
type ztyp =
| Cursor(htyp)
| LArrow(ztyp, htyp)
| RArrow(htyp, ztyp);

[@deriving (sexp, compare)]
type zexp =
| Cursor(hexp)
| Lam(string, zexp)
| LAp(zexp, hexp)
| RAp(hexp, zexp)
| LPlus(zexp, hexp)
| RPlus(hexp, zexp)
| LAsc(zexp, htyp)
| RAsc(hexp, ztyp)
| NEHole(zexp);
34 changes: 34 additions & 0 deletions hazelnut/hazelnut.rei
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[@deriving (sexp, compare)]
type htyp =
| Arrow(htyp, htyp)
| Num
| Hole;

[@deriving (sexp, compare)]
type hexp =
| Var(string)
| Lam(string, hexp)
| Ap(hexp, hexp)
| Num(int)
| Plus(hexp, hexp)
| Asc(hexp, htyp)
| EHole
| NEHole(hexp);

[@deriving (sexp, compare)]
type ztyp =
| Cursor(htyp)
| LArrow(ztyp, htyp)
| RArrow(htyp, ztyp);

[@deriving (sexp, compare)]
type zexp =
| Cursor(hexp)
| Lam(string, zexp)
| LAp(zexp, hexp)
| RAp(hexp, zexp)
| LPlus(zexp, hexp)
| RPlus(hexp, zexp)
| LAsc(zexp, htyp)
| RAsc(hexp, ztyp)
| NEHole(zexp);
42 changes: 24 additions & 18 deletions lib/app.re
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
open Core;
open Incr_dom;
module Hazelnut = Hazelnut_lib.Hazelnut;

let render_zexp: Hazelnut.zexp => string =
fun
| Cursor(_) => "Cursor"
| Lam(_, _) => "Lam"
| LAp(_, _) => "LAp"
| RAp(_, _) => "RAp"
| LPlus(_, _) => "LPlus"
| RPlus(_, _) => "RPlus"
| LAsc(_, _) => "LAsc"
| RAsc(_, _) => "RAsc"
| NEHole(_) => "NEHole";

module Model = {
[@deriving (sexp, fields, compare)]
type t = {counter: int};
type t = {e: Hazelnut.zexp};

let set_default_input = (counter: int): t => {counter: counter};
let set_default_expression = (e: Hazelnut.zexp): t => {e: e};

let init = (): t => set_default_input(0);
let reset_counter = (_: t): t => set_default_input(0);
let incr_counter = (t: t): t => set_default_input(t.counter + 1);
let decr_counter = (t: t): t => set_default_input(t.counter - 1);
let init = (): t => set_default_expression(Cursor(EHole));
let do_nothing = (e: t): t => e;
let cutoff = (t1: t, t2: t): bool => compare(t1, t2) == 0;
};

module Action = {
[@deriving sexp]
type t =
| Reset_counter
| Incr_counter
| Decr_counter;
| DoNothing;
};

module State = {
Expand All @@ -28,9 +37,7 @@ module State = {

let apply_action = (model, action, _, ~schedule_action as _) =>
switch ((action: Action.t)) {
| Reset_counter => Model.reset_counter(model)
| Incr_counter => Model.incr_counter(model)
| Decr_counter => Model.decr_counter(model)
| DoNothing => Model.do_nothing(model)
};

let on_startup = (~schedule_action as _, _) => Async_kernel.return();
Expand All @@ -52,16 +59,15 @@ let view =
);

let buttons = {
let reset_button = button("Reset", Action.Reset_counter);
let incr_button = button("+", Action.Incr_counter);
let decr_button = button("-", Action.Decr_counter);
let do_nothing_button = button("Do Nothing", Action.DoNothing);

Node.div([decr_button, reset_button, incr_button]);
Node.div([do_nothing_button]);
};

let%map counter = {
let%map counter = m >>| Model.counter;
Node.div([Node.textf("%d", counter)]);
let%map e = m >>| Model.e;
let e = render_zexp(e);
Node.div([Node.textf("%s", e)]);
};

Node.body([counter, buttons]);
Expand Down
4 changes: 2 additions & 2 deletions lib/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(library
(name hazelnut_lib)
(libraries core incr_dom)
(name app_lib)
(libraries core hazelnut_lib incr_dom)
(preprocess
(pps ppx_jane)))
2 changes: 1 addition & 1 deletion test/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(library
(name hazelnut_test)
(libraries async_kernel incr_dom_testing hazelnut_lib js_of_ocaml)
(libraries async_kernel incr_dom_testing app_lib js_of_ocaml)
(preprocess
(pps ppx_jane)))
2 changes: 1 addition & 1 deletion test/hazelnut_test.re
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
open! Core;
open! Incr_dom_testing;
module App = Hazelnut_lib.App;
module App = App_lib.App;

let make_helpers = () => {
let driver =
Expand Down

0 comments on commit fd27b9e

Please sign in to comment.