This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatom.go
161 lines (131 loc) · 3.13 KB
/
atom.go
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package alchemist
import (
"fmt"
"log"
"strings"
"unicode"
"unicode/utf8"
)
// Atom is an interface that declares certain types as atoms.
type Atom interface {
Parser
Run(Universe)
}
// SimpleAtom represents a simple atom in the universe.
type SimpleAtom string
// Parse parses a SimpleAtom.
func (a *SimpleAtom) Parse(str string) error {
str = strings.TrimSpace(str)
if strings.HasPrefix(str, "In_") || strings.HasPrefix(str, "Out_") {
return &SyntaxError{}
}
first, _ := utf8.DecodeRune([]byte(str))
if first == utf8.RuneError {
return &SyntaxError{}
}
if !unicode.IsLetter(first) && first != '_' {
return &SyntaxError{}
}
lastIndex := strings.IndexFunc(str, func(r rune) bool {
return !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '_'
})
if lastIndex == -1 {
lastIndex = len(str)
}
*a = SimpleAtom(str[:lastIndex])
return nil
}
// Run defines what this atom will do in the universe.
func (a *SimpleAtom) Run(u Universe) {
u[*a]++
}
func (a *SimpleAtom) String() string {
return string(*a)
}
// InAtom represents an atom used on the RHS used to add a number
// of atoms to the universe, based on standard input.
type InAtom struct {
AddTo SimpleAtom
}
// Parse parses an InAtom. Always returns SyntaxError.
func (a *InAtom) Parse(str string) error {
str = strings.TrimSpace(str)
const prefix = "In_"
n := strings.Index(str, prefix)
if n != 0 {
return &SyntaxError{}
}
rest := str[len(prefix):]
err := a.AddTo.Parse(rest)
if err != nil {
return err
}
return nil
}
func (a *InAtom) String() string {
return "In_" + a.AddTo.String()
}
// Run defines what this atom will do in the universe.
func (a *InAtom) Run(u Universe) {
var n int
_, err := fmt.Scan(&n)
if err != nil {
log.Fatalln(err)
}
u[a.AddTo] += n
}
// OutStrLitAtom represents an atom used on the RHS to output
// a string literal.
type OutStrLitAtom struct {
Printing string
}
// Parse parses an OutStrLitAtom.
func (a *OutStrLitAtom) Parse(str string) error {
str = strings.TrimSpace(str)
const prefix = "Out_\""
n := strings.Index(str, prefix)
if n != 0 {
return &SyntaxError{}
}
rest := str[len(prefix):]
closingQuote := strings.IndexByte(rest, '"')
if closingQuote == -1 {
return &SyntaxError{}
}
a.Printing = rest[:closingQuote]
return nil
}
// Run defines what this atom will do in the universe.
func (a *OutStrLitAtom) Run(u Universe) {
fmt.Println(a.Printing)
}
func (a *OutStrLitAtom) String() string {
return fmt.Sprintf("Out_%q", a.Printing)
}
// OutSimpleAtom represents an atom used on the RHS to output
// the number of a given atom in the universe.
type OutSimpleAtom struct {
Output SimpleAtom
}
// Parse parses an OutSimpleAtom.
func (a *OutSimpleAtom) Parse(str string) error {
str = strings.TrimSpace(str)
const prefix = "Out_"
n := strings.Index(str, prefix)
if n != 0 {
return &SyntaxError{}
}
rest := str[len(prefix):]
err := a.Output.Parse(rest)
if err != nil {
return err
}
return nil
}
// Run defines what this atom will do in the universe.
func (a *OutSimpleAtom) Run(u Universe) {
fmt.Println(u[a.Output])
}
func (a *OutSimpleAtom) String() string {
return "Out_" + a.Output.String()
}