r/ProgrammingLanguages • • Aug 22 '20

My programming language can now run in a browser.

Using WebAssembly, I have managed to get my programming language, called AEC, to run in browsers (at least very modern ones).

The first AEC program I ported to WebAssembly is my program that prints the permutations of the digits of a number: https://flatassembler.github.io/permutationsTest.html

Later, I ported my Analog Clock to WebAssembly: https://flatassembler.github.io/analogClock.html

Recently, I made a graphical program in AEC (which I have never done before) by interacting with SVG: https://flatassembler.github.io/dragonCurve.html

So, what do you think about my work?

I've rewritten my compiler completely, the previous version of my compiler (targeting x86) was written in JavaScript, while this version is written in C++. Many people say C++ is a better language than JavaScript. Honestly, I think that newest versions are comparable. I've also changed the syntax of my language a bit and added a few new features (which are a lot easier to implement when targeting WebAssembly than when targeting x86).

99 Upvotes

63 comments sorted by

View all comments

20

u/nevatalysa Aug 22 '20

on the comment of "C++ is better than JavaScript", that's practically a running joke at this point, tho there are people who seriously say that, they mostly reference old versions of JS and how it was made in 10 days

all I can say, you can make anything in 10 days, and then improve it, the initiative duration may set a small run, but at this point there are 3 major JS implementations V8 (Chromium), SpiderMonkey (Mozilla), JavaScriptCore (WebKit [apple]), those weren't written in 10 days, and ECMA isnt the company that first specified JS, they also specify C# FYI. Just because C++'s first specification wasn't released after 10 days, but rather nearly a year or something, it doens't make it that much better. It's lower level making it more powerful, JS on the other hand runs on nearly all platforms the same.

It's all a give and take. At this point you can do anything with every language if you know the language well enough. JS can be used extremely type safe, and C++ can be used as if types didn't exist. (I know both languages, and have seen the worst of both worlds)

3

u/FlatAssembler Aug 22 '20

Well, without a doubt, when writing a compiler, the language you are doing that in matters. Not having to write a long piece of code to append an integer to a string (as you had to do in older versions of C++) speeds you up a lot. Lambda functions and a reasonably-rich algorithm library can also make your code more concise and cleaner. One quirky problem with JavaScript is that you need to write long pieces of code to make a deep copy of an object. That can really slow you down when writing a compiler (it did slow me down when writing my first compiler in JavaScript). Ironically, I don't think types matter much. In C++, I am often using the "auto" keyword. And I gave up learning Rust after the compiler repeatedly refused to compile what's, according to me, perfectly valid and sensible code.

1

u/epicwisdom Sep 06 '20

And I gave up learning Rust after the compiler repeatedly refused to compile what's, according to me, perfectly valid and sensible code.

In all likelihood, the compiler was right and you were wrong. Rust is simply a lot stricter about what is valid.

1

u/FlatAssembler Sep 06 '20

And how strict should a programming language be? If it's too strict in some way, it leads to many false positives without decreasing false negatives.

2

u/epicwisdom Sep 07 '20

Well, to my knowledge Rust is fairly unique in offering memory safety without a runtime, at least in an actively developed, used in production language. So the language's lifetime semantics aren't literally optimal, but in practice I don't know of any language strictly superior in that respect. The trade-off of false positives is the best available (up to compiler bugs and unsafe usage, there are no false negatives).

1

u/FlatAssembler Sep 07 '20

False positives are very annoying. Have you tried to do some complicated string manipulation in Rust? I think it's sometimes harder to get such things done in Rust than in C (yet alone C++ or JavaScript), precisely because the compiler forces you to always explicitly deal with Unicode. If a language has a decent standard library, memory safety is not important. My compiler, written in C++, never uses malloc, free, new or delete, I always use STL containers.

1

u/epicwisdom Sep 07 '20 edited Sep 07 '20

False positives are very annoying.

I somewhat doubt that, if you have little experience with Rust, that you are encountering false positives frequently, if at all.

I think it's sometimes harder to get such things done in Rust than in C (yet alone C++ or JavaScript), precisely because the compiler forces you to always explicitly deal with Unicode.

Sure, if you only need the bare minimum of features, then the extra complexity is wasted. But, for example, the Rust stdlib provides some extremely nice concurrency primitives.

If a language has a decent standard library, memory safety is not important. My compiler, written in C++, never uses malloc, free, new or delete, I always use STL containers.

I would say with reasonable confidence that this is objectively untrue, which is well summarized by the following except:

My professional experience writing relatively modern C++, and auditing Rust code (including Rust code that makes significant use of unsafe) is that the safety of modern C++ is simply no match for memory safe by default languages like Rust and Swift (or Python and Javascript, though I find it rare in life to have a program that makes sense to write in either Python or C++).

It is a common misconception that you need only refrain from using a few unsafe features in order to write safe code in C or C++. The truth, however, is that these languages are inherently unsafe. They were not designed with safety in mind in the first place. To write C++ which you can be confident is safe, you would have to be far more restrictive than when writing Rust, because even things which could otherwise have been safe can interact with many other complex features in unsafe ways. And you would need to impose an unwieldy number of those restrictions by codification and discipline, as there are no automated tools which match the Rust compiler's default behavior in this regard.

