Skip to content

Commit

Permalink
xslt: merge eval_ast macroexpand, DEBUG-EVAL, reduce diffs between steps
Browse files Browse the repository at this point in the history
Add comments and line breaks for readability.

The diff should be reviewed with -b git option.  The intent is to
reduce the diff between steps, in the hope to ease further
maintenance.
  • Loading branch information
asarhaddon authored and kanaka committed Oct 21, 2024
1 parent 49d1da7 commit 0360eca
Show file tree
Hide file tree
Showing 8 changed files with 3,984 additions and 3,505 deletions.
236 changes: 112 additions & 124 deletions impls/xslt/step2_eval.inc.xslt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@
<state> ignored, preserved </state>
</mal>
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/02/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:map="http://www.w3.org/2005/xpath-functions/map" version="3.0" exclude-result-prefixes="fn xs map">
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
version="3.0"
exclude-result-prefixes="fn xs map">
<xsl:import href="reader.xslt"/>
<xsl:import href="printer.xslt"/>
<xsl:output method="xml" encoding="utf-8" indent="yes"/>

<xsl:template match="mal" name="rep">
<xsl:param name="display" select="false()" />
<mal>
Expand Down Expand Up @@ -49,6 +56,7 @@
</xsl:message>
</mal>
</xsl:template>

<xsl:template name="PRINT">
<xsl:param name="env"/>
<xsl:variable name="str">
Expand All @@ -58,88 +66,7 @@
</xsl:variable>
<xsl:value-of select="$str"/>
</xsl:template>
<xsl:template name="eval_ast">
<xsl:param name="env"/>
<xsl:choose>
<xsl:when test="value/malval/@kind = 'symbol'">
<value>
<xsl:sequence select="fn:env_lookup($env, value/malval/@value)"/>
</value>
</xsl:when>
<xsl:when test="value/malval/@kind = 'list'">
<value>
<malval kind="list">
<lvalue>
<xsl:for-each select="value/malval/lvalue/malval">
<xsl:variable name="ctx">
<value>
<xsl:sequence select="."/>
</value>
</xsl:variable>
<xsl:variable name="xctx">
<xsl:for-each select="$ctx">
<xsl:call-template name="EVAL">
<xsl:with-param name="env" select="$env"/>
</xsl:call-template>
</xsl:for-each>
</xsl:variable>
<xsl:sequence select="$xctx/value/malval"/>
</xsl:for-each>
</lvalue>
</malval>
</value>
</xsl:when>
<xsl:when test="value/malval/@kind = 'vector'">
<value>
<malval kind="vector">
<lvalue>
<xsl:for-each select="value/malval/lvalue/malval">
<xsl:variable name="ctx">
<value>
<xsl:sequence select="."/>
</value>
</xsl:variable>
<xsl:variable name="xctx">
<xsl:for-each select="$ctx">
<xsl:call-template name="EVAL">
<xsl:with-param name="env" select="$env"/>
</xsl:call-template>
</xsl:for-each>
</xsl:variable>
<xsl:sequence select="$xctx/value/malval"/>
</xsl:for-each>
</lvalue>
</malval>
</value>
</xsl:when>
<xsl:when test="value/malval/@kind = 'hash'">
<value>
<malval kind="hash">
<lvalue>
<xsl:for-each select="value/malval/lvalue/malval">
<xsl:variable name="ctx">
<value>
<xsl:sequence select="."/>
</value>
</xsl:variable>
<xsl:variable name="xctx">
<xsl:for-each select="$ctx">
<xsl:call-template name="EVAL">
<xsl:with-param name="env" select="$env"/>
</xsl:call-template>
</xsl:for-each>
</xsl:variable>
<xsl:sequence select="$xctx/value/malval"/>
</xsl:for-each>
</lvalue>
</malval>
</value>
</xsl:when>
<xsl:otherwise>
<xsl:sequence select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<!-- vapply[fn, args] :: fn/value/text() -->
<xsl:template name="vapply">
<xsl:param name="func"/>
Expand All @@ -166,52 +93,110 @@
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="EVAL">
<xsl:param name="env"/>
<xsl:choose>
<xsl:when test="value/malval/@kind = 'list'">
<xsl:choose>
<xsl:when test="count(value/malval/lvalue/malval) = 0">
<xsl:sequence select="."/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="new_list">
<xsl:call-template name="eval_ast">
<xsl:with-param name="env" select="$env"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="func">
<xsl:for-each select="$new_list">
<xsl:sequence select="value/malval/lvalue[1]/malval/text()"/>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="args">
<xsl:for-each select="$new_list">

