summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/thue/program.cr12
-rw-r--r--src/thue/rule.cr12
2 files changed, 17 insertions, 7 deletions
diff --git a/src/thue/program.cr b/src/thue/program.cr
index c59780b..f449f5a 100644
--- a/src/thue/program.cr
+++ b/src/thue/program.cr
@@ -6,8 +6,16 @@ class Thue::Program
end
def interpret
- something = @parser.parse
- puts something
+ rules, state = @parser.parse
+
+ loop do
+ r = rules.find { |r| r.matches?(state) }
+ break if r.nil?
+
+ state = state.sub(r.pattern, r.replacement)
+ end
+
+ puts state
end
end
diff --git a/src/thue/rule.cr b/src/thue/rule.cr
index 34c2396..cd45adc 100644
--- a/src/thue/rule.cr
+++ b/src/thue/rule.cr
@@ -1,14 +1,16 @@
require "./thue.cr"
abstract struct Thue::Rule
- @pattern : Regex
+ @regex : Regex
- def initialize(pattern : String)
- @pattern = Regex.new(pattern)
+ getter pattern : String
+
+ def initialize(@pattern : String)
+ @regex = Regex.new(Regex.escape(@pattern))
end
def matches?(str : String) : Bool
- str.matches?(@pattern)
+ str.matches?(@regex)
end
abstract def replacement : String
@@ -26,7 +28,7 @@ end
struct Thue::InputRule < Thue::Rule
def replacement : String
- gets
+ gets || ""
end
end