as2js

as2js compiler

The as2js compiler and library is used to convert C++ like scripts in JavaScript that will run in your browsers. The name comes "AlexScript to JavaScript" as the system is used to transcode an advanced version of JavaScript to a version that browsers can execute. Our version includes full support for classes, interfaces, and a complete system environment, contrary to browsers that have many limits (although since version 6, browsers also support classes).

Current status: the trascoder is still in development... I have a version that compiles to binary which is used by the Prinbee system. You can find the status of the coverage and tests below.

The source code compiles, but most certainly does not work as expected yet. You can find it here:

https://github.com/m2osw/as2js

Example

Here is a small example of a class one could use to handle a Popup with jQuery:

class Popup
{
public:
    function Popup()
    {
        jQuery("<div id='popup'></div>").appendTo("body");
        f_popup = jQuery("#popup");
    }

    function open()
    {
        f_popup.show(150);
    }

    function close()
    {
        f_popup.hide(150);
    }

private:
    var f_popup: jQuery;
};

As we can see, this has no prototype definitions and you would not have to use the 'this' keyword all over the place (this feature also forces you to properly name your variables). Of course, in the final output the open, close, and f_popup members are defined in the Popup function prototype definition as expected by browsers. Note also that the semi-colon (;) characters at the end of a line are mandatory.

Testing & Coverage

The coverage tests are to run 100% of the whole code base.

At this point, most of the base of the compiler are covered at 100%. This still doesn't mean it works as expected. To test that 100%, we need to enter a lot of code, compile it, and make sure that resulting program works as expected. Yet, this version is 100% converted to snapcatch2 and runs tests for over two and a half hours on my about 2Ghz processor, testing millions of possibilities. It's getting there.

The website shown in the frame above includes coverage, statistics about the number of lines of code, and the logs while running the coverage tests showing that it ran all the tests.

Supported Operators

The following is a complete list of all the supported operators. They are pretty much the same as the JavaScript operators, plus some perl operators and some extension that appears in g++.

Operator Priority Comments
false
FLOAT
IDENTIFIER
INTEGER
null
private
public
/.../
`...`
'...'
"..."
super
this
true
undefined
()
[]
{}
20 Primary or literal
Regular expressions can use the /.../ syntax, in some cases, the '/' can be viewed as a divide operator instead
The `...` string represents a template
Here the parenthesis represent a group of expressions
Here the square brackets are used to define an array literal
Here the curvly brackets are used to define an object literal
.
::
x++
x--
()
[]
19 Post operators
Member (.)
Scope (::)
Here the parenthesis represent a function call
Here the square brackets mean access an array or object property
delete
++x
--x
void
typeof
+
-
~
!
18 Unary
** 17 Power, note that this operator is right to left: a ** b **c is equivalent to a ** (b ** c) and not (a ** b) ** c.
~=
!~
16 Match, Not Match
If Not Match (!~) is found as a unary operator, it is reverted back to two unary operators: '!' and '~'.
*
/
%
15 Multiplicative
+
-
14 Additive (binary)
<<
>>
>>>
<%
>%
13 Shift
The <% and >% are rotate operators.
<
>
<=
>=
is
as
in
in ..
instanceof
12 Relational operators
The 'in' operator can be followed by a range (a in min .. max)
==
!=
<>
===
!==
<=>
~~
11 Equality
'<>' is an exact equivalent to '!='
<=> is the compare operator which returns -1, 0, 1, or undefined
~~ is the smart match operator; if found as a unary operator, we revert it to two ~ unary operators (bitwise not)
& 10 Bitwise AND
^ 9 Bitwise XOR
| 8 Bitwise OR
&& 7 Logical AND
^^ 6 Logical XOR
|| 5 Logical OR
<?
>?
4 Minimum and Maximum operators
?: 3 Conditional
=
:=
+=
&=
|=
^=
/=
&&=
||=
^^=
>?=
<?=
%=
*=
**=
<%=
>%=
<<=
>>=
>>>=
-=
2 Assignments
':=' is an exact equivalent to '='
See non-assignment operators for more details
, 1 Comma, separates items in a list of expressions.

 

Other Intersting Projects

Google Closure -- this is similar as it is a compiler that supports typing, however the types are to be defined in comments

Flow -- this is similar as it is a compiler that supports inline typing

Immutable -- a library to help with protecting objects against accidental changes

emscripten -- LLVM to JavaScript, write C++ and output JavaScript code

Snap! Websites
An Open Source CMS System in C++

Contact Us Directly