2024-06-06 22:47:04 +00:00
|
|
|
import init from "./out.mjs"
|
|
|
|
|
|
|
|
const mod = await init()
|
|
|
|
|
2024-09-09 22:23:13 +00:00
|
|
|
let res = null
|
2024-07-22 00:00:34 +00:00
|
|
|
|
2024-07-31 23:25:22 +00:00
|
|
|
let code = null
|
|
|
|
|
2024-06-06 22:47:04 +00:00
|
|
|
export function run (source) {
|
2024-07-31 23:25:22 +00:00
|
|
|
code = source
|
2024-07-22 00:00:34 +00:00
|
|
|
const output = mod.ludus(source).value
|
2024-09-09 22:23:13 +00:00
|
|
|
res = JSON.parse(output)
|
|
|
|
return res
|
2024-07-22 00:00:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function stdout () {
|
2024-09-09 22:23:13 +00:00
|
|
|
if (!res) return ""
|
|
|
|
return res.io.console.data
|
2024-06-06 22:47:04 +00:00
|
|
|
}
|
2024-07-22 00:00:34 +00:00
|
|
|
|
|
|
|
export function turtle_commands () {
|
2024-09-09 22:23:13 +00:00
|
|
|
if (!res) return []
|
|
|
|
return res.io.turtle.data
|
|
|
|
}
|
|
|
|
|
|
|
|
export function result () {
|
|
|
|
return res
|
2024-07-22 00:00:34 +00:00
|
|
|
}
|
|
|
|
|
2024-07-22 15:39:57 +00:00
|
|
|
const turtle_init = {
|
|
|
|
position: [0, 0],
|
|
|
|
heading: 0,
|
|
|
|
pendown: true,
|
|
|
|
pencolor: "white",
|
|
|
|
penwidth: 1,
|
|
|
|
visible: true
|
|
|
|
}
|
|
|
|
|
|
|
|
const colors = {
|
|
|
|
black: [0, 0, 0, 255],
|
|
|
|
silver: [192, 192, 192, 255],
|
|
|
|
gray: [128, 128, 128, 255],
|
|
|
|
white: [255, 255, 255, 255],
|
|
|
|
maroon: [128, 0, 0, 255],
|
|
|
|
red: [255, 0, 0, 255],
|
|
|
|
purple: [128, 0, 128, 255],
|
|
|
|
fuchsia: [255, 0, 255, 255],
|
|
|
|
green: [0, 128, 0, 255],
|
|
|
|
lime: [0, 255, 0, 255],
|
|
|
|
olive: [128, 128, 0, 255],
|
|
|
|
yellow: [255, 255, 0, 255],
|
|
|
|
navy: [0, 0, 128, 255],
|
|
|
|
blue: [0, 0, 255, 255],
|
|
|
|
teal: [0, 128, 128, 255],
|
|
|
|
aqua: [0, 255, 25, 255],
|
|
|
|
}
|
|
|
|
|
2024-07-30 22:36:13 +00:00
|
|
|
function resolve_color (color) {
|
|
|
|
if (typeof color === 'string') return colors[color]
|
|
|
|
if (typeof color === 'number') return [color, color, color, 255]
|
|
|
|
if (Array.isArray(color)) return color
|
2024-09-09 22:23:13 +00:00
|
|
|
return [0, 0, 0, 255] // default to black?
|
2024-07-22 15:39:57 +00:00
|
|
|
}
|
|
|
|
|
2024-07-30 22:36:13 +00:00
|
|
|
let background_color = "black"
|
|
|
|
|
2024-07-22 15:39:57 +00:00
|
|
|
function add (v1, v2) {
|
|
|
|
const [x1, y1] = v1
|
|
|
|
const [x2, y2] = v2
|
|
|
|
return [x1 + x2, y1 + y2]
|
|
|
|
}
|
|
|
|
|
|
|
|
function mult (vector, scalar) {
|
|
|
|
const [x, y] = vector
|
|
|
|
return [x * scalar, y * scalar]
|
|
|
|
}
|
|
|
|
|
|
|
|
function unit_of (heading) {
|
|
|
|
const turns = -heading + 0.25
|
|
|
|
const radians = turn_to_rad(turns)
|
2024-07-30 22:36:13 +00:00
|
|
|
return [Math.cos(radians), Math.sin(radians)]
|
2024-07-22 15:39:57 +00:00
|
|
|
}
|
|
|
|
|
2024-07-30 22:36:13 +00:00
|
|
|
function command_to_state (prev_state, curr_command) {
|
|
|
|
const verb = curr_command[0]
|
2024-07-22 15:39:57 +00:00
|
|
|
switch (verb) {
|
|
|
|
case "goto": {
|
2024-07-30 22:36:13 +00:00
|
|
|
const [_, x, y] = curr_command
|
|
|
|
return {...prev_state, position: [x, y]}
|
2024-07-22 15:39:57 +00:00
|
|
|
}
|
|
|
|
case "home": {
|
2024-07-30 22:36:13 +00:00
|
|
|
return {...prev_state, position: [0, 0], heading: 0}
|
2024-07-22 15:39:57 +00:00
|
|
|
}
|
|
|
|
case "right": {
|
2024-07-30 22:36:13 +00:00
|
|
|
const [_, angle] = curr_command
|
|
|
|
const {heading} = prev_state
|
|
|
|
return {...prev_state, heading: heading + angle}
|
2024-07-22 15:39:57 +00:00
|
|
|
}
|
|
|
|
case "left": {
|
2024-07-30 22:36:13 +00:00
|
|
|
const [_, angle] = curr_command
|
|
|
|
const {heading} = prev_state
|
|
|
|
return {...prev_state, heading: heading - angle}
|
2024-07-22 15:39:57 +00:00
|
|
|
}
|
|
|
|
case "forward": {
|
2024-07-30 22:36:13 +00:00
|
|
|
const [_, steps] = curr_command
|
|
|
|
const {heading, position} = prev_state
|
2024-07-22 15:39:57 +00:00
|
|
|
const unit = unit_of(heading)
|
|
|
|
const move = mult(unit, steps)
|
2024-07-30 22:36:13 +00:00
|
|
|
return {...prev_state, position: add(position, move)}
|
2024-07-22 15:39:57 +00:00
|
|
|
}
|
|
|
|
case "back": {
|
2024-07-30 22:36:13 +00:00
|
|
|
const [_, steps] = curr_command
|
|
|
|
const {heading, position} = prev_state
|
2024-07-22 15:39:57 +00:00
|
|
|
const unit = unit_of(heading)
|
|
|
|
const move = mult(unit, -steps)
|
2024-07-30 22:36:13 +00:00
|
|
|
return {...prev_state, position: add(position, move)}
|
2024-07-22 15:39:57 +00:00
|
|
|
}
|
|
|
|
case "penup": {
|
2024-07-30 22:36:13 +00:00
|
|
|
return {...prev_state, pendown: false}
|
2024-07-22 15:39:57 +00:00
|
|
|
}
|
|
|
|
case "pendown": {
|
2024-07-30 22:36:13 +00:00
|
|
|
return {...prev_state, pendown: true}
|
2024-07-22 15:39:57 +00:00
|
|
|
}
|
|
|
|
case "pendwith": {
|
2024-07-30 22:36:13 +00:00
|
|
|
const [_, width] = curr_command
|
|
|
|
return {...prev_state, penwidth: width}
|
2024-07-22 15:39:57 +00:00
|
|
|
}
|
|
|
|
case "pencolor": {
|
2024-08-01 21:05:41 +00:00
|
|
|
const [_, color] = curr_command
|
|
|
|
return {...prev_state, pencolor: color}
|
2024-07-22 15:39:57 +00:00
|
|
|
}
|
|
|
|
case "setheading": {
|
2024-07-30 22:36:13 +00:00
|
|
|
const [_, heading] = curr_command
|
|
|
|
return {...prev_state, heading: heading}
|
2024-07-22 15:39:57 +00:00
|
|
|
}
|
|
|
|
case "loadstate": {
|
2024-08-01 21:05:41 +00:00
|
|
|
const [_, x, y, heading, visible, pendown, penwidth, pencolor] = curr_command
|
2024-07-22 15:39:57 +00:00
|
|
|
return {position: [x, y], heading, visible, pendown, penwidth, pencolor}
|
|
|
|
}
|
|
|
|
case "show": {
|
2024-07-30 22:36:13 +00:00
|
|
|
return {...prev_state, visible: true}
|
2024-07-22 15:39:57 +00:00
|
|
|
}
|
|
|
|
case "hide": {
|
2024-07-30 22:36:13 +00:00
|
|
|
return {...prev_state, visible: false}
|
2024-07-22 15:39:57 +00:00
|
|
|
}
|
|
|
|
case "background": {
|
2024-08-01 21:31:36 +00:00
|
|
|
background_color = curr_command[1]
|
2024-07-31 23:25:22 +00:00
|
|
|
return prev_state
|
2024-07-22 15:39:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-02 18:55:46 +00:00
|
|
|
function eq_vect (v1, v2) {
|
2024-07-22 15:39:57 +00:00
|
|
|
const [x1, y1] = v1
|
|
|
|
const [x2, y2] = v2
|
|
|
|
return (x1 === x2) && (y1 === y2)
|
|
|
|
}
|
|
|
|
|
2024-08-02 18:55:46 +00:00
|
|
|
function eq_color (c1, c2) {
|
|
|
|
if (c1 === c2) return true
|
|
|
|
const res1 = resolve_color(c1)
|
|
|
|
const res2 = resolve_color(c2)
|
|
|
|
for (let i = 0; i < res1.length; ++i) {
|
|
|
|
if (res1[i] !== res2[i]) return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-07-30 22:36:13 +00:00
|
|
|
function states_to_call (prev, curr) {
|
|
|
|
const calls = []
|
|
|
|
// whose state should we use?
|
|
|
|
// pen states will only differ on more than one property
|
|
|
|
// if we use `loadstate`
|
|
|
|
// my sense is `prev`, but that may change
|
2024-08-02 18:55:46 +00:00
|
|
|
if (prev.pendown && !eq_vect(prev.position, curr.position)) {
|
2024-07-30 22:36:13 +00:00
|
|
|
calls.push(["line", prev.position[0], prev.position[1], curr.position[0], curr.position[1]])
|
|
|
|
}
|
2024-08-02 18:55:46 +00:00
|
|
|
if (!eq_color(curr.pencolor, prev.pencolor)) {
|
|
|
|
calls.push(["stroke", ...resolve_color(curr.pencolor)])
|
2024-07-30 22:36:13 +00:00
|
|
|
}
|
|
|
|
if (curr.penwidth !== prev.penwidth) {
|
|
|
|
calls.push(["strokeWeight", curr.penwidth])
|
|
|
|
}
|
|
|
|
return calls
|
2024-07-22 15:39:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const turtle_radius = 20
|
|
|
|
|
|
|
|
const turtle_angle = 0.385
|
|
|
|
|
|
|
|
const turtle_color = [255, 255, 255, 150]
|
|
|
|
|
2024-07-30 22:36:13 +00:00
|
|
|
const p5_call_root = [
|
2024-10-20 18:17:39 +00:00
|
|
|
["push"],
|
|
|
|
["rotate", Math.PI],
|
|
|
|
["scale", -1, 1],
|
2024-07-30 22:36:13 +00:00
|
|
|
["background", ...resolve_color(background_color)],
|
2024-10-20 18:17:39 +00:00
|
|
|
["stroke", ...resolve_color(turtle_init.pencolor)],
|
2024-07-30 22:36:13 +00:00
|
|
|
]
|
2024-07-22 15:39:57 +00:00
|
|
|
|
|
|
|
function rotate (vector, heading) {
|
|
|
|
const radians = turn_to_rad(heading)
|
|
|
|
const [x, y] = vector
|
|
|
|
return [
|
2024-07-30 22:36:13 +00:00
|
|
|
(x * Math.cos (radians)) - (y * Math.sin (radians)),
|
|
|
|
(x * Math.sin (radians)) + (y * Math.cos (radians))
|
2024-07-22 15:39:57 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
function turn_to_rad (heading) {
|
2024-08-01 22:50:09 +00:00
|
|
|
return (heading % 1) * 2 * Math.PI
|
2024-07-22 15:39:57 +00:00
|
|
|
}
|
|
|
|
|
2024-07-31 23:25:22 +00:00
|
|
|
function turn_to_deg (heading) {
|
2024-09-09 22:23:13 +00:00
|
|
|
return (heading % 1) * 360
|
2024-07-31 23:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function svg_render_line (prev, curr) {
|
|
|
|
if (!prev.pendown) return ""
|
2024-08-02 18:55:46 +00:00
|
|
|
if (eq_vect(prev.position, curr.position)) return ""
|
2024-07-31 23:25:22 +00:00
|
|
|
const {position: [x1, y1], pencolor, penwidth} = prev
|
|
|
|
const {position: [x2, y2]} = curr
|
|
|
|
const [r, g, b, a] = resolve_color(pencolor)
|
|
|
|
return `
|
|
|
|
<line x1="${x1}" y1="${y1}" x2="${x2}" y2="${y2}" stroke="rgb(${r} ${g} ${b})" stroke-opacity="${a/255}" stroke-width="${penwidth}"/>
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
|
|
|
function escape_svg (svg) {
|
|
|
|
return svg
|
|
|
|
.replace(/&/g, "&")
|
|
|
|
.replace(/</g, "<")
|
|
|
|
.replace(/>/g, ">")
|
|
|
|
.replace(/"/g, """)
|
|
|
|
.replace(/'/g, "'")
|
|
|
|
}
|
|
|
|
|
2024-09-09 22:23:13 +00:00
|
|
|
export function extract_ludus (svg) {
|
|
|
|
const code = svg.split("<ludus>")[1]?.split("</ludus>")[0] ?? ""
|
|
|
|
return code
|
|
|
|
.replace(/&/g, "&")
|
|
|
|
.replace(/</g, "<")
|
|
|
|
.replace(/>/g, ">")
|
|
|
|
.replace(/"/g, `"`)
|
|
|
|
.replace(/'/g, `'`)
|
|
|
|
}
|
|
|
|
|
2024-07-31 23:25:22 +00:00
|
|
|
function svg_render_path (states) {
|
|
|
|
const path = []
|
|
|
|
for (let i = 1; i < states.length; ++i) {
|
|
|
|
const prev = states[i - 1]
|
|
|
|
const curr = states[i]
|
|
|
|
path.push(svg_render_line(prev, curr))
|
|
|
|
}
|
|
|
|
return path.join("")
|
|
|
|
}
|
|
|
|
|
|
|
|
function svg_render_turtle (state) {
|
|
|
|
if (!state.visible) return ""
|
|
|
|
const [fr, fg, fb, fa] = turtle_color
|
|
|
|
const fill_alpha = fa/255
|
|
|
|
const {heading, pencolor, position: [x, y], pendown, penwidth} = state
|
|
|
|
const origin = [0, turtle_radius]
|
|
|
|
const [x1, y1] = origin
|
|
|
|
const [x2, y2] = rotate(origin, turtle_angle)
|
|
|
|
const [x3, y3] = rotate(origin, -turtle_angle)
|
|
|
|
const [pr, pg, pb, pa] = resolve_color(pencolor)
|
|
|
|
const pen_alpha = pa/255
|
|
|
|
const ink = pendown ? `<line x1="${x1}" y1="${y1}" x2="0" y2="0" stroke="rgb(${pr} ${pg} ${pb})" stroke-opacity="${pen_alpha}" stroke-width="${penwidth}" />` : ""
|
|
|
|
return `
|
|
|
|
<g transform="translate(${x}, ${y})rotate(${turn_to_deg(heading)})">
|
|
|
|
<polygon points="${x1} ${y1} ${x2} ${y2} ${x3} ${y3}" stroke="none" fill="rgb(${fr} ${fg} ${fb})" fill-opacity="${fill_alpha}"/>
|
|
|
|
${ink}
|
|
|
|
</g>
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
|
|
|
export function svg (commands) {
|
|
|
|
const states = [turtle_init]
|
|
|
|
commands.reduce((prev_state, command) => {
|
|
|
|
const new_state = command_to_state(prev_state, command)
|
|
|
|
states.push(new_state)
|
|
|
|
return new_state
|
|
|
|
}, turtle_init)
|
|
|
|
const {maxX, maxY, minX, minY} = states.reduce((accum, {position: [x, y]}) => {
|
|
|
|
accum.maxX = Math.max(accum.maxX, x)
|
|
|
|
accum.maxY = Math.max(accum.maxY, y)
|
|
|
|
accum.minX = Math.min(accum.minX, x)
|
|
|
|
accum.minY = Math.min(accum.minY, y)
|
|
|
|
return accum
|
|
|
|
|
|
|
|
}, {maxX: 0, maxY: 0, minX: 0, minY: 0})
|
|
|
|
const [r, g, b, a] = resolve_color(background_color)
|
2024-08-01 22:35:28 +00:00
|
|
|
const view_width = (maxX - minX) * 1.2
|
|
|
|
const view_height = (maxY - minY) * 1.2
|
2024-07-31 23:25:22 +00:00
|
|
|
const margin = Math.max(view_width, view_height) * 0.1
|
2024-08-01 22:35:28 +00:00
|
|
|
const x1 = minX - margin
|
2024-09-09 22:23:13 +00:00
|
|
|
// don't actually need these:
|
|
|
|
// const y1 = minY - margin
|
|
|
|
// const x2 = maxX + margin
|
2024-08-01 22:35:28 +00:00
|
|
|
const y2 = maxY + margin
|
2024-07-31 23:25:22 +00:00
|
|
|
const path = svg_render_path(states)
|
|
|
|
const turtle = svg_render_turtle(states[states.length - 1])
|
2024-08-01 20:43:01 +00:00
|
|
|
return `<?xml version="1.0" standalone="no"?>
|
2024-08-01 22:35:28 +00:00
|
|
|
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" style="background-color:rgb(${r} ${g} ${b}); background-opacity: ${a/255}" viewBox="${x1} ${-y2} ${view_width} ${view_height}">
|
2024-07-31 23:25:22 +00:00
|
|
|
|
2024-08-01 20:43:01 +00:00
|
|
|
<g transform="scale(-1, 1) rotate(180)">
|
2024-07-31 23:25:22 +00:00
|
|
|
${path}
|
|
|
|
${turtle}
|
|
|
|
</g>
|
|
|
|
|
|
|
|
<ludus>
|
|
|
|
${escape_svg(code)}
|
|
|
|
</ludus>
|
|
|
|
</svg>
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
|
|
|
function p5_render_turtle (state, calls) {
|
2024-07-22 15:39:57 +00:00
|
|
|
if (!state.visible) return
|
2024-07-30 22:36:13 +00:00
|
|
|
calls.push(["push"])
|
2024-07-22 15:39:57 +00:00
|
|
|
const [r, g, b, a] = turtle_color
|
2024-07-30 22:36:13 +00:00
|
|
|
calls.push(["fill", r, g, b, a])
|
2024-07-31 23:25:22 +00:00
|
|
|
const {heading, pencolor, position: [x, y], pendown, penwidth} = state
|
2024-07-22 15:39:57 +00:00
|
|
|
const origin = [0, turtle_radius]
|
|
|
|
const [x1, y1] = origin
|
|
|
|
const [x2, y2] = rotate(origin, turtle_angle)
|
|
|
|
const [x3, y3] = rotate(origin, -turtle_angle)
|
2024-07-30 22:36:13 +00:00
|
|
|
calls.push(["translate", x, y])
|
2024-10-20 18:17:39 +00:00
|
|
|
// need negative turtle rotation with the other p5 translations
|
|
|
|
calls.push(["rotate", -turn_to_rad(heading)])
|
2024-07-30 22:36:13 +00:00
|
|
|
calls.push(["noStroke"])
|
|
|
|
calls.push(["beginShape"])
|
|
|
|
calls.push(["vertex", x1, y1])
|
|
|
|
calls.push(["vertex", x2, y2])
|
|
|
|
calls.push(["vertex", x3, y3])
|
|
|
|
calls.push(["endShape"])
|
2024-07-31 23:25:22 +00:00
|
|
|
calls.push(["strokeWeight", penwidth])
|
2024-07-30 22:36:13 +00:00
|
|
|
calls.push(["stroke", ...resolve_color(pencolor)])
|
|
|
|
if (pendown) calls.push(["line", 0, 0, x1, y1])
|
|
|
|
calls.push(["pop"])
|
|
|
|
return calls
|
2024-07-22 15:39:57 +00:00
|
|
|
}
|
|
|
|
|
2024-08-01 22:35:28 +00:00
|
|
|
export function p5 (commands) {
|
2024-07-30 22:36:13 +00:00
|
|
|
const states = [turtle_init]
|
|
|
|
commands.reduce((prev_state, command) => {
|
|
|
|
const new_state = command_to_state(prev_state, command)
|
|
|
|
states.push(new_state)
|
|
|
|
return new_state
|
|
|
|
}, turtle_init)
|
|
|
|
const p5_calls = [...p5_call_root]
|
|
|
|
for (let i = 1; i < states.length; ++i) {
|
|
|
|
const prev = states[i - 1]
|
|
|
|
const curr = states[i]
|
|
|
|
const calls = states_to_call(prev, curr)
|
|
|
|
for (const call of calls) {
|
|
|
|
p5_calls.push(call)
|
|
|
|
}
|
2024-10-20 18:22:14 +00:00
|
|
|
p5_calls.push(["pop"])
|
2024-07-30 22:36:13 +00:00
|
|
|
}
|
2024-08-01 21:31:36 +00:00
|
|
|
p5_calls[0] = ["background", ...resolve_color(background_color)]
|
2024-07-31 23:25:22 +00:00
|
|
|
p5_render_turtle(states[states.length - 1], p5_calls)
|
2024-07-30 22:36:13 +00:00
|
|
|
return p5_calls
|
|
|
|
}
|
2024-07-31 23:25:22 +00:00
|
|
|
|