<xsl:choose>

<xsl:when test="value/malval/@kind = 'symbol'">
<value>
<xsl:sequence select="fn:env_lookup($env, value/malval/@value)"/>
</value>
</xsl:when>

<xsl:when test="value/malval/@kind = 'vector'">
<value>
<malval kind="vector">
<lvalue>
<xsl:for-each select="value/malval/lvalue/malval">
<xsl:variable name="ctx">
<value>
<xsl:sequence select="."/>
</value>
</xsl:variable>
<xsl:variable name="xctx">
<xsl:for-each select="$ctx">
<xsl:call-template name="EVAL">
<xsl:with-param name="env" select="$env"/>
</xsl:call-template>
</xsl:for-each>
</xsl:variable>
<xsl:sequence select="$xctx/value/malval"/>
</xsl:for-each>
</lvalue>
</malval>
</value>
</xsl:when>

<xsl:when test="value/malval/@kind = 'hash'">
<value>
<malval kind="hash">
<lvalue>
<xsl:for-each select="value/malval/lvalue/malval">
<xsl:variable name="ctx">
<value>
<xsl:sequence select="."/>
</value>
</xsl:variable>
<xsl:variable name="xctx">
<xsl:for-each select="$ctx">
<xsl:call-template name="EVAL">
<xsl:with-param name="env" select="$env"/>
</xsl:call-template>
</xsl:for-each>
</xsl:variable>
<xsl:sequence select="$xctx/value/malval"/>
</xsl:for-each>
</lvalue>
</malval>
</value>
</xsl:when>

<xsl:when test="value/malval/@kind != 'list'
or count(value/malval/lvalue/malval) = 0">
<xsl:sequence select="."/>
</xsl:when>

<!-- apply phase -->
<xsl:otherwise>
<xsl:variable name="new_list">
<xsl:for-each select="value/malval/lvalue/malval">
<xsl:variable name="ctx">
<value>
<malval kind="list">
<lvalue>
<xsl:for-each select="value/malval/lvalue/node()[position() != 1]">
<xsl:sequence select="."/>
</xsl:for-each>
</lvalue>
</malval>
<xsl:sequence select="."/>
</value>
</xsl:for-each>
</xsl:variable>
<xsl:call-template name="vapply">
<xsl:with-param name="func" select="$func"/>
<xsl:with-param name="args" select="$args"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="eval_ast">
<xsl:with-param name="env" select="$env"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="xctx">
<xsl:for-each select="$ctx">
<xsl:call-template name="EVAL">
<xsl:with-param name="env" select="$env"/>
</xsl:call-template>
</xsl:for-each>
</xsl:variable>
<xsl:sequence select="$xctx/value/malval"/>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="func">
<xsl:sequence select="$new_list/malval[1]/text()"/>
</xsl:variable>
<xsl:variable name="args">
<value>
<malval kind="list">
<lvalue>
<xsl:sequence select="$new_list/node()[position() != 1]"/>
</lvalue>
</malval>
</value>
</xsl:variable>
<xsl:call-template name="vapply">
<xsl:with-param name="func" select="$func"/>
<xsl:with-param name="args" select="$args"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="READ">
<xsl:variable name="context">
<str>
Expand All @@ -230,6 +215,7 @@
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:template>

<xsl:function name="fn:env_lookup">
<xsl:param name="env"/>
<xsl:param name="name"/>
Expand All @@ -247,11 +233,13 @@
</xsl:otherwise>
</xsl:choose>
</xsl:function>

<xsl:function name="fn:makeMALType">
<xsl:param name="value"/>
<xsl:param name="kind"/>
<value>
<malval kind="{$kind}" value="{$value}"/>
</value>
</xsl:function>

</xsl:stylesheet>
Loading

0 comments on commit 0360eca

Please sign in to comment.