From ATS to JavaScript

375 views
Skip to first unread message

gmhwxi

unread,
Aug 22, 2014, 2:58:09 PM8/22/14
to ats-lan...@googlegroups.com
Okay, I have got the ball rolling:

https://github.com/githwxi/ATS-Postiats-contrib/tree/master/projects/MEDIUM/ATS-parse-emit/JavaScript

I am currently targeting Node.js. This is the first running example I had:

/* ATSextcode_beg() */
// COMMENT_line                                                                                                                                                                                  
// COMMENT_line                                                                                                                                                                                  
// COMMENT_line                                                                                                                                                                                  
var fs = require('fs');
eval(fs.readFileSync('./../prelude/CATS/integer_cats.js').toString());
/* ATSextcode_end() */
function
fact
(arg0)
{
 
// __patsflab_fact                                                                                                                                                                              
  tmp1
= ats2jspre_gt_int0_int0(arg0, 0)
 
if (tmp1) {
    tmp3
= ats2jspre_sub_int0_int0(arg0, 1)
    tmp2
= fact(tmp3)
    tmpret0
= ats2jspre_mul_int0_int0(arg0, tmp2)
 
} else {
    tmpret0
= 1
 
} // endif                                                                                                                                                                                      
 
return tmpret0
}

/* ATSextcode_beg() */
console
.log("fact(10) = ", fact(10))
/* ATSextcode_end() */


For being used as a target language, I have to say that JS is a lot more accommodating than Python.

Be happy to hear comments and suggestions.

Cheers!

Martin DeMello

unread,
Aug 22, 2014, 3:18:21 PM8/22/14
to ats-lan...@googlegroups.com
Why not target asm.js rather than javascript proper?

martin




--
You received this message because you are subscribed to the Google Groups "ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.
Visit this group at http://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/b67fead7-b66a-40e0-82fe-2bf6a8279e1a%40googlegroups.com.

gmhwxi

unread,
Aug 22, 2014, 3:42:54 PM8/22/14
to ats-lan...@googlegroups.com
Will do it later. It probably just needs some compilation flags.

For now, I'd like to experiment co-programming with ATS and Node.js.

Greg Fitzgerald

unread,
Aug 22, 2014, 9:47:09 PM8/22/14
to ats-lan...@googlegroups.com
This is good stuff!

-Greg

On Fri, Aug 22, 2014 at 11:58 AM, gmhwxi <gmh...@gmail.com> wrote:
Message has been deleted
Message has been deleted

gmhwxi

unread,
Aug 23, 2014, 1:15:12 AM8/23/14
to ats-lan...@googlegroups.com
Now the code generator (atscc2js) from ATS2 to JavaScript starts to be functioning.
Here are some simple tests:

https://github.com/githwxi/ATS-Postiats-contrib/tree/master/projects/MEDIUM/ATS-parse-emit/JavaScript/TEST

Artyom Shalkhakov

unread,
Aug 23, 2014, 10:58:16 AM8/23/14
to ats-lan...@googlegroups.com
Hongwei,

Could you please emit variable declarations? JS has a funny notion of scope (where all local variables are hoisted to the top of the enclosing function), but reading code where variables are not declared prior to use is puzzling.

Great work!
 

gmhwxi

unread,
Aug 23, 2014, 11:33:18 AM8/23/14
to ats-lan...@googlegroups.com
Done. Here is the current output for the same example:

function
fact
(arg0)
{
//                                                                                                                                                    
 
var tmpret0
 
var tmp1
 
var tmp2
 
var tmp3
//                                                                                                                                                    
 
// __patsflab_fact                                                                                                                                  
  tmp1
= ats2jspre_gt_int0_int0(arg0, 0)

 
if (tmp1) {
    tmp3
= ats2jspre_sub_int0_int0(arg0, 1)
    tmp2
= fact(tmp3)
    tmpret0
= ats2jspre_mul_int0_int0(arg0, tmp2)
 
} else {
    tmpret0
= 1
 
} // endif                                                                                                                                          
 
return tmpret0
} // end-of-function                    

gmhwxi

unread,
Aug 23, 2014, 11:36:15 AM8/23/14
to ats-lan...@googlegroups.com


On Friday, August 22, 2014 2:58:09 PM UTC-4, gmhwxi wrote:

gmhwxi

unread,
Aug 27, 2014, 8:04:51 PM8/27/14
to ats-lan...@googlegroups.com
This is my first try of using JS code generated by atscc2js:

