aboutsummaryrefslogtreecommitdiff
path: root/src/tmachines.nim
blob: aae5401163520dedafda2e889ce8ee3b48de4f29 (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
import os, strutils

import docopt

from tmachinespkg/tm import nil
from tmachinespkg/ntm import nil

type Machine = enum
  DTM, NTM

proc tm_type_from_cli(value: string): Machine =
  case value
  of "dtm":
    return DTM
  of "ntm":
    return NTM
  else:
    return NTM

proc tm_type_from_filename(filename: string): Machine =
  if filename.ends_with(".dtm"):
    return DTM
  elif filename.ends_with(".ntm"):
    return NTM
  elif filename.ends_with(".tm"):
    return NTM

const tmname {.strdefine.}: string = ""

when tmname.len > 0:
  const tmd = parse(slurp(tmname))
  if paramCount() >= 1:
    echo makeDTM(tmd, paramStr(1)).test()
  else:
    echo "please specify a single string argument"
else:
  const doc = slurp("../USAGE.txt")

  let args = docopt(doc)
  let file = $args["<machine>"]

  var m_type: Machine
  if $args["--type"] != "nil":
    m_type = tm_type_from_cli($args["--type"])
  else:
    m_type = tm_type_from_filename(file)

  case m_type:
    of DTM:
      let tmd = tm.parse(readFile($args["<machine>"]))
      echo tm.test(tm.makeDTM(tmd, $args["<input>"]), not args["--quiet"])
    of NTM:
      let tmd = ntm.parse(readFile($args["<machine>"]))
      echo ntm.test(ntm.makeNTM(tmd, $args["<input>"]), not args["--quiet"])