Choose the Assembly syntax:
Verbose output:
Tokenized:
Parsed (AST converted to an S-expression):
Compiled (to x86 assembly
Interpreter output: null
WARNING: The tokenizer, the parser and the compiler appear to generate correct results, but they haven't been rigorously tested. Also, no optimization is being performed (aside from perhaps the code being the one it's easiest to compile into). If you want a web-app that uses well-tested compilation algorithms and optimizes the assembly code it outputs, try GodBolt.
By the way, if you ever find yourself having to use a programming language without a built-in math library, here is an example of how to make a simple math library in less than 150 lines of code. You can also see a diagram showing how accurate the functions there are for various inputs. (I had to do that in my programming language when writing Analog Clock for WebAssembly, because WebAssembly doesn't provide fsin and similar instructions.)
UPDATE on 15/10/2018: You can download my presentation (in Croatian) about how the compilers work here (It's a ZIP archive containing an ODP file, a PPT file and a PDF file, all saved from Impress. Hopefully, your computer can usefully open at least one of those files!). (UPDATE: A way better version, written more than a year later, is available here.) (UPDATE: About a year later, I've written an English-language version. You can see a YouTube video, and, if your browser does not support streaming MP4 videos, you can download an MP4 file and open it in VLC or something like that.)
UPDATE on 18/10/2018: If you have a modern browser, you can see the AST diagram (tree diagram, also known as syntax tree) by clicking here (it requires a relatively advanced SVG support, seen in, for example, Internet Explorer 11). If you don't have, don't worry, graphical representations of complex (that can't be done by hand) ASTs are useless (the maximal depth of a useful graphical AST is probably around 5).
UPDATE on 03/08/2020: I've started to develop a version of this program which doesn't target x86 processors, but WebAssembly (the JavaScript bytecode, which Mozilla has been pushing to get standardized since 2015, so that people can run programming languages better than JavaScript in a browser). Right now, it's in the very early stages of development, but you can see a program I've made today in it. My new compiler is only compatible with very modern browsers, which not only support WebAssembly, but also support
WebAssembly.Global
. Those are Firefox 62, released
05/09/2018, and Chrome 69, released 04/09/2018. Thus, the code it produces
cannot be run in any browser compatible with Windows XP. I have been
rewriting
the compiler
from scratch in C++, because most experienced programmers consider it to
be a better language than JavaScript.UPDATE on 07/08/2020: Perhaps a lot better proof of the concept that my programming language can be used on the web is my implementation of the permutations algorithm in it.
UPDATE on 30/08/2020: I've written an informal specification for my programming language.
UPDATE on 01/11/2020: As a part of an university project, I've began to write a PicoBlaze simulator (more accurately: an assembler and an emulator) in JavaScript (which can be run in a browser). So, if you want to try non-x86 assembly language programming to better understand how computers work, there might be no better way to do that than to try that app. It runs in Firefox 52 (the last version of Firefox to run on Windows XP, and also the version of Firefox that comes with Solaris 11.4) and newer browsers.
UPDATE on 19/07/2021: Please note that this compiler has a bug related to the way the ternary conditional operator
?:
is compiled. In short, do not use it as to protect yourself against the division by
zero and similar errors, it will not work then, because the second and the
third operand are computed before the first operand that is the condition.
I am sorry about that, but, given the way this compiler is structured
(because I made the core of it back when I was only 18), there does not
appear to be a simple solution. The compiler for my programming language
that is targetting WebAssembly is significantly more professionally
made.UPDATE on 18/01/2023: I've written a shell script so that you can more easily try out this compiler in a Unix shell:
mkdir ArithmeticExpressionCompiler cd ArithmeticExpressionCompiler if [ $(command -v wget > /dev/null 2>&1 ; echo $?) -eq 0 ] # Check if "wget" exists, see those StackOverflow answers for more details: # https://stackoverflow.com/a/75103891/8902065 # https://stackoverflow.com/a/75103209/8902065 then wget https://flatassembler.github.io/Duktape.zip else curl -o Duktape.zip https://flatassembler.github.io/Duktape.zip fi unzip Duktape.zip if [ $(command -v clang > /dev/null 2>&1 ; echo $?) -eq 0 ] # We prefer "clang" to "gcc" because... what if somebody tries to run this in CygWin terminal? GCC will not work then, CLANG might. then c_compiler="clang" else c_compiler="gcc" fi $c_compiler -o aec aec.c duktape.c -lm # The linker that comes with recent versions of Debian Linux insists that "-lm" is put AFTER the source files, or else it outputs some confusing error message. if [ "$OS" = "Windows_NT" ] then ./aec analogClockForWindows.aec $c_compiler -o analogClockForWindows analogClockForWindows.s -m32 ./analogClockForWindows else ./aec analogClock.aec $c_compiler -o analogClock analogClock.s -m32 ./analogClock fiIf everything is fine, the Analog Clock program should now print the current time in the terminal. I think this would work on the vast majority of Linux machines, as well as on many non-Linux (FreeBSD, Solaris...) machines, and on some Windows machines with a Unix shell (such as Git Bash).