summaryrefslogtreecommitdiffstats
path: root/src/flowparts/varcomparison.cpp
blob: d76ad7ccb50132c6e3a24316e6045a178556d921 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/***************************************************************************
 *   Copyright (C) 2003 by David Saxton                                    *
 *   david@bluehaze.org                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "varcomparison.h"

#include "libraryitem.h"
#include "flowcode.h"

#include <tdelocale.h>

Item* VarComparison::construct( ItemDocument *itemDocument, bool newItem, const char *id )
{
	return new VarComparison( (ICNDocument*)itemDocument, newItem, id );
}

LibraryItem* VarComparison::libraryItem()
{
	return new LibraryItem(
		TQString("flow/varcomparison"),
		i18n("Comparison"),
		i18n("Variables"),
		"branch.png",
		LibraryItem::lit_flowpart,
		VarComparison::construct );
}

VarComparison::VarComparison( ICNDocument *icnDocument, bool newItem, const char *id )
	: FlowPart( icnDocument, newItem, (id) ? id : "varcomparison" )
{
	m_name = i18n("Variable Comparison");
	m_desc = i18n("Conditional branch point, depending on the comparison of two values. The supported comparisons are:<ul><li><b>x == y</b> - Equality: true if x has the same value as y.</li><li><b>x &lt; y</b> - Less than: true if x is smaller than y.</li><li><b>x &gt; y</b> - Greater than: true if x is bigger than y.</li><li><b>x &lt;= y</b> - Less than or equal: true if x is less than or equal to y.</li><li><b>x &gt;= y</b> - Greater than or equal: true if x is greater than or equal to y.</li><li><b>x != y</b> - Does not equal: true if x does not have the same value as y.</li></ul>");
	initDecisionSymbol();
	createStdInput();
	createStdOutput();
	createAltOutput();
	
	createProperty( "0var1", Variant::Type::Combo );
	property("0var1")->setCaption( i18n("Variable") );
	property("0var1")->setValue("x");
	
	createProperty( "1op", Variant::Type::Select );
	property("1op")->setAllowed( TQStringList::split( ',', "==,<,>,<=,>=,!=" ) );
	property("1op")->setValue("==");
	property("1op")->setToolbarCaption(" ");
	property("1op")->setEditorCaption( i18n("Operation") );
	
	createProperty( "2var2", Variant::Type::Combo );
	property("2var2")->setToolbarCaption(" ");
	property("2var2")->setEditorCaption( i18n("Value") );
	property("2var2")->setValue("0");
	
	addDisplayText( "output_false", TQRect( offsetX()+width(), 2, 40, 20 ), "No" );
	addDisplayText( "output_true", TQRect( 0, offsetY()+height(), 50, 20 ), "Yes" ); 
}

VarComparison::~VarComparison()
{
}

void VarComparison::dataChanged()
{
	setCaption( dataString("0var1") + " " + dataString("1op") + " " + dataString("2var2") + " ?" );
}

TQString VarComparison::oppOp( const TQString &op )
{
	if		( op == "==" )	return "!=";
	if		( op == "!=" )	return "==";
	else if ( op == "<" )	return ">=";
	else if ( op == ">=" )	return "<";
	else if ( op == ">" )	return "<=";
	else if ( op == "<=" )	return ">";
	else return "__UNKNOWN_OP__";
}

void VarComparison::generateMicrobe( FlowCode *code )
{
	TQString var1 = dataString("0var1");
	TQString var2 = dataString("2var2");
	TQString test = dataString("1op");
	
	handleIfElse( code, var1+" "+test+" "+var2, var1+" "+oppOp(test)+" "+var2, "stdoutput", "altoutput" );
	
#if 0
	code->addCode( "if "+var1+" "+test+" "+var2+"\n{\n" );
	code->addCodeBranch( outputPart("stdoutput") );
	code->addCode("}");
	if ( outputPart("altoutput") )
	{
		code->addCode("else\n{");
		code->addCodeBranch( outputPart("altoutput") );
		code->addCode("}");
	}
#endif
	
#if 0
	TQString newCode;
	
	if ( FlowCode::isLiteral(var2) ) newCode += "movlw " + var2 + " ; Move literal to register w\n";
	else
	{
		code->addVariable(var2);
		newCode += "movf " + var2 + ",0 ; Move " + var2 + " to register w\n";
	}
	
	if ( FlowCode::isLiteral(var1) ) newCode += "sublw " + var1 + " ; Subtract register w from " + var1 + ", placing result in w\n";
	else
	{
		code->addVariable(var1);
		newCode += "subwf " + var1 + ",0 ; Subtract register w from " + var1 + ", placing result in w\n";
	}
	
	
	if		( test == "==" )
	{
		// check: works
		newCode += "btfss STATUS,2 ; Check if zero flag is set\n";
		newCode += gotoCode("altoutput") + " ; Result from calculation was non-zero; hence comparison is false\n";
		newCode += gotoCode("stdoutput") + " ; Ouput was zero; hence comparison is true, so continue from this point\n";
	}
	else if	( test == "!=" )
	{
		// check: works
		newCode += "btfsc STATUS,2 ; Check if zero flag is clear\n";
		newCode += gotoCode("altoutput") + " ; Result from calculation was zero; hence comparison is false\n";
		newCode += gotoCode("stdoutput") + " ; Output was non-zero; hence comparison is true, so continue from this point\n";
	}
	else if	( test == ">=" )
	{
		// check: works
		newCode += "btfss STATUS,0 ; Check if carry flag is set\n";
		newCode += gotoCode("altoutput") + " ; Result from calculation is negative; hence comparison is false\n";
		newCode += gotoCode("stdoutput") + " ; Result from calculation is positive or zero; so continue from this point\n";
	}
	else if	( test == ">" )
	{
		// check: works
		newCode += "btfss STATUS,0 ; Check if carry flag is set\n";
		newCode += gotoCode("altoutput") + " ; Result is negative; hence comparison is false\n";
		newCode += "btfsc STATUS,2 ; Check if zero flag is set\n";
		newCode += gotoCode("altoutput") + " ; Result is zero; hence comparison is false\n";
		newCode += gotoCode("stdoutput") + " ; Comparison is true, so continue from this point\n";
	}
	else if	( test == "<" )
	{
		// check: works
		newCode += "btfsc STATUS,0 ; Check if carry flag is set\n";
		newCode += gotoCode("altoutput");
		newCode += gotoCode("stdoutput");
	}
	else if	( test == "<=" )
	{
		// check: works
		newCode += "btfsc STATUS,2 ; Check if result is zero\n";
		newCode += gotoCode("stdoutput") + " ; Result is zero; hence comparison is true\n";
		newCode += "btfsc STATUS,0 ; Check if carry flag is set\n";
		newCode += gotoCode("altoutput") + " ; Result is positive (not zero, has already tested for this); hence comparison is false\n";
		newCode += gotoCode("stdoutput") + " ; Result is negative, hence comparison is true\n";
	}
	
	code->addCodeBlock( id(), newCode );
#endif
}