aboutsummaryrefslogtreecommitdiff
path: root/src/tmachinespkg/tmlib.nim
blob: 19248454eaad44fcd6fd215b4f83e88ff927f598 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import hashes, tables, strutils, sets

type
  state* = string
  letter* = char
  input* = tuple[state: state, letter: letter]

proc `$`*(input: input): string =
  "(" & $input[0] & " " & $input[1] & ")"

type Direction* = enum
  Dec = -1,
  None = 0,
  Inc = 1

proc `$`*(this: Direction): string =
  case this
  of Dec:
    return "L"
  of None:
    return "S"
  of Inc:
    return "R"

type transition* = tuple[
  newState: state,
  toWrite: letter,
  moveDir: Direction]

proc `$`*(t: transition): string =
  "[" & $t.newState & " " & $t.toWrite & " " & $t.moveDir & "]"

type tape* = Table[int, letter]

proc hash*(this: tape): Hash =
  return ($this).hash()

type ttable* = Table[input, transition]

type configuration* = object
  current_state*: state
  tape*: tape
  position*: int
type config* = configuration

proc hash*(this: config): Hash =
  ## Hashing configurations
  var h: Hash = 0
  h = h !& this.current_state.hash()
  h = h !& this.tape.hash()
  h = h !& this.position.hash()
  return !$h

# Parses a Direction from a string
proc to_direction*(token: string): Direction =
  case token[0]
  of 'R':
    return Inc
  of 'L':
    return Dec
  of 'S':
    return None
  else:
    return None

proc `[]`*(this: configuration, i: int): letter =
  if this.tape.hasKey(i):
    return this.tape[i]
  else:
    return '_'

proc `[]=`*(this: var configuration, i: int, l: letter) =
  this.tape[i] = l

proc get_input*(this: configuration): input =
  return (this.current_state, this[this.position])

proc move*(this: var config, trans: transition): config {.discardable.} =
  this.tape[this.position] = trans.toWrite
  this.position += cast[int](trans.moveDir)
  this.current_state = trans.newState
  return this

# Tape string conversion
proc `$`*(this: configuration): string =
  result = ""
  for i in -30..30:
    if i == 0:
      result &= '['
    result &= $this[this.position + i]
    if i == 0:
      result &= ']'
  result &= " " & $this.current_state

proc insert(this: var tape, input: string, p = 0): tape {.discardable.} =
  for i, c in input:
    this[i + p] = cast[letter](c)
  return this

# Makes a Tape containing this string
proc tapeOf(input: string): tape =
  result = initTable[int, letter]()
  result.insert(input)

proc load*(
  this: var configuration,
  input: string,
  reset: bool = true
): configuration =
  this.tape = tapeOf(input)
  if reset:
    this.position = 0
  return this

## Iterator over every line in a file
proc lineIterator*(f: File): (iterator(): TaintedString {.closure.}) =
  iterator yes(): TaintedString {.closure.} =
    var line: TaintedString
    while readLine(f, line):
      yield line
  return yes

proc make_configuration*(start: state, input: string): configuration =
  return configuration(
    current_state: start,
    tape: tapeOf(input),
    position: 0,
    )