<!--===============================================-->
<!--  tm.xsl                                       -->
<!--  The interpreter of the Turing machine        -->
<!--===============================================-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xt="http://www.jclark.com/xt"
		        version="1.0">

<xsl:template match="TM">
	<xsl:call-template name="TM2">
	<xsl:with-param name="TMS">
		<xsl:copy-of select="."/>
	</xsl:with-param>
	</xsl:call-template>
</xsl:template>

<!--===============================================-->
<!--        Head function                          -->
<!--===============================================-->

<xsl:template name="TM2"> 
<xsl:param name="TMS"/>
<xsl:for-each select="xt:node-set($TMS)/TM">

<xsl:choose>

<xsl:when test="State='Z'">
    <TM>
	<xsl:copy-of select="TapeLeft"/>
	<xsl:copy-of select="Symbol"/>
	<xsl:copy-of select="TapeRight"/>
	</TM>
</xsl:when>

<xsl:otherwise>
	<xsl:call-template name="TM2">
	<xsl:with-param name="TMS">
	<TM>
	<xsl:copy-of select="Instruction"/>
		<xsl:call-template name="Step">
		<xsl:with-param name="TMS">
		<TM>
			<xsl:copy-of select="Instruction"/>
			<xsl:copy-of select="State"/>
			<xsl:copy-of select="TapeLeft"/>
			<xsl:copy-of select="Symbol"/>
			<xsl:copy-of select="TapeRight"/>
		</TM>
		</xsl:with-param>
		</xsl:call-template>
	</TM>
	</xsl:with-param>
	</xsl:call-template>
</xsl:otherwise>

</xsl:choose>
</xsl:for-each>
</xsl:template>

<!--===============================================-->
<!--    One step of the Turing machine             -->
<!--===============================================-->

<xsl:template name="Step">
  <xsl:param name="TMS"/>
<xsl:for-each select="xt:node-set($TMS)/TM">

<xsl:choose>

<xsl:when test="Instruction/@CurrentState=State">
	<xsl:choose>
	<xsl:when test="Instruction/@CurrentSymbol=Symbol">
		<xsl:choose>
		<xsl:when test="Instruction/@Move='R'">
			<State>
				<xsl:value-of select="Instruction/@NextState"/>
			</State>
			<TapeLeft>
				<Node>
				<Square>
				<xsl:value-of select="Instruction/@NextSymbol"/>
				</Square>
				<xsl:copy-of select="TapeLeft/Node"/>
				</Node>
			</TapeLeft>
			<Symbol>
				<xsl:value-of select="TapeRight/Node/Square"/>
			</Symbol>
			<TapeRight>
    			<Node>
				<xsl:for-each select="TapeRight/Node/Node">
				<xsl:apply-templates/>
				</xsl:for-each>
				</Node>
			</TapeRight>
		</xsl:when>

		<xsl:when test="Instruction/@Move='L'">
			<State>
				<xsl:value-of select="Instruction/@NextState"/>
			</State>
			<TapeLeft>
				<Node>
				<xsl:for-each select="TapeLeft/Node/Node">
				<xsl:apply-templates/>
				</xsl:for-each>
				</Node>
			</TapeLeft>
			<Symbol>
				<xsl:value-of select="TapeLeft/Node/Square"/>
			</Symbol>
			<TapeRight>
				<Node>
				<Square>
				<xsl:value-of select="Instruction/@NextSymbol"/>
				</Square>
				<xsl:copy-of select="TapeRight/Node"/>
				</Node>
			</TapeRight>
		</xsl:when>
		</xsl:choose>
	</xsl:when>

	<xsl:otherwise>
		<xsl:call-template name="Step">
		<xsl:with-param name="TMS">
		<TM>
			<xsl:copy-of select="Instruction/Instruction"/>
			<xsl:copy-of select="State"/>
			<xsl:copy-of select="TapeLeft"/>
			<xsl:copy-of select="Symbol"/>
			<xsl:copy-of select="TapeRight"/>
		</TM>
		</xsl:with-param>
		</xsl:call-template>
	</xsl:otherwise>
	</xsl:choose>
</xsl:when>

<xsl:otherwise>
	<xsl:call-template name="Step">
	<xsl:with-param name="TMS">
	<TM>
		<xsl:copy-of select="Instruction/Instruction"/>
		<xsl:copy-of select="State"/>
		<xsl:copy-of select="TapeLeft"/>
		<xsl:copy-of select="Symbol"/>
		<xsl:copy-of select="TapeRight"/>
	</TM>
	</xsl:with-param>
	</xsl:call-template>
</xsl:otherwise>

</xsl:choose>
</xsl:for-each>
</xsl:template>

<!--===============================================-->
<!--      Processing of the end of a tape          -->
<!--===============================================-->

<xsl:template match="End">
    <Node><Square>b</Square>
    <Node><Square>b</Square>
    <End/>
    </Node></Node>
</xsl:template>

<xsl:template match="Node">
     <xsl:copy-of select="."/>
</xsl:template>

<xsl:template match="Square">
    <xsl:copy-of select="."/>
</xsl:template>

<!--===============================================-->

</xsl:stylesheet>

<!--===============================================-->