Skip to content

Commit

Permalink
inital commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tfburns committed Jul 24, 2019
1 parent 355457e commit 369f189
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
9 changes: 9 additions & 0 deletions example.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
include("simulator.jl")

input_file = "example_input_1.txt"
program_file = "example_program_1.txt"

input = load_input(input_file)
program, init_state, halt_state = load_program(program_file)

tape_left, tape_right = set_up(input)
1 change: 1 addition & 0 deletions example_input_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
01100101
29 changes: 29 additions & 0 deletions example_program_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Binary numbers divisible by 3

// Input: a binary number n
// Output: accepts if n mod 3 == 0
// Example: accepts 110 (=6)

// ------- States -----------|
// q0 - mod3 == 0 |
// q1 - mod3 == 1 |
// q2 - mod3 == 2 |
// qHalt - halting state |
// --------------------------|

init: q0
halt: qHalt

q0,0,q0,0,>

q0,1,q1,1,>

q1,0,q2,0,>

q1,1,q0,1,>

q2,0,q1,0,>

q2,1,q2,1,>

q0,_,qHalt,_,-
80 changes: 80 additions & 0 deletions simulator.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using DataStructures
using DelimitedFiles

# Read Input / Change value of current input (or do nothing), Move L or R or do nothing
# e.g. 1/0, L = if current cell value is 1, change it to 0, then move left
# e.g. 0/1, . = if current cell value is 0, change it to 1, then don't move

"""
load_input(file_name)
Takes a text file containing a continuous string of `n` integers (no spaces) and returns an `n`-element
array, where each element is an integer. The array is ordered as the input string.
Input
- `file_name`::string : path of text file containing an input string of `n` integers (no spaces)
Output
- ::`n`-element Array{Char, 1} : output array of integers, ordered as the input string
"""
function load_input(file_name)
file = readdlm(file_name, String)
return collect(file[])
end

"""
load_program(file_name)
Takes a text file containing a Turing program. Please see docs for how to write such a program.
Input
"""
function load_program(file_name)
program_file = readdlm("example_program_1.txt")
rows, cols = size(program_file)
program = Dict{Any,Any}()
init_state = []
halt_state = []
for i = 1:rows
if program_file[i,1] == "//"
continue
else
if program_file[i,1] == "init:"
push!(init_state, program_file[i,2])
elseif program_file[i,1] == "halt:"
push!(halt_state, program_file[i,2])
else
card = split(program_file[i,1], r",")
state_read = (String(card[1]),String(card[2]))
instruction = (String(card[3]),String(card[4]),String(card[5]))
program[state_read] = instruction
end
end
end
return program, init_state[], halt_state[]
end

"""
set_up(input)
Performs initial set-up for the simulation of single tape. The `length(input)` must be equal of greater
than 1. The input will be placed on tape to the right of the head.
Input
- `input`::`n`-element Array{Char, 1} : initial data to insert to the right of the head
Output
- `tape_left`::Deque{Int} : initial values for the stack to the left of the head
- `tape_right`::Deque{Int} : initial values for the stack to the right of the head
"""
function set_up(input)
# set up two stacks to simulate a single tape
tape_left = Deque{Int}() # simulates tape lying to the left of the head
tape_right = Deque{Int}() # simulates tape lying to the right of the head

for i = 1:length(input)
push!(tape_right, parse(Int, input[i]))
end

return tape_left, tape_right
end

0 comments on commit 369f189

Please sign in to comment.