I Created A LISP-like Scripting Language
Tags: lisp, javascript, programming, transpiled
Published: 2022-05-02 07:48:56

So Yesterday I released the source code of a Lisp-like language called "[LispLike](https://github.com/rare-earth/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:
```lisp
( ;; Progam Begins
  (print "Hello world") ;; its how you do it in LispLike
) ;; Program Ends
```
Javascript code:
```javascript
console.log("Hello world")
```

**Basic Maths**
```lisp
(
  (print (+ 1 2 (- 5 6)))
)
```
Javascript Code:
```javascript
console.log(1 + 2 + 5 - 6)
```

**Variables**
```lisp
(
  (let name "World")
  (print (+ "Hello " name)) ;; prints "Hello World"
  (= name "Universe")
  (print (+ "Hello " name)) ;; prints "Hello Universe"
)
```
Javscript code:
```javascript
let name = "World"

console.log("Hello " + name)
name = "Universe"
console.log("Hello " + name)
```

**Functions**
```lisp
(
 (defun greet (name) 
   (do
     (print (+ "Hello " name))
     (return "something")
   )
 )

 (greet "World") ;; prints "Hello World"
 (greet "Universe") ;; prints "Hello Universe"
)
```
Javascript Code:
```javascript
function greet (name) {
    console.log("Hello " + name)
    return "something"
}

greet("World")
greet("Universe")
```

**Arrays and Foreach loop**
```lisp
(
  (let names (array "Ram" "Shyam" "Mohan"))
  (loop names (key value)
    (do
      (print (+ "Hello " value))
    )
  )
)

;; output
;; Hello Ram
;; Hello Shyam
;; Hello Mohan
```
Javscript Code:
```javascript
let names = ["Ram","Shyam","Mohan"]

names.forEach((value, key) => {
  console.log("Hello " + value)
})
```

**While Loop**
Apart from foreach loop, while loop is also available. "for" loop is not available yet.
```lisp
(
  ;; it prints numbers from 1 to 10
  (let i 1)
  (while (<= i 10)
    (do
      (print i)
      (+= i 1)
    )
  )
)
```
Javascript Code:
```javascript
let i = 1

while(i <= 10) {
  console.log(i)
  i += 1
}
```
**If-Else Statement**
```lisp
(
  (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")
    )
  )
)
```
Javascript Code:
```javascript
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")
}
```

**OOP**
```lisp
(
  (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!
)
```
It gets transpiled to
```javascript
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")
```