#!/usr/bin/perl

eval 'exec perl -S $0 ${1+"$@"}'
    if 0;

# Simulates a Turing machine in CLC-INTERCAL

# The input to the program must be:

# The Turing program, as a sequence of 5 numbers followed by a single zero;
# the 5 numbers are state, symbol, movement, new_state, new_symbol
# movement is 1 for left, 2 for right, 3 for stop, anything else for no move

# The initial tape contents, as a sequence of numbers followed by a zero

# The initia state, a single number

# The default tape symbol, used when the head reaches an empty square.

# The program outputs the final state contents as two sequences of numbers -
# each one terminated by a zero - the first is the content to the left of the
# head, the second the content to the right of the head.

# An example prgram to do multiplication is at the end - it assumes the
# initial state and the default symbol are both FOUR, and the tape contains
# the two numbers in unary, the first number as ONEs, the second as TWOs:
# for example ONE ONE TWO TWO TWO means 2 * 3, and produces THREE THREE THREE
# THREE THREE THREE (aka 6). Note that the example at the end includes the
# above example input numbers. Note that you must convert the data at the end
# to EBCDIC before using it as input to the program.

# This file is part of CLC-INTERCAL.

# WARNING - do not operate heavy machinery while using CLC-INTERCAL

# Copyright (C) 1999 Claudio Calvelli <lunatic@assurdo.com>, all rights reserved

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

use Language::INTERCAL 'turing', '
@@@@@@@@@@M]

M]@@@@@@@@@@@`K@@@
@@@@@@@@@@@k@@@@k@@
@@@@@@@@K@@@@k@@@@@@
@@@@@@@@@K

@@@@@@@@@@@@@@@`z
@@@@@@@Lk@k@k@mk@mn
@@@@@@@@@@@@k@@@@@@k@@
@@@@@@@k@@@k@@@@@@@K@@@@
@@@@@@@@@K

@@@@@@@@@@@@@@@@@k
@@@@@@@@@@@@@K

@@@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