powerlang / egg

Egg Smalltalk
MIT License
13 stars 2 forks source link

Improve the js transpiler to generate "traditional" javascript #3

Open melkyades opened 1 year ago

melkyades commented 1 year ago

Surprisingly, V8 is not very good at inlining things like this:

Boolean.prototype.ifTrue_  = function(closure) { if (this == true) { return closure() } else {return nil } }

function x (aBoolean) {
    aBoolean.ifTrue_(() => { console.log('it is true!') });
}

So we have to improve our transliterator to generate code like this:

function x (aBoolean) {
    if (aBoolean) {
        console.log('it is true!');
    }
}

This is complex because usually ifTrue: is used like this:

a := aBoolean ifTrue: [x] ifFalse: [y]

or worse:

a := aBoolean ifTrue: [^self].

The same applies to ifTrue:/ifFalse:/ifTrue:ifFalse:/whileTrue/whileFalse/whileTrue:/whileFalse:/ifNil:/ifNotNil: among others

Instead of directly generating code, I think we may want to first generate an intermediate Javascript AST, and then write that down. See https://hal.archives-ouvertes.fr/hal-02297860/document