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
Javascript code:
console.log("Hello world")
Basic Maths
(
(print (+ 1 2 (- 5 6)))
)
Javascript Code:
console.log(1 + 2 + 5 - 6)
Variables
(
(let name "World")
(print (+ "Hello " name)) ;; prints "Hello World"
(= name "Universe")
(print (+ "Hello " name)) ;; prints "Hello Universe"
)
Javscript code:
let name = "World"
console.log("Hello " + name)
name = "Universe"
console.log("Hello " + name)
Functions
(
(defun greet (name)
(do
(print (+ "Hello " name))
(return "something")
)
)
(greet "World") ;; prints "Hello World"
(greet "Universe") ;; prints "Hello Universe"
)
Javascript Code:
function greet (name) {
console.log("Hello " + name)
return "something"
}
greet("World")
greet("Universe")
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
Javscript Code:
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.
(
;; it prints numbers from 1 to 10
(let i 1)
(while (<= i 10)
(do
(print i)
(+= i 1)
)
)
)
Javascript Code:
let i = 1
while(i <= 10) {
console.log(i)
i += 1
}
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")
)
)
)
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")
}
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!
)
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")
Top comments (0)