Compare commits

...

No commits in common. "main" and "actors" have entirely different histories.
main ... actors

12 changed files with 29 additions and 136 deletions

1
.gitattributes vendored
View File

@ -1 +0,0 @@
*.wasm filter=lfs diff=lfs merge=lfs -text

View File

@ -1,93 +1,3 @@
![Ludus logo](logo.png)
## Ludus: A friendly, dynamic, functional language
Ludus is a scripting programming language that is designed to be friendly, dynamic, and functional.
# rudus
This repo currently contains a work-in-progress implementation of an interpreter for the Ludus programming language, using [Janet](https://janet-lang.org) as a host language.
Ludus is part of the [_Thinking with Computers_ project](https://alea.ludus.dev/twc/), run by Scott Richmond at the University of Toronto, with collaborator Matt Nish-Lapidus; Bree Lohman and Mynt Marsellus are the RAs for the project. Ludus is our research language, which aspires to be a free translation of Logo for the 2020s.
Here are our design goals:
#### Friendly
Ludus, like Logo, is meant to be a teaching language, often for students who don't think of themselves as "computer people." Our intended audience are humanities and art people at the university level (undergrads, grads, faculty). Everything is kept as simple as possible, but no simpler. Everything is consistent as possible. We aspire to the best error messages we can muster, which is important for a language to be teachable. That means being as strict as we can muster, _in order to be friendlier_.
Our current development target is Ludus on the web: https://web.ludus.dev. That wires what we do on the langauge interpreter (here in this repo) to a web frontend.
Naturally, it starts with Logo's famed turtle graphics.
#### Dynamic
Statically typed programming languages generally give more helpful error messages than dynamic ones, but learning a type system (even one with robust type inference) requires learning two parallel systems: the type system and the expression system (well, and the pattern system). Type systems only really make sense once you've learned why they're necessary. And their benefits seem (to us, anyway) to be largely necessary when writing long-lived, maintainable, multi-author code. Ludus code is likely to be one-off, expressive, and single-authored.
To stay friendly, Ludus is dynamic. But: despite the dynamism, we aim to be as strict as possible. Certainly, we want to avoid the type conversion shenanigans of a language like JavaScript.
#### Functional
Ludus is emphatically functional: it uses functions for just about everything. This is both because your humble PI had his world reordered when he learned his first functional language (Elixir), and because the research into Logo and the programming cultures of MIT in the 1970s revolve around extremely functional Lisp code (i.e., Scheme). Logo is a weird little language, but it is a descendant of Lisp. So is Ludus.
Also, we believe that Ludus's immutable bindings and persistent or immutable data structures and careful approach to manipulating state lead to a lot of good pedagogical results. Learning a programming language involves learning how to model what's going on inside the computer; Ludus, we think, makes that both simpler and easier.
If you're looking for cognate languages, Ludus takes a _lot_ of design inspiration from Clojure and Elixir (which itself took a lot from Clojure). (The current--quick, dirty, and slow--version of Ludus is written in [Janet](https://janet-lang.org).) Clojure and Elixir are great! If you're asking why you should use Ludus instead of them, you're already at the point where you should be using them. Ludus is, maybe, for the people whom you'd like to work with in 5 years at your Pheonix shop (but even then, probably not).
### Status
Pre-alpha, still under active development. Lots of things change all the time.
The current version of Ludus is a pure function that runs in JavaScript as a WASM blob. We have plans for more and better things.
### Use
Current emphasis is on the web version: https://web.ludus.dev.
### Main features
* Expression-oriented: everything returns a value
* Pattern matching in all the places
* No operators: everything is called as a function
* Easy-peasy partial application with placeholders
* Function pipelines
* Persistent or immutable data structures
* Careful, explicit state management using `box`es
* Clean, concise, expressive syntax
* Value-based equality; only functions are reference types
#### Under construction
* Actor-model style concurrency.
* Faster, bytecode-based VM written in a systems language, for better performance.
* Performant persistent, immutable data structures, à la Clojure.
### `Hello, world!`
Ludus is a scripting language. At current it does not have a good REPL. Our aim is to get interactive coding absolutely correct, and our efforts in [ludus-web](https://github.com/thinking-with-computers/ludus-web) are currently under way to surface the right interactivity models for Ludus.
Either:
```
"Hello, world!"
```
`=> "Hello, world!"`
Ludus scripts (and blocks) simply return their last expression; this script returns the bare string and exits.
Or:
```
print! ("Hello, world!")
```
```
=> Hello, world!
=> :ok
```
Here, we use the `print!` function, which sends a string to a console (`stdout` on Unix, or a little console box on the web). Because `print!` returns the keyword `:ok` when it completes, that is the result of the last expression in the script--and so Ludus also prints this.
### Some code
Fibonacci numbers:
```
& fibonacci!, with multi-clause fns/pattern matching
fn fib {
"Returns the nth fibonacci number."
(1) -> 1
(2) -> 1
(n) -> add (
fib (sub (n, 1))
fib (sub (n, 2)))
}
fib (10) &=> 55
```
### More on Ludus
See the [language reference](language.md) and [the documentation for the prelude](prelude.md).
A Rust implementation of Ludus.

View File

@ -1137,7 +1137,7 @@ fn heed {
fn send_sync {
"Sends the message to the specified process, and waits for a response in the form of a `(:reply, response)` tuple."
(pid, msg) -> {
send! (pid, msg)
send (pid, msg)
receive {
(:reply, res) -> res
}
@ -1157,7 +1157,7 @@ fn request_fetch! {
request_fetch! (pid)
}
else {
send! (pid, (:reply, unbox (fetch_inbox)))
send (pid, (:reply, unbox (fetch_inbox)))
store! (fetch_inbox, ())
}
}
@ -1167,7 +1167,7 @@ fn fetch {
"Requests the contents of the URL passed in. Returns a result tuple of (:ok, <contents>) or (:err, <status code>)."
(url) -> {
let pid = self ()
spawn (fn () -> request_fetch! (pid, url))
spawn! (fn () -> request_fetch! (pid, url))
receive {
(:reply, (_, response)) -> response
}
@ -1182,7 +1182,7 @@ fn input_reader! {
input_reader! (pid)
}
else {
send! (pid, (:reply, unbox (input)))
send (pid, (:reply, unbox (input)))
store! (input, nil)
}
}
@ -1192,7 +1192,7 @@ fn read_input {
"Waits until there is input in the input buffer, and returns it once there is."
() -> {
let pid = self ()
spawn (fn () -> input_reader! (pid))
spawn! (fn () -> input_reader! (pid))
receive {
(:reply, response) -> response
}
@ -1250,6 +1250,8 @@ fn add_command! (turtle_id, command) -> {
update! (command_id, inc)
update! (turtle_commands, append (_, (turtle_id, idx, command)))
let prev = do turtle_states > unbox > turtle_id
print!("previous state: {turtle_id}", prev)
print!("applying command", command)
let curr = apply_command (prev, command)
update! (turtle_states, assoc (_, turtle_id, curr))
:ok
@ -1299,11 +1301,7 @@ let pd! = pendown!
fn pencolor! {
"Changes the turtle's pen color. Takes a single grayscale value, an rgb tuple, or an rgba tuple. Alias: `pencolour!`, `pc!`"
(color as :keyword) -> {
if color (colors)
then add_command! (:turtle_0, (:pencolor, color))
else panic! ("There is no color named {color} in the colors dict.")
}
(color as :keyword) -> add_command! (:turtle_0, (:pencolor, color))
(gray as :number) -> add_command! (:turtle_0, (:pencolor, (gray, gray, gray, 255)))
((r as :number, g as :number, b as :number)) -> add_command! (:turtle_0, (:pencolor, (r, g, b, 255)))
((r as :number, g as :number, b as :number, a as :number)) -> add_command! (:turtle_0, (:pencolor, (r, g, b, a)))
@ -1372,29 +1370,16 @@ fn loadstate! {
fn turtle_listener () -> {
receive {
(:forward!, steps as :number) -> add_command! (self (), (:forward, steps))
(:fd!, steps as :number) -> add_command! (self (), (:forward, steps))
(:back!, steps as :number) -> add_command! (self (), (:back, steps))
(:bk!, steps as :number) -> add_command! (self (), (:back, steps))
(:left!, turns as :number) -> add_command! (self (), (:left, turns))
(:lt!, turns as :number) -> add_command! (self (), (:left, turns))
(:right!, turns as :number) -> add_command! (self (), (:right, turns))
(:rt!, turns as :number) -> add_command! (self (), (:right, turns))
(:penup!) -> add_command! (self (), (:penup))
(:pu!) -> add_command! (self (), (:penup))
(:pendown!) -> add_command! (self (), (:pendown))
(:pd!) -> add_command! (self (), (:pendown))
(:pencolor!, color as :keyword) -> if color (colors)
then add_command! (self (), (:pencolor, color))
else panic! ("There is no color {color} in the colors dict.")
(:pencolor!, color as :keyword) -> add_command! (self (), (:pencolor, color))
(:pencolor!, gray as :number) -> add_command! (self (), (:pencolor, (gray, gray, gray, 255)))
(:pencolor!, (r as :number, g as :number, b as :number)) -> add_command! (self (), (:pencolor, (r, g, b, 255)))
(:pencolor!, (r as :number, g as :number, b as :number, a as :number)) -> add_command! (self (), (:pencolor, (r, g, b, a)))
(:pc!, color as :keyword) -> add_command! (self (), (:pencolor, color))
(:pc!, gray as :number) -> add_command! (self (), (:pencolor, (gray, gray, gray, 255)))
(:pc!, (r as :number, g as :number, b as :number)) -> add_command! (self (), (:pencolor, (r, g, b, 255)))
(:pc!, (r as :number, g as :number, b as :number, a as :number)) -> add_command! (self (), (:pencolor, (r, g, b, a)))
(:penwidth!, width as :number) -> add_command! (self (), (:penwidth, width))
(:pw!, width as :number) -> add_command! (self (), (:penwidth, width))
(:home!) -> add_command! (self (), (:home))
(:goto!, x as :number, y as :number) -> add_command! (self (), (:goto, (x, y)))
(:goto!, (x as :number, y as :number)) -> add_command! (self (), (:goto, (x, y)))
@ -1405,10 +1390,10 @@ fn turtle_listener () -> {
add_command! (self (), (:loadstate, position, heading, visible?, pendown?, penwidth, pencolor))
}
(:pencolor, pid) -> send! (pid, (:reply, do turtle_states > unbox > self () > :pencolor))
(:position, pid) -> send! (pid, (:reply, do turtle_states > unbox > self () > :position))
(:penwidth, pid) -> send! (pid, (:reply, do turtle_states > unbox > self () > :penwidth))
(:heading, pid) -> send! (pid, (:reply, do turtle_states > unbox > self () > :heading))
(:pencolor, pid) -> send (pid, (:reply, do turtle_states > unbox > self () > :pencolor))
(:position, pid) -> send (pid, (:reply, do turtle_states > unbox > self () > :position))
(:penwidth, pid) -> send (pid, (:reply, do turtle_states > unbox > self () > :penwidth))
(:heading, pid) -> send (pid, (:reply, do turtle_states > unbox > self () > :heading))
does_not_understand -> {
let pid = self ()
panic! "{pid} does not understand message: {does_not_understand}"
@ -1420,7 +1405,7 @@ fn turtle_listener () -> {
fn spawn_turtle {
"Spawns a new turtle in a new process. Methods on the turtle process mirror those of turtle graphics functions in prelude. Returns the pid of the new turtle."
() -> {
let pid = spawn (fn () -> turtle_listener ())
let pid = spawn! (fn () -> turtle_listener ())
update! (turtle_states, assoc (_, pid, turtle_init))
pid
}

View File

@ -5,7 +5,7 @@ e.g., running `doc! (add)` will send the documentation for `add` to the console.
For more information on the syntax & semantics of the Ludus language, see [language.md](./language.md).
The prelude itself is just a Ludus file, which you can see at [prelude.ld](../assets/prelude.ld).
The prelude itself is just a Ludus file, which you can see at [prelude.ld](./prelude.ld).
## A few notes
**Naming conventions.** Functions whose name ends with a question mark, e.g., `eq?`, return booleans.

View File

@ -81,7 +81,7 @@ e.g., running `doc! (add)` will send the documentation for `add` to the console.
For more information on the syntax & semantics of the Ludus language, see [language.md](./language.md).
The prelude itself is just a Ludus file, which you can see at [prelude.ld](../assets/prelude.ld).
The prelude itself is just a Ludus file, which you can see at [prelude.ld](./prelude.ld).
## A few notes
**Naming conventions.** Functions whose name ends with a question mark, e.g., `eq?`, return booleans.

BIN
logo.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB

View File

@ -194,7 +194,7 @@ export function key_up (key) {
export {p5} from "./p5.js"
export function svg (commands) {
console.log(`generating svg for ${code}`)
console.log("generating svg for ${code}")
return svg_2(commands, code)
}

View File

@ -68,13 +68,11 @@ export function p5 (commands) {
const new_state = command_to_state(prev_state, this_command)
all_states[turtle_id].push(new_state)
}
console.log(all_states)
const [r, g, b, _] = resolve_color(background_color)
if ((r + g + b)/3 > 128) set_turtle_color([0, 0, 0, 150])
const p5_calls = [...p5_call_root()]
for (const states of Object.values(all_states)) {
p5_calls.push(["strokeWeight", 1])
p5_calls.push(["stroke", 255])
// console.log(states)
for (let i = 1; i < states.length; ++i) {
const prev = states[i - 1]
const curr = states[i]

Binary file not shown.

View File

@ -79,7 +79,7 @@ export function svg (commands, code) {
const new_state = command_to_state(prev_state, this_command)
all_states[turtle_id].push(new_state)
}
let maxX = 0, maxY = 0, minX = 0, minY = 0
let maxX = -Infinity, maxY = -Infinity, minX = Infinity, minY = Infinity
for (const states of Object.values(all_states)) {
for (const {position: [x, y]} of states) {
maxX = Math.max(maxX, x)
@ -90,8 +90,8 @@ export function svg (commands, code) {
}
const [r, g, b] = resolve_color(background_color)
if ((r+g+b)/3 > 128) set_turtle_color([0, 0, 0, 150])
const view_width = Math.max((maxX - minX) * 1.2, 200)
const view_height = Math.max((maxY - minY) * 1.2, 200)
const view_width = (maxX - minX) * 1.2
const view_height = (maxY - minY) * 1.2
const margin = Math.max(view_width, view_height) * 0.1
const x_origin = minX - margin
const y_origin = -maxY - margin

View File

@ -1158,7 +1158,7 @@ impl Compiler {
}
self.pop_n(self.stack_depth - stack_depth);
self.emit_op(Op::Load);
self.stack_depth += 1;
// self.stack_depth += 1;
self.msg("********receive completed".to_string());
}
MatchClause(..) => unreachable!(),

View File

@ -503,10 +503,11 @@ impl World {
fn report_process_end(&mut self) {
let result = self.active_result().clone().unwrap();
if let Err(panic) = result {
let msg = format!("Process :{} panicked: {}", self.active_id().unwrap(), crate::errors::panic(panic));
self.send_ludus_msg(msg)
}
let msg = match result {
Ok(value) => format!("Process {} returned with {}", self.active_id().unwrap(), value.show()),
Err(panic) => format!("Process {} panicked with {}", self.active_id().unwrap(), crate::errors::panic(panic))
};
self.send_ludus_msg(msg);
}
pub async fn run(&mut self) {