Website Summary Files Commits Branches @commit @diff @stats
path: root / homework / 81319 / bfinterpreter / bf / mem.go
diff options
context:
space:
mode:
Commitc0567e2492ece720f87d1b5f74abce86fad815dc (patch)
Author
 Georgi Stefanov <geosteffanov@gmail.com>
2018-11-29 15:03:08 +0200
Committer
 dtrendafilov <trendafilov.dn@gmail.com>
2018-11-29 15:03:08 +0200
Files48669f35b66d15eb29cb26d29ab6e03a8b4a142a /homework/81319/bfinterpreter/bf/mem.go
IPL-c0567e2492ece720f87d1b5f74abce86fad815dc.tar
IPL-c0567e2492ece720f87d1b5f74abce86fad815dc.tar.gz
IPL-c0567e2492ece720f87d1b5f74abce86fad815dc.zip
Parent99a9f9389a41a1df544ab51fc0170e48f10e34c8 (diff)
Add brainfuck interpreter (#65)
Not sure if it builds correctly. You have to have go compiler.
1 files changed, 40 insertions, 0 deletions
-rw-r--r--homework/81319/bfinterpreter/bf/mem.go40
diff --git a/homework/81319/bfinterpreter/bf/mem.go b/homework/81319/bfinterpreter/bf/mem.go
new file mode 100644
index 0000000..8679584
--- /dev/null
+++ b/homework/81319/bfinterpreter/bf/mem.go
@@ -0,0 +1,40 @@
+package bf
+
+const (
+ MEMORY_SIZE uint = 1024
+)
+
+type memoryState struct {
+ memory [MEMORY_SIZE]cell
+ currentCellIdx uint
+}
+
+func (m *memoryState) incrementPtr() {
+ if m.currentCellIdx == MEMORY_SIZE {
+ return
+ }
+ m.currentCellIdx += 1
+}
+
+func (m *memoryState) decrementPtr() {
+ if m.currentCellIdx == 0 {
+ return
+ }
+ m.currentCellIdx -= 1
+}
+
+func (m *memoryState) incrementCell() {
+ m.memory[m.currentCellIdx].increment()
+}
+
+func (m *memoryState) decerementCell() {
+ m.memory[m.currentCellIdx].decrement()
+}
+
+func (m *memoryState) setCell(value byte) {
+ m.memory[m.currentCellIdx].setValue(value)
+}
+
+func (m *memoryState) getCell() byte {
+ return m.memory[m.currentCellIdx].getValue()
+}