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 ActionScript which is an extension of the ECMAScript language used by Flash Adobe that offers real classes, interfaces, and a complete system environment, contrary to browsers that only offer prototypes which are difficult to work with for most users.

Current status: in development... we are fixing problems by writing a complete test suite with 100% coverage whenever possible. 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 on SourceForge.net here:

http://sourceforge.net/p/snapcpp/code/ci/master/tree/as2js/

Example

There 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.

Testing & Coverage

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

At this point this is done on 13.10 so there are some limits to the coverage tools which in 14.04 have been enhanced. None the less, the tests are progressing and we already have part of the parser done (some files at 100%!)

To find coverage for other versions and other parts of the software (instead of just te library) you may want to go to the top coverage folder. That website includes other parts of the as2js tests with statistics about the number of lines of code and the logs while running the coverage tests showing that it ran all the tests. (Note in that regard that at this point a couple of those tests are skipped in the first run; I run these two tests after the first large set of tests. One of those tests completely messes up stdout and thus prevents the out from all the other tests if run along side. The other is not compatible with some other tests (i.e. some variables are set statically so once run once, we cannot test that variable with a different value!)

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 also be written `...`
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