DEV Community

Meghan (she/her)
Meghan (she/her)

Posted on

This is my dream language. Is it real?

For a while, I've had a vision for my "dream" language. ((Sorry @mortoray , I don't think it's Leaf, still love your content))

The syntax I've come up with is inspired by JavaScript but with some added flair that would make it (best case scenario) compatible with ASM and WASM compilation.
I've left some sample code below, and I was wondering if anyone knew if a language similar to the snippet below exists.

import { sqrt, pow } from "math"

export class Point {
    struct {
        Int x,
        Int y
    }
    constructor(Int a, Int b) {
        this.x = a;
        this.y = b;
    }
    constructor(Int a) {
        this(a, a);
    }
    constructor() {
        this(0, 0);
    }
    distanceTo(Point pt) -> Float {
        return sqrt(pow(pt.x - this.x, 2) + pow(pt.y - this.y, 2));
    }
}

export class Circle {
    struct {
        Point center,
        Int radius
    }
    constructor(Point c, Int r) {
        this.center = c;
        this.radius = r;
    }
    constructor(Int r) {
        this(new Point(0, 0), r);
    }
    constructor() {
        this(1);
    }
    intersects(Point pt) -> Boolean {
        return pt.distanceTo(this.center) <= this.radius;
    }
}
Enter fullscreen mode Exit fullscreen mode

Thanks in advance for any help!


In hindsight this looks a lot like a real Java-Script like syntax but I was still curious if anyone knew if this existed before I went out and tried to start making a compiler for a project that's already been worked on :)

Top comments (18)

Collapse
 
dmfay profile image
Dian Fay

TypeScript seems closest. There are other static type checkers like flow.

Collapse
 
gsonderby profile image
Gert Sønderby

Flow is much like that, yeah. Variable declaration is slightly different, but overall, at first I thought the example was ES2015. 😀

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

Your dream language has semi-colons!? :P

The challenge of any language, as I'm learning, is the addition of more features. In isolation one can come up with pretty syntax. Thrown into an app where a coder wants to do a lot, ugliness starts to appear.

Having examples of what people like and dislike though is an important process in language design.

Collapse
 
idanarye profile image
Idan Arye

I find it confusing that the types come before the name for variables and after the name for functions...

Collapse
 
overcache profile image
overcache
   name          type
    | name  type   |
    |  |    |      |
    V  V    V      V
fn foo(x: i32) -> i32

did you make the illustration by hands? or there is some tools can do this

Collapse
 
idanarye profile image
Idan Arye

Just use a monospace font - any programming-oriented text editor will do.

Collapse
 
thermatix profile image
Martin Becker • Edited

That looks like the return value, similar to how it's done in rust, what goes in the function is on the left, what comes out of the function is on the right.

Collapse
 
idanarye profile image
Idan Arye

In Rust the type is on the right in both variables and the return:

   name          type
    | name  type   |
    |  |    |      |
    V  V    V      V
fn foo(x: i32) -> i32

In C, the type is on the left for both the variables and the function (though one can argue that the arguments are also part of the function's type):

type name
 |   | type name
 |   |   |  |
 V   V   V  V
int foo(int x)

In this syntax the order is sometimes type...name and sometimes name...type:

name          type
 | type name   |
 |   |  |      |
 V   V  V      V
foo(Int x) -> Int
Thread Thread
 
thermatix profile image
Martin Becker

AH, yes you're right, I missed what you were specifically talking about.

Collapse
 
nektro profile image
Meghan (she/her)

The type is on the left when it's required and on the right when it's optional

Collapse
 
1bardesign profile image
Max Cahill

Wren might be getting there? It's not 1:1, of course, and it's not "for the web".

As a few other folks have mentioned, Typescript indeed also sounds close to what you want, especially if you're looking for interoperability with the JS ecosystem.

Collapse
 
jochemstoel profile image
Jochem Stoel

I don't know if such a language exists but writing an interpreter for it should be fairly easy. You can modify an existing Javascript interpreter or use Pegjs to generate a parser.

Collapse
 
cretezy profile image
Charles Crete

Seems close to Dart, especially in strong mode.

Collapse
 
hyper_debugger profile image
Harrison

Looks a lot like es6 but then if you wanted something smoother then typescript

Collapse
 
thedeemon_lj profile image
Dmitry Popov

Looks like Kotlin, Dart and lots of other OO languages.

Collapse
 
niorad profile image
Antonio Radovcic

Swift gets close.

Collapse
 
xtabdeveloping profile image
Márton Kardos

This is basically every statically typed OOP language ever. Btw no idea on semantics? Really? I mean syntax is probably one of tge least inportant aspects in a programming language...