<!--===============================================-->
<!--  TMNPQ.xsl                                    -->
<!--  The interpreter of the Turing machine        -->
<!--===============================================-->
<!-- Example of the program for the Turing machine -->
<!-- Replacement of each P by Q                    -->
<!--===============================================-->


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

<xsl:template match="Go">
	<xsl:call-template name="Go2">
	<xsl:with-param name="TMS">
	<TMState>
<Instruction CurrentState="start"
             CurrentSymbol="B" NextSymbol="B"
             NextState="stop"  Movement="right">
<Instruction CurrentState="start"
             CurrentSymbol="P" NextSymbol="Q"
             NextState="start" Movement="right">
</Instruction>
</Instruction>
		<State>start</State>
		<TapeLeft>
		<Nod><Square>B</Square><Nod><Square>B</Square>
		<Nod><N/></Nod>
		</Nod></Nod>
		</TapeLeft>
		<xsl:copy-of select="Symbol"/>
		<xsl:copy-of select="TapeRight"/>
	</TMState>
	</xsl:with-param>
	</xsl:call-template>

</xsl:template>

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

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

<xsl:choose>

<xsl:when test="State='stop'">
    <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="Go2">
	<xsl:with-param name="TMS">
	<TMState>
	<xsl:copy-of select="Instruction"/>
		<xsl:call-template name="Step">
		<xsl:with-param name="TMS">
		<TMState>
			<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"/>
		</TMState>
		</xsl:with-param>
		</xsl:call-template>
	</TMState>
	</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)/TMState">

<xsl:choose>

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

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

	<xsl:otherwise>
		<xsl:call-template name="Step">
		<xsl:with-param name="TMS">
		<TMState>
			<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"/>
		</TMState>
		</xsl:with-param>
		</xsl:call-template>
	</xsl:otherwise>
	</xsl:choose>
</xsl:when>

<xsl:otherwise>
	<xsl:call-template name="Step">
	<xsl:with-param name="TMS">
	<TMState>
		<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"/>
	</TMState>
	</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="N">
    <Square>B</Square>
    <Nod><Square>B</Square>
    <Nod><N/>
    </Nod></Nod>
</xsl:template>

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

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

</xsl:stylesheet>