-
Install language, clone this repo then:
npm link
-
Run this in terminal for REPL:
> (add pi 1) 4.141592653589793 > (add pi 0) NaN > (add pi 1 (subtract 1 2)) 3.141592653589793 >
-
Run speficic files in a
.msaki
extension// example.msaki (add pi (subtract 3 (multiply 3 (min 1 (max 1 2 3 (modulo 1000 3))))))
msaki run examples/example.msaki
Make sure you have the testing framework jest
npm install --save-dev jest
- Peter Norvig's lispy - a lisp written in python
- Eloquent javascript Chapter 12 - Egg programming language
- Jamie Kyle's Super tiny compiler
- Parsing: take source code and turn it to a representation of that code
- Transformation: take source code ans turn it to whatever the compiler wants it to do
- Generation: take transformation and turn it into a new string of code
npm test parse.test
-
Lexical analysis: take a string of code and turn it into tokens
npm test tokenize.test
-
Syntactic analysis
-
Psychoanalysis
how might a lexer work?
- Accept an input string of code.
- Create a variable for tracking our position, like a cursor.
- Make an array of tokens.
- Write a while loop that iterates through the source code input.
- Check each token. See if it matches one of your types.
- Add it to the array of tokens.
Example resources
- see AST Explorer example with babel
- see estree spec
- Read
- Evaluate
npm test evaluate.test
- Loop
-
Manipuleate AST and do your thing
-
The visitor pattern form Design Patterns
- we do a depth-first search on the tree
- we should be able travel to all the nodes in the ast tree
npm test traverse.test
- Write your own low-level CPU-instruction compiler (terrible idea?)
- Use a compiler framework like LLM like other languages e.g rust, swift, obective c etc.
- Target the JVM (Java Virtual Machine)
- Transpile
you can alse rely on bable's generator import generate from '@babel/generator'
the generate(ast, options, code)
you can also use the reverse from babel but we create our own
we can create a toJavascript
transformation using the babel parser and make our ast to somthing like this
Run:
npm test to-javascript.test
You can follow along here