Added do-while loop support

Sun, 12 Aug 2012 04:10:25 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 12 Aug 2012 04:10:25 +0300
changeset 44
6bbaebc472b5
parent 43
1b35c9985989
child 45
e1d3b7ea975c

Added do-while loop support

main.cxx file | annotate | diff | comparison | revisions
objwriter.h file | annotate | diff | comparison | revisions
parser.cxx file | annotate | diff | comparison | revisions
scriptreader.h file | annotate | diff | comparison | revisions
--- a/main.cxx	Sun Aug 12 03:23:33 2012 +0300
+++ b/main.cxx	Sun Aug 12 04:10:25 2012 +0300
@@ -57,6 +57,7 @@
 #include "botcommands.h"
 
 const char* g_Keywords[] = {
+	"do",
 	"event",
 	"for",
 	"goto",
@@ -74,7 +75,6 @@
 	"case",
 	"continue",
 	"default",
-	"do",
 	"else",
 	"enum", // Would enum actually be useful? I think so.
 	"func", // Would function support need external support from zandronum?
--- a/objwriter.h	Sun Aug 12 03:23:33 2012 +0300
+++ b/objwriter.h	Sun Aug 12 04:10:25 2012 +0300
@@ -72,6 +72,8 @@
 	void WriteBuffers ();
 	void WriteStringTable ();
 	void WriteToFile ();
+	void StartDo ();
+	void EndDo ();
 	DataBuffer* GetCurrentBuffer ();
 	
 	unsigned int AddMark (int type, str name);
--- a/parser.cxx	Sun Aug 12 03:23:33 2012 +0300
+++ b/parser.cxx	Sun Aug 12 04:10:25 2012 +0300
@@ -323,6 +323,19 @@
 		}
 		
 		// ============================================================
+		// Do/while loop
+		if (!token.icompare ("do")) {
+			MUST_NOT_TOPLEVEL
+			PushBlockStack ();
+			MustNext ("{");
+			
+			// Store the marks and incrementor
+			blockstack[g_BlockStackCursor].mark1 = w->AddMark (MARKTYPE_INTERNAL, "");
+			blockstack[g_BlockStackCursor].type = BLOCKTYPE_DO;
+			continue;
+		}
+		
+		// ============================================================
 		if (!token.compare ("}")) {
 			// Closing brace
 			
@@ -345,6 +358,20 @@
 					
 					// Move the closing mark here since we're at the end of the while loop
 					w->MoveMark (info->mark2);
+					break;
+				case BLOCKTYPE_DO:
+					MustNext ("while");
+					MustNext ("(");
+					MustNext ();
+					DataBuffer* expr = ParseExpression (TYPE_INT);
+					MustNext (")");
+					MustNext (";");
+					
+					// If the condition runs true, go back to the start.
+					w->WriteBuffer (expr);
+					w->Write<long> (DH_IFGOTO);
+					w->AddReference (info->mark1);
+					break;
 				}
 				
 				// Descend down the stack
@@ -613,7 +640,7 @@
 		// If nothing else, check for literal
 		switch (reqtype) {
 		case TYPE_VOID:
-			ParserError ("bad syntax");
+			ParserError ("unknown identifier `%s` (expected keyword, function or variable)", token.chars());
 			break;
 		case TYPE_INT: {
 			MustNumber (true);
--- a/scriptreader.h	Sun Aug 12 03:23:33 2012 +0300
+++ b/scriptreader.h	Sun Aug 12 04:10:25 2012 +0300
@@ -168,7 +168,8 @@
 enum {
 	BLOCKTYPE_IF,
 	BLOCKTYPE_WHILE,
-	BLOCKTYPE_FOR
+	BLOCKTYPE_FOR,
+	BLOCKTYPE_DO,
 };
 
 #endif // __SCRIPTREADER_H__
\ No newline at end of file

mercurial