* For loop in programming dialect is now working gscript

Fri, 13 Feb 2015 10:25:05 +0200

author
cemkalyoncu
date
Fri, 13 Feb 2015 10:25:05 +0200
branch
gscript
changeset 625
dbefa4e69a01
parent 624
e19a4fa7c547
child 626
5a1c61c1bd0d

* For loop in programming dialect is now working

Source/Scripting/Compilers/AST.cpp file | annotate | diff | comparison | revisions
Source/Scripting/Compilers/PD.cpp file | annotate | diff | comparison | revisions
--- a/Source/Scripting/Compilers/AST.cpp	Thu Feb 12 11:48:52 2015 +0200
+++ b/Source/Scripting/Compilers/AST.cpp	Fri Feb 13 10:25:05 2015 +0200
@@ -181,6 +181,24 @@
 			return v;
 		}
 		
+		//variable, use as is
+		if(tree.Type==ASTNode::Variable) {
+			Value v;
+			v.Type=ValueType::Variable;
+			v.Name=tree.Text;
+			
+			return v;
+		}
+		
+		//constant, use as is
+		if(tree.Type==ASTNode::Constant) {
+			Value v;
+			v.Type=ValueType::Constant;
+			v.Name=tree.Text;
+			
+			return v;
+		}
+		
 		//function call
 		if(tree.Type==ASTNode::FunctionCall || tree.Type==ASTNode::MethodCall) {
 			ASSERT(tree.Leaves.GetSize()>0, "Function name is missing");
--- a/Source/Scripting/Compilers/PD.cpp	Thu Feb 12 11:48:52 2015 +0200
+++ b/Source/Scripting/Compilers/PD.cpp	Fri Feb 13 10:25:05 2015 +0200
@@ -697,19 +697,36 @@
 			if(token == Token::EoS) return nullptr;
 
 			if(token!=Token::Identifier) {
-				throw ParseError{ExceptionType::UnexpectedToken, "Expected identifier.", index};
+				throw ParseError{ExceptionType::UnexpectedToken, "Expected identifier, found: "+token.repr, index};
 			}
 			
 			if(KeywordNames.count(token.repr)) { //keyword
 				root=NewNode(ASTNode::Keyword, token);
 				
-				token=peeknexttoken(input, index);
-				
-				while(token!=Token::EoS) {
+				if(String::ToLower(token.repr)=="for") {
+					auto tokenvar=consumenexttoken(input, index);
+					if(tokenvar!=Token::Identifier) {
+						throw ParseError{ExceptionType::UnexpectedToken, "Expected identifier, found: "+token.repr, index};
+					}
+					root->Leaves.Push(NewNode(ASTNode::Variable, tokenvar));
+					
+					auto tokenin=consumenexttoken(input, index);					
+					if(String::ToLower(tokenin.repr)!="in") {
+						throw ParseError{ExceptionType::UnexpectedToken, "Expected `in`, found: "+tokenin.repr, index};
+					}
+					
 					auto expr=parseexpression(input, index);
 					root->Leaves.Push(expr);
+				}
+				else {
+					token=peeknexttoken(input, index);
 					
-					token=peeknexttoken(input, index);
+					while(token!=Token::EoS) {
+						auto expr=parseexpression(input, index);
+						root->Leaves.Push(expr);
+						
+						token=peeknexttoken(input, index);
+					}
 				}
 			}
 			else {

mercurial