http://www.ats-lang.org/COMPILED/doc/PROJECT/MEDIUM/ATS-parse-emit/JavaScript/mytest.html

Cheers!

--Hongwei

Raoul Duke

unread,
Aug 27, 2014, 8:09:31 PM8/27/14
to ats-lang-users
> http://www.ats-lang.org/COMPILED/doc/PROJECT/MEDIUM/ATS-parse-emit/JavaScript/mytest.html

so would you catch exceptions and stuff? like, ackermann of easily leads to:
Uncaught RangeError: Maximum call stack size exceeded
:-)

Raoul Duke

unread,
Aug 27, 2014, 8:16:07 PM8/27/14
to ats-lang-users
warning: this comment can easily be interpreted to mean the compiler
is slow, taking 19+ hours to compile! pretty please reword it to avoid
fudding ats2js. :-) (and even possibly do include the actual
compilation time stats.)

/*
**
** The JavaScript code is generated by atscc2js
** The compilation time is: 2014-8-27: 19h:27m
**
*/

gmhwxi

unread,
Aug 27, 2014, 8:30:47 PM8/27/14
to ats-lan...@googlegroups.com
Fixed this one. Thanks!

gmhwxi

unread,
Aug 27, 2014, 8:38:44 PM8/27/14
to ats-lan...@googlegroups.com
Good point. Now the comment reads:

The starting compilation time is ...

I will add the finishing time at the end of each generated file.

gmhwxi

unread,
Sep 3, 2014, 10:51:04 PM9/3/14
to ats-lan...@googlegroups.com
Here is a version of Game-of-24 that runs on the JS code generated from ATS source:

http://www.ats-lang.org/COMPILED/doc/PROJECT/SMALL/GameOf24/JavaScript/inputform.html

Yannick Duchêne

unread,
May 12, 2015, 6:53:19 AM5/12/15
to ats-lan...@googlegroups.com


Le vendredi 22 août 2014 20:58:09 UTC+2, gmhwxi a écrit :
I am currently targeting Node.js. This is the first running example I had: […]

What's the difference between `pats_ccomp_instrset.h`  and `pats_ccomp.h`? They both seems to define similar things.

I came to this question trying to understand how ATSCC2JS generate a JS file. I guess it parse the file, searching for macros it expects to find, but they are defined at two places.

Also, as the compilation from C source to the final binary target make uses of macro, why not do the same for C to JS and use the C preprocessor to generate JS files from the C files, as the C files seems to be made of a predefined set of macros?

Yannick Duchêne

unread,
May 12, 2015, 7:09:34 AM5/12/15
to ats-lan...@googlegroups.com


Le mardi 12 mai 2015 12:53:19 UTC+2, Yannick Duchêne a écrit :
Also, as the compilation from C source to the final binary target make uses of macro, why not do the same for C to JS and use the C preprocessor to generate JS files from the C files, as the C files seems to be made of a predefined set of macros?

Just gave it a try, and as there are other includes, this cannot be that easy. The includes are really made for C.

Can I still use either one of these two include files as a reference or should I look to other things too? 

gmhwxi

unread,
May 12, 2015, 10:48:42 AM5/12/15
to ats-lan...@googlegroups.com
Using C macros to generate JS would encounter many difficulties.

For instance, there are goto's in C code but JS does not support goto's.

Yannick Duchêne

unread,
May 13, 2015, 3:16:45 PM5/13/15
to ats-lan...@googlegroups.com


Le vendredi 22 août 2014 20:58:09 UTC+2, gmhwxi a écrit :
[…]

For the “main” of an ATS application targeting JavaScript, I would suggest to use something like this:

%{$
 
try {
    js_main_dynload
();
 
}
 
catch (e) {
    alert
(e);
 
}
%}


Without it, runtime check error, like that of `assertloc`, occurs silently.

Yannick Duchêne

unread,
May 14, 2015, 12:12:00 AM5/14/15
to ats-lan...@googlegroups.com


Le vendredi 22 août 2014 20:58:09 UTC+2, gmhwxi a écrit :
Something which ATSCC2JS seems to not handle.

First the test ATS source, then the error  ATSCC2JS complains about.

The ATS test source:
#define ATS_MAINATSFLAG 1
#define ATS_DYNLOADNAME "js_main_dynload"

