60 |
60 |
61 void ScriptReader::BeginParse (ObjWriter* w) { |
61 void ScriptReader::BeginParse (ObjWriter* w) { |
62 bool gotMainLoop = false; |
62 bool gotMainLoop = false; |
63 while (Next()) { |
63 while (Next()) { |
64 // printf ("got token %s\n", token.chars()); |
64 // printf ("got token %s\n", token.chars()); |
65 if (!token.compare ("#include")) { |
65 if (!token.icompare ("#include")) { |
66 MustString (); |
66 MustString (); |
67 |
67 |
68 // First ensure that the file can be opened |
68 // First ensure that the file can be opened |
69 FILE* newfile = fopen (token.chars(), "r"); |
69 FILE* newfile = fopen (token.chars(), "r"); |
70 if (!newfile) |
70 if (!newfile) |
71 ParserError ("couldn't open included file `%s`!", token.chars()); |
71 ParserError ("couldn't open included file `%s`!", token.chars()); |
72 fclose (newfile); |
72 fclose (newfile); |
73 ScriptReader* newreader = new ScriptReader (token.chars()); |
73 ScriptReader* newreader = new ScriptReader (token.chars()); |
74 newreader->BeginParse (w); |
74 newreader->BeginParse (w); |
75 } else if (!token.compare ("state")) { |
75 } else if (!token.icompare ("state")) { |
76 MUST_TOPLEVEL |
76 MUST_TOPLEVEL |
77 |
77 |
78 MustString (); |
78 MustString (); |
79 |
79 |
80 // State name must be a word. |
80 // State name must be a word. |
90 // Must end in a colon |
90 // Must end in a colon |
91 MustNext (":"); |
91 MustNext (":"); |
92 |
92 |
93 // If the previous state did not define a mainloop, |
93 // If the previous state did not define a mainloop, |
94 // define a dummy one now, since one has to be present. |
94 // define a dummy one now, since one has to be present. |
95 if (g_CurState.compare ("") != 0 && !gotMainLoop) { |
95 if (g_CurState.len() && !gotMainLoop) { |
96 w->Write (DH_MAINLOOP); |
96 w->Write (DH_MAINLOOP); |
97 w->Write (DH_ENDMAINLOOP); |
97 w->Write (DH_ENDMAINLOOP); |
98 } |
98 } |
99 |
99 |
100 w->Write (DH_STATENAME); |
100 w->Write (DH_STATENAME); |
121 g_CurMode = MODE_EVENT; |
121 g_CurMode = MODE_EVENT; |
122 |
122 |
123 w->Write (DH_EVENT); |
123 w->Write (DH_EVENT); |
124 w->Write<long> (e->number); |
124 w->Write<long> (e->number); |
125 g_NumEvents++; |
125 g_NumEvents++; |
126 } else if (!token.compare ("mainloop")) { |
126 } else if (!token.icompare ("mainloop")) { |
127 MUST_TOPLEVEL |
127 MUST_TOPLEVEL |
128 MustNext ("{"); |
128 MustNext ("{"); |
129 g_CurMode = MODE_MAINLOOP; |
129 g_CurMode = MODE_MAINLOOP; |
130 w->Write (DH_MAINLOOP); |
130 w->Write (DH_MAINLOOP); |
131 gotMainLoop = true; |
131 gotMainLoop = true; |
132 } else if (!token.compare ("onenter") || !token.compare ("onexit")) { |
132 } else if (!token.icompare ("onenter") || !token.icompare ("onexit")) { |
133 MUST_TOPLEVEL |
133 MUST_TOPLEVEL |
134 bool onenter = !token.compare ("onenter"); |
134 bool onenter = !token.compare ("onenter"); |
135 |
135 |
136 MustNext ("{"); |
136 MustNext ("{"); |
137 g_CurMode = onenter ? MODE_ONENTER : MODE_ONEXIT; |
137 g_CurMode = onenter ? MODE_ONENTER : MODE_ONEXIT; |
153 // Check if it's a command. |
153 // Check if it's a command. |
154 CommandDef* comm = GetCommandByName (token); |
154 CommandDef* comm = GetCommandByName (token); |
155 if (comm) |
155 if (comm) |
156 ParseCommand (comm, w); |
156 ParseCommand (comm, w); |
157 else |
157 else |
158 ParserError ("unknown keyword `%s`!", token.chars()); |
158 ParserError ("unknown keyword `%s`", token.chars()); |
159 } |
159 } |
160 } |
160 } |
161 |
161 |
162 if (g_CurMode != MODE_TOPLEVEL) |
162 if (g_CurMode != MODE_TOPLEVEL) |
163 ParserError ("script did not end at top level! did you forget a `}`?"); |
163 ParserError ("script did not end at top level; did you forget a `}`?"); |
164 |
164 |
165 // stateSpawn must be defined! |
165 // stateSpawn must be defined! |
166 if (!g_stateSpawnDefined) |
166 if (!g_stateSpawnDefined) |
167 ParserError ("script must have a state named `stateSpawn`!"); |
167 ParserError ("script must have a state named `stateSpawn`!"); |
168 |
168 |