Added rudimentary global var support

Sun, 15 Jul 2012 00:31:08 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 15 Jul 2012 00:31:08 +0300
changeset 22
b48e10ca8832
parent 21
ae602e667879
child 23
ba351235688e

Added rudimentary global var support

Makefile file | annotate | diff | comparison | revisions
common.h file | annotate | diff | comparison | revisions
main.cxx file | annotate | diff | comparison | revisions
parser.cxx file | annotate | diff | comparison | revisions
scriptreader.cxx file | annotate | diff | comparison | revisions
scriptreader.h file | annotate | diff | comparison | revisions
--- a/Makefile	Sat Jul 14 22:03:34 2012 +0300
+++ b/Makefile	Sun Jul 15 00:31:08 2012 +0300
@@ -7,7 +7,8 @@
 	g++ -Wall -c -o events.o events.cxx
 	g++ -Wall -c -o commands.o commands.cxx
 	g++ -Wall -c -o stringtable.o stringtable.cxx
-	g++ -Wall -o botc scriptreader.o objwriter.o str.o main.o parser.o events.o commands.o stringtable.o
+	g++ -Wall -c -o variables.o variables.cxx
+	g++ -Wall -o botc scriptreader.o objwriter.o str.o main.o parser.o events.o commands.o stringtable.o variables.o
 
 clean:
 	rm -f *.o *~ botc
--- a/common.h	Sat Jul 14 22:03:34 2012 +0300
+++ b/common.h	Sun Jul 15 00:31:08 2012 +0300
@@ -64,6 +64,8 @@
 	char* out = vdynformat (in, v, 256); \
 	va_end (v);
 
+#define PLURAL(n) (n != 1) ? "s" : ""
+
 void error (const char* text, ...);
 
 #ifndef __PARSER_CXX__
--- a/main.cxx	Sat Jul 14 22:03:34 2012 +0300
+++ b/main.cxx	Sun Jul 15 00:31:08 2012 +0300
@@ -49,6 +49,7 @@
 #include "events.h"
 #include "commands.h"
 #include "stringtable.h"
+#include "variables.h"
 
 #include "bots.h"
 #include "botcommands.h"
@@ -88,8 +89,9 @@
 	ReadEvents ();
 	ReadCommands ();
 	
-	// Init string table
+	// Init stuff
 	InitStringTable();
+	InitVariables ();
 	
 	// Prepare reader and writer
 	ScriptReader *r = new ScriptReader (argv[1]);
@@ -100,8 +102,10 @@
 	r->BeginParse (w);
 	
 	// Parse done, print statistics
-	printf ("%d states written\n", g_NumStates);
-	printf ("%d events written\n", g_NumEvents);
+	unsigned int globalcount = CountGlobalVars ();
+	printf ("%u global variable%s\n", globalcount, PLURAL (globalcount));
+	printf ("%d state%s written\n", g_NumStates, PLURAL (g_NumStates));
+	printf ("%d event%s written\n", g_NumEvents, PLURAL (g_NumEvents));
 	printf ("-- %u bytes written to %s\n", w->numWrittenBytes, argv[2]);
 	
 	// Clear out the junk
--- a/parser.cxx	Sat Jul 14 22:03:34 2012 +0300
+++ b/parser.cxx	Sun Jul 15 00:31:08 2012 +0300
@@ -49,6 +49,7 @@
 #include "events.h"
 #include "commands.h"
 #include "stringtable.h"
+#include "variables.h"
 
 #define MUST_TOPLEVEL if (g_CurMode != MODE_TOPLEVEL) \
 	ParserError ("%ss may only be defined at top level!", token.chars());
@@ -136,6 +137,31 @@
 			MustNext ("{");
 			g_CurMode = onenter ? MODE_ONENTER : MODE_ONEXIT;
 			w->Write (onenter ? DH_ONENTER : DH_ONEXIT);
+		} else if (!token.compare ("int") || !token.compare ("bool")) {
+			// For now, only globals are supported
+			if (g_CurMode != MODE_TOPLEVEL || g_CurState.len())
+				ParserError ("variables must only be global for now");
+			
+			// Variable definition
+			int type = !token.compare ("int") ? RETURNVAL_INT: RETURNVAL_BOOLEAN;
+			
+			MustNext ();
+			str varname = token;
+			ScriptVar* var = DeclareGlobalVariable (this, varname, type);
+			
+			if (!var)
+				ParserError ("declaring %s variable %s failed",
+					g_CurState.len() ? "state" : "global", varname.chars());
+			
+			// Assign it, if desired
+			if (!PeekNext().compare ("=")) {
+				MustNext ("=");
+				MustValue (type);
+				
+				var->value = token;
+			}
+			
+			MustNext (";");
 		} else if (!token.compare ("}")) {
 			// Closing brace
 			int dataheader =	(g_CurMode == MODE_EVENT) ? DH_ENDEVENT :
@@ -182,7 +208,8 @@
 		for (unsigned int a = 0; a < stringcount; a++)
 			w->WriteString (g_StringTable[a]);
 	}
-	printf ("%u string%s written\n", stringcount, (stringcount != 1) ? "s" : "");
+	
+	printf ("%u string%s written\n", stringcount, PLURAL (stringcount));
 }
 
 void ScriptReader::ParseCommand (CommandDef* comm, ObjWriter* w) {
--- a/scriptreader.cxx	Sat Jul 14 22:03:34 2012 +0300
+++ b/scriptreader.cxx	Sun Jul 15 00:31:08 2012 +0300
@@ -235,4 +235,12 @@
 
 bool ScriptReader::BoolValue () {
 	return (!token.compare ("1") || !token.compare ("true") || !token.compare ("yes"));
+}
+
+void ScriptReader::MustValue (int type) {
+	switch (type) {
+	case RETURNVAL_INT: MustNumber (); break;
+	case RETURNVAL_STRING: MustString (); break;
+	case RETURNVAL_BOOLEAN: MustBool (); break;
+	}
 }
\ No newline at end of file
--- a/scriptreader.h	Sat Jul 14 22:03:34 2012 +0300
+++ b/scriptreader.h	Sun Jul 15 00:31:08 2012 +0300
@@ -79,6 +79,7 @@
 	void MustString ();
 	void MustNumber ();
 	void MustBool ();
+	void MustValue (int type);
 	bool BoolValue ();
 	
 	void ParserError (const char* message, ...);

mercurial