A simple scripting language
Take a look at the Documentation »
FAQ
·
VS Code extension
·
Report a Problem
ATTENTION: This project is a work in progress! It's an experiment and by no means a production-ready language.
module Factorial {
fn of { |n|
if n <= 1 then return 1
n * Factorial.of(n - 1)
}
}
if let n = readln.to_n!() {
println "Factorial of {n} is {Factorial.of(n)}"
} else {
println "Not a valid number"
}
So, yet another scripting language. What's the deal?
I'm primarily a Ruby developer, and I love the workflow of a scripting language!
I never felt the need for type annotations in Ruby, but I often saw instances of
undefined method 'something' for nil
errors in production code.
I thought "How can I get rid of this kind of error without going all the way down to full static typing?". That's how Lit was born.
Lit indeed has some static typing, but it is subtle. Let's get back to the factorial example, in particular this part:
if let n = readln.to_n!() {
println "Factorial of {n} is {Factorial.of(n)}"
} else {
println "Not a valid number"
}
What happens is that readln
returns a string, and to_n!
tries to convert it to an integer. But
what happens if the string is not a valid number? What should "wut".to_n!()
do? I'm not sure, so
to_n!
returns an error. The exclamation mark at the end of to_n!
is a type annotation that
means it's a "dangerous" method.
If a method is marked as dangerous, we have to handle it. Here are some alternatives:
- Provide a default value:
let n = readln.to_n!() or 0
- Panic/Exit the program:
let n = readln.to_n!() or { panic "not a valid number" }
- Propagate the error:
fn read_number! {
let n = readln
n = n.to_n!() or { return err("Not a number") } # caller decides how to handle the error
n
}
- You can also use
if let
andwhile let
to handle errors:
if let n = readln.to_n!() {
println "{n} is a number"
} else {
println "Not a valid number"
}
while let n = readln.to_n!() {
println "Yay! {n} is a number"
} else {
println "Not a valid number"
}
Lit is my first attempt at language design. My goals are:
- Be straightforward:
- If the same concept/syntax could be used in other parts of the language, great!
- It is interpreted because this keeps things simpler.
- Be functional-friendly:
- It has to have good function support (anonymous functions, composition, pipe operator).
- Help with immutability.
- Light static typing:
- No type annotations (in the usual sense);
- No
undefined method 'x' for nil
errors; - It still has to feel like a scripting language.
- Be pretty:
- I'm a Rubyist, after all. So, beautiful code matters.
- I want to keep the language consistent, though.
- Don't take it too serious:
- This is my first language, so I want it to be fun (and learn from experience);
- Speed is not a priority.
TODO: Write installation instructions here
You can find the documentation here.
TODO: Write development instructions here
Ruby, of course, is my primary source of inspiration for how a scripting language should feel. Due to my limited knowledge of creating a programming language, I went with JavaScript-like syntax. There's also Rust and V sprinkled in the mix.
First and foremost, I'd like to thank Bob Nystrom for his incredible book Crafting Interpreters, which made it possible for me to start writing this language. If you can, please consider buying it.
I also would like to thank all the languages that inspired me, and you for reading my this!
- Fork it (https://github.com/your-github-user/lit/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request