DEV Community

Cover image for I Created A LISP-like Scripting Language
Harsh Singh
Harsh Singh

Posted on

I Created A LISP-like Scripting Language

So Yesterday I released the source code of a Lisp-like language called "LispLike" (Creativity runs in my blood) that transpiles to Javascript.

Why?

Because Why Not?
It helped me better understand the logic behind most scripting languages ​​(including JavaScript itself). This helped me understand "recursion" better. Overall it was a good mental exercise.

The Language

This language is a combination of Javascript and Lisp. It's syntax is similar to Lisp but uses operators and dot notation similar to Javascript. Because it is transpiled to Javascript and uses dot notation, you can use methods and classes offered by Javascript. The Good thing is that you can use Lisp code highlighting, by saving the code with ".lisp" extension. To transpile it, just import "lispLike" function from the "transpiler.js" module and pass the LispLike code as a string and it will output the transpiled code.

Here is a Good Old "Hello World" example:

( ;; Progam Begins
  (print "Hello world") ;; its how you do it in LispLike
) ;; Program Ends
Enter fullscreen mode Exit fullscreen mode

Javascript code:

console.log("Hello world")
Enter fullscreen mode Exit fullscreen mode

Basic Maths

(
  (print (+ 1 2 (- 5 6)))
)
Enter fullscreen mode Exit fullscreen mode

Javascript Code:

console.log(1 + 2 + 5 - 6)
Enter fullscreen mode Exit fullscreen mode

Variables

(
  (let name "World")
  (print (+ "Hello " name)) ;; prints "Hello World"
  (= name "Universe")
  (print (+ "Hello " name)) ;; prints "Hello Universe"
)
Enter fullscreen mode Exit fullscreen mode

Javscript code:

let name = "World"

console.log("Hello " + name)
name = "Universe"
console.log("Hello " + name)
Enter fullscreen mode Exit fullscreen mode

Functions

(
 (defun greet (name) 
   (do
     (print (+ "Hello " name))
     (return "something")
   )
 )

 (greet "World") ;; prints "Hello World"
 (greet "Universe") ;; prints "Hello Universe"
)
Enter fullscreen mode Exit fullscreen mode

Javascript Code:

function greet (name) {
    console.log("Hello " + name)
    return "something"
}

greet("World")
greet("Universe")
Enter fullscreen mode Exit fullscreen mode

Arrays and Foreach loop

(
  (let names (array "Ram" "Shyam" "Mohan"))
  (loop names (key value)
    (do
      (print (+ "Hello " value))
    )
  )
)

;; output
;; Hello Ram
;; Hello Shyam
;; Hello Mohan
Enter fullscreen mode Exit fullscreen mode

Javscript Code:

let names = ["Ram","Shyam","Mohan"]

names.forEach((value, key) => {
  console.log("Hello " + value)
})
Enter fullscreen mode Exit fullscreen mode

While Loop
Apart from foreach loop, while loop is also available. "for" loop is not available yet.

(
  ;; it prints numbers from 1 to 10
  (let i 1)
  (while (<= i 10)
    (do
      (print i)
      (+= i 1)
    )
  )
)
Enter fullscreen mode Exit fullscreen mode

Javascript Code:

let i = 1

while(i <= 10) {
  console.log(i)
  i += 1
}
Enter fullscreen mode Exit fullscreen mode

If-Else Statement

(
  (let age 18)

  (if (< age 18) ;; condition
    (do ;; if block
      (print "You are below 18 years of age")
    )
    (   ;; else block
      (print "You age is 18 years or above")
    )
  )
)
Enter fullscreen mode Exit fullscreen mode

Javascript Code:

let age = 18

if(age < 18) {
  console.log("You are below 18 years of age")
} else {
  console.log("You age is 18 years or above")
}
Enter fullscreen mode Exit fullscreen mode

OOP

(
  (defun Student (name rollno cls) 
    (do
      (= this.name name)
      (= this.rollno rollno)
      (= this.cls cls)

      (= this.greet (defun (name)
        (do (print (+ "Hello " name "!"))) 
      ))
    )
  )

  (let john (new Student ("John Doe" 1234 "High school")))
  (print john.name) ;; John Doe
  (john.greet "Joe") ;; Hello Joe!
)
Enter fullscreen mode Exit fullscreen mode

It gets transpiled to

function Student (name,rollno,cls) {
    this.name = name
    this.rollno = rollno
    this.cls = cls
    this.greet = function(name) {
        console.log("Hello " + name + "!")
    }

}

let john = new Student("John Doe",1234,"High school")

console.log(john.name)
john.greet("Joe")
Enter fullscreen mode Exit fullscreen mode

Top comments (0)