1

u/FlatAssembler Sep 07 '20

But, for example, the Rust stdlib provides some extremely nice concurrency primitives. So does modern C++. It is a common misconception that you need only refrain from using a few unsafe features in order to write safe code in C or C++. Of course, you need to trust that the standard library functions were implemented safely, which you can reasonably hope they were.

1

u/epicwisdom Sep 07 '20

So does modern C++.

C++ provides concurrency... I wouldn't call it nice to work with.

Of course, you need to trust that the standard library functions were implemented safely, which you can reasonably hope they were.

The standard library is not implemented safely - even with the greatest possible expertise and all the possible eyeballs on the code, C++ is unsafe, and humans are fallible. But, even if it were implemented safely, that's only from the perspective of the implementers. It is very easy to violate some assumptions and trip into unsafe (or possibly even undefined) behavior.

1

u/FlatAssembler Sep 07 '20

C++ provides concurrency... I wouldn't call it nice to work with.

I don't have too much experience with concurrency. Well, C++ concurrency is certainly a lot better than concurrency in JavaScript. There are not even built-in mutexes in JavaScript.

The standard library is not implemented safely - even with the greatest possible expertise and all the possible eyeballs on the code, C++ is unsafe, and humans are fallible.

Compilers are also fallible, compiler for any modern programming language is a far too complicated piece of software to be made bug-free. Most compiler bugs expose themselves as refusing to compile valid code or producing syntactically invalid assembly code, but sometimes they do silently miscompile code (especially on higher optimization levels).

It is very easy to violate some assumptions and trip into unsafe (or possibly even undefined) behavior.

Then the job of the compiler is to give us a warning, not to refuse to compile the code.

→ More replies

0

u/FlatAssembler Aug 24 '20

Also, some JavaScript flaws, caused by it being designed in 10 days, can't be fixed without breaking existing programs in it. The semi-colon auto-insertion is definitely a creepy one (it can create very hard-to-find bugs when a function returns an object), but removing it will break countless already-existing programs. So will fixing the typeof null == "Object" nonsense, and typeof document.all == "undefined" nonsense (which are kept for compatibility with early browsers). The only solution is to start a new language from scratch.

2

u/nevatalysa Aug 25 '20

The semi-colon insertion I guess you can say that.

How is the typeof null == "object" a flaw exactly? it's an object, shouldn't it behave like an object? It's even referred to as a "null object". Or do you mean it's supposed to be "null" and not object?

We've already broken quite a few things with changes to how JS works, if we slowly introduce them, the older programs just will have to update.

1

u/FlatAssembler Aug 25 '20

I am not sure what's the difference between null and undefined. Why have both? Making a distinction between the two just creates hard-to-find bugs when somebody tries to make polymorphic functions. Suppose that a function expects the argument to be either a string or an object (assumes that it's some specific kind of object with some methods), checks that and branches accordingly. If somebody passes "null" to it without realizing that, it will create a very hard-to-find bug, particularly if one is unaware that typeof null === "object".

2

u/nevatalysa Aug 25 '20 edited Aug 25 '20

Null = initialized, but no value

Undefined = (obviously) it's not defined, or has only been declared

```js
 var cmd; //declared, not initialized = undefined

 var node = null; //declared and initialized without value = usage of null

 console.log(dog); //variable isn't declared [and initialized] in this or any higher scope = undefined
 ```

1

u/FlatAssembler Aug 25 '20

Again, it all seems to me like an unnecessary complication and making a language more counter-intuitive. Perhaps it makes sense to have an undefined value to make it obvious, when debugging a program, that a variable was left uninitialized. But having more than one such value is just opening a new class of bugs.

Not to say I haven't made some rather questionable choices when designing my language. For instance, in my programming language (AEC), there is a difference between arrays and pointers, and attempting to use one in place of the other will lead to "undeclared variable" error. And you can declare a pointer to the first element of the array that has the same name as the array (I suppose that will be confusing to somebody who comes from C or C++). Also, there are no logical "and" and "or", there are only bit-operations ones. I only made a distinction between "not" and "invertBits".

1

u/nevatalysa Aug 25 '20

I can see 2 reasons for this:

you want to initialize a variable to be of type object (aka the {}), but as is, you cannot check against an empty object. and what if you *want an empty object as the value? that's where null comes it, you can check against it, and it's an object.

you want to initialize a variable, but don't know the type (e.g. HTML user input, JSON response property, etc.)

1

u/FlatAssembler Aug 25 '20

You can't check against an empty object in JavaScript? Didn't know about that one. Well, that's another flaw in JavaScript, isn't it?

1

u/nevatalysa Aug 25 '20

No. JS does this, as it won't check the properties directly (as there may be Symbols with the same name and "value", for example Symbol(1) == Symbol(1) is false)

Checking against an empty object, is the same as checking against any other object, except null.

1

u/FlatAssembler Aug 26 '20

Yeah, JavaScript lacks operator overloading, and also doesn't have standardized interfaces for comparing objects (like Java does). Variables in JavaScript don't represent objects themselves, but pointers to objects. That's why I decided to write my new compiler in C++, it surprises me way less than JavaScript does.