Skip to content

Commit e89164b

Browse files
committed
update samples
1 parent 901d34b commit e89164b

File tree

10 files changed

+18
-187
lines changed

10 files changed

+18
-187
lines changed

samples/Wa/brainfuck.wa

+18-10
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
1-
// 版权 @2019 凹语言 作者。保留所有权利。
1+
// BrainFuck VM
2+
// https://github.com/chai2010/brainfuck-wa
23

34
func main {
45
// print hi
56
const code = "++++++++++[>++++++++++<-]>++++.+."
67
vm := NewBrainFuck(code)
7-
vm.Run()
8+
println(string(vm.Run()))
89
}
910

10-
type BrainFuck struct {
11-
mem :[30000]byte
12-
code :string
13-
pos :int
14-
pc :int
11+
func Run(code: string) => string {
12+
vm := NewBrainFuck(code)
13+
return string(vm.Run())
14+
}
15+
16+
// BF VM
17+
type BrainFuck :struct {
18+
mem: [30000]byte
19+
code: string
20+
pos: int
21+
pc: int
1522
}
1623

1724
func NewBrainFuck(code: string) => *BrainFuck {
1825
return &BrainFuck{code: code}
1926
}
2027

21-
func BrainFuck.Run {
28+
func BrainFuck.Run => []byte {
29+
output := make([]byte, 0, 64)
2230
for ; this.pc != len(this.code); this.pc++ {
2331
switch x := this.code[this.pc]; x {
2432
case '>':
@@ -38,12 +46,12 @@ func BrainFuck.Run {
3846
this.loop(-1)
3947
}
4048
case '.':
41-
print(rune(this.mem[this.pos]))
49+
output = append(output, this.mem[this.pos])
4250
case ',':
4351
// TODO: support read byte
4452
}
4553
}
46-
return
54+
return output
4755
}
4856

4957
func BrainFuck.loop(inc: int) {

samples/Wa/closure.wa

-42
This file was deleted.

samples/Wa/complex.wa

-9
This file was deleted.

samples/Wa/count.wa

-19
This file was deleted.

samples/Wa/defer.wa

-15
This file was deleted.

samples/Wa/heart.wa

-16
This file was deleted.

samples/Wa/hello-p5.wa

-17
This file was deleted.

samples/Wa/hello.wa

-15
This file was deleted.

samples/Wa/iface.wa

-32
This file was deleted.

samples/Wa/map.wa

-12
This file was deleted.

0 commit comments

Comments
 (0)