Browse Source

Added Strings to C code generator (YAKHMI-563)

benjamin.schwertfeger@gmail.com 13 years ago
parent
commit
364b7b055d

+ 1 - 0
TestScenarios/test-c/Test_Expression/CMakeLists.txt

@@ -9,3 +9,4 @@ add_test(Expression_test_initialization_and_first_entry TestExpressionMain "1")
 add_test(Expression_test_default_other_var1 TestExpressionMain "2")
 add_test(Expression_test_default_other_var2_var3 TestExpressionMain "3")
 add_test(Expression_test_onCycle TestExpressionMain "4")
+add_test(Expression_test_string_compare_and_assignment TestExpressionMain "5")

+ 33 - 0
TestScenarios/test-c/Test_Expression/main.c

@@ -86,8 +86,39 @@ int test_initialization_and_first_entry()
 
 }
 
+/*@Test: test_default_var1 test behavior of var1 in default interface */
+int test_string_compare_and_assignment()
+{
+	Test_ExpressionStatemachine machine;
+	Timer dummyTimer;
+	EventPool eventPool;
+
+	/*@Desc: setup initial statemachine */
+	setupStatemachine(&machine, &dummyTimer, &eventPool);
+
+	/*@Desc: check whether var6 from default interface is initially set to "foo" */
+	assert( strcmp(test_Expression_if_get_var6(&machine.iface), "foo") == 0);
+	printf("var6: %s\n", test_Expression_if_get_var6(&machine.iface));
+
+	/*@Desc: set var5 to "true" to avoid other transition */
+	test_Expression_if_set_var5(&machine.iface, bool_true);
+
+	/*@Desc: raise event1 on default interface with value 5 (actually unused) */
+	test_Expression_if_raise_event1(&machine.iface, 5);
+
+	/*@Desc: run an explicit cycle */
+	test_ExpressionStatemachine_runCycle(&machine);
+	printf("var6: %s\n", test_Expression_if_get_var6(&machine.iface));
 
+	/*@Desc: check whether var6 from default interface is initially set to "foo" */
+	assert( strcmp(test_Expression_if_get_var6(&machine.iface), "bar") == 0);
 
+	/*@Desc: teardown statemachine */
+	teardownStatemachine(&machine, &dummyTimer, &eventPool);
+
+	return 0;
+
+}
 
 /*@Test: test_default_var1 test behavior of var1 in default and other interface */
 int test_default_other_var1()
@@ -281,6 +312,8 @@ int main(int argc, char** argv)
 		return test_default_other_var2_var3();
 	case 4:
 		return test_onCycle();
+	case 5:
+		return test_string_compare_and_assignment();
 	}
 
 	return 0;

+ 1 - 0
plugins/org.yakindu.sct.generator.c/src/org/yakindu/sct/generator/c/templates/CustomStatemachineC.xpt

@@ -284,6 +284,7 @@ void 
 #define «STNAME()»_FRIENDS
 
 #include "«StName()».h"
+#include <stdlib.h>
 
 «IF this.isSingleton() == true»
 /* singleton handle and singleton handle pointer declaration */

+ 8 - 1
plugins/org.yakindu.sct.generator.c/src/org/yakindu/sct/generator/c/templates/Expression.ext

@@ -6,6 +6,7 @@ import types;
 
 extension org::yakindu::sct::generator::c::templates::Naming;
 extension org::yakindu::sct::generator::c::templates::Constants;
+extension org::yakindu::sct::generator::core::extensions::TypeAnalyzerExtensions;
 
 InterfaceScope getInterfaceScope(AssignmentExpression this) : ((InterfaceScope)this.varRef.eContainer);
 
@@ -23,6 +24,9 @@ String toCCode(Statement statement) :
 String toCCode(Literal lit) :
    "Unknown Literal Type" ;
 
+String toCCode(StringLiteral lit) :
+   "\""+lit.value+"\"" ;
+
 String toCCode(BoolLiteral bLit) :
    (bLit.value == true) ? getBoolTrue() : getBoolFalse(); 
 
@@ -79,7 +83,10 @@ String toCCode(LogicalNotExpression expression) :
   	" ~" + " ( " + expression.operand.toCCode() + " ) ";
 
 String toCCode(LogicalRelationExpression expression) :
-   " ( " + expression.leftOperand.toCCode() + " ) " + getOperator(expression.operator) + " ( " + expression.rightOperand.toCCode() + " ) ";
+	if (expression.leftOperand.inferType().isString()) then
+		"("+expression.leftOperand.toCCode()+"==NULL?"+expression.rightOperand.toCCode()+"==NULL:strcmp("+expression.leftOperand.toCCode()+", "+expression.rightOperand.toCCode()+")==0)"
+	else
+		" ( " + expression.leftOperand.toCCode() + " ) " + getOperator(expression.operator) + " ( " + expression.rightOperand.toCCode() + " ) ";
     
 String toCCode(BitwiseAndExpression expression) :
   	" ( " + expression.leftOperand.toCCode() + " ) " + " & " + " ( " + expression.rightOperand.toCCode() + " ) ";

+ 1 - 0
plugins/org.yakindu.sct.generator.c/src/org/yakindu/sct/generator/c/templates/StatemachineBaseC.xpt

@@ -18,6 +18,7 @@ Templates for the main statechart c file.
 «getLicenseHeader()»
 #include "StatemachineBase.h"
 #include <stdlib.h>
+#include <string.h>
 
 void statemachineBase_init(StatemachineBase* handle, uint16_t maxStates, uint16_t maxHistorys, Timer* _timer)
 {

+ 1 - 0
plugins/org.yakindu.sct.generator.c/src/org/yakindu/sct/generator/c/templates/Statemachine_cyC.xpt

@@ -17,6 +17,7 @@ Templates for the main statechart c file.
 «FILE 'Statemachine_cy.c'»
 «getLicenseHeader()»
 #include <stdlib.h>
+#include <string.h>
 #include "definition.h"
 #include "Statemachine_cy.h"