• Stars
    star
    234
  • Rank 165,916 (Top 4 %)
  • Language
    Erlang
  • License
    ISC License
  • Created almost 11 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿ‰ SHEN: Erlang JavaScript Compiler

SHEN: Erlang JavaScript Compiler

Simple

Libraries

We support following stack by Erlang JavaScript compiler:

  • matches.js -- Erlang-like matching syntax
  • tailrec.js -- optimize tail calls
  • beacon.js -- FRP event streaming

The only real pratical fast solution is to translate Erlang AST into JavaScript using JavaScript helpers like matches.js and tailrec.js.

Usage

  • Compilation to JavaScript, node.js, Browser, Client-Side FRP
  • Macros, String Templates, Embedding Mode, Server-Side, N2O

Compilation to Pure JavaScript

In case of Client-Logic, Off-line clients, Client-side FRP Event System use can export you functions in module with -js attribute. All function will be stored to the same filename with js extension.

fac.erl:

    -module(fac).
    -compile({parse_transform, shen}).
    -compile(export_all).

    -js([start/0,fac/1]).

    start() ->
        N = fac(5),
        console:log("factorial ~p", [J, N]).

    fac(0) -> 1;
    fac(N) -> N * fac(N-1).

Compile with Erlang:

$ erlc shen.erl
$ erlc -pa . fac.erl

And you will get fac.js:

    var pattern = require("matches").pattern;
    var start = pattern({
        '': function() {
            j = 5;
            n = fac(j);
            return console.log('factorial ~p',[j,[n,[]]]);
    }});
    var fac = pattern({
        '0': function(x1) {
            return 1;
        },
        'n': function(n) {
            return n * fac(n - 1);
    }});
    start();

Now you can check:

$ node fac.js
factorial ~p [ 5, [ 120, [] ] ]

JavaScript Macros

Let say we want to generate JavaScript in our Erlang code which is very useful for Server-Logic Event System, so you can write programs in Erlang and expand them into JavaScript using -jsmacro attribute. Specified functions will be expanded to JavaScript during compilation.

    -module(fac).
    -compile({parse_transform, shen}).
    -compile(export_all).

    -jsmacro([macro/3]).

    macro(A,B,C) ->
        ws:send('Bert':encodebuf(
            [{source,'Bert':binary(A)},
             {x,C},
             {pickle,'Bert':binary(B)},
             {linked,C}])).

    main() ->
        A = "1",
        B = "2",
        Script = macro(A,B,"3"),
        io:format("JS Macro: ~s",[Script]).

Lets try it:

7> fac:main().
JS Macro: ws.send(Bert.encodebuf({source:Bert.binary(1),x:3,pickle:Bert.binary(2),linked:3}));
ok

Roadmap

  1. multiple clauses for lambdas
  2. list comprehensions
  3. more JavaScript/OTP mappings
  4. if statement :-)

Credits

* Maxim Sokhatsky
* Andrew Zadorozhny

OM A HUM

More Repositories

1

n2o

โญ• N2O: Distributed WebSocket Application Server ISO 20922
Erlang
1,335
star
2

bpe

๐Ÿ’  BPE: BPMN Process Engine ISO 19510
Erlang
259
star
3

kvs

๐Ÿ’ฟ KVS: NVMe Key-Value Store
Erlang
174
star
4

mad

โšก MAD: Managing Application Dependencies LING/UNIX
Erlang
172
star
5

rest

โ˜• REST: RFC-2616 Framework
Erlang
82
star
6

cr

๐Ÿ’  CR: Chain Replication Database for KVS
Erlang
57
star
7

ldap

๐ŸŒ LDAP: Directory Server. ITU/IETF: 2849, 3296, 3671-3673, 3866, 4510-4518, 4522, 4525, 4526, 4929, 5480, X.519.
Erlang
53
star
8

nitro

๐Ÿ”ฅ NITRO: Nitrogen Web Framework RFC 6455
Erlang
53
star
9

form

๐Ÿงพ FORM: Business X-Forms
Erlang
39
star
10

exe

๐Ÿ–ฅ๏ธ EXE: Shell Exec
Erlang
36
star
11

mail

โœ‰๏ธ MAIL: Message Handling Service. ISO/IEC: 10021; ITU/IETF: X.400โ€”X.403, X.407โ€”X.408, X.411โ€”X.413, X.419โ€”X.420.
Erlang
23
star
12

rpc

โ˜Ž๏ธ RPC: Type Driven Parser Generator
Erlang
17
star
13

ca

๐Ÿ›ก๏ธ CA: Certificate Authority. ะ”ะกะขะฃ: 4145. ITU/IETF: 3279, 5755, X.501, X.509, X.511, X.520.
Erlang
17
star
14

n2o.dev

๐ŸŒ N2O.DEV: Open Source Enterprise Frameworks
CSS
12
star
15

sample

๐Ÿ’ง SAMPLE: WebSocket Sample Application
Erlang
9
star
16

rt

โ˜€ RT: Erlang Runtime Library
Erlang
8
star
17

synrc.com

๐ŸŒ SYNRC: Commercial Support
HTML
7
star
18

base

โ˜€ BASE: Erlang Base Library
Elixir
6
star
19

chat

๐Ÿ’ฌ CHAT: Instant Messenger. ISO/IEC: 20922; ITU/IETF: 3394, 3565, 5280, 5480, 5652, 5755 8551, X.509, CMS, PKCS-10, PCKS-7, OCSP, LDAP, DNS; ANSI: X9-42, X9-62, X25519, X488; NIST: SECP384r1.
Erlang
6
star
20

svg

๐Ÿš€ SVG: Vector HTML5 DSL
Erlang
5
star
21

review

๐Ÿ’ง REVIEW: MQTT Sample Application
Erlang
5
star
22

ns

๐Ÿ”’ NS: Name Server IETF 1034, 1035, 1101, 2065, 2535, 2539, 4033-4035 4398, 6944
Erlang
5
star
23

mq

๐Ÿ“Ÿ MQ: MQTT Server ISO 20922
HTML
4
star
24

storybook

๐Ÿ“’ NITRO: Storybook
Erlang
1
star