Go で書き直した Ikemen
Révision | b197271a2d7f79884eeecb100e4af0d883f54bee (tree) |
---|---|
l'heure | 2016-12-08 00:30:47 |
Auteur | SUEHIRO <supersuehiro@user...> |
Commiter | SUEHIRO |
コマンドの読み込みとステートのパーズ少し
@@ -5,17 +5,64 @@ import ( | ||
5 | 5 | "strings" |
6 | 6 | ) |
7 | 7 | |
8 | -type ByteCode struct{} | |
8 | +type StateByteCode struct{} | |
9 | +type ByteCode struct{ states map[int32]StateByteCode } | |
9 | 10 | |
10 | 11 | func newByteCode() *ByteCode { |
11 | - return &ByteCode{} | |
12 | + return &ByteCode{states: make(map[int32]StateByteCode)} | |
12 | 13 | } |
13 | 14 | |
14 | -type Compiler struct{ ver [2]int16 } | |
15 | +type Compiler struct{ cmdl *CommandList } | |
15 | 16 | |
16 | 17 | func newCompiler() *Compiler { |
17 | 18 | return &Compiler{} |
18 | 19 | } |
20 | +func (c *Compiler) parseSection(lines []string, i *int, | |
21 | + f func(name, data string) error) (IniSection, error) { | |
22 | + is := NewIniSection() | |
23 | + unimplemented() | |
24 | + return is, nil | |
25 | +} | |
26 | +func (c *Compiler) stateCompile(bc *ByteCode, filename, def string) error { | |
27 | + var lines []string | |
28 | + if err := LoadFile(&filename, def, func(filename string) error { | |
29 | + str, err := LoadText(filename) | |
30 | + if err != nil { | |
31 | + return err | |
32 | + } | |
33 | + lines = SplitAndTrim(str, "\n") | |
34 | + return nil | |
35 | + }); err != nil { | |
36 | + return err | |
37 | + } | |
38 | + i := 0 | |
39 | + errmes := func(err error) error { | |
40 | + return Error(fmt.Sprintf("%v:%v:\n%v", filename, i+1, err.Error())) | |
41 | + } | |
42 | + existInThisFile := make(map[int32]bool) | |
43 | + for ; i < len(lines); i++ { | |
44 | + line := strings.ToLower(lines[i]) | |
45 | + if idx := strings.Index(line, ";"); idx >= 0 { | |
46 | + line = strings.TrimSpace(line[:idx]) | |
47 | + } | |
48 | + if len(line) < 11 || line[0] != '[' || line[len(line)-1] != ']' || | |
49 | + line[1:10] != "statedef " { | |
50 | + continue | |
51 | + } | |
52 | + n := Atoi(line[11:]) | |
53 | + if existInThisFile[n] { | |
54 | + continue | |
55 | + } | |
56 | + existInThisFile[n] = true | |
57 | + i++ | |
58 | + is, err := c.parseSection(lines, &i, nil) | |
59 | + if err != nil { | |
60 | + return errmes(err) | |
61 | + } | |
62 | + unimplemented() | |
63 | + } | |
64 | + return nil | |
65 | +} | |
19 | 66 | func (c *Compiler) Compile(n int, def string) (*ByteCode, error) { |
20 | 67 | bc := newByteCode() |
21 | 68 | str, err := LoadText(def) |
@@ -63,8 +110,9 @@ func (c *Compiler) Compile(n int, def string) (*ByteCode, error) { | ||
63 | 110 | sys.chars[n][0].cmd[i] = *NewCommandList(b) |
64 | 111 | } |
65 | 112 | } |
66 | - cl, remap, defaults := &sys.chars[n][0].cmd[n], true, true | |
67 | - ckr := NewCommandKeyRemap() | |
113 | + c.cmdl = &sys.chars[n][0].cmd[n] | |
114 | + remap, defaults, ckr := true, true, NewCommandKeyRemap() | |
115 | + var cmds []IniSection | |
68 | 116 | for i < len(lines) { |
69 | 117 | is, name, _ := ReadIniSection(lines, &i) |
70 | 118 | switch name { |
@@ -100,9 +148,45 @@ func (c *Compiler) Compile(n int, def string) (*ByteCode, error) { | ||
100 | 148 | case "defaults": |
101 | 149 | if defaults { |
102 | 150 | defaults = false |
151 | + is.ReadI32("command.time", &c.cmdl.DefaultTime) | |
152 | + var i32 int32 | |
153 | + if is.ReadI32("command.buffer.time", &i32) { | |
154 | + c.cmdl.DefaultBufferTime = Max(1, i32) | |
155 | + } | |
156 | + } | |
157 | + default: | |
158 | + if len(name) >= 7 && name[:7] == "command" { | |
159 | + cmds = append(cmds, is) | |
103 | 160 | } |
104 | 161 | } |
105 | 162 | } |
106 | - unimplemented() | |
163 | + for _, is := range cmds { | |
164 | + cm, err := ReadCommand(is["name"], is["command"], ckr) | |
165 | + if err != nil { | |
166 | + return nil, Error(cmd + ":\nname = " + is["name"] + | |
167 | + "\ncommand = " + is["command"] + "\n" + err.Error()) | |
168 | + } | |
169 | + is.ReadI32("time", &cm.time) | |
170 | + var i32 int32 | |
171 | + if is.ReadI32("buffer.time", &i32) { | |
172 | + cm.buftime = Max(1, i32) | |
173 | + } | |
174 | + c.cmdl.Add(*cm) | |
175 | + } | |
176 | + for _, s := range st { | |
177 | + if len(s) > 0 { | |
178 | + if err := c.stateCompile(bc, s, def); err != nil { | |
179 | + return nil, err | |
180 | + } | |
181 | + } | |
182 | + } | |
183 | + if err := c.stateCompile(bc, cmd, def); err != nil { | |
184 | + return nil, err | |
185 | + } | |
186 | + if len(stcommon) > 0 { | |
187 | + if err := c.stateCompile(bc, stcommon, def); err != nil { | |
188 | + return nil, err | |
189 | + } | |
190 | + } | |
107 | 191 | return bc, nil |
108 | 192 | } |
@@ -548,7 +548,7 @@ type Command struct { | ||
548 | 548 | } |
549 | 549 | |
550 | 550 | func newCommand() *Command { return &Command{tamei: -1, time: 1, buftime: 1} } |
551 | -func ReadCommand(name, cmdstr string) (*Command, error) { | |
551 | +func ReadCommand(name, cmdstr string, kr *CommandKeyRemap) (*Command, error) { | |
552 | 552 | c := newCommand() |
553 | 553 | c.name = name |
554 | 554 | cmd := strings.Split(cmdstr, ",") |
@@ -664,51 +664,51 @@ func ReadCommand(name, cmdstr string) (*Command, error) { | ||
664 | 664 | tilde = false |
665 | 665 | case 'a': |
666 | 666 | if tilde { |
667 | - ce.key = append(ce.key, CK_na) | |
667 | + ce.key = append(ce.key, kr.na) | |
668 | 668 | } else { |
669 | - ce.key = append(ce.key, CK_a) | |
669 | + ce.key = append(ce.key, kr.a) | |
670 | 670 | } |
671 | 671 | tilde = false |
672 | 672 | case 'b': |
673 | 673 | if tilde { |
674 | - ce.key = append(ce.key, CK_nb) | |
674 | + ce.key = append(ce.key, kr.nb) | |
675 | 675 | } else { |
676 | - ce.key = append(ce.key, CK_b) | |
676 | + ce.key = append(ce.key, kr.b) | |
677 | 677 | } |
678 | 678 | tilde = false |
679 | 679 | case 'c': |
680 | 680 | if tilde { |
681 | - ce.key = append(ce.key, CK_nc) | |
681 | + ce.key = append(ce.key, kr.nc) | |
682 | 682 | } else { |
683 | - ce.key = append(ce.key, CK_c) | |
683 | + ce.key = append(ce.key, kr.c) | |
684 | 684 | } |
685 | 685 | tilde = false |
686 | 686 | case 'x': |
687 | 687 | if tilde { |
688 | - ce.key = append(ce.key, CK_nx) | |
688 | + ce.key = append(ce.key, kr.x) | |
689 | 689 | } else { |
690 | - ce.key = append(ce.key, CK_x) | |
690 | + ce.key = append(ce.key, kr.x) | |
691 | 691 | } |
692 | 692 | tilde = false |
693 | 693 | case 'y': |
694 | 694 | if tilde { |
695 | - ce.key = append(ce.key, CK_ny) | |
695 | + ce.key = append(ce.key, kr.ny) | |
696 | 696 | } else { |
697 | - ce.key = append(ce.key, CK_y) | |
697 | + ce.key = append(ce.key, kr.y) | |
698 | 698 | } |
699 | 699 | tilde = false |
700 | 700 | case 'z': |
701 | 701 | if tilde { |
702 | - ce.key = append(ce.key, CK_nz) | |
702 | + ce.key = append(ce.key, kr.nz) | |
703 | 703 | } else { |
704 | - ce.key = append(ce.key, CK_z) | |
704 | + ce.key = append(ce.key, kr.z) | |
705 | 705 | } |
706 | 706 | tilde = false |
707 | 707 | case 's': |
708 | 708 | if tilde { |
709 | - ce.key = append(ce.key, CK_ns) | |
709 | + ce.key = append(ce.key, kr.ns) | |
710 | 710 | } else { |
711 | - ce.key = append(ce.key, CK_s) | |
711 | + ce.key = append(ce.key, kr.s) | |
712 | 712 | } |
713 | 713 | tilde = false |
714 | 714 | case '$': |
@@ -81,7 +81,7 @@ func scriptCommonInit(l *lua.LState) { | ||
81 | 81 | if !ok { |
82 | 82 | userDataError(l, 1, cl) |
83 | 83 | } |
84 | - c, err := ReadCommand(strArg(l, 2), strArg(l, 3)) | |
84 | + c, err := ReadCommand(strArg(l, 2), strArg(l, 3), NewCommandKeyRemap()) | |
85 | 85 | if err != nil { |
86 | 86 | l.RaiseError(err.Error()) |
87 | 87 | } |
@@ -91,6 +91,7 @@ type System struct { | ||
91 | 91 | numTurns [2]int |
92 | 92 | esc bool |
93 | 93 | loadMutex sync.Mutex |
94 | + ignoreMostErrors bool | |
94 | 95 | } |
95 | 96 | |
96 | 97 | func (s *System) init(w, h int32) *lua.LState { |