Significantly reduce parenthesis and whitespace in output

The intent here is simply to reduce byte count, as well as make the
generated code easier to read and find patterns in for future
optimizations.
master
Mike Gerwitz 2021-01-14 09:36:15 -05:00
parent 3434efcdef
commit 8d25d60c60
2 changed files with 119 additions and 129 deletions

View File

@ -69,19 +69,17 @@
<template mode="compile" priority="1"
match="c:*">
<if test="$calcc-debug = 'yes'">
<text>(/*!+*/result = /*!-*/</text>
<text>/*!+*/(result=/*!-*/</text>
</if>
<apply-templates select="." mode="compile-pre" />
<if test="$calcc-debug = 'yes'">
<text>/*!+*/,( debug['</text>
<text>/*!+*/,(debug['</text>
<value-of select="@_id" />
<text>'] || ( debug['</text>
<text>']||(debug['</text>
<value-of select="@_id" />
<text>'] = [] ) ).push( result ),result/*!-*/ </text>
<text>)</text>
<text>']=[])).push(result),result)/*!-*/</text>
</if>
</template>
@ -110,30 +108,26 @@
<template match="c:*" mode="compile-pre" priority="1">
<!-- ensure everything is grouped (for precedence) and converted to a
number -->
<text>( </text>
<apply-templates select="." mode="compile-calc" />
<text> )</text>
</template>
<template match="c:const[ ./c:when ]|c:value-of[ ./c:when ]" mode="compile-pre" priority="5">
<text>( </text>
<text>(</text>
<!-- first, do what we normally would do (compile the value) -->
<text>( </text>
<apply-templates select="." mode="compile-calc" />
<text> )</text>
<apply-templates select="." mode="compile-calc" />
<!-- then multiply by the c:when result -->
<text> * ( </text>
<for-each select="./c:when">
<if test="position() > 1">
<text> * </text>
</if>
<text> * </text>
<apply-templates select="." mode="compile" />
</for-each>
<text> )</text>
<text> )</text>
<for-each select="./c:when">
<if test="position() > 1">
<text> * </text>
</if>
<apply-templates select="." mode="compile" />
</for-each>
<text>)</text>
</template>
@ -246,10 +240,10 @@
<!-- introduce scope both to encapsulate values and so we can insert this as
part of a larger expression (will return a value) -->
<text>( function() {</text>
<text>(function() {</text>
<!-- will store result of the summation/product -->
<text>var sum = 0;</text>
<text>var sum=0;</text>
<!-- XXX: this needs to use compile-calc-value, but can't right now
beacuse it's not a c:value-of! -->
@ -260,22 +254,22 @@
<!-- if we're looking to generate a set, initialize it -->
<if test="@generates">
<text>var G = []; </text>
<text>var G=[];</text>
</if>
<!-- loop through each value -->
<text>for ( var </text>
<text>for (var </text>
<value-of select="$index" />
<text> in </text>
<value-of select="$value" />
<text> ) {</text>
<text>) {</text>
<text>var result = </text>
<text>var result=</text>
<!-- if caller wants to yield a vector, don't cast -->
<sequence select="if ( not( $dim gt 0 ) ) then
concat( 'precision(', $precision, ', +(+( ')
concat( 'precision(', $precision, ', +(')
else
'(( '" />
'('" />
<choose>
<!-- if there are child nodes, use that as the summand/expression -->
<when test="./c:*">
@ -290,7 +284,7 @@
<text>]</text>
</otherwise>
</choose>
<text> ))</text>
<text>)</text>
<!-- if caller wants to yield a vector, don't truncate -->
<if test="not( $dim gt 0 )">
@ -301,13 +295,13 @@
<!-- if generating a set, store this result -->
<if test="@generates">
<text>G.push( result ); </text>
<text>G.push(result); </text>
</if>
<!-- generate summand -->
<text>sum </text>
<value-of select="$operator" />
<text>= +result;</text>
<text>=+result;</text>
<!-- end of loop -->
<text>}</text>
@ -316,11 +310,11 @@
<if test="@generates">
<text>args['</text>
<value-of select="@generates" />
<text>'] = G; </text>
<text>']=G;</text>
</if>
<text>return sum;</text>
<text>} )()</text>
<text>})()</text>
</template>
@ -333,12 +327,12 @@
validator, then the result is undefined.
-->
<template match="c:product[@dot]" mode="compile-calc" priority="5">
<text>( function() { </text>
<text>(function() { </text>
<!-- we need to determine which vector is the longest to ensure that we
properly compute every value (remember, undefined will be equivalent to
0, so the vectors needn't be of equal length *gasp* blasphemy!) -->
<text>var _$dlen$ = longerOf( </text>
<text>var _$dlen$=longerOf(</text>
<for-each select=".//c:value-of">
<if test="position() > 1">
<text>, </text>
@ -348,30 +342,30 @@
compile)-->
<apply-templates select="." mode="compile-calc" />
</for-each>
<text> ); </text>
<text>); </text>
<!-- will store the total sum -->
<text>var _$dsum$ = 0;</text>
<text>var _$dsum$=0;</text>
<!-- sum the product of each -->
<text disable-output-escaping="yes">for ( var _$d$ = 0; _$d$ &lt; _$dlen$; _$d$++ ) {</text>
<text>_$dsum$ += </text>
<text disable-output-escaping="yes">for(var _$d$=0; _$d$ &lt; _$dlen$; _$d$++) {</text>
<text>_$dsum$ +=</text>
<!-- product of each -->
<for-each select=".//c:value-of">
<if test="position() > 1">
<text> * </text>
</if>
<text>( ( </text>
<text>((</text>
<apply-templates select="." mode="compile" />
<text> || [] )[ _$d$ ] || 0 )</text>
<text>||[])[_$d$]||0)</text>
</for-each>
<text>; </text>
<text>}</text>
<text>return _$dsum$;</text>
<text> } )()</text>
<text>})()</text>
</template>
@ -392,6 +386,7 @@
<apply-templates select="." mode="compile-getop" />
</variable>
<text>(</text>
<for-each select="./c:*">
<!-- add operator between each expression -->
<if test="position() > 1">
@ -402,6 +397,7 @@
<apply-templates select="." mode="compile" />
</for-each>
<text>)</text>
</template>
@ -855,7 +851,7 @@
<with-param name="name" select="@name" />
</call-template>
<text>( args</text>
<text>(args</text>
<variable name="arg-prefix" select="concat( ':', $name, ':' )" />
@ -886,7 +882,7 @@
</choose>
</for-each>
<text> )</text>
<text>)</text>
<!-- if c:when was provided, compile it in such a way that we retain the
function call (we want the result for providing debug information) -->
@ -1003,11 +999,8 @@
whatever calls this, so we're probably fine -->
<!-- return a 1 or a 0 depending on the result of the expression -->
<text>+( </text>
<text>(</text>
<!-- get the value associated with this node -->
<apply-templates select="." mode="compile-calc-value" />
<text>) </text>
<text>+(</text>
<apply-templates select="." mode="compile-calc-value" />
<!-- generate remainder of expression -->
<apply-templates select="./c:*[1]" mode="compile-calc-when" />
@ -1028,6 +1021,7 @@
<calc-compiler:c id="lte">&lt;=</calc-compiler:c>
</variable>
<text> </text>
<value-of disable-output-escaping="yes" select="$map/*[ @id=$name ]" />
<text> </text>
@ -1041,7 +1035,7 @@
<template match="c:cases" mode="compile-calc">
<text>( function() {</text>
<text>((function() {</text>
<for-each select="./c:case">
<!-- turn "if" into an "else if" if needed -->
@ -1058,10 +1052,10 @@
<apply-templates select="." mode="compile" />
</for-each>
<text> ) { return </text>
<text>){return </text>
<!-- process on its own so that we can debug its final value -->
<apply-templates select="." mode="compile" />
<text>; } </text>
<text>;}</text>
</for-each>
<!-- check for the existence of an "otherwise" clause, which should be
@ -1076,7 +1070,7 @@
</when>
<otherwise>
<text>if ( true )</text>
<text>if (true)</text>
</otherwise>
</choose>
@ -1085,7 +1079,7 @@
<text>; } </text>
</if>
<text> } )() || 0</text>
<text> })()||0)</text>
</template>
<template match="c:case" mode="compile-calc">
@ -1133,12 +1127,12 @@
<text>(function(){</text>
<!-- duplicate the array just in case...if we notice a performance impact,
then we can determine if such a duplication is necessary -->
<text>var cdr = Array.prototype.slice.call(</text>
<text>var cdr=Array.prototype.slice.call(</text>
<apply-templates select="$cdr" mode="compile" />
<text>, 0);</text>
<text>cdr.unshift( </text>
<apply-templates select="$car" mode="compile" />
<text> ); </text>
<text>); </text>
<!-- no longer the cdr -->
<text>return cdr; </text>
<text>})()</text>
@ -1161,9 +1155,9 @@
Returns the length of any type of set (not just a vector)
-->
<template match="c:length-of" mode="compile-calc">
<text>( </text>
<text>(</text>
<apply-templates select="./c:*[1]" mode="compile" />
<text>.length || 0 )</text>
<text>.length||0)</text>
</template>
@ -1181,9 +1175,9 @@
</if>
</variable>
<text>function </text>
<text>(function </text>
<value-of select="$fname" />
<text>( </text>
<text>(</text>
<!-- generate arguments -->
<for-each select="$values">
<if test="position() > 1">
@ -1192,28 +1186,25 @@
<value-of select="@name" />
</for-each>
<text> ) { </text>
<text>){</text>
<!-- the second node is the body -->
<text>return </text>
<apply-templates select="./c:*[2]" mode="compile" />
<text>;</text>
<text>}</text>
<text>})</text>
<!-- assign the arguments according to the calculations -->
<text>( </text>
<text>(</text>
<for-each select="$values">
<if test="position() > 1">
<text>,</text>
</if>
<!-- compile the argument value (the parenthesis are just to make it
easier to read the compiled code) -->
<text>(</text>
<apply-templates select="./c:*[1]" mode="compile" />
<text>)</text>
<!-- compile the argument value -->
<apply-templates select="./c:*[1]" mode="compile" />
</for-each>
<text> ) </text>
<text>)</text>
</template>
@ -1233,20 +1224,20 @@
<template match="c:debug-to-console" mode="compile-calc">
<text>(function(){</text>
<text>var result = </text>
<text>var result=</text>
<apply-templates select="./c:*[1]" mode="compile" />
<text>;</text>
<!-- log the result and return it so that we do not inhibit the calculation
(allowing it to be inlined anywhere) -->
<text>console.log( </text>
<text>console.log(</text>
<if test="@label">
<text>'</text>
<value-of select="@label" />
<text>', </text>
</if>
<text>result ); </text>
<text>result); </text>
<text>return result; </text>
<text>})()</text>
</template>

View File

@ -245,7 +245,7 @@
<!-- generate key using param name -->
<text>params['</text>
<value-of select="@name" />
<text>'] = {</text>
<text>']={</text>
<!-- param properties -->
<text>type: '</text>
@ -292,7 +292,7 @@
<!-- generate key using type name -->
<text>types['</text>
<value-of select="../@name" />
<text>'] = {</text>
<text>']={</text>
<!-- its type will be the type of its first enum (all must share the same
domain) -->
@ -324,7 +324,7 @@
<!-- generate key using type name -->
<text>types['</text>
<value-of select="../@name" />
<text>'] = {</text>
<text>']={</text>
<!-- domain of all values -->
<text>type: '</text>
@ -349,7 +349,7 @@
<template match="lv:typedef/lv:base-type" mode="compile" priority="5">
<text>types['</text>
<value-of select="../@name" />
<text>'] = {</text>
<text>']={</text>
<!-- base types are their own type -->
<text>type: '</text>
@ -413,7 +413,7 @@
match="lv:const[ element() or @values ]">
<text>consts['</text>
<value-of select="@name" />
<text>'] = [ </text>
<text>']=[</text>
<!-- matrices -->
<for-each select="compiler:const-sets( . )[ not( . = '' ) ]">
@ -421,7 +421,7 @@
<text>, </text>
</if>
<text>[ </text>
<text>[</text>
<for-each select="compiler:set-items( ., true() )">
<if test="position() > 1">
<text>, </text>
@ -429,7 +429,7 @@
<value-of select="compiler:js-number( . )" />
</for-each>
<text> ]</text>
<text>]</text>
</for-each>
<!-- vectors -->
@ -441,7 +441,7 @@
<value-of select="compiler:js-number( . )" />
</for-each>
<text> ]; </text>
<text>];</text>
</template>
@ -452,7 +452,7 @@
match="lv:const">
<text>consts['</text>
<value-of select="@name" />
<text>'] = </text>
<text>']=</text>
<value-of select="compiler:js-number( @value )" />
<text>;</text>
</template>
@ -652,12 +652,12 @@
<choose>
<!-- universal -->
<when test="not( @any='true' )">
<text>tmp = true; </text>
<text>tmp=true; </text>
</when>
<!-- existential -->
<otherwise>
<text>tmp = false; </text>
<text>tmp=false; </text>
</otherwise>
</choose>
</if>
@ -669,12 +669,12 @@
<choose>
<!-- universal -->
<when test="not( @any='true' )">
<text> = 1;</text>
<text>=1;</text>
</when>
<!-- existential -->
<otherwise>
<text> = 0;</text>
<text>=0;</text>
</otherwise>
</choose>
</if>
@ -688,22 +688,22 @@
<text>classes['</text>
<value-of select="@as" />
<text>'] = tmp;</text>
<text>']=tmp;</text>
</if>
<!-- support termination on certain classifications (useful for eligibility
and error conditions) -->
<if test="@terminate = 'true'">
<text>if ( _canterm &amp;&amp; </text>
<text>if (_canterm &amp;&amp; </text>
<if test="@preproc:generated='true'">
<text>gen</text>
</if>
<text>classes['</text>
<value-of select="@as" />
<text>'] ) throw Error( '</text>
<text>']) throw Error( '</text>
<value-of select="replace( @desc, '''', '\\''' )" />
<text>' );</text>
<text>');</text>
<value-of select="$compiler:nl" />
</if>
@ -718,7 +718,7 @@
<!-- TODO: this can be simplified, since @yields is always provided -->
<if test="$criteria and @yields and ( $sym/@dim='0' )">
<value-of select="$dest" />
<text> = </text>
<text>=</text>
<value-of select="$dest" />
<text>[0];</text>
@ -747,7 +747,7 @@
<variable name="sym-on" as="element( preproc:sym )"
select="$symtable-map( $name )" />
<text> tmp = </text>
<text> tmp=</text>
<variable name="input-raw">
<choose>
@ -788,7 +788,7 @@
<when test="@scalar = 'true'">
<text>stov( </text>
<value-of select="$input-raw" />
<text>, ( ( </text>
<text>, ((</text>
<value-of select="$yieldto" />
<!-- note that we default to 1 so that there is at least a single
element (which will be the case of the scalar is the first match)
@ -797,7 +797,7 @@
happen, and the length is checked on the inner grouping rather than
on the outside of the entire expression to ensure that it will
yield the intended result if yieldto.length === 0 -->
<text> || [] ).length || 1 ) )</text>
<text>||[]).length||1))</text>
</when>
<otherwise>
@ -867,24 +867,24 @@
</when>
<when test="@pattern">
<text>function( val ) { </text>
<text>function(val) {</text>
<text>return /</text>
<value-of select="@pattern" />
<text>/.test( val );</text>
<text> }</text>
<text>/.test(val);</text>
<text>}</text>
</when>
<when test="./c:*">
<text>function( val, __$$i ) { </text>
<text>return ( </text>
<text>function(val, __$$i) { </text>
<text>return (</text>
<for-each select="./c:*">
<if test="position() > 1">
<text disable-output-escaping="yes"> &amp;&amp; </text>
</if>
<text>( val </text>
<text>(val </text>
<apply-templates select="." mode="compile-calc-when" />
<text> ) </text>
<text>)</text>
</for-each>
<text>);</text>
<text>}</text>
@ -928,7 +928,7 @@
</if>
<!-- end of anyValue() call -->
<text> ) </text>
<text>)</text>
<!-- end of assuming function call -->
<if test="lv:assuming">
@ -937,11 +937,11 @@
<text>;</text>
<text>/*!+*/( debug['</text>
<text>/*!+*/(debug['</text>
<value-of select="@_id" />
<text>'] || ( debug['</text>
<text>']||(debug['</text>
<value-of select="@_id" />
<text>'] = [] ) ).push( tmp );/*!-*/ </text>
<text>']=[])).push(tmp);/*!-*/ </text>
</template>
<template name="compiler:gen-match-yieldto">
@ -974,12 +974,12 @@
-->
<template match="lv:match[ @anyOf='float' ]" mode="compiler:match-anyof" priority="5">
<!-- ceil(x) - floor(x) = [ x is not an integer ] -->
<text>function( val ) {</text>
<text>return ( typeof +val === 'number' ) </text>
<text>function(val) {</text>
<text>return (typeof +val === 'number') </text>
<text disable-output-escaping="yes">&amp;&amp; </text>
<!-- note: greater than or equal to, since we want to permit integers as
well -->
<text disable-output-escaping="yes">( Math.ceil( val ) >= Math.floor( val ) )</text>
<text disable-output-escaping="yes">(Math.ceil(val) >= Math.floor(val))</text>
<text>;</text>
<text>}</text>
</template>
@ -996,10 +996,10 @@
-->
<template match="lv:match[ @anyOf='integer' ]" mode="compiler:match-anyof" priority="5">
<!-- ceil(x) - floor(x) = [ x is not an integer ] -->
<text>function( val ) {</text>
<text>return ( typeof +val === 'number' ) </text>
<text>function(val) {</text>
<text>return (typeof +val === 'number') </text>
<text disable-output-escaping="yes">&amp;&amp; </text>
<text>( Math.floor( val ) === Math.ceil( val ) )</text>
<text>( Math.floor(val) === Math.ceil(val))</text>
<text>;</text>
<text>}</text>
</template>
@ -1012,9 +1012,9 @@
-->
<template match="lv:match[ @anyOf='empty' ]" mode="compiler:match-anyof" priority="5">
<!-- ceil(x) - floor(x) = [ x is not an integer ] -->
<text>function( val ) {</text>
<text>return ( val === '' ) </text>
<text>|| ( val === undefined ) || ( val === null )</text>
<text>function(val) {</text>
<text>return (val==='')</text>
<text>||(val===undefined)||(val===null)</text>
<text>;</text>
<text>}</text>
</template>
@ -1026,10 +1026,10 @@
<template match="lv:match[ @anyOf=root(.)//lv:typedef[ ./lv:base-type ]/@name ]"
mode="compiler:match-anyof" priority="3">
<text>function( val ) {</text>
<text>function(val){</text>
<text>throw Error( 'CRITICAL: Unhandled base type: </text>
<value-of select="@anyOf" />
<text>' );</text>
<text>');</text>
<text>}</text>
</template>
@ -1095,11 +1095,11 @@
<text>do{__experimental_guided_tco=0;</text>
</if>
<text>var fresult = ( </text>
<text>var fresult=(</text>
<!-- begin calculation generation (there should be only one calculation node
as a child, so only it will be considered) -->
<apply-templates select="./c:*[1]" mode="compile" />
<text> );</text>
<text>);</text>
<!-- bottom of this function's trampoline, if TCO was requested; if the
flag is set (meaning a relevant tail call was hit), jump back to
@ -1142,13 +1142,13 @@
</choose>
</variable>
<text>predmatch = ( </text>
<text>predmatch=</text>
<apply-templates select="." mode="compile-class-condition" />
<text> ); </text>
<text>; </text>
<!-- set the magic _CMATCH_ var to represent a list of indexes that meet all
the classifications -->
<text>consts['_CMATCH_'] = </text>
<text>consts['_CMATCH_']=</text>
<apply-templates select="." mode="compile-cmatch" />
<text>;</text>
@ -1178,15 +1178,14 @@
<text>.length)).fill(0);</text>
</for-each>
<value-of select="$store" />
<text> = </text>
<text> 0;</text>
<text>=0;</text>
<!-- predicate matches -->
<text>} else {</text>
<!-- store the premium -->
<value-of select="$store" />
<text> = precision(</text>
<text>=precision(</text>
<value-of select="$precision" />
<!-- return the result of the calculation for this rate block -->
<text>, +(</text>
@ -1217,7 +1216,7 @@
set by rate-each expansion, then we want to ignore them entirely,
since we do not want it to clear our the final yield (generators take
care of this using _CMATCH_). -->
<text>( </text>
<text>(</text>
<variable name="class-set"
select="./lv:class[
( @no = 'true'
@ -1255,7 +1254,7 @@
<text>true</text>
</otherwise>
</choose>
<text> )</text>
<text>)</text>
</template>
@ -1265,7 +1264,7 @@
<variable name="root" select="root(.)" />
<!-- generate cmatch call that will generate the cmatch set -->
<text>cmatch( [</text>
<text>cmatch([</text>
<for-each select="lv:class[ not( @no='true' ) ]">
<if test="position() > 1">
<text>, </text>
@ -1293,7 +1292,7 @@
</call-template>
<text>']</text>
</for-each>
<text>] )</text>
<text>])</text>
</template>
@ -1345,7 +1344,7 @@
<template match="lv:meta/lv:prop" mode="compile">
<text>meta['</text>
<value-of select="@name" />
<text>'] = </text>
<text>']=</text>
<call-template name="util:json">
<with-param name="array">