Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added functional programming version of go bf2 #113

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
131 changes: 131 additions & 0 deletions brainfuck2/bf_func.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package main

import "fmt"
import "os"
import "io/ioutil"

const (
INC = iota
MOVE
PRINT
LOOP
)

type Op func(*Tape)

type StringIterator struct {
Text []byte
Pos int
}

func NewStringIterator(s string) *StringIterator {
si := &StringIterator{Text: []byte(s), Pos: 0}
return si
}

func (si *StringIterator) Next() byte {
if si.Pos < len(si.Text) {
res := si.Text[si.Pos]
si.Pos++
return res
} else {
return byte(0)
}
}

type Tape struct {
tape []int
pos int
}

func NewTape() *Tape {
t := &Tape{pos: 0}
t.tape = make([]int, 1)
t.Move(1)
return t
}

func (t *Tape) Inc(x int) {
t.tape[t.pos] += x
}

func (t *Tape) Move(x int) {
t.pos += x
for t.pos >= len(t.tape) {
t.tape = append(t.tape, 0)
}
}

func (t *Tape) Get() int {
return t.tape[t.pos]
}

type Program struct {
Ops []Op
}

func NewProgram(code string) *Program {
return &Program{Ops: parse(NewStringIterator(code))}
}

func (p *Program) Run() {
_run(p.Ops, NewTape())
}

func parse(si *StringIterator) []Op {
var res []Op
var do Op
for true {
c := si.Next()
switch c {
case '+':
do = func(t *Tape) {
t.Inc(1)
}
case '-':
do = func(t *Tape) {
t.Inc(-1)
}
case '>':
do = func(t *Tape) {
t.Move(1)
}
case '<':
do = func(t *Tape) {
t.Move(-1)
}
case '.':
do = func(t *Tape) {
fmt.Printf("%c", t.tape[t.pos])
}
case '[':
program := parse(si)
do = func(t *Tape) {
for t.Get() != 0 {
// Call run as long as val != 0
_run(program, t)
}
}
case ']', 0:
return res
default:
do = func(t *Tape) {} // no-op
}
res = append(res, do)
}
return res
}

func _run(program []Op, tape *Tape) {
for _, op := range program {
op(tape)
}
}

func main() {
Code, err := ioutil.ReadFile(os.Args[1])
if err != nil {
panic(fmt.Sprintf("%v", err))
}
NewProgram(string(Code)).Run()
}
1 change: 1 addition & 0 deletions brainfuck2/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dotnet restore && dotnet build -c Release
javac bf.java
kotlinc bf2.kt -include-runtime -d bf2-kt.jar
go build -o bin_go bf.go
go build -o bin_go_func bf_func.go
gccgo -O3 -g -o bin_go_gccgo bf.go
dmd -ofbin_d -O -release -inline bf.d
gdc -o bin_d_gdc -O3 -frelease -finline bf.d
Expand Down
2 changes: 2 additions & 0 deletions brainfuck2/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ echo Javascript Node
../xtime.rb node bf.js bench.b
echo Go
../xtime.rb ./bin_go bench.b
echo Functional Go
../xtime.rb ./bin_go_func bench.b
echo Go Gcc
../xtime.rb ./bin_go_gccgo bench.b
echo D Dmd
Expand Down
2 changes: 2 additions & 0 deletions brainfuck2/run2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ echo Kotlin
../xtime.rb java -jar bf2-kt.jar mandel.b > /dev/null
echo Go
../xtime.rb ./bin_go mandel.b > /dev/null
echo Functional Go
../xtime.rb ./bin_go_func mandel.b > /dev/null
echo Go Gcc
../xtime.rb ./bin_go_gccgo mandel.b > /dev/null
echo Javascript Node
Expand Down