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

start universal entrypoint stuff #53

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
22 changes: 22 additions & 0 deletions editor/package-lock.json

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

1 change: 1 addition & 0 deletions editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"devDependencies": {
"@rollup/plugin-commonjs": "^17.0.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^11.0.0",
"rollup": "^2.3.4",
"rollup-plugin-css-only": "^3.1.0",
Expand Down
940 changes: 317 additions & 623 deletions editor/public/saved.json

Large diffs are not rendered by default.

143 changes: 80 additions & 63 deletions editor/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,93 @@
import svelte from 'rollup-plugin-svelte';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import livereload from 'rollup-plugin-livereload';
import { terser } from 'rollup-plugin-terser';
import css from 'rollup-plugin-css-only';
import svelte from "rollup-plugin-svelte";
import commonjs from "@rollup/plugin-commonjs";
import resolve from "@rollup/plugin-node-resolve";
import livereload from "rollup-plugin-livereload";
import { terser } from "rollup-plugin-terser";
import css from "rollup-plugin-css-only";
import json from "@rollup/plugin-json";

const production = !process.env.ROLLUP_WATCH;

function serve() {
let server;
let server;

function toExit() {
if (server) server.kill(0);
}
function toExit() {
if (server) server.kill(0);
}

return {
writeBundle() {
if (server) return;
server = require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
stdio: ['ignore', 'inherit', 'inherit'],
shell: true
});
return {
writeBundle() {
if (server) return;
server = require("child_process").spawn(
"npm",
["run", "start", "--", "--dev"],
{
stdio: ["ignore", "inherit", "inherit"],
shell: true,
}
);

process.on('SIGTERM', toExit);
process.on('exit', toExit);
}
};
process.on("SIGTERM", toExit);
process.on("exit", toExit);
},
};
}

export default {
input: 'src/main.js',
output: {
sourcemap: true,
format: 'iife',
name: 'app',
file: 'public/build/bundle.js'
},
plugins: [
svelte({
compilerOptions: {
// enable run-time checks when not in production
dev: !production
}
}),
// we'll extract any component CSS out into
// a separate file - better for performance
css({ output: 'bundle.css' }),
export default [
{
input: "src/other.js",
output: {
sourcemap: true,
format: "iife",
name: "app",
file: "public/build/other.js",
},
plugins: [json()],
},
{
input: "src/main.js",
output: {
sourcemap: true,
format: "iife",
name: "app",
file: "public/build/bundle.js",
},
plugins: [
svelte({
compilerOptions: {
// enable run-time checks when not in production
dev: !production,
},
}),
// we'll extract any component CSS out into
// a separate file - better for performance
css({ output: "bundle.css" }),

// If you have external dependencies installed from
// npm, you'll most likely need these plugins. In
// some cases you'll need additional configuration -
// consult the documentation for details:
// https://github.com/rollup/plugins/tree/master/packages/commonjs
resolve({
browser: true,
dedupe: ['svelte']
}),
commonjs(),
// If you have external dependencies installed from
// npm, you'll most likely need these plugins. In
// some cases you'll need additional configuration -
// consult the documentation for details:
// https://github.com/rollup/plugins/tree/master/packages/commonjs
resolve({
browser: true,
dedupe: ["svelte"],
}),
commonjs(),

// In dev mode, call `npm run start` once
// the bundle has been generated
!production && serve(),
// In dev mode, call `npm run start` once
// the bundle has been generated
!production && serve(),

// Watch the `public` directory and refresh the
// browser on changes when not in production
!production && livereload('public'),
// Watch the `public` directory and refresh the
// browser on changes when not in production
!production && livereload("public"),

// If we're building for production (npm run build
// instead of npm run dev), minify
production && terser()
],
watch: {
clearScreen: false
}
};
// If we're building for production (npm run build
// instead of npm run dev), minify
production && terser(),
],
watch: {
clearScreen: false,
},
},
];
1 change: 1 addition & 0 deletions editor/src/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// import { main } from "../../eyg/build/dev/javascript/eyg/dist/eyg/workspace/main";
// TODO remove deploy
import { deploy } from "../../eyg/build/dev/javascript/eyg/dist/eyg/workspace/ui.mjs";
import * as Spreasheet from "../../eyg/build/dev/javascript/eyg/dist/spreadsheet/main.mjs";

Expand Down
22 changes: 22 additions & 0 deletions editor/src/other.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as Entry from "../../eyg/build/dev/javascript/eyg/dist/eyg/entry.mjs";
import * as Encode from "../../eyg/build/dev/javascript/eyg/dist/eyg/ast/encode.mjs";

// Roll up adds a default object that breaks matchings
import data from "../public/saved.json";
// This is not generic has because we have the server or maybe not. this is the arbitrary script pull
// let target = new URL(document.currentScript.src).hash.slice(1);
// console.log(target, Analysis);

async function GoGoGo() {
const source = Encode.from_json(data);
const initial = Entry.interpret_client(source, "counter");
console.log(initial);
const { default: next } = await import("../public/saved.json");
console.log(Encode.from_json(next));
}

console.log(window, GoGoGo);
window.GoGoGo = GoGoGo;
GoGoGo();
// Started doing this because the code gen was troublesome, also in code gen we will nee to pull in the loader
// here we have can use the already existing gleam .run
4 changes: 4 additions & 0 deletions eyg/src/eyg/analysis.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ pub fn infer(untyped, type_, variables) {
typer.infer(untyped, type_, t.empty, state)
}

pub fn infer_unbound(untyped) {
infer(untyped, t.Unbound(-1), [])
}

