summaryrefslogtreecommitdiffstats
path: root/src/flowparts/varassignment.cpp
blob: d40af6129ce76b7578b675af3d34793459e9dd94 (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
/***************************************************************************
 *   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 "varassignment.h"

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

#include <klocale.h>

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

LibraryItem* VarAssignment::libraryItem()
{
	return new LibraryItem(
		TQString("flow/varassignment"),
		i18n("Assignment"),
		i18n("Variables"),
		"assignment.png",
		LibraryItem::lit_flowpart,
		VarAssignment::construct );
}

VarAssignment::VarAssignment( ICNDocument *icnDocument, bool newItem, const char *id )
	: FlowPart( icnDocument, newItem, (id) ? id : "varassignment" )
{
	m_name = i18n("Variable Assignment");
	m_desc = i18n("Assigns the evaluation of an expression to a variable. The expression can take many forms. For example:<ul><li><b>x = 2</b></li><li><b>x = y + 3</b></li><li><b>x = y + z</b></li><li><b>x = 2 * y</b></ul>");
	initProcessSymbol();
	createStdInput();
	createStdOutput();
	
	createProperty( "0-var1", Variant::Type::VarName );
	property("0-var1")->setCaption( i18n("Variable") );
	property("0-var1")->setValue("x");
	
	createProperty( "2-var2", Variant::Type::Combo );
	property("2-var2")->setToolbarCaption(" = ");
	property("2-var2")->setEditorCaption( i18n("Value") );
	property("2-var2")->setValue("0");
	
}

VarAssignment::~VarAssignment()
{
}

void VarAssignment::dataChanged()
{
	setCaption( dataString("0-var1") + " " + "=" /*dataString("1-op")*/ + " " + dataString("2-var2") );
}

void VarAssignment::generateMicrobe( FlowCode *code )
{
	code->addCode( dataString("0-var1")+" "+"="/*dataString("1-op")*/+" "+dataString("2-var2") );
	code->addCodeBranch( outputPart("stdoutput") );

#if 0
	TQString var1 = dataString("0-var1");
	TQString var2 = dataString("2-var2");
	TQString op = dataString("1-op");
	
	if ( FlowCode::isLiteral(var1) ) return;
	code->addVariable(var1);
	
	TQString newCode;
	
	if ( !FlowCode::isLiteral(var1) )
	{
		if ( FlowCode::isLiteral(var2) ) newCode += "movlw " + var2 + " ; Assign " + var2 + " to w register\n";
	}
	
	if ( !FlowCode::isLiteral(var2) )
	{
		code->addVariable(var2);
		newCode += "movf " + var2 + ",0 ; Move " + var2 + " to w register\n";
	}
	
	if		( op == "=" ) newCode += "movwf " + var1 + " ; Move contents of w register to " + var1 + "\n";
	else if ( op == "+=" ) newCode += "addwf " + var1 + ",1 ; Add contents of w register to " + var1 + " and place result back in " + var1 + "\n";
	else if ( op == "-=" ) newCode += "subwf " + var1 + ",1 ; Subtract contents of w register from " + var1 + " and place result back in " + var1 + "\n";
	else if ( op == "&=" ) newCode += "andwf " + var1 + ",1 ; Binary AND contents of w register with " + var1 + " and place result back in " + var1 + "\n";
	else if ( op == "or=" ) newCode += "iorwf " + var1 + ",1 ; Binary inclusive OR contents of w register with " + var1 + " and place result back in " + var1 + "\n";
	else if ( op == "xor=" ) newCode += "xorwf " + var1 + ",1 ; Binary exclusive OR contents of w register with " + var1 + " and place result back in " + var1 + "\n";
	
	newCode += gotoCode("stdoutput");
	
	code->addCodeBlock( id(), newCode );
#endif
}