-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdip.go
More file actions
67 lines (57 loc) · 1.09 KB
/
dip.go
File metadata and controls
67 lines (57 loc) · 1.09 KB
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
package main
import (
"fmt"
"os"
"io/ioutil"
"math/big"
)
func run_dip(code string, stack []*big.Int) []*big.Int {
ip := 0
for ip < len(code) {
switch code[ip] {
case '0':
stack = append(stack, big.NewInt(0))
case '\'':
stack[len(stack) - 1] .Add(
stack[len(stack) - 1],
big.NewInt(1))
case ';':
y := stack[len(stack) - 1]
copy(stack[1:], stack)
stack[0] = y
case '.':
fmt.Print(stack[len(stack) - 1], " ")
case '!':
fmt.Println(stack)
case '(':
lvl := 1
l_bound := ip + 1
for ip < len(code) && lvl > 0 {
ip ++
if code[ip] == '(' {
lvl ++
}
if code[ip] == ')' {
lvl --
}
}
for stack[len(stack) - 1].Cmp(big.NewInt(0)) != 0 {
stack[len(stack) - 1] .Sub (
stack[len(stack) - 1],
big.NewInt(1))
stack = run_dip(code[l_bound:ip], stack)
}
stack = stack[:len(stack) - 1]
}
ip ++
}
return stack
}
func main() {
if len(os.Args) != 2 {
fmt.Printf("Usage: %s <file>\n", os.Args[0])
return
}
prog, _ := ioutil.ReadFile(os.Args[1])
run_dip(string(prog), []*big.Int{})
}