lit

The LIT Programming Language

Lit

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"
}

Why?

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.

Enter Lit

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:

  1. Provide a default value:
let n = readln.to_n!() or 0
  1. Panic/Exit the program:
let n = readln.to_n!() or { panic "not a valid number" }
  1. 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
}
  1. You can also use if let and while 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"
}

Goals

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.

Installation

TODO: Write installation instructions here

Docs

You can find the documentation here.

Development

TODO: Write development instructions here

Inspiration

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.

Acknowledgements

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!

Contributing

  1. Fork it (https://github.com/your-github-user/lit/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request
Repository

lit

Owner
Statistic
  • 9
  • 1
  • 21
  • 0
  • 3
  • over 2 years ago
  • October 27, 2020
License

MIT License

Links
Synced at

Sun, 24 Nov 2024 17:14:27 GMT

Languages