pub fn infer_effectful(untyped, type_, effects, variables) {
let checker = typer.init()
let scope = typer.root_scope(variables)
Expand Down
4 changes: 3 additions & 1 deletion eyg/src/eyg/ast/encode.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ external fn from_array(value: JSON) -> List(JSON) =

pub fn from_json(json: JSON) {
assert Ok(#(node, rest)) = list.key_pop(entries(json), "node")

// TODO need to load out properly
// TODO make this robust no asserts or assumption of order use key pop
// find node and order rest
case assert_string(node) {
"Binary" -> {
Expand Down Expand Up @@ -203,7 +206,6 @@ pub fn from_json(json: JSON) {
let [#("label", label)] = rest
ast.variable(assert_string(label))
}

"Let" -> {
let [#("pattern", pattern), #("value", value), #("then", then)] = rest
let pattern = pattern_from_json(pattern)
Expand Down
3 changes: 1 addition & 2 deletions eyg/src/eyg/codegen/javascript.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ pub fn escape_string(raw) {
|> string.replace("\\", "\\\\")
|> string.replace("'", "\\'")
|> string.replace("\"", "\\\"")

// Not js but browser
// Not js but browser
|> string.replace("<", "\\<")
|> string.replace(">", "\\>")
}
Expand Down
59 changes: 59 additions & 0 deletions eyg/src/eyg/entry.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import gleam/io
import gleam/map
import gleam/option.{None}
import gleam/string
import eyg/ast/encode
import eyg/ast/expression as e
import eyg/interpreter/effectful
// TODO need an effectful version that allows us to access stuff here
import eyg/interpreter/tail_call
import gleam/javascript/array
import eyg/interpreter/interpreter as r
import eyg/ast/pattern as p
import eyg/analysis
import eyg/typer
import eyg/typer/monotype as t
import eyg/editor/editor

fn update(page, interrupt, display, on_click) {
io.debug(page)
display(page)
}

fn b(args) {
Ok(args)
}

fn tree() {
r.Function(
p.Variable("x"),
e.tuple_([e.variable("x"), e.variable("x")]),
map.new(),
None,
)
}

// uses default builtin that need moving out of effectful
// has an entry point key should eventually be a hash
// maybe rename interpret standard
// builtin is always the same but env things are passed in
// All the runtime stuff is in gleam terms
// TODO are there any gleam helpers to turn tuples into things
pub fn interpret_client(source, key, display, on_click) {
io.debug("hooo")
let init = e.access(e.access(source, key), "init")
io.debug(init)
let #(typed, typer) = analysis.infer(init, t.Unbound(-1), [])
io.debug("---- typed")
let #(xtyped, typer) =
typer.expand_providers(typed, typer, [])
// Running the interpreter kills the client
// assert Ok(term) = effectful.eval(editor.untype(xtyped))
// io.debug(term)
|> io.debug
io.debug("expanded")
effectful.eval_call(tree(), r.Binary("nothing exciting"), effectful.real_log)
|> io.debug
// // TODO make an AST the requires rendering
// term
}
15 changes: 15 additions & 0 deletions eyg/src/eyg/interpreter/effectful.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,23 @@ fn term_serialize(term) {
"({
on_click: (f) => { document.onclick = () => f() },
display: (value) => document.body.innerHTML = value,
// TODO does with work can we have outside poller
on_code: (f) => { document.oncode = f }
});",
)
// This didn't work because of rendering captured variables for a fn within some scope.
// We need a whole review of rendering or use interpreter
// "(({init}) => {
// console.log(init, 'initial state')
// const update = ({page, interrupt}) => {
// document.body.innerHTML = page
// document.onclick = () => update(interrupt({Click: 'key'}))
// document.oncode = (code) => update(interrupt({Code: code}))
// TODO this needs the codegen part of loader
// }
// update(init)
// // TODO set interval
// })"
let page =
string.concat(["<head></head><body></body><script>", program, "</script>"])
// assert r.Function() = term
Expand Down
15 changes: 9 additions & 6 deletions eyg/src/eyg/interpreter/interpreter.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import eyg/analysis
import eyg/typer
import eyg/typer/monotype as t


pub type Object {
Binary(String)
Pid(Int)
Expand Down Expand Up @@ -64,6 +63,7 @@ pub fn render_var(assignment) {
#("equal", _) -> "let equal = ([a, b]) => a == b;"
// TODO remove duplication of this builtincode
// can i import * as builtin from /gleam/version
// TODO builtin needs to include render
#("builtin", _) -> "let builtin = {append: ([a, b]) => a + b}"
// TODO have a standard builtin to lookup table
#("send", BuiltinFn(_)) ->
Expand Down Expand Up @@ -117,12 +117,15 @@ pub fn render_object(object) {
string.concat(["{", tag, ":", render_object(value), "}"])
// Builtins should never be included, I need to check variables used in a previous step
// Function(_,_,_,_) -> todo("this needs compile again but I need a way to do this without another type check")
Function(pattern, body, _, _) -> {
let #(typed, typer) = analysis.infer(e.function(pattern, body), t.Unbound(-1), [])
let #(typed, typer) = typer.expand_providers(typed, typer, [])
javascript.render_to_string(typed, typer)
Function(pattern, body, captured, _) -> {
// TODO this needs to render captured to be useful
let #(typed, typer) =
analysis.infer(e.function(pattern, body), t.Unbound(-1), [])
let #(typed, typer) = typer.expand_providers(typed, typer, [])
javascript.render_to_string(typed, typer)
}
BuiltinFn(_) -> "null /* we aren't using builtin here should be part of env */"
BuiltinFn(_) ->
"null /* we aren't using builtin here should be part of env */"
// TODO remove Coroutine/ready there where and old experiment
Coroutine(_) -> "null"
Ready(_, _) -> "null"
Expand Down
Loading