Compare commits
No commits in common. "main" and "tg_protocol" have entirely different histories.
main
...
tg_protoco
Binary file not shown.
|
@ -15,7 +15,7 @@ export function run (source) {
|
||||||
|
|
||||||
export function stdout () {
|
export function stdout () {
|
||||||
if (!res) return ""
|
if (!res) return ""
|
||||||
return res.io.stdout.data
|
return res.io.console.data
|
||||||
}
|
}
|
||||||
|
|
||||||
export function turtle_commands () {
|
export function turtle_commands () {
|
||||||
|
@ -121,7 +121,7 @@ function command_to_state (prev_state, curr_command) {
|
||||||
case "pendown": {
|
case "pendown": {
|
||||||
return {...prev_state, pendown: true}
|
return {...prev_state, pendown: true}
|
||||||
}
|
}
|
||||||
case "penwidth": {
|
case "pendwith": {
|
||||||
const [_, width] = curr_command
|
const [_, width] = curr_command
|
||||||
return {...prev_state, penwidth: width}
|
return {...prev_state, penwidth: width}
|
||||||
}
|
}
|
||||||
|
@ -134,8 +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)
|
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}
|
||||||
}
|
}
|
||||||
case "show": {
|
case "show": {
|
||||||
|
@ -193,10 +192,7 @@ const turtle_color = [255, 255, 255, 150]
|
||||||
|
|
||||||
const p5_call_root = [
|
const p5_call_root = [
|
||||||
["background", ...resolve_color(background_color)],
|
["background", ...resolve_color(background_color)],
|
||||||
["push"],
|
["stroke", ...resolve_color(turtle_init.pencolor)]
|
||||||
["rotate", Math.PI],
|
|
||||||
["scale", -1, 1],
|
|
||||||
["stroke", ...resolve_color(turtle_init.pencolor)],
|
|
||||||
]
|
]
|
||||||
|
|
||||||
function rotate (vector, heading) {
|
function rotate (vector, heading) {
|
||||||
|
@ -269,7 +265,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>
|
||||||
|
@ -277,14 +273,12 @@ function svg_render_turtle (state) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function svg (commands) {
|
export function svg (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)
|
|
||||||
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)
|
||||||
|
@ -330,8 +324,7 @@ function p5_render_turtle (state, calls) {
|
||||||
const [x2, y2] = rotate(origin, turtle_angle)
|
const [x2, y2] = rotate(origin, turtle_angle)
|
||||||
const [x3, y3] = rotate(origin, -turtle_angle)
|
const [x3, y3] = rotate(origin, -turtle_angle)
|
||||||
calls.push(["translate", x, y])
|
calls.push(["translate", x, y])
|
||||||
// need negative turtle rotation with the other p5 translations
|
calls.push(["rotate", turn_to_rad(heading)])
|
||||||
calls.push(["rotate", -turn_to_rad(heading)])
|
|
||||||
calls.push(["noStroke"])
|
calls.push(["noStroke"])
|
||||||
calls.push(["beginShape"])
|
calls.push(["beginShape"])
|
||||||
calls.push(["vertex", x1, y1])
|
calls.push(["vertex", x1, y1])
|
||||||
|
@ -352,7 +345,6 @@ 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)
|
|
||||||
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]
|
||||||
|
@ -364,7 +356,6 @@ export function p5 (commands) {
|
||||||
}
|
}
|
||||||
p5_calls[0] = ["background", ...resolve_color(background_color)]
|
p5_calls[0] = ["background", ...resolve_color(background_color)]
|
||||||
p5_render_turtle(states[states.length - 1], p5_calls)
|
p5_render_turtle(states[states.length - 1], p5_calls)
|
||||||
p5_calls.push(["pop"])
|
|
||||||
return p5_calls
|
return p5_calls
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6489,7 +6489,7 @@ var __emscripten_stack_alloc = (a0) => (__emscripten_stack_alloc = wasmExports['
|
||||||
var _emscripten_stack_get_current = () => (_emscripten_stack_get_current = wasmExports['emscripten_stack_get_current'])();
|
var _emscripten_stack_get_current = () => (_emscripten_stack_get_current = wasmExports['emscripten_stack_get_current'])();
|
||||||
var ___cxa_is_pointer_type = createExportWrapper('__cxa_is_pointer_type', 1);
|
var ___cxa_is_pointer_type = createExportWrapper('__cxa_is_pointer_type', 1);
|
||||||
var dynCall_jiji = Module['dynCall_jiji'] = createExportWrapper('dynCall_jiji', 5);
|
var dynCall_jiji = Module['dynCall_jiji'] = createExportWrapper('dynCall_jiji', 5);
|
||||||
var ___emscripten_embedded_file_data = Module['___emscripten_embedded_file_data'] = 1819972;
|
var ___emscripten_embedded_file_data = Module['___emscripten_embedded_file_data'] = 1816104;
|
||||||
function invoke_i(index) {
|
function invoke_i(index) {
|
||||||
var sp = stackSave();
|
var sp = stackSave();
|
||||||
try {
|
try {
|
||||||
|
|
BIN
build/out.wasm
BIN
build/out.wasm
Binary file not shown.
|
@ -3,9 +3,13 @@ import {run, p5} from "./ludus.mjs"
|
||||||
const code = `
|
const code = `
|
||||||
print! ("Hello, world!")
|
print! ("Hello, world!")
|
||||||
pencolor! (colors :white)
|
pencolor! (colors :white)
|
||||||
fd! (50)
|
repeat 10 {
|
||||||
pw! (3)
|
repeat 4 {
|
||||||
`
|
fd! (500)
|
||||||
|
rt! (0.25)
|
||||||
|
}
|
||||||
|
rt! (0.1)
|
||||||
|
}`
|
||||||
|
|
||||||
const result = run(code)
|
const result = run(code)
|
||||||
|
|
||||||
|
|
|
@ -1,22 +1,15 @@
|
||||||
import {run, svg, stdout} from "./ludus.mjs"
|
import {run, svg} from "./ludus.mjs"
|
||||||
|
|
||||||
const code = `
|
const code = `
|
||||||
let start = unbox (turtle_state)
|
pencolor! (colors :white)
|
||||||
fd! (100)
|
repeat 10 {
|
||||||
rt! (0.25)
|
repeat 4 {
|
||||||
fd! (100)
|
fd! (500)
|
||||||
|
rt! (0.25)
|
||||||
loadstate! (start)
|
}
|
||||||
& home! ()
|
rt! (0.1)
|
||||||
|
}`
|
||||||
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))
|
||||||
|
|
|
@ -1,16 +1,90 @@
|
||||||
<?xml version="1.0" standalone="no"?>
|
<?xml version="1.0" standalone="no"?>
|
||||||
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" style="background-color:rgb(0 0 0); background-opacity: 1" viewBox="-12 -112 120 120">
|
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" style="background-color:rgb(0 0 0); background-opacity: 1" viewBox="-866.0173929337993 -866.0173929338016 1676.162696000903 1676.1626960009057">
|
||||||
|
|
||||||
<g transform="scale(-1, 1) rotate(180)">
|
<g transform="scale(-1, 1) rotate(180)">
|
||||||
|
|
||||||
<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="3.061616997868383e-14" y2="500" 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="3.061616997868383e-14" y1="500" x2="500.00000000000006" y2="500" 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"/>
|
<line x1="500.00000000000006" y1="500" x2="500.0000000000001" y2="0" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="500.0000000000001" y1="0" x2="1.1368683772161603e-13" y2="-6.123233995736766e-14" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="1.1368683772161603e-13" y1="-6.123233995736766e-14" x2="293.892626146237" y2="404.50849718747344" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="293.892626146237" y1="404.50849718747344" x2="698.4011233337105" y2="110.61587104123663" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="698.4011233337105" y1="110.61587104123663" x2="404.50849718747384" y2="-293.892626146237" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="404.50849718747384" y1="-293.892626146237" x2="2.2737367544323206e-13" y2="-2.8421709430404007e-13" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="2.2737367544323206e-13" y1="-2.8421709430404007e-13" x2="475.5282581475771" y2="154.5084971874731" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="475.5282581475771" y1="154.5084971874731" x2="630.0367553350503" y2="-321.01976096010384" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="630.0367553350503" y1="-321.01976096010384" x2="154.50849718747332" y2="-475.528258147577" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="154.50849718747332" y1="-475.528258147577" x2="-2.842170943040401e-14" y2="-1.1368683772161603e-13" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="-2.842170943040401e-14" y1="-1.1368683772161603e-13" x2="475.5282581475765" y2="-154.5084971874746" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="475.5282581475765" y1="-154.5084971874746" x2="321.01976096010196" y2="-630.0367553350511" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="321.01976096010196" y1="-630.0367553350511" x2="-154.50849718747457" y2="-475.5282581475766" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="-154.50849718747457" y1="-475.5282581475766" x2="1.1368683772161603e-12" y2="-4.547473508864641e-13" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="1.1368683772161603e-12" y1="-4.547473508864641e-13" x2="293.8926261462368" y2="-404.5084971874748" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="293.8926261462368" y1="-404.5084971874748" x2="-110.61587104123754" y2="-698.4011233337105" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="-110.61587104123754" y1="-698.4011233337105" x2="-404.5084971874731" y2="-293.89262614623607" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="-404.5084971874731" y1="-293.89262614623607" x2="1.3642420526593924e-12" y2="-5.115907697472721e-13" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="1.3642420526593924e-12" y1="-5.115907697472721e-13" x2="1.3948582226380762e-12" y2="-500.0000000000005" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="1.3948582226380762e-12" y1="-500.0000000000005" x2="-499.9999999999986" y2="-500.00000000000057" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="-499.9999999999986" y1="-500.00000000000057" x2="-499.9999999999987" y2="-5.684341886080801e-13" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="-499.9999999999987" y1="-5.684341886080801e-13" x2="1.3073986337985843e-12" y2="-5.684341886080801e-13" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="1.3073986337985843e-12" y1="-5.684341886080801e-13" x2="-293.8926261462343" y2="-404.508497187475" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="-293.8926261462343" y1="-404.508497187475" x2="-698.4011233337087" y2="-110.61587104123936" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="-698.4011233337087" y1="-110.61587104123936" x2="-404.5084971874733" y2="293.8926261462352" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="-404.5084971874733" y1="293.8926261462352" x2="1.0800249583553523e-12" y2="-4.547473508864641e-13" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="1.0800249583553523e-12" y1="-4.547473508864641e-13" x2="-475.528258147575" y2="-154.5084971874763" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="-475.528258147575" y1="-154.5084971874763" x2="-630.0367553350509" y2="321.0197609600998" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="-630.0367553350509" y1="321.0197609600998" x2="-154.50849718747486" y2="475.52825814757574" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="-154.50849718747486" y1="475.52825814757574" x2="9.663381206337363e-13" y2="-3.410605131648481e-13" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="9.663381206337363e-13" y1="-3.410605131648481e-13" x2="-475.5282581475769" y2="154.50849718747014" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="-475.5282581475769" y1="154.50849718747014" x2="-321.0197609601066" y2="630.0367553350482" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="-321.0197609601066" y1="630.0367553350482" x2="154.50849718747122" y2="475.5282581475776" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="154.50849718747122" y1="475.5282581475776" x2="7.105427357601002e-13" y2="-2.2737367544323206e-13" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="7.105427357601002e-13" y1="-2.2737367544323206e-13" x2="-293.89262614623954" y2="404.5084971874708" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="-293.89262614623954" y1="404.5084971874708" x2="110.61587104123151" y2="698.401123333711" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="110.61587104123151" y1="698.401123333711" x2="404.5084971874717" y2="293.8926261462399" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
<line x1="404.5084971874717" y1="293.8926261462399" x2="6.252776074688882e-13" y2="-2.8421709430404007e-13" stroke="rgb(255 255 255)" stroke-opacity="1" stroke-width="1"/>
|
||||||
|
|
||||||
|
|
||||||
<g transform="translate(100, 0)rotate(90)">
|
<g transform="translate(6.252776074688882e-13, -2.8421709430404007e-13)rotate(359.9999999999994)">
|
||||||
<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>
|
||||||
|
@ -19,19 +93,14 @@
|
||||||
|
|
||||||
<ludus>
|
<ludus>
|
||||||
|
|
||||||
|
pencolor! (colors :white)
|
||||||
let home = unbox (turtle_state)
|
repeat 10 {
|
||||||
|
repeat 4 {
|
||||||
fd! (100)
|
fd! (500)
|
||||||
|
rt! (0.25)
|
||||||
loadstate! (home)
|
}
|
||||||
|
rt! (0.1)
|
||||||
rt! (0.25)
|
}
|
||||||
fd! (100)
|
|
||||||
|
|
||||||
do turtle_state > unbox
|
|
||||||
|
|
||||||
|
|
||||||
</ludus>
|
</ludus>
|
||||||
</svg>
|
</svg>
|
||||||
|
|
||||||
|
|
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 7.7 KiB |
18
build/test.svg
Normal file
18
build/test.svg
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
<?xml version="1.0" standalone="no"?>
|
||||||
|
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" style="background-color:navy" viewBox="-100 -100 500 500">
|
||||||
|
|
||||||
|
<rect x="-10.5" y="6.123233995736766e-15" width="300" height="300.2" stroke="white" fill="transparent" stroke-width="1"/>
|
||||||
|
|
||||||
|
<g transform="translate(100, 100) rotate(180) rotate(180)">
|
||||||
|
<polygon points="0 20 -13.2262 -15.0022 13.2262 -15.0022" stroke="none" fill="rgb(150 150 150)" fill-opacity="0.6"/>
|
||||||
|
|
||||||
|
<line x1="0" y1="0" x2="0" y2="20" stroke="white" stroke-width="1"/>
|
||||||
|
</g>
|
||||||
|
|
||||||
|
<ludus>
|
||||||
|
& this is some code
|
||||||
|
forward! (100)
|
||||||
|
right! (0.25)
|
||||||
|
</ludus>
|
||||||
|
</svg>
|
||||||
|
|
After Width: | Height: | Size: 620 B |
4
package-lock.json
generated
4
package-lock.json
generated
|
@ -1,12 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "@ludus/ludus-js-pure",
|
"name": "@ludus/ludus-js-pure",
|
||||||
"version": "0.1.36",
|
"version": "0.1.27",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@ludus/ludus-js-pure",
|
"name": "@ludus/ludus-js-pure",
|
||||||
"version": "0.1.36",
|
"version": "0.1.27",
|
||||||
"license": "GPL-3.0",
|
"license": "GPL-3.0",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"shadow-cljs": "^2.26.0",
|
"shadow-cljs": "^2.26.0",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@ludus/ludus-js-pure",
|
"name": "@ludus/ludus-js-pure",
|
||||||
"version": "0.1.36",
|
"version": "0.1.27",
|
||||||
"description": "A Ludus interpreter in a pure JS function.",
|
"description": "A Ludus interpreter in a pure JS function.",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "build/ludus.mjs",
|
"main": "build/ludus.mjs",
|
||||||
|
|
15
prelude.ld
15
prelude.ld
|
@ -279,10 +279,9 @@ fn concat {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set {
|
fn set {
|
||||||
"Takes an ordered collection--list or tuple--and turns it into a set. Returns sets unharmed."
|
"Takes an ordered collection--list or tuple--and turns it into a set."
|
||||||
(xs as :list) -> fold (append, xs, ${})
|
(xs as :list) -> fold (append, xs, ${})
|
||||||
(xs as :tuple) -> do xs > list > set
|
(xs as :tuple) -> do xs > list > set
|
||||||
(xs as :set) -> xs
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set? {
|
fn set? {
|
||||||
|
@ -435,11 +434,6 @@ fn sentence {
|
||||||
(strs as :list) -> join (strs, " ")
|
(strs as :list) -> join (strs, " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_number {
|
|
||||||
"Takes a string that presumably contains a representation of a number, and tries to give you back the number represented. Returns a result tuple."
|
|
||||||
(num as :string) -> base :to_number (num)
|
|
||||||
}
|
|
||||||
|
|
||||||
&&& boxes: mutable state and state changes
|
&&& boxes: mutable state and state changes
|
||||||
|
|
||||||
fn box? {
|
fn box? {
|
||||||
|
@ -984,7 +978,7 @@ fn heading/vector {
|
||||||
|
|
||||||
&&& more number functions
|
&&& more number functions
|
||||||
fn random {
|
fn random {
|
||||||
"Returns a random something. With zero arguments, returns a random number between 0 (inclusive) and 1 (exclusive). With one argument, returns a random number between 0 and n. With two arguments, returns a random number between m and n. Alternately, given a collection (tuple, list, dict, set), it returns a random member of that collection."
|
"Returns a random something. With zero arguments, returns a random number between 0 (inclusive) and 1 (exclusive). With one argument, returns a random number between 0 and n. With two arguments, returns a random number between m and n. Alternately, given a collection (list, dict, set), it returns a random member of that collection."
|
||||||
() -> base :random ()
|
() -> base :random ()
|
||||||
(n as :number) -> mult (n, random ())
|
(n as :number) -> mult (n, random ())
|
||||||
(m as :number, n as :number) -> add (m, random (sub (n, m)))
|
(m as :number, n as :number) -> add (m, random (sub (n, m)))
|
||||||
|
@ -992,10 +986,6 @@ fn random {
|
||||||
let i = do l > count > random > floor
|
let i = do l > count > random > floor
|
||||||
at (l, i)
|
at (l, i)
|
||||||
}
|
}
|
||||||
(t as :tuple) -> {
|
|
||||||
let i = do t > count > random > floor
|
|
||||||
at (t, i)
|
|
||||||
}
|
|
||||||
(d as :dict) -> {
|
(d as :dict) -> {
|
||||||
let key = do d > keys > random
|
let key = do d > keys > random
|
||||||
get (key, d)
|
get (key, d)
|
||||||
|
@ -1444,7 +1434,6 @@ pkg Prelude {
|
||||||
sum_of_squares & math
|
sum_of_squares & math
|
||||||
tan & math
|
tan & math
|
||||||
tau & math
|
tau & math
|
||||||
to_number & strings numbers
|
|
||||||
trim & strings
|
trim & strings
|
||||||
tuple? & tuples
|
tuple? & tuples
|
||||||
turn/deg & math
|
turn/deg & math
|
||||||
|
|
207
prelude.md
207
prelude.md
File diff suppressed because one or more lines are too long
|
@ -1,8 +1,6 @@
|
||||||
# A base library for Ludus
|
# A base library for Ludus
|
||||||
# Only loaded in the prelude
|
# Only loaded in the prelude
|
||||||
|
|
||||||
(import /src/scanner :as s)
|
|
||||||
|
|
||||||
(defn bool [x] (if (= :^nil x) nil x))
|
(defn bool [x] (if (= :^nil x) nil x))
|
||||||
|
|
||||||
(defn ludus/and [& args] (every? (map bool args)))
|
(defn ludus/and [& args] (every? (map bool args)))
|
||||||
|
@ -250,20 +248,6 @@
|
||||||
([e] (break [:err e])))
|
([e] (break [:err e])))
|
||||||
[:ok out])
|
[:ok out])
|
||||||
|
|
||||||
(defn to_number [str]
|
|
||||||
(when (string/find "&" str)
|
|
||||||
(break [:err (string "Could not parse `" str "` as a number")]))
|
|
||||||
(def scanned (s/scan (string/trim str)))
|
|
||||||
(when (< 0 (length (scanned :errors)))
|
|
||||||
(break [:err (string "Could not parse `" str "` as a number")]))
|
|
||||||
(def tokens (scanned :tokens))
|
|
||||||
(when (< 3 (length tokens))
|
|
||||||
(break [:err (string "Could not parse `" str "` as a number")]))
|
|
||||||
(def fst (first tokens))
|
|
||||||
(when (not= :number (fst :type))
|
|
||||||
(break [:err (string "Could not parse `" str "` as a number")]))
|
|
||||||
[:ok (fst :literal)])
|
|
||||||
|
|
||||||
(def ctx {
|
(def ctx {
|
||||||
"add" +
|
"add" +
|
||||||
"and" ludus/and
|
"and" ludus/and
|
||||||
|
@ -320,7 +304,6 @@
|
||||||
"sub" -
|
"sub" -
|
||||||
"tan" math/tan
|
"tan" math/tan
|
||||||
"to_list" to_list
|
"to_list" to_list
|
||||||
"to_number" to_number
|
|
||||||
"trim" string/trim
|
"trim" string/trim
|
||||||
"triml" string/triml
|
"triml" string/triml
|
||||||
"trimr" string/trimr
|
"trimr" string/trimr
|
||||||
|
@ -334,4 +317,4 @@
|
||||||
(set (b (keyword k)) v))
|
(set (b (keyword k)) v))
|
||||||
b))
|
b))
|
||||||
|
|
||||||
(to_number " 123 a ")
|
|
||||||
|
|
|
@ -12,8 +12,7 @@
|
||||||
|
|
||||||
(defn escape-punctuation [str] (->> str
|
(defn escape-punctuation [str] (->> str
|
||||||
(string/replace "?" "")
|
(string/replace "?" "")
|
||||||
(string/replace "!" "")
|
(string/replace "!" "")))
|
||||||
(string/replace "/" "")))
|
|
||||||
|
|
||||||
(defn toc-entry [name]
|
(defn toc-entry [name]
|
||||||
(def escaped (escape-underscores name))
|
(def escaped (escape-underscores name))
|
||||||
|
@ -23,18 +22,18 @@
|
||||||
(string/join (map toc-entry sorted-names) " "))
|
(string/join (map toc-entry sorted-names) " "))
|
||||||
|
|
||||||
(def topics {
|
(def topics {
|
||||||
"math" ["abs" "add" "angle" "atan/2" "between?" "ceil" "cos" "dec" "deg/rad" "deg/turn" "dist" "div" "div/0" "div/safe" "even?" "floor" "gt?" "gte?" "heading/vector" "inc" "inv" "inv/0" "inv/safe" "lt?" "lte?" "max" "min" "mod" "mod/0" "mod/safe" "mult" "neg" "neg?" "odd?" "pi" "pos?" "rad/deg" "rad/turn" "random" "random_int" "range" "round" "sin" "sqrt" "sqrt/safe" "square" "sub" "sum_of_squares" "tan" "tau" "to_number" "turn/deg" "turn/rad" "zero?"]
|
"math" ["abs" "add" "angle" "atan/2" "between?" "ceil" "cos" "dec" "deg/rad" "deg/turn" "dist" "div" "div/0" "div/safe" "even?" "floor" "gt?" "gte?" "heading/vector" "inc" "inv" "inv/0" "inv/safe" "lt?" "lte?" "max" "min" "mod" "mod/0" "mod/safe" "mult" "neg" "neg?" "odd?" "pi" "pos?" "rad/deg" "rad/turn" "random" "random_int" "range" "round" "sin" "sqrt" "sqrt/safe" "square" "sub" "sum_of_squares" "tan" "tau" "turn/deg" "turn/rad" "zero?"]
|
||||||
"boolean" ["and" "bool" "bool?" "false?" "not" "or" "true?"]
|
"boolean" ["and" "bool" "bool?" "false?" "not" "or" "true?"]
|
||||||
"dicts" ["any?" "assoc" "assoc?" "coll?" "count" "dict" "dict?" "diff" "dissoc" "empty?" "get" "keys" "random" "update" "values"]
|
"dicts" ["any?" "assoc" "assoc?" "coll?" "count" "dict" "dict?" "diff" "dissoc" "empty?" "get" "keys" "random" "update" "values"]
|
||||||
"lists" ["any?" "append" "at" "butlast" "coll?" "concat" "count" "each!" "empty?" "filter" "first" "fold" "join" "keep" "last" "list" "list?" "map" "ordered?" "random" "range" "rest" "second" "sentence" "slice"]
|
"lists" ["any?" "append" "at" "butlast" "coll?" "concat" "count" "each!" "empty?" "filter" "first" "fold" "join" "keep" "last" "list" "list?" "map" "ordered?" "random" "range" "rest" "second" "sentence" "slice"]
|
||||||
"sets" ["any?" "append" "coll?" "concat" "contains?" "count" "empty?" "omit" "random" "set" "set?"]
|
"sets" ["any?" "append" "coll?" "concat" "contains?" "count" "empty?" "omit" "random" "set" "set?"]
|
||||||
"tuples" ["any?" "at" "coll?" "count" "empty?" "first" "last" "ordered?" "rest" "second" "tuple?"]
|
"tuples" ["any?" "at" "coll?" "count" "empty?" "first" "last" "ordered?" "rest" "second" "tuple?"]
|
||||||
"strings" ["any?" "chars" "chars/safe" "concat" "count" "downcase" "empty?" "join" "sentence" "show" "slice" "split" "string" "string?" "strip" "to_number" "trim" "upcase" "words"]
|
"strings" ["any?" "chars" "chars/safe" "concat" "count" "downcase" "empty?" "join" "sentence" "show" "slice" "split" "string" "string?" "strip" "trim" "upcase" "words"]
|
||||||
"types and values" ["assoc?" "bool?" "box?" "coll?" "dict?" "eq?" "fn?" "keyword?" "list?" "neq?" "nil?" "number?" "ordered?" "set?" "show" "some" "some?" "string?" "tuple?" "type"]
|
"types and values" ["assoc?" "bool?" "box?" "coll?" "dict?" "eq?" "fn?" "keyword?" "list?" "neq?" "nil?" "number?" "ordered?" "set?" "show" "some" "some?" "string?" "tuple?" "type"]
|
||||||
"boxes and state" ["box?" "unbox" "store!" "update!"]
|
"boxes and state" ["box?" "unbox" "store!" "update!"]
|
||||||
"results" ["err" "err?" "ok" "ok?" "unwrap!" "unwrap_or"]
|
"results" ["err" "err?" "ok" "ok?" "unwrap!" "unwrap_or"]
|
||||||
"errors" ["assert!"]
|
"errors" ["assert!"]
|
||||||
"turtle graphics" ["back!" "background!" "bk!" "clear!" "colors" "fd!" "forward!" "goto!" "heading" "heading/vector" "hideturtle!" "home!" "left!" "loadstate!" "lt!" "pc!" "pd!" "pencolor" "pencolor!" "pendown!" "pendown?" "penup!" "penwidth" "penwidth!" "position" "pu!" "pw!" "render_turtle!" "reset_turtle!" "right!" "rt!" "setheading!" "showturtle!" "turtle_state"]
|
"turtle graphics" ["back!" "background!" "bk!" "clear!" "colors" "fd!" "forward!" "goto!" "heading" "heading/vector" "home!" "left!" "lt!" "pc!" "pd!" "pencolor" "pencolor!" "pendown!" "pendown?" "penup!" "penwidth" "penwidth!" "position" "pu!" "pw!" "render_turtle!" "reset_turtle!" "right!" "rt!" "turtle_state"]
|
||||||
"environment and i/o" ["doc!" "print!" "report!" "state"]
|
"environment and i/o" ["doc!" "print!" "report!" "state"]
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -349,7 +349,8 @@
|
||||||
(recur (-> scanner (scan-token) (next-token)))))
|
(recur (-> scanner (scan-token) (next-token)))))
|
||||||
(recur (new-scanner source input)))
|
(recur (new-scanner source input)))
|
||||||
|
|
||||||
# (comment
|
(comment
|
||||||
(do
|
# (do
|
||||||
(def source " -123 ")
|
(def source "add 1 2 () four")
|
||||||
(length ((scan source) :tokens)))
|
(scan source)
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user