Don't try to read expressions as statements. That yields nothing useful and inevidently bombs out anyway.

2012-08-26

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 26 Aug 2012 23:54:58 +0300 (2012-08-26)
changeset 65
ec04357f5bb9
parent 64
dc5db6335601
child 66
4fc1ec88aa41

Don't try to read expressions as statements. That yields nothing useful and inevidently bombs out anyway.

commands.cxx file | annotate | diff | comparison | revisions
parser.cxx file | annotate | diff | comparison | revisions
scriptreader.h file | annotate | diff | comparison | revisions
--- a/commands.cxx	Sun Aug 26 23:28:09 2012 +0300
+++ b/commands.cxx	Sun Aug 26 23:54:58 2012 +0300
@@ -163,11 +163,24 @@
 		!t.compare ("float") ? TYPE_FLOAT :
 		!t.compare ("str") ? TYPE_STRING :
 		!t.compare ("void") ? TYPE_VOID :
-		!t.compare ("bool") ? TYPE_INT : -1;
+		!t.compare ("bool") ? TYPE_BOOL : -1;
 }
 
 // ============================================================================
 // Inverse operation - type name by value
+str GetTypeName (int r) {
+	switch (r) {
+	case TYPE_INT: return "int"; break;
+	case TYPE_STRING: return "str"; break;
+	case TYPE_VOID: return "void"; break;
+	case TYPE_FLOAT: return "float"; break;
+	case TYPE_BOOL: return "bool"; break;
+	}
+	
+	return "";
+}
+
+// ============================================================================
 str GetReturnTypeName (int r) {
 	switch (r) {
 	case RETURNVAL_INT: return "int"; break;
@@ -195,7 +208,7 @@
 // Returns the prototype of the command
 str GetCommandPrototype (CommandDef* comm) {
 	str text;
-	text += GetReturnTypeName (comm->returnvalue);
+	text += GetTypeName (comm->returnvalue);
 	text += ' ';
 	text += comm->name;
 	text += '(';
@@ -210,7 +223,7 @@
 		if (i)
 			text += ", ";
 		
-		text += GetReturnTypeName (comm->argtypes[i]);
+		text += GetTypeName (comm->argtypes[i]);
 		text += ' ';
 		text += comm->argnames[i];
 		
--- a/parser.cxx	Sun Aug 26 23:28:09 2012 +0300
+++ b/parser.cxx	Sun Aug 26 23:54:58 2012 +0300
@@ -97,7 +97,7 @@
 			
 			// State name must be a word.
 			if (token.first (" ") != token.len())
-				ParserError ("state name must be a single word! got `%s`", token.chars());
+				ParserError ("state name must be a single word, got `%s`", token.chars());
 			str statename = token;
 			
 			// stateSpawn is special - it *must* be defined. If we
@@ -133,7 +133,7 @@
 			
 			EventDef* e = FindEventByName (token);
 			if (!e)
-				ParserError ("bad event! got `%s`\n", token.chars());
+				ParserError ("bad event, got `%s`\n", token.chars());
 			
 			MustNext ("{");
 			
@@ -679,13 +679,15 @@
 	while (1) {
 		if (!token.compare (")")) {
 			if (curarg < comm->numargs)
-				ParserError ("too few arguments passed to %s\n", comm->name.chars());
+				ParserError ("too few arguments passed to %s\n\tprototype: %s",
+					comm->name.chars(), GetCommandPrototype (comm).chars());
 			break;
 			curarg++;
 		}
 		
 		if (curarg >= comm->maxargs)
-			ParserError ("too many arguments passed to %s\n", comm->name.chars());
+			ParserError ("too many arguments passed to %s\n\tprototype: %s",
+				comm->name.chars(), GetCommandPrototype (comm).chars());
 		
 		r->Merge (ParseExpression (comm->argtypes[curarg]));
 		MustNext ();
@@ -797,13 +799,13 @@
 	int oper;
 	while ((oper = ParseOperator (true)) != -1) {
 		// We peeked the operator, move forward now
-		MustNext();
+		Next ();
 		
 		// Can't be an assignement operator, those belong in assignments.
 		if (IsAssignmentOperator (oper))
 			ParserError ("assignment operator inside expression");
 		
-		// Parse the right operand,
+		// Parse the right operand.
 		MustNext ();
 		DataBuffer* rb = ParseExprValue (reqtype);
 		
@@ -943,6 +945,7 @@
 		case TYPE_VOID:
 			ParserError ("unknown identifier `%s` (expected keyword, function or variable)", token.chars());
 			break;
+		case TYPE_BOOL:
 		case TYPE_INT: {
 			MustNumber (true);
 			
@@ -1069,9 +1072,8 @@
 		return b;
 	}
 	
-	// If it's not a keyword, parse it as an expression.
-	DataBuffer* b = ParseExpression (TYPE_VOID);
-	return b;
+	ParserError ("bad statement");
+	return NULL;
 }
 
 void ScriptReader::AddSwitchCase (ObjWriter* w, DataBuffer* b) {
--- a/scriptreader.h	Sun Aug 26 23:28:09 2012 +0300
+++ b/scriptreader.h	Sun Aug 26 23:54:58 2012 +0300
@@ -151,7 +151,8 @@
 	TYPE_VOID = 0,
 	TYPE_INT,
 	TYPE_STRING,
-	TYPE_FLOAT
+	TYPE_FLOAT,
+	TYPE_BOOL
 };
 
 // Operators

mercurial