Skip to content

Building a programming language from scratch in nodejs

Notifications You must be signed in to change notification settings

mmsaki/msakilang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

a programming language

  1. Install language, clone this repo then:

    npm link
  2. Run this in terminal for REPL:

    > (add pi 1)
    4.141592653589793
    > (add pi 0)
    NaN
    > (add pi 1 (subtract 1 2))
    3.141592653589793
    >
  3. 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

Getting Started

Make sure you have the testing framework jest

npm install --save-dev jest

Inspirations

Stages of a compiler

  1. Parsing: take source code and turn it to a representation of that code
  2. Transformation: take source code ans turn it to whatever the compiler wants it to do
  3. Generation: take transformation and turn it into a new string of code

a) Parsing

npm test parse.test
  1. Lexical analysis: take a string of code and turn it into tokens lexical tokenization

    npm test tokenize.test
  2. Syntactic analysis

  3. Psychoanalysis

how might a lexer work?

  1. Accept an input string of code.
  2. Create a variable for tracking our position, like a cursor.
  3. Make an array of tokens.
  4. Write a while loop that iterates through the source code input.
  5. Check each token. See if it matches one of your types.
  6. Add it to the array of tokens.

Example resources

  1. see AST Explorer example with babel
  2. see estree spec

repl

  1. Read
  2. Evaluate
    npm test evaluate.test
  3. Print
  4. Loop

b) transformation

  1. Manipuleate AST and do your thing

  2. 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

c) Code Generation options

  1. Write your own low-level CPU-instruction compiler (terrible idea?)
  2. Use a compiler framework like LLM like other languages e.g rust, swift, obective c etc.
  3. Target the JVM (Java Virtual Machine)
  4. 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

babel add ast

d) Other features: Variable declaration

Documnentation

You can follow along here

About

Building a programming language from scratch in nodejs

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published