Small Lang is a small programming language you can build yourself as your first programming language. It is a part of a course for learning how to build a programming language using JavaScript.
This language is small, as its name suggests. But it is also powerful as it follows the example of LISP in leveraging the expressive power of lambda functions.
- numbers
- strings
- variables
- functions
- lambda functions
- other data types (like arrays and dictionaries) can be supported via adding runtime functions
The following code example contains all of the features of the language:
print("Hello, world")
print("3 + 5 =" add(3 5))
hello = (subject) => {
print(concat("Hello, " subject))
print(concat("Hello, " subject))
}
doIt = () => {
print("Do it!")
}
doIt()
hello("Brother")
classmates = split("Jerry Jordan Johnny Jack Jeffery" " ")
each(classmates (peep) =>
print(concat("Hello " peep))
)
fib = (n) =>
if(eq(n 1)
() => 1
() =>
if(eq(n 2)
() => 1
() =>
add(
fib(subtract(n 1))
fib(subtract(n 2))
)
)
)
print(fib(9))
- Episode 1: The Lexer - (code branch 01-lexer)
- Episode 2: The Parser - (code branch 02-parse-script-and-var-assign)
- Episode 3: Parsing Function Calls - (code branch 03-fun-call-and-multi-line)
- Episode 4: The Generator - (code branch 04-generator)
- Episode 5: Runtime Functions - (code branch 05-runtime-functions)
- Episode 6: Parsing Lambda Functions - (code branch 06-lambda-function)
- Episode 7: White Space and Ambiguous Grammar - (code branch 07-whitespaces-and-ambiguous-grammars)
- Episode 8: Generator for Lambda - (code branch 08-code-gen-lambda)
- Episode 9: Comments and More Ambiguous grammar - (code branch 09-code-challenge-part-A)
- Episode 10: Coding Challenge - (code branch 10-code-challenge-part-B)
- parser
- define moo lexer
- number literal
- string literal
- assignment operator
- identifier
- parans
- braces
- fat arrow
- whitespace
- newline
- make a parse.js that creates .ast files
- variable assignment
- function call
- multiple lines
- lambda function
- comments
- define moo lexer
- generator
- make a generate.js that creates .js files
- variable assignment
- function call
- runtime functions
- lambda function
- comments
- code challenges
If you are on Windows, you will encounter a lexer error similar to:
name = "Bob"
^
Unexpected input (lexer error). Instead, I was expecting to see one of the following:
A NL token based on:
statements → statements ● %NL statement
at Parser.feed (C:\Users\dflyn\thingscript_test\node_modules\nearley\lib\nearley.js:295:27)
at main (C:\Users\dflyn\thingscript_test\parse.js:19:8)
The fix is to change the following line in lexer.js
:
NL: { match: /[\n]+/, lineBreaks: true }
to
NL: { match: /[\r\n]+/, lineBreaks: true }
Thank you very much to Derek for finding this fix!
- Checkout my full video series on How to Make a Programming Language
- Live Code a Programming Language Series
- A few other programming languages:
- LLVM Tutorial Walkthrough