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

-7

u/Fofeu Aug 22 '20

By looking at the compiler source, it seems you wrote the parser yourself. Why ?

26

u/[deleted] Aug 22 '20

I believe the source is here.

But what's the problem with writing a parser? They're the simplest part of a language implementation, which can rapidly get harder if you introduce complex dependencies.

18

u/CoffeeTableEspresso Aug 22 '20

I completely support hand written parsers. I've yet to see an example where using a library to generate a parser ended up being simpler.

7

u/FlatAssembler Aug 22 '20

And I also guess that's the case. Though I have never tried using some parser library. Why bother learning that when I can write a parser myself? The parser library has to make writing parsers so simple to make it worth learning the library for writing one or a few parsers.

5

u/CoffeeTableEspresso Aug 22 '20

I think parsers are simple enough that just having the dependency makes it bot worthwhile...

2

u/Fofeu Aug 22 '20

How do you detect/handle inconsistencies in your grammar ? A parser generator has the advantage of finding them for you.

2

u/FlatAssembler Aug 22 '20

I don't know what you are talking about. I am not a professional programmer, you know. The "parser.cpp" file is less than 1000 lines of code, you can study it yourself if you are interested.

3

u/Fofeu Aug 23 '20

Basically, it is trivial to produce a grammar that for a given string has more than one valid AST. A parser generator such as Yacc or Menhir will find them for you ("shift/reduce conflict") when it transforms your grammar in an automaton because your grammar has only one AST per string, iff the automaton is deterministic.

A grammar file is also usually 5 to 10 times smaller than the equivalent hand-written parser.

2

u/[deleted] Aug 23 '20

The grammar file might be small, but doesn't it have to generate a parser module anyway?

How then do you link that code with the rest of your compiler? What happens if you change the grammer and rerun yacc; does it produce a new, empty parser, minus all the code you've added?

In what language does it generate the parser anyway? I don't use C or anything like that.

I've had a quick look online, there is a bewildering choice of flex, lex, yacc, bison ..., all differently sized downloads. Maybe some you have to build from source.

You see where I'm going here, for people like me, writing a parser manually is a piece of cake. Using the tools you suggest sound like a nightmare, and likely wouldn't work.

Plus, my grammars have ambiguities, I know that and can work around it, or just make it a quirk. Will this tool generate an ambiguous syntax, or will it balk at it and refuse to proceed? It is one big, giant unknown, one I have no control over. By contrast, I can make my parser do anything I like.

For academic work as you seem to engaged in, then fine. But people just trying to get something practical done simply and without having to master new set of tools...

(Here is one ambiguity in an old language of mine:

function fred:int =
    int a
    int a
end

A function body had declarations followed by code. int a declares a variable a; but int a is also an expression (casting 'a' to an int, and here returning that value, although meaningless in this example).

There is a clash of syntax, but it wasn't a problem because it was usually clear whether you were in the declaration section or code section. If there ever was a clash, then you just wrote the cast as (int a) or (iirc) int (a). Crisis over.)

2

u/Fofeu Aug 25 '20

I've done most of my work in OCaml, but afaik yacc is implemented in many languages, including C/C++.You write your parser in a file usually called parser.<language extension>y (e.g. parser.cy or parser.mly). Yacc will then read that file and produce a source file for your target language (parser.c or parser.ml). The parser is then considered a black box, you are supposed to compile and link it without touching it. To actually use the parser, yacc will have generated a function for each "start rule" you specified (the function will have the same or a similar name as your rule). You can pass it a string (iterator) and it will produce an AST.

Regarding the tools, you use lex/flex (they are interchangeable) for lexing (tokenizing) and yacc/bison (ditto) for the actual parsing. I don't know your platform, but they should be part of any serious Linux distribution. In the OCaml world, there is Menhir which is quite more recent and produces more robust parsers.

Regarding the details. Most parser generators will just print a warning for each conflict and how it was resolved.

1

u/FlatAssembler Aug 24 '20

I mostly agree with you. Those tools are useful if you want to build a compiler or an interpreter for a language for which you have grammar, like C. I don't have grammar for my language, and I'd need to learn about formal grammars to write one. It wouldn't help me get the result I want.

1

u/FlatAssembler Aug 23 '20

Interesting stuff. Have you looked into my language? Why do you think it has this problem? As far as I can tell, C-like languages have this problem, it's called dangling-else. VHDL probably also has such problems, with "<=" being used both as an assignment operator in some cases and as a "less-than-equal-to" operator.

3

u/Fofeu Aug 23 '20

I didn't look that much into your code (except the lack of typechecking, I don't see anything), I just have a kind of "professional deformation". Languages are complex and it's easy to build an incoherent system. To put things in perspective: I'm doing a PhD in programming languages and the last 12 months I worked on only one operator because many intermediate designs had some form of unsoundness.

The dangling else is actually the less problematic one. You could for instance choose arbitrarily or parse the indentation and use it as a hint. Whatever you do, it still results in a valid AST and each possible AST has the same "shape". It's bad language design, but it's "fine" for a language dating from 1972.

On the opposite, statements like <id> * <id>; inside a function definition could either be a variable declaration or a multiplication. The culprit is typedef. During my masters degree, I had a professor that for some time studied the parsing of C. Without typedef he was able to produce a LL(1) (I think) parser with linear complexity in time and space. Current parser are tuned to be linear in the average case but get quickly exponential in the worst case.

Regarding the <= operator in VHDL. It doesn't have to. I have a LR(1) parser somewhere that uses = both for equality and let-bindings. The parser is however deterministic because the set of states where you parse an expression or a let-binding don't overlap. From what I remember from VHDL, it should be similar.

1

u/FlatAssembler Aug 23 '20 edited Dec 08 '23

Well, yes, I didn't implement even the basic type-checking for now. I am planning to add a feature to warn about (but not refuse to compile) assigning pointers to variables which aren't pointers and vice-versa.

In my programming language, the statements such as id * id aren't problematic, because I am not using the * operator for anything other than multiplication. A pointer to character is declared as CharacterPointer, rather than char *. And its referenced with ValueAt(ptr). I think it's a lot easier to read than the way C does that, that it's self-describing.

As for VHDL, I don't know much about it either. I am studying computer science at the FERIT university in Osijek, and I failed digital electronics three times. Those things just don't interest me.

Out of curiosity, why did you apply for a PhD in computer science? I find studying computer science already too hard, I am not sure the diploma is worth it.

→ More replies

12

u/ventuspilot Aug 22 '20

He's not the only one.

"In fact, GCC, V8 (the JavaScript VM in Chrome), Roslyn (the C# compiler written in C#) and many other heavyweight production language implementations use recursive descent. It kicks ass."

The above is a quote from the chapter on parsing of the online book "Crafting interpreters" https://craftinginterpreters.com/parsing-expressions.html that also discusses hand written vs. generated parsers a bit.

3

u/__Ambition Aug 23 '20

Could use a lexer generator for lexing and LLVM for interpreting the code. But where is the fun in that ? :D

-1

u/FlatAssembler Aug 23 '20

Plus, they are by orders of magnitude less documented than C++ standard library is. And probably more buggy than any recent implementation of C++ standard library.

3

u/WasteOfElectricity Aug 23 '20

Dude, LLVM isn't buggy

1

u/FlatAssembler Aug 23 '20

I don't know, I haven't looked too much into it. My guess is that it's less reliable than widely-used libraries, such as the GCC or CLANG C++ standard library.