diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..686c806 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +*.a +*.bak +*.dSYM +*.o +*.sw* +*.test +*~ +/.bin diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json new file mode 100644 index 0000000..62ae034 --- /dev/null +++ b/Godeps/Godeps.json @@ -0,0 +1,10 @@ +{ + "ImportPath": "github.com/jonathanmarvens/turing-machine", + "GoVersion": "go1.3", + "Deps": [ + { + "ImportPath": "github.com/codegangsta/cli", + "Rev": "bb9189510af1" + } + ] +} diff --git a/Godeps/Readme b/Godeps/Readme new file mode 100644 index 0000000..4cdaa53 --- /dev/null +++ b/Godeps/Readme @@ -0,0 +1,5 @@ +This directory tree is generated automatically by godep. + +Please do not edit. + +See https://github.com/tools/godep for more information. diff --git a/Godeps/_workspace/.gitignore b/Godeps/_workspace/.gitignore new file mode 100644 index 0000000..f037d68 --- /dev/null +++ b/Godeps/_workspace/.gitignore @@ -0,0 +1,2 @@ +/pkg +/bin diff --git a/Godeps/_workspace/src/github.com/codegangsta/cli b/Godeps/_workspace/src/github.com/codegangsta/cli new file mode 160000 index 0000000..bb91895 --- /dev/null +++ b/Godeps/_workspace/src/github.com/codegangsta/cli @@ -0,0 +1 @@ +Subproject commit bb9189510af1f49580c073c9e59e8bf288f0df27 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..70b6def --- /dev/null +++ b/LICENSE @@ -0,0 +1,11 @@ + * The MIT License (MIT). + * + * https://github.com/jonathanmarvens/turing-machine + * + * Copyright (c) 2014 Jonathan Barronville (jonathan@belairlabs.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e259627 --- /dev/null +++ b/Makefile @@ -0,0 +1,26 @@ +########## +default: build +########## + +build: deps lint test + gox -output=".bin/{{.OS}}-{{.Arch}}/turing-machine" -verbose + +clean: + rm -fr ./.bin + +deps: + godep go install -v -x ./... + +errcheck: + errcheck github.com/jonathanmarvens/turing-machine + +fmt: + gofmt -e -s -w . + +lint: fmt errcheck + +test: lint + # TODO(@jonathanmarvens): Add some damn tests to fix this. + +.PHONY: default +.PHONY: build clean deps errcheck fmt lint test diff --git a/README.md b/README.md new file mode 100644 index 0000000..9bb5455 --- /dev/null +++ b/README.md @@ -0,0 +1,66 @@ +# Turing machinez tho. + +This is a simple turing machine I created for an article I’ve been writing titled, “*Neurons, turing machines, & lambda abstractions: an interesting combo?*” + +## Setup. + +There are 3 ways you can install this on your system in order to play with it. + +----- + +__(1) Download a pre-built binary.__ + +You should be able to grab a pre-built binary on the [__Releases__ page](https://github.com/jonathanmarvens/turing-machine/releases) (oh, the power of Go cross-compiling __:)__ … I’m looking at you, GCC). + +----- + +Assuming you have your [Go workspace](http://golang.org/doc/code.html) all set up, the following are your two other options. + +----- + +__(2) Using `go get`.__ + +```sh +go get -t -u github.com/jonathanmarvens/turing-machine +``` + +----- + +__(3) Building from source.__ + +```sh +go get -t -u github.com/kisielk/errcheck github.com/mitchellh/gox github.com/tools/godep +git clone https://github.com/jonathanmarvens/turing-machine.git $GOPATH/src/github.com/jonathanmarvens/turing-machine +cd $GOPATH/src/github.com/jonathanmarvens/turing-machine +make +``` + +Now look in the __.bin/__ directory. + +----- + +## Usage. + +Running `turing-machine help` should be helpful enough about the usage. + +----- + +Feel free to use the example *programs* in [__examples/__](https://github.com/jonathanmarvens/turing-machine/tree/master/examples). + +```sh +# ./.bin/{OS}-{ARCH}/turing-machine --prog="./examples/n-plus-1.btm.json" +``` + +## Author. + +__Jonathan Barronville__ < [__http://乔纳森.com__](http://乔纳森.com) > ( *jonathan@belairlabs.com* ) + +## Acknowledgements. + +Here’s a great document that includes a formal (mathematical) definition of a turing machine (from Cornell University’s CS department): [http://www.cs.cornell.edu/courses/cs4820/2012su/handouts/turingm.pdf](http://www.cs.cornell.edu/courses/cs4820/2012su/handouts/turingm.pdf) … I liked this document more compared to like 6 others I read (the math is clearer and pretty straightforward to follow, there are real examples, the author doesn’t make stupid assumptions about things __-.-__, *et cetera*), so I (loosely) modeled the turing machine around it. You should read it if you want a better mathematical understanding of turing machines! Please don’t rely on Wikipedia for a good understanding of turing machines. + +Here’s a good document, which also includes a formal definition of a turing machine: [http://plato.stanford.edu/entries/turing-machine](http://plato.stanford.edu/entries/turing-machine) … although I didn’t particularly love this document, it’s pretty informative (you probably wanna read it first if you’re new to turing machines). + +## License. + +See [__LICENSE__](https://github.com/jonathanmarvens/turing-machine/blob/master/LICENSE). diff --git a/error/error.go b/error/error.go new file mode 100644 index 0000000..dae62fe --- /dev/null +++ b/error/error.go @@ -0,0 +1,23 @@ +/** + * The MIT License (MIT). + * + * https://github.com/jonathanmarvens/turing-machine + * + * Copyright (c) 2014 Jonathan Barronville (jonathan@belairlabs.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package error + +import ( + "errors" +) + +func New(msg string) error { + return errors.New("[github.com/jonathanmarvens/turing-machine]: " + msg) +} diff --git a/examples/binary-string-prepend-zero.tm.json b/examples/binary-string-prepend-zero.tm.json new file mode 100644 index 0000000..936aa3d --- /dev/null +++ b/examples/binary-string-prepend-zero.tm.json @@ -0,0 +1,128 @@ +{ + "K": [ + "l", "r0", "r1", "s" + ], + + "s": "s", + + "Σ": [ + "^", "$", "0", "1" + ], + + "x": [ + "^", "0", "1", "0", "1", "1", "1", "0", + "$" + ], + + "δ": [ + { + "map": { + "dir": "R", + "state": "r0", + "sym": "^" + }, + + "state": "s", + "sym": "^" + }, + + { + "map": { + "dir": "R", + "state": "r0", + "sym": "0" + }, + + "state": "r0", + "sym": "0" + }, + + { + "map": { + "dir": "R", + "state": "r1", + "sym": "0" + }, + + "state": "r0", + "sym": "1" + }, + + { + "map": { + "dir": "L", + "state": "l", + "sym": "0" + }, + + "state": "r0", + "sym": "$" + }, + + { + "map": { + "dir": "R", + "state": "r0", + "sym": "1" + }, + + "state": "r1", + "sym": "0" + }, + + { + "map": { + "dir": "R", + "state": "r1", + "sym": "1" + }, + + "state": "r1", + "sym": "1" + }, + + { + "map": { + "dir": "L", + "state": "l", + "sym": "1" + }, + + "state": "r1", + "sym": "$" + }, + + { + "map": { + "dir": "L", + "state": "l", + "sym": "0" + }, + + "state": "l", + "sym": "0" + }, + + { + "map": { + "dir": "L", + "state": "l", + "sym": "1" + }, + + "state": "l", + "sym": "1" + }, + + { + "map": { + "dir": "N", + "state": "@HALT", + "sym": "^" + }, + + "state": "l", + "sym": "^" + } + ] +} diff --git a/examples/blank.tm.json b/examples/blank.tm.json new file mode 100644 index 0000000..19d9c42 --- /dev/null +++ b/examples/blank.tm.json @@ -0,0 +1,11 @@ +{ + "K": [], + + "s": "", + + "Σ": [], + + "x": [], + + "δ": [] +} diff --git a/machine/conf.go b/machine/conf.go new file mode 100644 index 0000000..c75bd13 --- /dev/null +++ b/machine/conf.go @@ -0,0 +1,35 @@ +/** + * The MIT License (MIT). + * + * https://github.com/jonathanmarvens/turing-machine + * + * Copyright (c) 2014 Jonathan Barronville (jonathan@belairlabs.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package machine + +type Conf struct { + currState State + currTapePos uint +} + +func (self *Conf) MovTapePosLeft() { + if int(self.currTapePos) == 0 { + return + } + self.currTapePos-- +} + +func (self *Conf) MovTapePosRight() { + self.currTapePos++ +} + +func (self *Conf) SetCurrState(state State) { + self.currState = state +} diff --git a/machine/dir.go b/machine/dir.go new file mode 100644 index 0000000..db8ed73 --- /dev/null +++ b/machine/dir.go @@ -0,0 +1,23 @@ +/** + * The MIT License (MIT). + * + * https://github.com/jonathanmarvens/turing-machine + * + * Copyright (c) 2014 Jonathan Barronville (jonathan@belairlabs.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package machine + +type Dir string + +const ( + DirMovLeft Dir = "L" + DirMovRight Dir = "R" + DirStay Dir = "N" +) diff --git a/machine/machine.go b/machine/machine.go new file mode 100644 index 0000000..aae0226 --- /dev/null +++ b/machine/machine.go @@ -0,0 +1,157 @@ +/** + * The MIT License (MIT). + * + * https://github.com/jonathanmarvens/turing-machine + * + * Copyright (c) 2014 Jonathan Barronville (jonathan@belairlabs.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package machine + +import ( + "fmt" + error_ "github.com/jonathanmarvens/turing-machine/error" + "log" +) + +type Machine struct { + conf *Conf + tape *Tape + transMap map[MachineTransMapKey]MachineTransMapVal +} + +type MachineDesc struct { + Alphabet []Sym `json:"Σ"` + InitState State `json:"s"` + InputSyms []Sym `json:"x"` + States []State `json:"K"` + TransMap []struct { + Map struct { + Dir Dir `json:"dir"` + State State `json:"state"` + Sym Sym `json:"sym"` + } + State State `json:"state"` + Sym Sym `json:"sym"` + } `json:"δ"` +} + +type MachineTransMapKey struct { + state State + sym Sym +} + +type MachineTransMapVal struct { + dir Dir + state State + sym Sym +} + +func NewMachine(machineDesc *MachineDesc) (*Machine, error) { + alphabet := make(map[Sym]bool) + for i := range machineDesc.Alphabet { + alphabet[machineDesc.Alphabet[i]] = true + } + states := make(map[State]bool) + for i := range machineDesc.States { + states[machineDesc.States[i]] = true + } + states[StateValHalt] = true + for i := range machineDesc.InputSyms { + if !alphabet[machineDesc.InputSyms[i]] { + return nil, error_.New( + fmt.Sprintf(" Input symbol \"%s\" isn't in the alphabet!", machineDesc.InputSyms[i]), + ) + } + } + machine := Machine{ + conf: &Conf{ + currState: machineDesc.InitState, + currTapePos: uint(0), + }, + tape: NewTape(machineDesc.InputSyms), + transMap: make(map[MachineTransMapKey]MachineTransMapVal), + } + for i := range machineDesc.TransMap { + if !states[machineDesc.TransMap[i].Map.State] { + return nil, error_.New( + fmt.Sprintf(" Transition map output state \"%s\" isn't in the set of finite states!", machineDesc.TransMap[i].Map.State), + ) + } + if !alphabet[machineDesc.TransMap[i].Map.Sym] { + return nil, error_.New( + fmt.Sprintf(" Transition map output symbol \"%s\" isn't in the alphabet!", machineDesc.TransMap[i].Map.Sym), + ) + } + if !states[machineDesc.TransMap[i].State] { + return nil, error_.New( + fmt.Sprintf(" Transition map input state \"%s\" isn't in the set of finite states!", machineDesc.TransMap[i].State), + ) + } + if !alphabet[machineDesc.TransMap[i].Sym] { + return nil, error_.New( + fmt.Sprintf(" Transition map input symbol \"%s\" isn't in the alphabet!", machineDesc.TransMap[i].Sym), + ) + } + switch machineDesc.TransMap[i].Map.Dir { + case DirMovLeft, DirMovRight, DirStay: + break + default: + return nil, error_.New( + fmt.Sprintf(" \"%s\" isn't a valid direction!", machineDesc.TransMap[i].Map.Dir), + ) + } + machine.transMap[MachineTransMapKey{ + state: machineDesc.TransMap[i].State, + sym: machineDesc.TransMap[i].Sym, + }] = MachineTransMapVal{ + dir: machineDesc.TransMap[i].Map.Dir, + state: machineDesc.TransMap[i].Map.State, + sym: machineDesc.TransMap[i].Map.Sym, + } + } + return &machine, nil +} + +func (self *Machine) Run() { + self.nextTrans() +} + +func (self *Machine) nextTrans() { + currSym := self.tape.ReadSym(self.conf.currTapePos) + nextTrans := self.transMap[MachineTransMapKey{ + state: self.conf.currState, + sym: currSym, + }] + if nextTrans.state == StateValHalt { + _, err := fmt.Println("Machine halted!") + if err != nil { + log.Print(err) + } + _, err = fmt.Print("Tape: ") + if err != nil { + log.Print(err) + } + fmt.Println(self.tape) + return + } + self.conf.SetCurrState(nextTrans.state) + if currSym != nextTrans.sym { + self.tape.WriteSym(self.conf.currTapePos, nextTrans.sym) + } + switch nextTrans.dir { + case DirMovLeft: + self.conf.MovTapePosLeft() + case DirMovRight: + self.conf.MovTapePosRight() + case DirStay: + break + } + self.nextTrans() +} diff --git a/machine/state.go b/machine/state.go new file mode 100644 index 0000000..4c6708f --- /dev/null +++ b/machine/state.go @@ -0,0 +1,21 @@ +/** + * The MIT License (MIT). + * + * https://github.com/jonathanmarvens/turing-machine + * + * Copyright (c) 2014 Jonathan Barronville (jonathan@belairlabs.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package machine + +type State string + +const ( + StateValHalt State = "@HALT" +) diff --git a/machine/sym.go b/machine/sym.go new file mode 100644 index 0000000..aa1ba25 --- /dev/null +++ b/machine/sym.go @@ -0,0 +1,17 @@ +/** + * The MIT License (MIT). + * + * https://github.com/jonathanmarvens/turing-machine + * + * Copyright (c) 2014 Jonathan Barronville (jonathan@belairlabs.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package machine + +type Sym string diff --git a/machine/tape.go b/machine/tape.go new file mode 100644 index 0000000..68983ea --- /dev/null +++ b/machine/tape.go @@ -0,0 +1,60 @@ +/** + * The MIT License (MIT). + * + * https://github.com/jonathanmarvens/turing-machine + * + * Copyright (c) 2014 Jonathan Barronville (jonathan@belairlabs.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package machine + +import ( + "fmt" +) + +type Tape struct { + syms []Sym +} + +func NewTape(syms []Sym) *Tape { + return &Tape{ + syms: syms, + } +} + +func (self *Tape) ReadSym(i uint) Sym { + if int(i) >= cap(self.syms) { + return Sym("") + } + return self.syms[i] +} + +func (self *Tape) String() string { + var val string + for i := range self.syms { + switch i { + case 0: + val += fmt.Sprintf("[ %s | ", self.syms[i]) + case (len(self.syms) - 1): + val += fmt.Sprintf("%s ]\n", self.syms[i]) + default: + val += fmt.Sprintf("%s | ", self.syms[i]) + } + } + return val +} + +func (self *Tape) WriteSym(i uint, sym Sym) { + if int(i) >= cap(self.syms) { + syms := make([]Sym, len(self.syms), (int(i) + 1)) + copy(syms, self.syms) + self.syms = syms + } + self.syms[int(i)] = sym +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..a777c05 --- /dev/null +++ b/main.go @@ -0,0 +1,65 @@ +/** + * The MIT License (MIT). + * + * https://github.com/jonathanmarvens/turing-machine + * + * Copyright (c) 2014 Jonathan Barronville (jonathan@belairlabs.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package main + +import ( + "encoding/json" + "github.com/codegangsta/cli" + error_ "github.com/jonathanmarvens/turing-machine/error" + "github.com/jonathanmarvens/turing-machine/machine" + "log" + "os" +) + +func main() { + app := cli.NewApp() + app.Action = func(ctx *cli.Context) { + prog := ctx.GlobalString("prog") + if prog == "" { + log.Fatal( + error_.New("--prog (or -p) required!"), + ) + } + file, err := os.Open(prog) + if err != nil { + log.Fatal(err) + } + jsonDec := json.NewDecoder(file) + var machineDesc machine.MachineDesc + err = jsonDec.Decode(&machineDesc) + if err != nil { + log.Fatal(err) + } + machine, err := machine.NewMachine(&machineDesc) + if err != nil { + log.Fatal(err) + } + machine.Run() + } + app.Flags = []cli.Flag{ + cli.StringFlag{ + "prog,p", + "", + "Path to the turing machine \"program\" to run.", + }, + } + app.Name = "turing-machine" + app.Usage = "Turing machinez tho." + app.Version = "0.0.1" + err := app.Run(os.Args) + if err != nil { + log.Fatal(err) + } +}