Skip to content

Commit

Permalink
updated reame
Browse files Browse the repository at this point in the history
  • Loading branch information
mgerhold committed Nov 11, 2023
1 parent 3f5a922 commit 939848d
Showing 1 changed file with 61 additions and 33 deletions.
94 changes: 61 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# Laszlo

Laszlo is both a programming language and an interpreter that can execute Laszlo programs.
Laszlo is both a programming language and an interpreter that can execute Laszlo
programs.

You can try it out [right here](https://mgerhold.github.io/laszlo/).

This is just a little private project that resulted from a few hours of ~~work~~ fun. Play around with it, but don't expect much!
This is just a little private project that resulted from a few hours of ~~work~~
fun. Play around with it, but don't expect much!

According to ChatGPT, Laszlo is a programming language strongly influenced by other languages (ordered by amount of influence):
According to ChatGPT, Laszlo is a programming language strongly influenced by
other languages (ordered by amount of influence):

- Rust
- `let`
Expand All @@ -32,28 +35,36 @@ According to ChatGPT, Laszlo is a programming language strongly influenced by ot
- imperative style
- control structures like `if` and `while`
- JavaScript
- array operations and general "look and feel" of the code, which resembles modern JavaScript
- array operations and general "look and feel" of the code, which resembles
modern JavaScript

I won't judge whether ChatGPT is correct or not, though ¯\\\_(ツ)_

## The Laszlo Programming Language

- `print()` and `println()`: Print the string representation of something to stdout. `println` adds a newline. Example:
```js
- `print()` and `println()`: Print the string representation of something to
stdout. `println` adds a newline. Example:
```
println("Hello, world!")
print("Hello, ");
print("world!");
println();
println("The answer is " + 42);
```
- `function`: You can declare functions using the `function` keyword. Functions require type annotations (like in the renowned [Backseat™️](https://www.youtube.com/watch?v=dQw4w9WgXcQ) language). Example:
```js
- `function`: You can declare functions using the `function` keyword. Functions
require type annotations (like in the
renowned [Backseat™️](https://www.youtube.com/watch?v=dQw4w9WgXcQ) language).
Example:
```
function add(lhs: I32, rhs: I32) ~> I32 {
return lhs + rhs;
}
```
Just like in Python, you can only call functions that have been defined before. If all your logic resides inside functions (except one function call at the end of the program), the order of function definitions doesn't matter. Example:
```js
Just like in Python, you can only call functions that have been defined
before. If all your logic resides inside functions (except one function call
at the end of the program), the order of function definitions doesn't matter.
Example:
```
function main() { // missing return type equals "~> Nothing"
say_hello();
}
Expand All @@ -64,20 +75,22 @@ I won't judge whether ChatGPT is correct or not, though ¯\\\_(ツ)_/¯
main();
```
- `let`: You can create variables using `let`. All variables have to be initialized upon definition. Example:
```js
- `let`: You can create variables using `let`. All variables have to be
initialized upon definition. Example:
```
let number = 42;
let text = "Hello!";
let predicate = true;
let array = [1, 2, 3];
```
- `=`: You can assign to variables using `=`. Example:
```js
```
let a = 10;
a = 20;
```
- `if`/`else if`/`else`: They work just like in C, but you don't need parentheses around the condition. Example:
```js
- `if`/`else if`/`else`: They work just like in C, but you don't need
parentheses around the condition. Example:
```
let temperature = 22;
if temperature > 25 {
println("it's really hot");
Expand All @@ -87,8 +100,9 @@ I won't judge whether ChatGPT is correct or not, though ¯\\\_(ツ)_/¯
println("it's rather cold");
}
```
- `for`: Loop over an iterable. The only iterables currently available are ranges and arrays. Example:
```js
- `for`: Loop over an iterable. The only iterables currently available are
ranges and arrays. Example:
```
for i in 1..=10 { // will run up until 10 (inclusive)
println(i);
}
Expand All @@ -101,39 +115,46 @@ I won't judge whether ChatGPT is correct or not, though ¯\\\_(ツ)_/¯
}
```
- `while`: Loop as long as a condition is `true`. Example:
```js
```
let i = 5;
while i > 2 {
println(i);
i = i - 1;
}
```
- `break`/`continue`/`return`: They work just like in C 👍.
- `assert`: Assert a certain condition. A failing assert results in aborting the program. Example:
```js
- `assert`: Assert a certain condition. A failing assert results in aborting the
program. Example:
```
assert(42 == 42); // this is fine
assert(false); // crashes
```

## Call Semantics

Laszlo has "uncommon" semantics when functions are called. In Laszlo, values are passed "by reference" if possible ("by value" otherwise). This has some unusual implications:
```js
Laszlo has "uncommon" semantics when functions are called. In Laszlo, values are
passed "by reference" if possible ("by value" otherwise). This has some unusual
implications:

```
function twice(n: I32) {
n = n * 2;
n = n * 2;
}
let a = 10;
twice(a);
assert(a == 20);
twice(10); // crashes, since 10 is not an Lvalue
```
If you want to take a parameter by value, you will have to store it in a new variable. You can "re-use" the parameter name, though:
```js

If you want to take a parameter by value, you will have to store it in a new
variable. You can "re-use" the parameter name, though:

```
function twice(n: I32) {
let n = n; // take a copy
n = n * 2; // no effect outside this function
println(n);
let n = n; // take a copy
n = n * 2; // no effect outside this function
println(n);
}
let a = 10;
Expand All @@ -144,9 +165,12 @@ twice(10); // prints "20"

## "Type Erasure"

You can use `?` to specify the *"unspecified"* type. In function calls, `?` matches to *any* type. Example:
```js
function calculate_length(any_array: [?]) ~> I32 {
You can use `?` to specify the *"unspecified"* type. In function calls, `?`
matches to *any* type. Example:

```
function calculate_length(any_array: [?])~ > I32
{
return any_array.size;
}
Expand All @@ -158,8 +182,12 @@ println(calculate_length([19, 45, 7])); // prints "3"
print_anything(42); // prints "42"
calculate_length("Hello"); // crashes, type mismatch (String is not an array)
```

*Note: An empty array has the type `[?]`.*

## Now what?

Play around with the interpreter and report back any issues if you encounter them. Solve some [Advent of Code](https://adventofcode.com/) challenges using Laszlo! Have fun with it! Fork it! Extend it! It's a small codebase and can be a great starting point for experiments 🙂
Play around with the interpreter and report back any issues if you encounter
them. Solve some [Advent of Code](https://adventofcode.com/) challenges using
Laszlo! Have fun with it! Fork it! Extend it! It's a small codebase and can be a
great starting point for experiments 🙂

0 comments on commit 939848d

Please sign in to comment.