Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

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 modeExit fullscreen mode

Javascript code:

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

Basic Maths

((print(+12(-56))))
Enter fullscreen modeExit fullscreen mode

Javascript Code:

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

Variables

((letname"World")(print(+"Hello "name));; prints "Hello World"(=name"Universe")(print(+"Hello "name));; prints "Hello Universe")
Enter fullscreen modeExit fullscreen mode

Javscript code:

letname="World"console.log("Hello"+name)name="Universe"console.log("Hello"+name)
Enter fullscreen modeExit fullscreen mode

Functions

((defungreet(name)(do(print(+"Hello "name))(return"something")))(greet"World");; prints "Hello World"(greet"Universe");; prints "Hello Universe")
Enter fullscreen modeExit fullscreen mode

Javascript Code:

functiongreet(name){console.log("Hello"+name)return"something"}greet("World")greet("Universe")
Enter fullscreen modeExit fullscreen mode

Arrays and Foreach loop

((letnames(array"Ram""Shyam""Mohan"))(loopnames(keyvalue)(do(print(+"Hello "value)))));; output;; Hello Ram;; Hello Shyam;; Hello Mohan
Enter fullscreen modeExit fullscreen mode

Javscript Code:

letnames=["Ram","Shyam","Mohan"]names.forEach((value,key)=>{console.log("Hello"+value)})
Enter fullscreen modeExit 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(leti1)(while(<=i10)(do(printi)(+=i1))))
Enter fullscreen modeExit fullscreen mode

Javascript Code:

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

If-Else Statement

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

Javascript Code:

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

OOP

((defunStudent(namerollnocls)(do(=this.namename)(=this.rollnorollno)(=this.clscls)(=this.greet(defun(name)(do(print(+"Hello "name"!")))))))(letjohn(newStudent("John Doe"1234"High school")))(printjohn.name);; John Doe(john.greet"Joe");; Hello Joe!)
Enter fullscreen modeExit fullscreen mode

It gets transpiled to

functionStudent(name,rollno,cls){this.name=namethis.rollno=rollnothis.cls=clsthis.greet=function(name){console.log("Hello"+name+"!")}}letjohn=newStudent("John Doe",1234,"High school")console.log(john.name)john.greet("Joe")
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

A self-taught Web developer and aspiring Pixel artist.
  • Location
    INDIA
  • Work
    Web developer, Indie Game Developer at self employed
  • Joined

More fromHarsh Singh

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp