-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (64 loc) · 976 Bytes
/
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
package main
import (
"fmt"
"os"
"github.com/pkg/errors"
)
type CommandType int
const (
C_STACK_PUSH CommandType = iota + 1
C_STACK_DUP
C_STACK_SWAP
C_STACK_POP
C_CALC_ADD
C_CALC_SUB
C_CALC_MULTI
C_CALC_DIV
C_CALC_MOD
C_HEAP_SAVE
C_HEAP_LOAD
C_FLOW_DEF
C_FLOW_CALL
C_FLOW_JUMP
C_FLOW_JUMP_IF_ZERO
C_FLOW_JUMP_IF_NEG
C_FLOW_END
C_FLOW_EXIT
C_IO_WRITE_CH
C_IO_WRITE_NUM
C_IO_READ_CH
C_IO_READ_NUM
)
func main() {
err := Main(os.Args)
if err != nil {
fmt.Fprintf(os.Stderr, "%+v\n", err)
os.Exit(1)
}
}
func Main(args []string) error {
if len(args) != 2 {
return errors.New("Usage: gows PATH_TO_WS")
}
wsPath := args[1]
f, err := os.Open(wsPath)
if err != nil {
return err
}
defer f.Close()
p := NewParser(f)
cs, err := p.Parse()
if err != nil {
return err
}
vm := NewVM(cs)
err = vm.Eval(os.Stdin, os.Stdout)
if err != nil {
return err
}
return nil
}
type Command struct {
Type CommandType
Operand int
}