This commit is contained in:
Scott Richmond 2024-11-03 17:19:11 -05:00
parent 2f5557a552
commit 1ead4a8c5c
3 changed files with 19 additions and 20 deletions

View File

@ -10,13 +10,12 @@ export function run (source) {
code = source code = source
const output = mod.ludus(source).value const output = mod.ludus(source).value
res = JSON.parse(output) res = JSON.parse(output)
console.log(res)
return res return res
} }
export function stdout () { export function stdout () {
if (!res) return "" if (!res) return ""
return res.io.console.data return res.io.stdout.data
} }
export function turtle_commands () { export function turtle_commands () {
@ -135,7 +134,7 @@ function command_to_state (prev_state, curr_command) {
return {...prev_state, heading: heading} return {...prev_state, heading: heading}
} }
case "loadstate": { case "loadstate": {
console.log("LOADSTATE: ", curr_command) // console.log("LOADSTATE: ", curr_command)
const [_, [x, y], heading, visible, pendown, penwidth, pencolor] = curr_command const [_, [x, y], heading, visible, pendown, penwidth, pencolor] = curr_command
return {position: [x, y], heading, visible, pendown, penwidth, pencolor} return {position: [x, y], heading, visible, pendown, penwidth, pencolor}
} }
@ -270,7 +269,7 @@ function svg_render_turtle (state) {
const pen_alpha = pa/255 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}" />` : "" 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 ` return `
<g transform="translate(${x}, ${y})rotate(${turn_to_deg(heading)})"> <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}"/> <polygon points="${x1} ${y1} ${x2} ${y2} ${x3} ${y3}" stroke="none" fill="rgb(${fr} ${fg} ${fb})" fill-opacity="${fill_alpha}"/>
${ink} ${ink}
</g> </g>
@ -278,14 +277,14 @@ function svg_render_turtle (state) {
} }
export function svg (commands) { export function svg (commands) {
console.log(commands) // console.log(commands)
const states = [turtle_init] const states = [turtle_init]
commands.reduce((prev_state, command) => { commands.reduce((prev_state, command) => {
const new_state = command_to_state(prev_state, command) const new_state = command_to_state(prev_state, command)
states.push(new_state) states.push(new_state)
return new_state return new_state
}, turtle_init) }, turtle_init)
console.log(states) // console.log(states)
const {maxX, maxY, minX, minY} = states.reduce((accum, {position: [x, y]}) => { const {maxX, maxY, minX, minY} = states.reduce((accum, {position: [x, y]}) => {
accum.maxX = Math.max(accum.maxX, x) accum.maxX = Math.max(accum.maxX, x)
accum.maxY = Math.max(accum.maxY, y) accum.maxY = Math.max(accum.maxY, y)
@ -353,7 +352,7 @@ export function p5 (commands) {
states.push(new_state) states.push(new_state)
return new_state return new_state
}, turtle_init) }, turtle_init)
console.log(states) // console.log(states)
const p5_calls = [...p5_call_root] const p5_calls = [...p5_call_root]
for (let i = 1; i < states.length; ++i) { for (let i = 1; i < states.length; ++i) {
const prev = states[i - 1] const prev = states[i - 1]

View File

@ -1,20 +1,22 @@
import {run, svg} from "./ludus.mjs" import {run, svg, stdout} from "./ludus.mjs"
const code = ` const code = `
let start = unbox (turtle_state)
let home = unbox (turtle_state)
fd! (100) fd! (100)
loadstate! (home)
rt! (0.25) rt! (0.25)
fd! (100) fd! (100)
do turtle_state > unbox loadstate! (start)
& home! ()
rt! (0.25)
fd! (100)
lt! (0.25)
` `
const result = run(code) const result = run(code)
// console.log(stdout(result))
console.log(svg(result.io.turtle.data)) console.log(svg(result.io.turtle.data))

View File

@ -5,12 +5,12 @@
<line x1="0" y1="0" x2="6.123233995736766e-15" y2="100" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/> <line x1="0" y1="0" x2="6.123233995736766e-15" y2="100" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
<line x1="6.123233995736766e-15" y1="100" x2="100" y2="100" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/> <line x1="6.123233995736766e-15" y1="100" x2="0" y2="0" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
<line x1="100" y1="100" x2="100" y2="0" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/> <line x1="0" y1="0" x2="100" y2="0" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
<g transform="translate(100, 0)rotate(180)"> <g transform="translate(100, 0)rotate(90)">
<polygon points="0 20 -13.226237306473037 -15.00222139260919 13.226237306473037 -15.00222139260919" stroke="none" fill="rgb(255 255 255)" fill-opacity="0.5882352941176471"/> <polygon points="0 20 -13.226237306473037 -15.00222139260919 13.226237306473037 -15.00222139260919" stroke="none" fill="rgb(255 255 255)" fill-opacity="0.5882352941176471"/>
<line x1="0" y1="20" x2="0" y2="0" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1" /> <line x1="0" y1="20" x2="0" y2="0" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1" />
</g> </g>
@ -22,11 +22,9 @@
let home = unbox (turtle_state) let home = unbox (turtle_state)
fd! (100)
rt! (0.25)
fd! (100) fd! (100)
&amp; loadstate! (home) loadstate! (home)
rt! (0.25) rt! (0.25)
fd! (100) fd! (100)

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB