Created
July 14, 2013 18:32
-
-
Save jordanorelli/5995251 to your computer and use it in GitHub Desktop.
simple DOL-System
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"fmt" | |
) | |
type ProductionSet map[rune]string | |
type Alphabet map[rune]bool | |
type DOLSystem struct { | |
Alphabet | |
Axiom string | |
Productions ProductionSet | |
} | |
func (s *DOLSystem) Next() string { | |
var buf bytes.Buffer | |
for _, r := range s.Axiom { | |
p, ok := s.Productions[r] | |
if !ok { | |
panic("no production exists for rune " + string(r)) | |
} | |
buf.WriteString(p) | |
} | |
n := buf.String() | |
s.Axiom = n | |
return n | |
} | |
func NewDOLSystem(V Alphabet, axiom string, P ProductionSet) *DOLSystem { | |
return &DOLSystem{V, axiom, P} | |
} | |
func main() { | |
s := DOLSystem{ | |
Alphabet{'a': true, 'b': true}, | |
"a", | |
ProductionSet{'a': "ab", 'b': "a"}, | |
} | |
for i := 0; i < 5; i++ { | |
fmt.Println(s.Next()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment