varcomparison.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2003 by David Saxton                                    *
00003  *   david@bluehaze.org                                                    *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  ***************************************************************************/
00010 
00011 #include "varcomparison.h"
00012 
00013 #include "libraryitem.h"
00014 #include "flowcode.h"
00015 
00016 #include <klocale.h>
00017 
00018 Item* VarComparison::construct( ItemDocument *itemDocument, bool newItem, const char *id )
00019 {
00020         return new VarComparison( (ICNDocument*)itemDocument, newItem, id );
00021 }
00022 
00023 LibraryItem* VarComparison::libraryItem()
00024 {
00025         return new LibraryItem(
00026                 QString("flow/varcomparison"),
00027                 i18n("Comparison"),
00028                 i18n("Variables"),
00029                 "branch.png",
00030                 LibraryItem::lit_flowpart,
00031                 VarComparison::construct );
00032 }
00033 
00034 VarComparison::VarComparison( ICNDocument *icnDocument, bool newItem, const char *id )
00035         : FlowPart( icnDocument, newItem, (id) ? id : "varcomparison" )
00036 {
00037         m_name = i18n("Variable Comparison");
00038         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>");
00039         initDecisionSymbol();
00040         createStdInput();
00041         createStdOutput();
00042         createAltOutput();
00043         
00044         createProperty( "0var1", Variant::Type::Combo );
00045         property("0var1")->setCaption( i18n("Variable") );
00046         property("0var1")->setValue("x");
00047         
00048         createProperty( "1op", Variant::Type::Select );
00049         property("1op")->setAllowed( QStringList::split( ',', "==,<,>,<=,>=,!=" ) );
00050         property("1op")->setValue("==");
00051         property("1op")->setToolbarCaption(" ");
00052         property("1op")->setEditorCaption( i18n("Operation") );
00053         
00054         createProperty( "2var2", Variant::Type::Combo );
00055         property("2var2")->setToolbarCaption(" ");
00056         property("2var2")->setEditorCaption( i18n("Value") );
00057         property("2var2")->setValue("0");
00058         
00059         addDisplayText( "output_false", QRect( offsetX()+width(), 2, 40, 20 ), "No" );
00060         addDisplayText( "output_true", QRect( 0, offsetY()+height(), 50, 20 ), "Yes" ); 
00061 }
00062 
00063 VarComparison::~VarComparison()
00064 {
00065 }
00066 
00067 void VarComparison::dataChanged()
00068 {
00069         setCaption( dataString("0var1") + " " + dataString("1op") + " " + dataString("2var2") + " ?" );
00070 }
00071 
00072 QString VarComparison::oppOp( const QString &op )
00073 {
00074         if              ( op == "==" )  return "!=";
00075         if              ( op == "!=" )  return "==";
00076         else if ( op == "<" )   return ">=";
00077         else if ( op == ">=" )  return "<";
00078         else if ( op == ">" )   return "<=";
00079         else if ( op == "<=" )  return ">";
00080         else return "__UNKNOWN_OP__";
00081 }
00082 
00083 void VarComparison::generateMicrobe( FlowCode *code )
00084 {
00085         QString var1 = dataString("0var1");
00086         QString var2 = dataString("2var2");
00087         QString test = dataString("1op");
00088         
00089         handleIfElse( code, var1+" "+test+" "+var2, var1+" "+oppOp(test)+" "+var2, "stdoutput", "altoutput" );
00090         
00091 #if 0
00092         code->addCode( "if "+var1+" "+test+" "+var2+"\n{\n" );
00093         code->addCodeBranch( outputPart("stdoutput") );
00094         code->addCode("}");
00095         if ( outputPart("altoutput") )
00096         {
00097                 code->addCode("else\n{");
00098                 code->addCodeBranch( outputPart("altoutput") );
00099                 code->addCode("}");
00100         }
00101 #endif
00102         
00103 #if 0
00104         QString newCode;
00105         
00106         if ( FlowCode::isLiteral(var2) ) newCode += "movlw " + var2 + " ; Move literal to register w\n";
00107         else
00108         {
00109                 code->addVariable(var2);
00110                 newCode += "movf " + var2 + ",0 ; Move " + var2 + " to register w\n";
00111         }
00112         
00113         if ( FlowCode::isLiteral(var1) ) newCode += "sublw " + var1 + " ; Subtract register w from " + var1 + ", placing result in w\n";
00114         else
00115         {
00116                 code->addVariable(var1);
00117                 newCode += "subwf " + var1 + ",0 ; Subtract register w from " + var1 + ", placing result in w\n";
00118         }
00119         
00120         
00121         if              ( test == "==" )
00122         {
00123                 // check: works
00124                 newCode += "btfss STATUS,2 ; Check if zero flag is set\n";
00125                 newCode += gotoCode("altoutput") + " ; Result from calculation was non-zero; hence comparison is false\n";
00126                 newCode += gotoCode("stdoutput") + " ; Ouput was zero; hence comparison is true, so continue from this point\n";
00127         }
00128         else if ( test == "!=" )
00129         {
00130                 // check: works
00131                 newCode += "btfsc STATUS,2 ; Check if zero flag is clear\n";
00132                 newCode += gotoCode("altoutput") + " ; Result from calculation was zero; hence comparison is false\n";
00133                 newCode += gotoCode("stdoutput") + " ; Output was non-zero; hence comparison is true, so continue from this point\n";
00134         }
00135         else if ( test == ">=" )
00136         {
00137                 // check: works
00138                 newCode += "btfss STATUS,0 ; Check if carry flag is set\n";
00139                 newCode += gotoCode("altoutput") + " ; Result from calculation is negative; hence comparison is false\n";
00140                 newCode += gotoCode("stdoutput") + " ; Result from calculation is positive or zero; so continue from this point\n";
00141         }
00142         else if ( test == ">" )
00143         {
00144                 // check: works
00145                 newCode += "btfss STATUS,0 ; Check if carry flag is set\n";
00146                 newCode += gotoCode("altoutput") + " ; Result is negative; hence comparison is false\n";
00147                 newCode += "btfsc STATUS,2 ; Check if zero flag is set\n";
00148                 newCode += gotoCode("altoutput") + " ; Result is zero; hence comparison is false\n";
00149                 newCode += gotoCode("stdoutput") + " ; Comparison is true, so continue from this point\n";
00150         }
00151         else if ( test == "<" )
00152         {
00153                 // check: works
00154                 newCode += "btfsc STATUS,0 ; Check if carry flag is set\n";
00155                 newCode += gotoCode("altoutput");
00156                 newCode += gotoCode("stdoutput");
00157         }
00158         else if ( test == "<=" )
00159         {
00160                 // check: works
00161                 newCode += "btfsc STATUS,2 ; Check if result is zero\n";
00162                 newCode += gotoCode("stdoutput") + " ; Result is zero; hence comparison is true\n";
00163                 newCode += "btfsc STATUS,0 ; Check if carry flag is set\n";
00164                 newCode += gotoCode("altoutput") + " ; Result is positive (not zero, has already tested for this); hence comparison is false\n";
00165                 newCode += gotoCode("stdoutput") + " ; Result is negative, hence comparison is true\n";
00166         }
00167         
00168         code->addCodeBlock( id(), newCode );
00169 #endif
00170 }

Generated on Tue May 8 17:05:34 2007 for KTechLab by  doxygen 1.5.1