#include "share/atspre_define.hats"
#include "{$LIBATSCC2JS}/staloadall.hats"

sortdef reference
= int
typedef reference = [r: reference] int(r)
typedef reference(r: reference) = int(r)
abstype element

stacst not_null
(r: reference): bool
extern fun {r: reference} not_null(r: reference r): bool(not_null(r))

extern fun element{r: reference | not_null(r)}(r: reference r): element
extern fun reference(k: string): reference

val r
: reference = reference("foo")
val b
: bool = not_null(r)
val
() =
 
if b then
    let val e
: element = element(r)
   
in () end

%{$
  js_main_dynload
();
%}

The error ATSCC2JS complains about:
Hello from atscc2js!
ParsingErrors:
:236:35:
   error
(parsing): the keyword [)] is needed.
:236:1:
   error
(parsing): the keyword [}] is needed.

Here is the corresponding line #236 in the C file:
ATSINSmove(statmp1, PMVtmpltcstmat[0](not_null<S2Evar(r$1076(4441))>)(statmp0)) ;

Offset 35 is at the “[” character. It also seems PMVtmpltcstmat is not defined anywhere.

Hongwei Xi

unread,
May 14, 2015, 12:14:46 AM5/14/15
to ats-lan...@googlegroups.com
not_null is a template, and it is never implemented.


--
You received this message because you are subscribed to the Google Groups "ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.
Visit this group at http://groups.google.com/group/ats-lang-users.

Yannick Duchêne

unread,
May 14, 2015, 12:50:49 AM5/14/15
to ats-lan...@googlegroups.com


Le jeudi 14 mai 2015 06:14:46 UTC+2, gmhwxi a écrit :
not_null is a template, and it is never implemented.

Oops, my apologizes. Indeed, turning it into this, there is no more issue:
extern fun not_null{r: reference}(r: reference r): bool(not_null(r))

 Sometime I wish the syntax for template declarations also use the “<>” pair instead of the “{}” quantifier pair.

Yannick Duchêne

unread,
May 20, 2015, 12:50:17 PM5/20/15
to ats-lan...@googlegroups.com


Le vendredi 22 août 2014 20:58:09 UTC+2, gmhwxi a écrit :
Okay, I have got the ball rolling:

https://github.com/githwxi/ATS-Postiats-contrib/tree/master/projects/MEDIUM/ATS-parse-emit/JavaScript

I am currently targeting Node.js. This is the first running example I had:
[…]

Not a big issue, just a bit annoying, atscc2js does not output the file name with its error messages (or it's just with me?), just the “line=”/“offs=”.

Hongwei Xi

unread,
May 20, 2015, 1:38:14 PM5/20/15
to ats-lan...@googlegroups.com
Because it has never been implemented. I will fix it shortly.


--
You received this message because you are subscribed to the Google Groups "ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.
Visit this group at http://groups.google.com/group/ats-lang-users.

Hongwei Xi

unread,
May 20, 2015, 2:03:41 PM5/20/15
to ats-lan...@googlegroups.com
Okay, I added a quick fix.

I am in the process of restructuring atscc2js. I will have something more modular soon.

Brandon Barker

unread,
May 20, 2015, 2:12:13 PM5/20/15
to ats-lang-users
Exciting. If I understand based on your previous assessment, atscc2js
would probably be the best existing template for a atscc2java?

While there are multiple paths to target the JVM, I'm *guessing* that
having an atscc2java is the most reasonable for the moment, and will
probably still generate fairly performant code.

My medium term plans would be to write atscc2java, then convert
IntelliJATS to it ... I hope this might generate more interest for
others in the community as well, but I would say my primary reason is
to just have something to program ATS in, and this seems to be some
potentially nice synergy.
> https://groups.google.com/d/msgid/ats-lang-users/CAPPSPLo053PGnhnv5%3Dw-1BsfaA82DL7Ugk216yAzaTgzBBduaw%40mail.gmail.com.



--
Brandon Barker
brandon...@gmail.com

Brandon Barker

unread,
May 20, 2015, 2:21:38 PM5/20/15
to ats-lang-users
Related to this, did you have to implement TCO directly in atscc2js (I
ask also because I assume this would need to be done for atscc2java)?
--
Brandon Barker
brandon...@gmail.com

Yannick Duchêne

unread,
May 20, 2015, 4:13:52 PM5/20/15
to ats-lan...@googlegroups.com


Le mercredi 20 mai 2015 20:03:41 UTC+2, gmhwxi a écrit :
Okay, I added a quick fix.

I am in the process of restructuring atscc2js. I will have something more modular soon.

Ok, so I will wait before looking at ATSCC2JS again. It happans I was talking about the messages content, because I wanted to try to change some things (did not tell, as I'm not sure it's right or wrong), and for that purpose, it's easier to the have filenames in the messages.

gmhwxi

unread,
May 20, 2015, 5:24:41 PM5/20/15
to ats-lan...@googlegroups.com
Yes, TCO is implemented for JS.

Yannick Duchêne

unread,
May 22, 2015, 11:39:44 AM5/22/15
to ats-lan...@googlegroups.com


Le vendredi 22 août 2014 20:58:09 UTC+2, gmhwxi a écrit :
[…]

Just to notice, as I will try to look at it myself when the new version of ATSCC2JS will be available:

There seems to still be an issue with dynload, precisely with duplicated definitions.

Here is the test:

`test-1.dats`
#define ATS_MAINATSFLAG 1
#define ATS_DYNLOADNAME "js_main_dynload"

#include "share/atspre_define.hats"
#include "{$LIBATSCC2JS}/staloadall.hats"


staload NS
= "test-2.dats"
dynload
"test-2.dats"
val y
= $NS.x + "b"

(*JS*)
%{$
  js_main_dynload
();
%}


`test-2.dats`
#define ATS_MAINATSFLAG 1


#include "share/atspre_define.hats"
#include "{$LIBATSCC2JS}/staloadall.hats"

extern val x: string
implement x
= "a"


Closure (the JS compiler/minifier) complains:
./test-2.js:10: WARNING - Redeclared variable: […]_dats__dynloadflag
var […]_dats__dynloadflag = 0;
   
^
./test-2.js:10: ERROR - Variable […]_dats__dynloadflag first declared in ./test-1.js
var […]_dats__dynloadflag = 0;
   
^
1 error(s), 1 warning(s)

I wanted to have a look at it yesterday, but had an issue compiling the new ATSCCJS, so will wait before looking at it.

gmhwxi

unread,
May 22, 2015, 12:41:11 PM5/22/15
to ats-lan...@googlegroups.com
I think I know what the problem is. I will try to fix it later today.

gmhwxi

unread,
May 23, 2015, 2:47:05 AM5/23/15
to ats-lan...@googlegroups.com
Okay, I think I have fixed this issue,

The changes are now in ATS2-github.

By the way, I feel that it is a good practice to try to generate
only a single C or JS file from several ATS source files. This is
actually quite easy to do. For instance, see CATS-parsemit for a
good example.

Brandon Barker

unread,
Jun 20, 2015, 12:24:35 AM6/20/15
to ats-lan...@googlegroups.com
This is the first I've heard of WebAssembly; http://www.osnews.com/story/28637/The_web_is_getting_its_bytecode_WebAssembly

Looks like it is quite a ways from even a draft spec for now.

On Friday, 22 August 2014 15:42:54 UTC-4, gmhwxi wrote:
Will do it later. It probably just needs some compilation flags.

For now, I'd like to experiment co-programming with ATS and Node.js.

On Friday, August 22, 2014 3:18:21 PM UTC-4, Martin DeMello wrote:
Why not target asm.js rather than javascript proper?

martin


On Fri, Aug 22, 2014 at 11:58 AM, gmhwxi <gmh...@gmail.com> wrote:
I am currently targeting Node.js. This is the first running example I had:

--
You received this message because you are subscribed to the Google Groups "ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.
Visit this group at http://groups.google.com/group/ats-lang-users.

Yannick Duchêne

unread,
Jun 20, 2015, 3:38:21 AM6/20/15
to ats-lan...@googlegroups.com


Le samedi 20 juin 2015 06:24:35 UTC+2, Brandon Barker a écrit :
This is the first I've heard of WebAssembly; http://www.osnews.com/story/28637/The_web_is_getting_its_bytecode_WebAssembly

Looks like it is quite a ways from even a draft spec for now.

I though “another ASM‑JS” , but it looks relevant, as the W2C is involved: https://www.w3.org/community/webassembly/

Probably not available by tomorrow anyway, as we still don't even have full HTML5 support while it's promoted since long.

What ever, nice to know this initiative exists.

Reply all
Reply to author
Forward
0 new messages