├── .gitignore ├── LICENSE ├── README.md ├── doc └── intro.md ├── examples ├── adder.eo └── doubler.eo ├── images ├── abcdae.png ├── step-1.png ├── step-2.png ├── step-3.png ├── step-4.png ├── step-5.png ├── step-6.png ├── step-7.png ├── step-8.png ├── step-9.png └── thequickbrownfoxjumpsoverthelazydog.png ├── project.clj ├── src └── eodermdrome │ ├── core.clj │ ├── graph.clj │ └── parser.clj └── test └── eodermdrome ├── core_test.clj └── parser_test.clj /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | *.class 8 | /.lein-* 9 | /.nrepl-port 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC 2 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM 3 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 4 | 5 | 1. DEFINITIONS 6 | 7 | "Contribution" means: 8 | 9 | a) in the case of the initial Contributor, the initial code and 10 | documentation distributed under this Agreement, and 11 | 12 | b) in the case of each subsequent Contributor: 13 | 14 | i) changes to the Program, and 15 | 16 | ii) additions to the Program; 17 | 18 | where such changes and/or additions to the Program originate from and are 19 | distributed by that particular Contributor. A Contribution 'originates' from 20 | a Contributor if it was added to the Program by such Contributor itself or 21 | anyone acting on such Contributor's behalf. Contributions do not include 22 | additions to the Program which: (i) are separate modules of software 23 | distributed in conjunction with the Program under their own license 24 | agreement, and (ii) are not derivative works of the Program. 25 | 26 | "Contributor" means any person or entity that distributes the Program. 27 | 28 | "Licensed Patents" mean patent claims licensable by a Contributor which are 29 | necessarily infringed by the use or sale of its Contribution alone or when 30 | combined with the Program. 31 | 32 | "Program" means the Contributions distributed in accordance with this 33 | Agreement. 34 | 35 | "Recipient" means anyone who receives the Program under this Agreement, 36 | including all Contributors. 37 | 38 | 2. GRANT OF RIGHTS 39 | 40 | a) Subject to the terms of this Agreement, each Contributor hereby grants 41 | Recipient a non-exclusive, worldwide, royalty-free copyright license to 42 | reproduce, prepare derivative works of, publicly display, publicly perform, 43 | distribute and sublicense the Contribution of such Contributor, if any, and 44 | such derivative works, in source code and object code form. 45 | 46 | b) Subject to the terms of this Agreement, each Contributor hereby grants 47 | Recipient a non-exclusive, worldwide, royalty-free patent license under 48 | Licensed Patents to make, use, sell, offer to sell, import and otherwise 49 | transfer the Contribution of such Contributor, if any, in source code and 50 | object code form. This patent license shall apply to the combination of the 51 | Contribution and the Program if, at the time the Contribution is added by the 52 | Contributor, such addition of the Contribution causes such combination to be 53 | covered by the Licensed Patents. The patent license shall not apply to any 54 | other combinations which include the Contribution. No hardware per se is 55 | licensed hereunder. 56 | 57 | c) Recipient understands that although each Contributor grants the licenses 58 | to its Contributions set forth herein, no assurances are provided by any 59 | Contributor that the Program does not infringe the patent or other 60 | intellectual property rights of any other entity. Each Contributor disclaims 61 | any liability to Recipient for claims brought by any other entity based on 62 | infringement of intellectual property rights or otherwise. As a condition to 63 | exercising the rights and licenses granted hereunder, each Recipient hereby 64 | assumes sole responsibility to secure any other intellectual property rights 65 | needed, if any. For example, if a third party patent license is required to 66 | allow Recipient to distribute the Program, it is Recipient's responsibility 67 | to acquire that license before distributing the Program. 68 | 69 | d) Each Contributor represents that to its knowledge it has sufficient 70 | copyright rights in its Contribution, if any, to grant the copyright license 71 | set forth in this Agreement. 72 | 73 | 3. REQUIREMENTS 74 | 75 | A Contributor may choose to distribute the Program in object code form under 76 | its own license agreement, provided that: 77 | 78 | a) it complies with the terms and conditions of this Agreement; and 79 | 80 | b) its license agreement: 81 | 82 | i) effectively disclaims on behalf of all Contributors all warranties and 83 | conditions, express and implied, including warranties or conditions of title 84 | and non-infringement, and implied warranties or conditions of merchantability 85 | and fitness for a particular purpose; 86 | 87 | ii) effectively excludes on behalf of all Contributors all liability for 88 | damages, including direct, indirect, special, incidental and consequential 89 | damages, such as lost profits; 90 | 91 | iii) states that any provisions which differ from this Agreement are offered 92 | by that Contributor alone and not by any other party; and 93 | 94 | iv) states that source code for the Program is available from such 95 | Contributor, and informs licensees how to obtain it in a reasonable manner on 96 | or through a medium customarily used for software exchange. 97 | 98 | When the Program is made available in source code form: 99 | 100 | a) it must be made available under this Agreement; and 101 | 102 | b) a copy of this Agreement must be included with each copy of the Program. 103 | 104 | Contributors may not remove or alter any copyright notices contained within 105 | the Program. 106 | 107 | Each Contributor must identify itself as the originator of its Contribution, 108 | if any, in a manner that reasonably allows subsequent Recipients to identify 109 | the originator of the Contribution. 110 | 111 | 4. COMMERCIAL DISTRIBUTION 112 | 113 | Commercial distributors of software may accept certain responsibilities with 114 | respect to end users, business partners and the like. While this license is 115 | intended to facilitate the commercial use of the Program, the Contributor who 116 | includes the Program in a commercial product offering should do so in a 117 | manner which does not create potential liability for other Contributors. 118 | Therefore, if a Contributor includes the Program in a commercial product 119 | offering, such Contributor ("Commercial Contributor") hereby agrees to defend 120 | and indemnify every other Contributor ("Indemnified Contributor") against any 121 | losses, damages and costs (collectively "Losses") arising from claims, 122 | lawsuits and other legal actions brought by a third party against the 123 | Indemnified Contributor to the extent caused by the acts or omissions of such 124 | Commercial Contributor in connection with its distribution of the Program in 125 | a commercial product offering. The obligations in this section do not apply 126 | to any claims or Losses relating to any actual or alleged intellectual 127 | property infringement. In order to qualify, an Indemnified Contributor must: 128 | a) promptly notify the Commercial Contributor in writing of such claim, and 129 | b) allow the Commercial Contributor tocontrol, and cooperate with the 130 | Commercial Contributor in, the defense and any related settlement 131 | negotiations. The Indemnified Contributor may participate in any such claim 132 | at its own expense. 133 | 134 | For example, a Contributor might include the Program in a commercial product 135 | offering, Product X. That Contributor is then a Commercial Contributor. If 136 | that Commercial Contributor then makes performance claims, or offers 137 | warranties related to Product X, those performance claims and warranties are 138 | such Commercial Contributor's responsibility alone. Under this section, the 139 | Commercial Contributor would have to defend claims against the other 140 | Contributors related to those performance claims and warranties, and if a 141 | court requires any other Contributor to pay any damages as a result, the 142 | Commercial Contributor must pay those damages. 143 | 144 | 5. NO WARRANTY 145 | 146 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON 147 | AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER 148 | EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR 149 | CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A 150 | PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the 151 | appropriateness of using and distributing the Program and assumes all risks 152 | associated with its exercise of rights under this Agreement , including but 153 | not limited to the risks and costs of program errors, compliance with 154 | applicable laws, damage to or loss of data, programs or equipment, and 155 | unavailability or interruption of operations. 156 | 157 | 6. DISCLAIMER OF LIABILITY 158 | 159 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY 160 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, 161 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION 162 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 163 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 164 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 165 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY 166 | OF SUCH DAMAGES. 167 | 168 | 7. GENERAL 169 | 170 | If any provision of this Agreement is invalid or unenforceable under 171 | applicable law, it shall not affect the validity or enforceability of the 172 | remainder of the terms of this Agreement, and without further action by the 173 | parties hereto, such provision shall be reformed to the minimum extent 174 | necessary to make such provision valid and enforceable. 175 | 176 | If Recipient institutes patent litigation against any entity (including a 177 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself 178 | (excluding combinations of the Program with other software or hardware) 179 | infringes such Recipient's patent(s), then such Recipient's rights granted 180 | under Section 2(b) shall terminate as of the date such litigation is filed. 181 | 182 | All Recipient's rights under this Agreement shall terminate if it fails to 183 | comply with any of the material terms or conditions of this Agreement and 184 | does not cure such failure in a reasonable period of time after becoming 185 | aware of such noncompliance. If all Recipient's rights under this Agreement 186 | terminate, Recipient agrees to cease use and distribution of the Program as 187 | soon as reasonably practicable. However, Recipient's obligations under this 188 | Agreement and any licenses granted by Recipient relating to the Program shall 189 | continue and survive. 190 | 191 | Everyone is permitted to copy and distribute copies of this Agreement, but in 192 | order to avoid inconsistency the Agreement is copyrighted and may only be 193 | modified in the following manner. The Agreement Steward reserves the right to 194 | publish new versions (including revisions) of this Agreement from time to 195 | time. No one other than the Agreement Steward has the right to modify this 196 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The 197 | Eclipse Foundation may assign the responsibility to serve as the Agreement 198 | Steward to a suitable separate entity. Each new version of the Agreement will 199 | be given a distinguishing version number. The Program (including 200 | Contributions) may always be distributed subject to the version of the 201 | Agreement under which it was received. In addition, after a new version of 202 | the Agreement is published, Contributor may elect to distribute the Program 203 | (including its Contributions) under the new version. Except as expressly 204 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or 205 | licenses to the intellectual property of any Contributor under this 206 | Agreement, whether expressly, by implication, estoppel or otherwise. All 207 | rights in the Program not expressly granted under this Agreement are 208 | reserved. 209 | 210 | This Agreement is governed by the laws of the State of New York and the 211 | intellectual property laws of the United States of America. No party to this 212 | Agreement will bring a legal action under this Agreement more than one year 213 | after the cause of action arose. Each party waives its rights to a jury trial 214 | in any resulting litigation. 215 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # eodermdrome 2 | 3 | An experimental implementation of the esolang [Eodermdrome](https://esolangs.org/wiki/Eodermdrome) in Clojure, which is a [Kolmogorov-Uspensky machine](https://esolangs.org/wiki/Kolmogorov_machine). Inspiration taken from the [python version](https://github.com/tadeboro/eodermdrome). 4 | 5 | The Kolmogorov-Uspensky machine, (KUM), is an abstract machine. Abstract machines were introduced to Computer Science as a mean of formalizing the concept of an _algorithm_. The most familiar example of this is the [Turing Machine](https://en.wikipedia.org/wiki/Turing_machine) where the machine operates on an infinite memory tape dived into cells. Both the Turning Machine and the KUM are in the computational class and considered Turing-complete. The difference between them is in the storage. In the KUM, the tape can change its topology. 6 | 7 | The KUM storage is a finite connected undirected graph with an active node that is associated with the active region, or neighborhood, of the graph. The control of the machine reacts configuration of the graph and then modifies it by rewriting it. 8 | 9 | An Eodermdrome program creates graphs from a string of letters. For example the graph of _abcdae_ would produce 10 | 11 | ![abcdae](./images/abcdae.png) 12 | 13 | The program itself consists of series of commands or rules. The command will be executed if the following prereqs are met: 14 | * The match graph in the command is a subgraph of the system graph. 15 | * If an input set is part of the command, the input character read of the system input must match it. 16 | 17 | A command is of the form: 18 | 19 | * **match-graph graph-replacement** 20 | * This will execute if the match-graph is a subgraph and then transform the match to the replacement. 21 | * Example:`a abc` 22 | * **(input-set) match-graph graph-replacement**. 23 | * This will execute if the match is a subgraph and if the next character of the system input matches. On executing, it will read one char from the input and then transform the match graph with the replacement. 24 | * Example: `(1) a abc` 25 | * **match-graph (output) graph-replacement.** 26 | * This will execute if the match-graph is a subgraph. On executing, it will print the output to the system and transform the match with the replacement. 27 | * Example: `a (1) abc` 28 | * **(input-set) match-graph (output) graph-replacement.** 29 | * This will execute if the match is a subgraph and if the next character of the system input matches. On executing, it will read one char from the input, print the output to the system, and then transform the match graph with the replacement. 30 | * Example: `(0) a (1) abc` 31 | 32 | Comments are also allowed as text in between commas. In this implementation, they must be contained on a single line. Example: `,this is a comment,(1) a abc` 33 | 34 | The initial state of the graph with a program is the denoted by the graph string _thequickbrownfoxjumpsoverthelazydog_. 35 | 36 | ![thequickbrownfoxjumpsoverthelazydog](./images/thequickbrownfoxjumpsoverthelazydog.png) 37 | 38 | 39 | Example program for adding two string of ones together separated by zeros. 40 | 41 | ``` 42 | ,takes input of ones seperated by zeros and adds the ones, thequickbrownfoxjumpsoverthelazydog a 43 | (1) a ab 44 | (0) a a 45 | ab (1) a 46 | ``` 47 | 48 | Given a system input of "11011", it will print out "1111". 49 | 50 | ## Usage 51 | 52 | It can be run from the command line with `lein run program-file input-string`. There is also an optional debug flag, which if used, will cause graph png files to be placed in the home directory to document all the graph transformations. 53 | 54 | Examples: 55 | 56 | ``` 57 | $ lein run examples/adder.eo "11011" 58 | Running program examples/adder.eo with input 11011 59 | 1111 60 | ``` 61 | 62 | The doubler.eo example program takes in a string of ones and doubles them 63 | 64 | ``` 65 | lein run examples/doubler.eo "111" 66 | Running program examples/doubler.eo with input 111 67 | 111111 68 | ``` 69 | 70 | using debug option 71 | 72 | ``` 73 | lein run examples/adder.eo "101" debug 74 | ``` 75 | 76 | FIXME 77 | 78 | ## References/ Further Reading 79 | * [My blog post that walks through an example](http://gigasquidsoftware.com/blog/2016/03/16/kolmogorov-uspensky-machine/) 80 | * [On Kolmogorov Machines And Related Issues](http://research.microsoft.com/en-us/um/people/gurevich/opera/78.pdf) 81 | * [Kolmogorov's Heritage in Mathematics](https://books.google.com/books?id=SpTv44Ia-J0C&pg=PA284&lpg=PA284&dq=active+node+kolmogorov+uspensky+machine&source=bl&ots=uQQSLaaKOS&sig=9-V_m8z-Yh9zlzy6vX9MplGMbjw&hl=en&sa=X&ved=0ahUKEwjy8820rMDLAhVByYMKHWP5A8oQ6AEILDAC#v=onepage&q=active%20node%20kolmogorov%20uspensky%20machine&f=false) 82 | * [What is a "pointer machine"](http://dl.acm.org/citation.cfm?id=202846) 83 | 84 | 85 | ## License 86 | 87 | Copyright © 2016 Carin Meier 88 | 89 | Distributed under the Eclipse Public License either version 1.0 or (at 90 | your option) any later version. 91 | -------------------------------------------------------------------------------- /doc/intro.md: -------------------------------------------------------------------------------- 1 | # Introduction to eodermdrome 2 | 3 | TODO: write [great documentation](http://jacobian.org/writing/what-to-write/) 4 | -------------------------------------------------------------------------------- /examples/adder.eo: -------------------------------------------------------------------------------- 1 | ,takes input of ones seperated by zeros and adds the ones, thequickbrownfoxjumpsoverthelazydog a 2 | (1) a ab 3 | (0) a a 4 | ab (1) a -------------------------------------------------------------------------------- /examples/doubler.eo: -------------------------------------------------------------------------------- 1 | ,takes input of string of ones and doubles them,thequickbrownfoxjumpsoverthelazydog a 2 | (1) a ab 3 | ab (1) abc 4 | abc (1) a -------------------------------------------------------------------------------- /images/abcdae.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gigasquid/eodermdrome/681b9205a8d27c86c72e5a22ae5d82496cbd29f0/images/abcdae.png -------------------------------------------------------------------------------- /images/step-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gigasquid/eodermdrome/681b9205a8d27c86c72e5a22ae5d82496cbd29f0/images/step-1.png -------------------------------------------------------------------------------- /images/step-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gigasquid/eodermdrome/681b9205a8d27c86c72e5a22ae5d82496cbd29f0/images/step-2.png -------------------------------------------------------------------------------- /images/step-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gigasquid/eodermdrome/681b9205a8d27c86c72e5a22ae5d82496cbd29f0/images/step-3.png -------------------------------------------------------------------------------- /images/step-4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gigasquid/eodermdrome/681b9205a8d27c86c72e5a22ae5d82496cbd29f0/images/step-4.png -------------------------------------------------------------------------------- /images/step-5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gigasquid/eodermdrome/681b9205a8d27c86c72e5a22ae5d82496cbd29f0/images/step-5.png -------------------------------------------------------------------------------- /images/step-6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gigasquid/eodermdrome/681b9205a8d27c86c72e5a22ae5d82496cbd29f0/images/step-6.png -------------------------------------------------------------------------------- /images/step-7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gigasquid/eodermdrome/681b9205a8d27c86c72e5a22ae5d82496cbd29f0/images/step-7.png -------------------------------------------------------------------------------- /images/step-8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gigasquid/eodermdrome/681b9205a8d27c86c72e5a22ae5d82496cbd29f0/images/step-8.png -------------------------------------------------------------------------------- /images/step-9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gigasquid/eodermdrome/681b9205a8d27c86c72e5a22ae5d82496cbd29f0/images/step-9.png -------------------------------------------------------------------------------- /images/thequickbrownfoxjumpsoverthelazydog.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gigasquid/eodermdrome/681b9205a8d27c86c72e5a22ae5d82496cbd29f0/images/thequickbrownfoxjumpsoverthelazydog.png -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject eodermdrome "0.1.0-SNAPSHOT" 2 | :description "FIXME: write description" 3 | :url "http://example.com/FIXME" 4 | :license {:name "Eclipse Public License" 5 | :url "http://www.eclipse.org/legal/epl-v10.html"} 6 | :dependencies [[org.clojure/clojure "1.8.0"] 7 | [aysylu/loom "0.5.4"] 8 | [instaparse "1.4.1"]] 9 | :main eodermdrome.core) 10 | -------------------------------------------------------------------------------- /src/eodermdrome/core.clj: -------------------------------------------------------------------------------- 1 | (ns eodermdrome.core 2 | (:require [eodermdrome.parser :as parser] 3 | [eodermdrome.graph :as eg] 4 | [loom.graph :refer [graph edges directed? nodes remove-edges 5 | remove-nodes add-nodes add-edges] :as graph] 6 | [loom.io :as lio] 7 | [clojure.java.io :refer [output-stream]] 8 | (:gen-class))) 9 | 10 | (def ^:dynamic *debug* false) 11 | (def png-counter (atom 0)) 12 | 13 | (defn input-set-match? [input-set system-input] 14 | (if input-set 15 | (= (str (first system-input)) input-set) 16 | true)) 17 | 18 | (defn transform-graph [g-sys {:keys [input output g-in g-out]}] 19 | (let [in-nodes (nodes g-in) 20 | in-edges (edges g-in) 21 | out-nodes (nodes g-out) 22 | out-edges (edges g-out) 23 | closed-nodes-match (clojure.set/difference in-nodes out-nodes) 24 | closed-nodes-replacement (clojure.set/difference out-nodes in-nodes)] 25 | (-> g-sys 26 | ((fn [g] (apply remove-edges g in-edges))) 27 | ((fn [g] (apply remove-nodes g closed-nodes-match))) 28 | ((fn [g] (apply add-nodes g closed-nodes-replacement))) 29 | ((fn [g] (apply add-edges g out-edges)))))) 30 | 31 | (defn run-command [g-sys {:keys [input output g-in g-out] :as cmd} system-input system-output] 32 | (when (and (input-set-match? input system-input) 33 | (eg/subgraph? g-in g-sys)) 34 | (when output 35 | (print output)) 36 | {:system-input (if input (apply str (rest system-input)) system-input) 37 | :system-output (if output (conj system-output output) system-output) 38 | :g (transform-graph g-sys cmd)})) 39 | 40 | (defn run-commands [cmds g input output] 41 | (reduce (fn [result cmd] 42 | (let [last-result (last result) 43 | rg (:g last-result) 44 | rs (:system-input last-result) 45 | ro (:system-output last-result) 46 | run (run-command rg cmd rs ro)] 47 | (if run 48 | (conj result run) 49 | result))) 50 | [{:g g :system-input input :system-output output}] 51 | cmds)) 52 | 53 | (defn write-png [g] 54 | (with-open [out (output-stream (str "step-" (swap! png-counter inc) ".png"))] 55 | (.write out (lio/render-to-bytes g)))) 56 | 57 | (defn run-program [cmds sys-input] 58 | (loop [g (eg/make-graph "thequickbrownfoxjumpsoverthelazydog") 59 | input sys-input 60 | output nil 61 | matches? true] 62 | (if (false? matches?) 63 | (do 64 | (flush) 65 | {:g g :input input :output output}) 66 | (let [run-results (run-commands cmds g input output) 67 | match (> (count run-results) 1) 68 | last-run (last run-results)] 69 | (when *debug* 70 | (mapv write-png (mapv :g run-results))) 71 | (recur (:g last-run) (:system-input last-run) (:system-output last-run) match))))) 72 | 73 | (defn run [program input] 74 | (let [cmds (parser/parse program)] 75 | (run-program cmds input))) 76 | 77 | (defn -main [& args] 78 | (when (not (or (= 3 (count args)) (= 2 (count args)))) 79 | (println "Usage: file input (Example add.eo \"11100111\" or add.eo \"101\" debug)") 80 | (println "debug mode will create pngs of all the graph states") 81 | (System/exit 1)) 82 | (do (let [[file input debug] args] 83 | (println "Running program " file " with input " input) 84 | (if debug 85 | (binding [*debug* true] 86 | (run (slurp file) input)) 87 | (run (slurp file) input)) 88 | (println) 89 | (System/exit 0)))) 90 | -------------------------------------------------------------------------------- /src/eodermdrome/graph.clj: -------------------------------------------------------------------------------- 1 | (ns eodermdrome.graph 2 | (:require [loom.graph :refer [graph edges directed? nodes] :as graph])) 3 | 4 | (defn graph-edges [graph-str] 5 | (let [nodes (map (comp keyword str) graph-str)] 6 | (if (= (count nodes) 1) 7 | (set nodes) 8 | (into (set (partition 2 nodes)) (partition 2 (rest nodes)))))) 9 | 10 | (defn make-graph [graph-str] 11 | (apply graph (graph-edges graph-str))) 12 | 13 | ;;; 14 | ;;; taken from the HEAD version of loom to compare graphs 15 | ;;; 16 | (defn subgraph? 17 | "Returns true iff g1 is a subgraph of g2. An undirected graph is never 18 | considered as a subgraph of a directed graph and vice versa." 19 | [g1 g2] 20 | (and (= (directed? g1) (directed? g2)) 21 | (let [edge-test-fn (if (directed? g1) 22 | graph/has-edge? 23 | (fn [g x y] 24 | (or (graph/has-edge? g x y) 25 | (graph/has-edge? g y x))))] 26 | (and (every? #(graph/has-node? g2 %) (nodes g1)) 27 | (every? (fn [[x y]] (edge-test-fn g2 x y)) 28 | (edges g1)))))) 29 | -------------------------------------------------------------------------------- /src/eodermdrome/parser.clj: -------------------------------------------------------------------------------- 1 | (ns eodermdrome.parser 2 | (:require [eodermdrome.graph :as eg] 3 | [clojure.set :as set] 4 | [instaparse.core :as insta])) 5 | 6 | (def parser (insta/parser 7 | "program = cmdline (<'\n'> cmdline)* 8 | cmdline = ( *)* cmd (* )* 9 | cmd = cmd-part cmd-part 10 | cmd-part = (io )* graph 11 | io = <'('> #'[A-z0-9\\s]*'+ <')'> 12 | seperator = space | space comment space 13 | comment = #'\\,.*\\,' 14 | space = #'[\\s]'+ 15 | graph = #'[a-z]*'+")) 16 | 17 | (def transform-options 18 | {:program vector 19 | :cmdline identity 20 | :cmd-part vector 21 | :cmd (fn [& v] 22 | (let [[in out] v 23 | input (:io (first (filter :io in))) 24 | output (:io (first (filter :io out))) 25 | g-in (:g (first (filter :g in))) 26 | g-out (:g (first (filter :g out)))] 27 | {:input input 28 | :output output 29 | :g-in g-in 30 | :g-out g-out})) 31 | :graph (fn [v] {:g (eg/make-graph v)}) 32 | :io (fn [v] {:io (str v)})}) 33 | 34 | (defn parse [input] 35 | (->> (parser input) (insta/transform transform-options))) 36 | -------------------------------------------------------------------------------- /test/eodermdrome/core_test.clj: -------------------------------------------------------------------------------- 1 | (ns eodermdrome.core-test 2 | (:require [clojure.test :refer :all] 3 | [eodermdrome.core :refer :all] 4 | [eodermdrome.graph :as eg] 5 | [eodermdrome.parser :as parser] 6 | [loom.graph :refer [edges nodes] :as graph])) 7 | 8 | 9 | (deftest test-run-comment 10 | (testing "with two graphs" 11 | (let [result (run-command (eg/make-graph "abc") (first (parser/parse "bc bcd")) nil nil)] 12 | (is (= #{:c :b :d :a} (nodes (:g result)))) 13 | (is (= #{[:c :b] [:c :d] [:b :c] [:b :a] [:d :c] [:a :b]} 14 | (set (edges (:g result))))) 15 | (is (= nil (:system-input result))))) 16 | (testing "with two graphs and an input with matching system input" 17 | (let [result (run-command (eg/make-graph "abc") (first (parser/parse "(1) bc bcd")) "111" nil)] 18 | (is (= #{:c :b :d :a} (nodes (:g result)))) 19 | (is (= #{[:c :b] [:c :d] [:b :c] [:b :a] [:d :c] [:a :b]} 20 | (set (edges (:g result))))) 21 | (is (= "11" (:system-input result))))) 22 | (testing "with two graphs and an input with non matching system input" 23 | (let [result (run-command (eg/make-graph "abc") (first (parser/parse "(0) bc bcd")) "111" nil)] 24 | (nil? result))) 25 | (testing "with two graphs and an input and an output with matching system input" 26 | (let [result (run-command (eg/make-graph "abc") (first (parser/parse "(1) bc (0) bcd")) "111" nil)] 27 | (is (= #{:c :b :d :a} (nodes (:g result)))) 28 | (is (= #{[:c :b] [:c :d] [:b :c] [:b :a] [:d :c] [:a :b]} 29 | (set (edges (:g result))))) 30 | (is (= "11" (:system-input result))) 31 | (is (= ["0"] (:system-output result))))) 32 | (testing "with two graphs and an output" 33 | (let [result (run-command (eg/make-graph "abc") (first (parser/parse "bc (0) bcd")) "111" nil)] 34 | (is (= #{:c :b :d :a} (nodes (:g result)))) 35 | (is (= #{[:c :b] [:c :d] [:b :c] [:b :a] [:d :c] [:a :b]} 36 | (set (edges (:g result))))) 37 | (is (= "111" (:system-input result))) 38 | (is (= ["0"] (:system-output result)))))) 39 | 40 | (def add-program 41 | "thequickbrownfoxjumpsoverthelazydog a 42 | (1) a ab 43 | (0) a a 44 | ab (1) a") 45 | 46 | (deftest test-run 47 | (testing "adding two binary strings together sepearted by zero" 48 | (let [program add-program 49 | result (run program "1110111")] 50 | (is (= "111111" (apply str (:output result)))))) 51 | (testing "with multiple zeros " 52 | (let [program add-program 53 | result (run program "111000111")] 54 | (is (= "111111" (apply str (:output result)))))) 55 | (testing "with just ones " 56 | (let [program add-program 57 | result (run program "111")] 58 | (is (= "111" (apply str (:output result))))))) 59 | 60 | (deftest test-file-programs 61 | (testing "adder" 62 | (let [program (slurp "examples/adder.eo") 63 | result (run program "1110011")] 64 | (is (= "11111" (apply str (:output result)))))) 65 | (testing "incrementer" 66 | (let [program (slurp "examples/doubler.eo") 67 | result (run program "11")] 68 | (is (= "1111" (apply str (:output result))))))) 69 | -------------------------------------------------------------------------------- /test/eodermdrome/parser_test.clj: -------------------------------------------------------------------------------- 1 | (ns eodermdrome.parser-test 2 | (:require [eodermdrome.parser :refer :all] 3 | [clojure.test :refer :all] 4 | [loom.graph :refer [nodes]])) 5 | 6 | (deftest test-parse 7 | (testing "two graphs and an input and an output" 8 | (let [cmd "(2) ab (1) a"] 9 | (is (= "2" (:input (first (parse cmd))))) 10 | (is (= "1" (:output (first (parse cmd))))) 11 | (is (= #{:b :a} (nodes (:g-in (first (parse cmd)))))) 12 | (is (= #{:a} (nodes (:g-out (first (parse cmd)))))))) 13 | (testing "two graphs and an input" 14 | (let [cmd "(2) ab a"] 15 | (is (= "2" (:input (first (parse cmd))))) 16 | (is (nil? (:output (first (parse cmd))))) 17 | (is (= #{:b :a} (nodes (:g-in (first (parse cmd)))))) 18 | (is (= #{:a} (nodes (:g-out (first (parse cmd)))))))) 19 | (testing "two graphs and an output" 20 | (let [cmd "ab (1 text) a"] 21 | (is (nil? (:input (first (parse cmd))))) 22 | (is (= "1 text" (:output (first (parse cmd))))) 23 | (is (= #{:b :a} (nodes (:g-in (first (parse cmd)))))) 24 | (is (= #{:a} (nodes (:g-out (first (parse cmd)))))))) 25 | (testing "two graphs" 26 | (let [cmd "ab a"] 27 | (is (nil? (:input (first (parse cmd))))) 28 | (is (nil? (:output (first (parse cmd))))) 29 | (is (= #{:b :a} (nodes (:g-in (first (parse cmd)))))) 30 | (is (= #{:a} (nodes (:g-out (first (parse cmd))))))))) 31 | 32 | (deftest test-parse-with-comments 33 | (testing "comment at the beginning" 34 | (let [cmd ",hi, (2) ab (1) a"] 35 | (is (= "2" (:input (first (parse cmd))))) 36 | (is (= "1" (:output (first (parse cmd))))) 37 | (is (= #{:b :a} (nodes (:g-in (first (parse cmd)))))) 38 | (is (= #{:a} (nodes (:g-out (first (parse cmd)))))))) 39 | (testing "comment at the end" 40 | (let [cmd "(2) ab (1) a ,comment,"] 41 | (is (= "2" (:input (first (parse cmd))))) 42 | (is (= "1" (:output (first (parse cmd))))) 43 | (is (= #{:b :a} (nodes (:g-in (first (parse cmd)))))) 44 | (is (= #{:a} (nodes (:g-out (first (parse cmd)))))))) 45 | (testing "comment in the middel" 46 | (let [cmd "(2) ab ,comment, (1) a"] 47 | (is (= "2" (:input (first (parse cmd))))) 48 | (is (= "1" (:output (first (parse cmd))))) 49 | (is (= #{:b :a} (nodes (:g-in (first (parse cmd)))))) 50 | (is (= #{:a} (nodes (:g-out (first (parse cmd))))))))) 51 | 52 | (deftest test-parse-with-newlines 53 | (let [program "(2) ab (1) a\n(3) a b\nd e" 54 | cmds (parse program)] 55 | (is (= 3 (count cmds))))) 56 | --------------------------------------------------------------------------------