Skip to content

Commit

Permalink
lazy to js
Browse files Browse the repository at this point in the history
  • Loading branch information
maxonfjvipon committed May 18, 2024
1 parent 95bf243 commit e89ca5a
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 75 deletions.
2 changes: 1 addition & 1 deletion eo2js/src/resources/xsl/attrs.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ SOFTWARE.
<xsl:text>bound</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>free</xsl:text>
<xsl:text>void</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
Expand Down
98 changes: 36 additions & 62 deletions eo2js/src/resources/xsl/to-js.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ SOFTWARE.
<xsl:variable name="TAB">
<xsl:text> </xsl:text>
</xsl:variable>
<!-- RHO -->
<xsl:variable name="RHO">
<xsl:text>ρ</xsl:text>
</xsl:variable>
<!-- PHI -->
<xsl:variable name="PHI">
<xsl:text>φ</xsl:text>
</xsl:variable>
<!-- FUNCTIONS -->
<!-- EOL with tabs -->
<xsl:function name="eo:eol">
Expand Down Expand Up @@ -97,10 +105,10 @@ SOFTWARE.
<xsl:param name="attr" as="xs:string"/>
<xsl:choose>
<xsl:when test="$attr='@'">
<xsl:text>φ</xsl:text>
<xsl:value-of select="$PHI"/>
</xsl:when>
<xsl:when test="$attr='^'">
<xsl:text>ρ</xsl:text>
<xsl:value-of select="$RHO"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('', $attr)"/>
Expand Down Expand Up @@ -158,7 +166,7 @@ SOFTWARE.
<xsl:value-of select="eo:eol(0)"/>
<xsl:text>const </xsl:text>
<xsl:value-of select="eo:object-name(@name, eo:suffix(@line, @pos))"/>
<xsl:text> = function(sigma) {</xsl:text>
<xsl:text> = function() {</xsl:text>
<xsl:value-of select="eo:eol(0)"/>
<xsl:apply-templates select="." mode="ctor"/>
<!-- <xsl:if test="//meta[head='junit' or head='tests'] and not(@parent)">-->
Expand All @@ -180,7 +188,7 @@ SOFTWARE.
<!-- OBJECT CONSTRUCTING -->
<xsl:template match="object" mode="ctor">
<xsl:value-of select="eo:tabs(1)"/>
<xsl:text>const obj = object(sigma, '</xsl:text>
<xsl:text>const obj = object('</xsl:text>
<xsl:value-of select="@name"/>
<xsl:text>')</xsl:text>
<!-- <xsl:value-of select="eo:eol(1)"/>-->
Expand All @@ -205,11 +213,6 @@ SOFTWARE.
<!-- <xsl:text>super(sigma);</xsl:text>-->
<!-- </xsl:otherwise>-->
<!-- </xsl:choose>-->
<!-- <xsl:variable name="type" select="concat(//meta[head='package']/tail, '.', @name)"/>-->
<!-- <xsl:if test="$type='org.eolang.bytes'">-->
<!-- <xsl:value-of select="eo:eol(2)"/>-->
<!-- <xsl:text>this.add("Δ", new AtFree(new AtSimple()));</xsl:text>-->
<!-- </xsl:if>-->
<xsl:apply-templates select="attr">
<xsl:with-param name="object" select="."/>
<xsl:with-param name="indent">
Expand All @@ -231,7 +234,7 @@ SOFTWARE.
</xsl:apply-templates>
</xsl:template>
<!-- Void attribute -->
<xsl:template match="free">
<xsl:template match="void">
<xsl:param name="name"/>
<xsl:text>attr.void('</xsl:text>
<xsl:value-of select="$name"/>
Expand All @@ -249,14 +252,7 @@ SOFTWARE.
<xsl:with-param name="indent" select="4"/>
</xsl:apply-templates>
<xsl:value-of select="eo:eol(4)"/>
<xsl:choose>
<xsl:when test="o[@const]">
<xsl:text>return cached(ret)</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>return ret</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:text>return ret</xsl:text>
<xsl:value-of select="eo:eol(3)"/>
<xsl:text>}</xsl:text>
<xsl:value-of select="eo:eol(2)"/>
Expand Down Expand Up @@ -314,8 +310,6 @@ SOFTWARE.
<xsl:choose>
<xsl:when test="@primitive and @base">
<xsl:value-of select="eo:fetch(@base)"/>
<!-- <xsl:value-of select="eo:object-name(@base, eo:suffix(@line, @pos))"/>-->
<xsl:text>.copy()</xsl:text>
</xsl:when>
<xsl:when test="@base='$'">
<xsl:text>rho</xsl:text>
Expand All @@ -324,10 +318,9 @@ SOFTWARE.
<xsl:text>phi</xsl:text>
</xsl:when>
<xsl:when test="@base='^'">
<xsl:text>rho.take('ρ')</xsl:text>
</xsl:when>
<xsl:when test="@base='&amp;'">
<xsl:text>rho.take('σ')</xsl:text>
<xsl:text>taken(rho, '</xsl:text>
<xsl:value-of select="$RHO"/>
<xsl:text>')</xsl:text>
</xsl:when>
<xsl:when test="$source/@ancestors">
<xsl:value-of select="eo:object-name($source/@name, eo:suffix(@line, @pos))"/>
Expand All @@ -337,13 +330,21 @@ SOFTWARE.
<xsl:value-of select="eo:fetch(concat($source/@package, '.', $source/@name))"/>
</xsl:when>
<xsl:when test="$source/@level">
<xsl:for-each select="0 to $source/@level">
<xsl:text>taken(</xsl:text>
</xsl:for-each>
<xsl:text>rho</xsl:text>
<xsl:for-each select="1 to $source/@level">
<xsl:text>.take('σ')</xsl:text>
<xsl:text>, '</xsl:text>
<xsl:value-of select="$RHO"/>
<xsl:text>')</xsl:text>
</xsl:for-each>
<xsl:text>, '</xsl:text>
<xsl:value-of select="$source/@name"/>
<xsl:text>')</xsl:text>
</xsl:when>
<xsl:when test="$source">
<xsl:text>rho.take('</xsl:text>
<xsl:text>taken(rho, '</xsl:text>
<xsl:value-of select="eo:attr-name(@base)"/>
<xsl:text>')</xsl:text>
</xsl:when>
Expand Down Expand Up @@ -378,19 +379,13 @@ SOFTWARE.
<xsl:value-of select="eo:eol($indent)"/>
<xsl:text>let </xsl:text>
<xsl:value-of select="$name"/>
<xsl:text> = </xsl:text>
<xsl:text> = taken(</xsl:text>
<xsl:value-of select="$name"/>
<xsl:text>_base.take('</xsl:text>
<xsl:text>_base, '</xsl:text>
<xsl:variable name="method" select="substring-after(@base, '.')"/>
<xsl:choose>
<xsl:when test="$method='^'">
<xsl:text>ρ</xsl:text>
</xsl:when>
<xsl:when test="$method='&amp;'">
<xsl:text>σ</xsl:text>
</xsl:when>
<xsl:when test="$method='&lt;'">
<xsl:text>ν</xsl:text>
<xsl:value-of select="$RHO"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="eo:attr-name($method)"/>
Expand All @@ -406,20 +401,6 @@ SOFTWARE.
<xsl:with-param name="indent" select="$indent"/>
<xsl:with-param name="skip" select="1"/>
</xsl:apply-templates>
<xsl:apply-templates select=".[@copy]" mode="copy">
<xsl:with-param name="name" select="$name"/>
<xsl:with-param name="indent" select="$indent"/>
</xsl:apply-templates>
</xsl:template>
<!-- COPY -->
<xsl:template match="o[@copy]" mode="copy">
<xsl:param name="indent"/>
<xsl:param name="name"/>
<xsl:value-of select="eo:eol($indent)"/>
<xsl:value-of select="$name"/>
<xsl:text> = </xsl:text>
<xsl:value-of select="$name"/>
<xsl:text>.copy()</xsl:text>
</xsl:template>
<!-- <xsl:template match="*" mode="located">-->
<!-- <xsl:param name="indent"/>-->
Expand Down Expand Up @@ -449,10 +430,6 @@ SOFTWARE.
<xsl:param name="skip" select="0"/>
<!-- CREATE OBJECTS TO PUT -->
<xsl:for-each select="./*[name()!='value' and position()&gt;$skip][not(@level)]">
<!-- COPY TAKEN OBJECT -->
<xsl:if test="position()=1">
<xsl:text>.copy()</xsl:text>
</xsl:if>
<!-- VARIABLE TO PUT -->
<xsl:variable name="put">
<xsl:value-of select="$name"/>
Expand All @@ -470,9 +447,9 @@ SOFTWARE.
<xsl:if test="count($to_put)&gt;0">
<xsl:value-of select="eo:eol($indent)"/>
<xsl:value-of select="$name"/>
<xsl:text> = </xsl:text>
<xsl:text> = applied(</xsl:text>
<xsl:value-of select="$name"/>
<xsl:text>.with({</xsl:text>
<xsl:text>, {</xsl:text>
<xsl:value-of select="eo:eol($indent+1)"/>
<xsl:for-each select="$to_put">
<xsl:choose>
Expand All @@ -488,9 +465,6 @@ SOFTWARE.
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:when test="../@base = 'org.eolang.error'">
<xsl:text>'α'</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="position()-1"/>
</xsl:otherwise>
Expand Down Expand Up @@ -596,10 +570,10 @@ SOFTWARE.
<xsl:text>const object = require('eo2js-runtime/src/runtime/object')</xsl:text>
<xsl:value-of select="eo:eol(0)"/>
<xsl:text>const phi = require('eo2js-runtime/src/runtime/phi')</xsl:text>
<xsl:if test="//o[@const]">
<xsl:value-of select="eo:eol(0)"/>
<xsl:text>const cached = require('eo2js-runtime/src/runtime/cached')</xsl:text>
</xsl:if>
<xsl:value-of select="eo:eol(0)"/>
<xsl:text>const taken = require('eo2js-runtime/src/runtime/taken')</xsl:text>
<xsl:value-of select="eo:eol(0)"/>
<xsl:text>const applied = require('eo2js-runtime/src/runtime/applied')</xsl:text>
</xsl:template>
<!-- Atom imports -->
<xsl:template match="object" mode="atom-imports">
Expand Down
2 changes: 1 addition & 1 deletion eo2js/test/resources/transpile/packs/adds-attrs.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
],
"tests": [
".program.objects.object{.\"@_name\" == 'simple'}{.attr{.bound.o{.\"@_base\" == 'org.eolang.stdout'}}}",
".program.objects.object{.\"@_name\" == 'simple'}{.attr{.free.o{.\"@_name\" == 'x'}}}"
".program.objects.object{.\"@_name\" == 'simple'}{.attr{.void.o{.\"@_name\" == 'x'}}}"
],
"eo": [
"+package com.eo2js\n",
Expand Down
4 changes: 2 additions & 2 deletions eo2js/test/resources/transpile/packs/bindings-to-js.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
"method": "includes",
"args": [
[
" ret = ret.with({",
" ret = applied(ret, {",
" 0: ret_1",
" })"
],
[
" ret = ret.with({",
" ret = applied(ret, {",
" 'text': ret_1",
" })"
]
Expand Down
14 changes: 7 additions & 7 deletions eo2js/test/resources/transpile/packs/data-as-bytes.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
"method": "includes",
"args": [
[
" let ret = phi.take('org').take('eolang').take('int').copy()",
" let ret_1 = phi.take('org').take('eolang').take('bytes').copy()",
" let ret = phi.take('org').take('eolang').take('int')",
" let ret_1 = phi.take('org').take('eolang').take('bytes')",
" ret_1.assets['Δ'] = ['0x00', '0x00', '0x00', '0x00', '0x00', '0x00', '0x00', '0x2A']",
" ret = ret.with({",
" ret = applied(ret, {",
" 0: ret_1",
" })",
" return ret"
Expand All @@ -37,10 +37,10 @@
"method": "includes",
"args": [
[
" let ret = phi.take('org').take('eolang').take('float').copy()",
" let ret_1 = phi.take('org').take('eolang').take('bytes').copy()",
" let ret = phi.take('org').take('eolang').take('float')",
" let ret_1 = phi.take('org').take('eolang').take('bytes')",
" ret_1.assets['Δ'] = ['0x40', '0x0C', '0x00', '0x00', '0x00', '0x00', '0x00', '0x00']",
" ret = ret.with({",
" ret = applied(ret, {",
" 0: ret_1",
" })",
" return ret"
Expand All @@ -52,7 +52,7 @@
"method": "includes",
"args": [
[
" let ret = phi.take('org').take('eolang').take('bytes').copy()",
" let ret = phi.take('org').take('eolang').take('bytes')",
" ret.assets['Δ'] = ['0x01', '0xAF']",
" return ret"
]
Expand Down
4 changes: 2 additions & 2 deletions eo2js/test/resources/transpile/packs/embedded-object.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
" obj.attrs['φ'] = attr.once(",
" attr.lambda(",
" obj, function(rho) {",
" let ret_base = rho.take('σ').take('σ')",
" let ret = ret_base.take('x')",
" let ret_base = taken(taken(taken(rho, 'ρ'), 'ρ'), 'this')",
" let ret = taken(ret_base, 'x')",
" return ret",
" }",
" )",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"xsls": [
"objects",
"package",
"attrs",
"data",
"to-js"
],
"tests": [
{
"node": ".program.objects.object{.\"@_name\" == 'simple$second'}.javascript",
"method": "includes",
"args": [
[
" obj.attrs['five'] = attr.once(",
" attr.lambda(",
" obj, function(rho) {",
" let ret_base = taken(taken(rho, 'ρ'), 'first')",
" let ret = taken(ret_base, 'x')",
" return ret",
" }",
" )",
" )"
]
]
}
],
"eo": [
"# This is the default 64+ symbols comment in front of named abstract object.",
"[] > simple",
" # This is the default 64+ symbols comment in front of named abstract object.",
" [] > first",
" 5 > x",
" # This is the default 64+ symbols comment in front of named abstract object.",
" [] > second",
" first.x > five"
]
}

5 comments on commit e89ca5a

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on e89ca5a May 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 3-70345161 discovered in eo2js-runtime/src/objects/org/eolang/rust.js) and submitted as #26. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on e89ca5a May 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 3-2513eadb discovered in eo2js-runtime/src/objects/org/eolang/seq.js) and submitted as #27. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on e89ca5a May 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 3-125c5140 discovered in eo2js-runtime/src/objects/org/eolang/string$length.js) and submitted as #28. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on e89ca5a May 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 3-0039ee65 discovered in eo2js-runtime/src/objects/org/eolang/string$slice.js) and submitted as #29. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on e89ca5a May 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 3-a611c30a discovered in eo2js-runtime/src/objects/org/eolang/try.js) and submitted as #30. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.