-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
90 lines (83 loc) · 1.58 KB
/
main.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
package main
import (
. "github.com/Zephyyrr/BiG"
"flag"
"log"
"os"
"io"
"time"
"bufio"
)
const (
SPEED = time.Millisecond
)
var (
verbose = flag.Bool("v", false, "Verbose output.")
timelimit = flag.Duration("t", 0, "Timelimit for execution. 0 for no limit.")
speed = flag.Duration("s", SPEED, "time between two ticks in the VM.")
fastmode = flag.Bool("f", false, "Fast-mode. No delay.")
)
func main() {
flag.Parse()
if flag.Arg(0) == "" {
log.Println("Needs a file on commandline. Exiting.")
os.Exit(1)
}
file, err := os.Open(flag.Arg(0))
if err != nil {
log.Println(flag.Arg(0))
log.Fatal(err)
}
fs := loadFungeSpace(file)
vm := NewVM(fs)
timer := time.NewTimer(*timelimit)
if *timelimit <= 0 {
timer.Stop()
}
done := make(chan bool)
if *verbose {
log.Println("Program:\n", vm.FS)
log.Println("Timelimit:", *timelimit)
log.Println("---BEGIN---")
}
if *fastmode {
go func() {
for !vm.Done() {
vm.Tick()
}
}()
} else {
ticker := time.NewTicker(*speed)
go vm.Run(ticker)
}
go func(){
for !vm.Done(){
time.Sleep(10*time.Millisecond)
}
done<-true
}()
select {
case <-timer.C:
if *verbose {
log.Println("---END---")
}
log.Println("Execution took to long.")
os.Exit(1)
case <-done:
if *verbose {
log.Println("---END---")
}
}
}
func loadFungeSpace(in io.Reader) FungeSpace {
buff := bufio.NewReaderSize(in, 80)
fs := new(Befunge93Space)
l, _, err := buff.ReadLine()
for i := 0; err == nil && i < 25 ; i++ {
for j, v := range l {
fs[i][j] = int8(v)
}
l, _, err = buff.ReadLine()
}
return fs
}