Welcome to L
L is a new programming language. It is:
- simple, easy to read (when you dig it)
- concise, easy to type
Factor, Forth, Lisp, Clojure, Erlang, Python, Paul Graham's work on Arc, partially by Ruby and Crystal, and lately RetroForth and Koka.
One reason Lisp cores evolve so slowly is that we get used to them. You start to think in the operators that already exist. It takes a conscious effort to imagine how your code might be rewritten using operators that don't. -- Paul Graham
Making things easy to do is a false economy. Focus on making things easy to understand and the rest will follow. -- Peter Bourgon
Straight to the code!
5 puts
Feels a bit backwards? It is, but it is always the way the computer runs it anyway. When you'll get used to it, you will never have to think what evaluates first again:
Ruby:
puts File.read(gets.chomp)
in that order: gets
-> chomp
-> File.read
-> puts
L:
gets chomp file:read puts
Word, a named function:
recent-emails
Literal:
1
'hello'
6.626
Quote, an anonymous function, a value denoting a snippet of code:
[ 3 + ]
One-word quote, can be used as a unique value:
'album
or as a singleton if a correspondingly named function exists:
'false
it is equal to itself only.
Functions and methods most probably sound familiar to you. L is not exception to that.
Quotes can be associated with a word.
First comes quoted word that serves as a function handle, then the quote the function implementation, and a def
to associate the two:
'multiply-by-two [ 2 * ] def
It can be used right away, pass it a 5
as input, tell it to print the result, and, to no surprise, this prints 10
:
5 multiply-by-two puts
You can build nearly anything using a list: hash maps, trees.
[
[ 'name 'Bob' ]
[ 'surname 'Bean' ]
]
'surname of = > 'Bean'
Or define a blueprint:
'person [
'name
'surname
optional 'age
] blueprint
'person 'full-name [ [ name ' ' surname ] join ] def-method
[ 'Bob' 'Bean' ] 'person new
'full-name of puts = > 'Bob Bean'
Install Ruby.
Run the test suite:
ruby naive-interpreter.rb tests/all.l
Run REPL:
ruby naive-interpreter.rb repl.l
No learning resources for a language that young exist (it's just one day old!).
Articles about concatenative languages should get you going:
https://github.com/andreaferretti/factor-tutorial https://evincarofautumn.blogspot.com/2012/02/why-concatenative-programming-matters.html
Work in progress. The current lexer/parser/interpreter are written in Ruby.
Short-term plans:
- parsing of simplified form for single-word quotes
'square
- add error traces
- bootstrap so L can interpret itself
- settle on a set of base functions
Concatenative, Reverse Polish Notation (vs parenthesis).
Homoiconicity, code is data.
Names and special characters:
- hyphen vs underscore in names (
paint-green
) - lower-case (
president-of-united-world
) - single-quote (
'hello, united world!'
,divide 'conquer each
) - equals sign for comments (
1 puts = this outputs '1' to the standard output
) - square brackets (
[ spare-your-pinkies ]
) - question mark for words with a boolean result (
pick-apple yellow? 'eat if
)
No special syntax if possible, e.g. no symbols (a one-word quote is identical to itself).
Parsing words vs compile-time inlining. Due to homoiconicity, it is impossible to tell if a quote will be evaluated or used as a data structure:
[ 1 1 + ] call = 2
= vs
[ 1 1 + ] [ 1 ] compose = [ 1 1 + 1 ]
A parsing word would run during parsing, and can specify inlining and any other code transformation before execution.
'two [ 1 1 + ] \inline def
= transformed to just:
'two [ 2 ] def
= or
'red [ f00 \rgb ] def
= transformed to:
'red [ '#FF0000' ] def
Copyright 2021 Phil Pirozhkov
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this software except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.