Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strudelize Hazel #1395

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions src/haz3lcore/dynamics/Builtins.re
Original file line number Diff line number Diff line change
Expand Up @@ -349,28 +349,50 @@ module Pervasives = {
);
};

let ctx_init: Ctx.t = {
let meta_cons_map: ConstructorMap.t(Typ.t) = [
let stepper_types: Ctx.t = {
let stepper_meta: ConstructorMap.t(Typ.t) = [
Variant("$e", [Id.mk()], None),
Variant("$v", [Id.mk()], None),
];
let name = "$Meta";
let meta =
Ctx.TVarEntry({
name: "$Meta",
name,
id: Id.invalid,
kind: Ctx.Singleton(Sum(meta_cons_map) |> Typ.fresh),
kind: Ctx.Singleton(Sum(stepper_meta) |> Typ.fresh),
});
[meta] |> Ctx.add_ctrs(_, name, Id.invalid, stepper_meta);
};

let strudel_types: Ctx.t = {
let strudel: ConstructorMap.t(Typ.t) = [
Variant("Note", [Id.mk()], Some(IdTagged.fresh(Typ.Var("String")))),
];
let name = "Sound";
let sound =
Ctx.TVarEntry({
name,
id: Id.invalid,
kind: Ctx.Singleton(Sum(strudel) |> Typ.fresh),
});

[sound] |> Ctx.add_ctrs(_, name, Id.invalid, strudel);
};

let builtin_types: Ctx.t = stepper_types @ strudel_types;

let builtin_values: Ctx.t = {
List.map(
fun
| (name, Const(typ, _)) => Ctx.VarEntry({name, typ, id: Id.invalid})
| (name, Fn(t1, t2, _)) =>
Ctx.VarEntry({name, typ: Arrow(t1, t2) |> Typ.fresh, id: Id.invalid}),
Pervasives.builtins,
)
|> Ctx.extend(_, meta)
|> Ctx.add_ctrs(_, "$Meta", Id.invalid, meta_cons_map);
);
};

let ctx_init: Ctx.t = builtin_values @ builtin_types;

let forms_init: forms =
List.filter_map(
fun
Expand Down
117 changes: 76 additions & 41 deletions src/haz3lweb/Init.ml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/haz3lweb/Main.re
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ open Js_of_ocaml;
open Incr_dom;
open Haz3lweb;

Strudel.initOnLoad();

let scroll_to_caret = ref(true);
let edit_action_applied = ref(true);
let last_edit_action = ref(JsUtil.timestamp());
Expand Down
68 changes: 66 additions & 2 deletions src/haz3lweb/view/Cell.re
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,65 @@ let status_of: ProgramResult.t => string =
| ResultFail(_) => "fail"
| Off(_) => "off";

let lastnote: ref(string) = ref("");

let strudel_view = s =>
div([
button(
~attrs=[
Attr.class_("wrap"),
Attr.on_click(_ => {
Strudel.playNote(s);
Effect.Ignore;
}),
],
[div(~attrs=[], [text("PLAY")])],
),
text(" " ++ s ++ " "),
button(
~attrs=[
Attr.class_("wrap"),
Attr.on_click(_ => {
Strudel.stopMusic();
Effect.Ignore;
}),
],
[div(~attrs=[], [text("STOP")])],
),
]);

let audio_view = (~play, dhexp: DHExp.t): option(Node.t) =>
//the audio predicate avoids eg running audio when rendering explainthis
//print_endline("dhcode: " ++ DHExp.show_term(dhexp.term));
switch (dhexp.term) {
| Ap(_, {term: Constructor("Note", _), _}, arg) when !play =>
//TODO(andrew): more robust sln
//TODO(andrew): check if same audio is already playing, if so don't restart
//TODO(andrew): model state to enable stopping?
switch (DHExp.strip_casts(arg)) {
| {term: String(s), _} =>
switch (lastnote^ != s ? Strudel.playNote(s) : ()) {
| exception _ =>
lastnote := "";
// TODO(andrew): show strudel error view
None;
| _ =>
lastnote := s;
Some(strudel_view(s));
}
| _ =>
Strudel.stopMusic();
lastnote := "";
None;
}
| _ =>
if (!play) {
Strudel.stopMusic();
};
lastnote := "";
None;
};

let live_eval =
(
~inject,
Expand All @@ -178,7 +237,7 @@ let live_eval =
| (ResultPending, ResultOk(res)) => ProgramResult.get_dhexp(res)
| _ => result.elab.d
};
let dhcode_view =
let dh_view = dhexp =>
DHCode.view(
~locked,
~inject,
Expand All @@ -190,6 +249,11 @@ let live_eval =
~infomap=Id.Map.empty,
dhexp,
);
let live_view =
switch (audio_view(~play=locked, dhexp)) {
| Some(view) => view
| None => dh_view(dhexp)
};
let exn_view =
switch (result.evaluation) {
| ResultFail(err) => [
Expand All @@ -213,7 +277,7 @@ let live_eval =
),
div(
~attrs=[Attr.classes(["result", status_of(result.evaluation)])],
[dhcode_view],
[live_view],
),
Widgets.toggle(~tooltip="Show Stepper", "s", false, _ =>
inject(UpdateAction.ToggleStepper(result_key))
Expand Down
3 changes: 2 additions & 1 deletion src/haz3lweb/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
}
</script>
<script type="module" src="ninja_module.js"></script>

</head>

<body spellcheck="false">
Expand All @@ -39,6 +39,7 @@
</div>
</body>
<ninja-keys id="ninja-keys" placeholder="Find Action" hideBreadcrumbs></ninja-keys>
<script src="https://unpkg.com/@strudel/[email protected]"></script>

<script src="hazel.js"></script>
</html>
Loading
Loading