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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
/**
* @file am_param.cpp
*/
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 1992-2017 Jean-Pierre Charras <jp.charras at wanadoo.fr>
* Copyright (C) 2010 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <am_param.h>
#include <am_primitive.h>
#include <aperture_macro.h>
#include <macros.h>
extern int ReadInt( char*& text, bool aSkipSeparator = true );
extern double ReadDouble( char*& text, bool aSkipSeparator = true );
extern double Evaluate( AM_PARAM_EVAL_STACK& aExp );
/* Class AM_PARAM
* holds a parameter value for an "aperture macro" as defined within
* standard RS274X. The parameter can be a constant, i.e. "immediate" parameter,
* or depend on some defered values, defined in a D_CODE, by the ADD command.
* Note the actual value could need an evaluation from an arithmetical expression
* items in the expression are stored in .
* A simple definition is just a value stored in one item in m_paramStack
*/
AM_PARAM::AM_PARAM( )
{
m_index = -1;
}
/**
* Function IsImmediate
* tests if this AM_PARAM holds an immediate parameter or has parameter
* held by an owning D_CODE.
*/
bool AM_PARAM::IsImmediate() const
{
bool is_immediate = true;
for( unsigned ii = 0; ii < m_paramStack.size(); ii++ )
{
if( m_paramStack[ii].IsDefered() )
{ // a defered value is found in operand list,
// so the parameter is not immediate
is_immediate = false;
break;
}
}
return is_immediate;
}
double AM_PARAM::GetValueFromMacro( APERTURE_MACRO* aApertureMacro ) const
{
// In macros, actual values are sometimes given by an expression like:
// 0-$2/2-$4
// Because arithmetic predence is used, the parameters (values (double) and operators)
// are stored in a stack, with all numeric values converted to the actual values
// when they are defered parameters
// Each item is stored in a AM_PARAM_EVAL (a value or an operator)
//
// Then the stack with all values resolved is parsed and numeric values
// calculated according to the precedence of operators
double curr_value = 0.0;
parm_item_type op_code;
AM_PARAM_EVAL_STACK ops;
for( unsigned ii = 0; ii < m_paramStack.size(); ii++ )
{
AM_PARAM_ITEM item = m_paramStack[ii];
switch( item.GetType() )
{
case ADD:
case SUB:
case MUL:
case DIV: // just an operator for next parameter value
case OPEN_PAR:
case CLOSE_PAR: // Priority modifiers: store in stack
op_code = item.GetType();
ops.emplace_back( op_code );
break;
case PUSHPARM:
// a defered value: get the actual parameter from the aperture macro
if( aApertureMacro ) // should be always true here
{
// Get the actual value
curr_value = aApertureMacro->GetLocalParamValue( item.GetIndex() );
}
else
{
wxFAIL_MSG( wxT( "AM_PARAM::GetValue(): NULL param aApertureMacro" ) );
}
ops.emplace_back( curr_value );
break;
case PUSHVALUE: // a value is on the stack:
curr_value = item.GetValue();
ops.emplace_back( curr_value );
break;
default:
wxFAIL_MSG( wxString::Format( wxT( "AM_PARAM::GetValue(): unexpected prm type %d" ),
item.GetType() ) );
break;
}
}
double result = Evaluate( ops );
return result;
}
/**
* add an operator/operand to the current stack
* aType = NOP, PUSHVALUE, PUSHPARM, ADD, SUB, MUL, DIV, EQUATE
* aValue required only for PUSHVALUE (double) or PUSHPARM (int) aType.
*/
void AM_PARAM::PushOperator( parm_item_type aType, double aValue )
{
AM_PARAM_ITEM item( aType, aValue);
m_paramStack.push_back( item );
}
void AM_PARAM::PushOperator( parm_item_type aType, int aValue )
{
AM_PARAM_ITEM item( aType, aValue);
m_paramStack.push_back( item );
}
/**
* Function ReadParam
* Read one aperture macro parameter
* a parameter can be:
* a number
* a reference to an aperture definition parameter value: $1 ot $3 ...
* a parameter definition can be complex and have operators between numbers and/or other parameter
* like $1+3 or $2x2..
* Note minus sign is not always an operator. It can be the sign of a value.
* Parameters are separated by a comma ( of finish by *)
* @param aText = pointer to the parameter to read. Will be modified to point to the next field
* @return true if a param is read, or false
*/
bool AM_PARAM::ReadParamFromAmDef( char*& aText )
{
bool found = false;
int ivalue;
double dvalue;
bool end = false;
while( !end )
{
switch( *aText )
{
case ',':
aText++;
if( !found ) // happens when a string starts by ',' before any param
break; // just skip this separator
KI_FALLTHROUGH;
case '\n':
case '\r':
case 0: // EOL
case '*': // Terminator in a gerber command
end = true;
break;
case ' ':
aText++;
break;
case '$':
// defered value defined later, in ADD command which define defered parameters
++aText;
ivalue = ReadInt( aText, false );
if( m_index < 1 )
SetIndex( ivalue );
PushOperator( PUSHPARM, ivalue );
found = true;
break;
case '/':
PushOperator( DIV );
aText++;
break;
case '(': // Open a block to evaluate an expression between '(' and ')'
PushOperator( OPEN_PAR );
aText++;
break;
case ')': // close a block between '(' and ')'
PushOperator( CLOSE_PAR );
aText++;
break;
case 'x':
case 'X':
PushOperator( MUL );
aText++;
break;
case '-':
case '+':
// Test if this is an operator between 2 params, or the sign of a value
if( m_paramStack.size() > 0 && !m_paramStack.back().IsOperator() )
{ // Seems an operator
PushOperator( *aText == '+' ? ADD : SUB );
aText++;
}
else
{ // seems the sign of a value
dvalue = ReadDouble( aText, false );
PushOperator( PUSHVALUE, dvalue );
found = true;
}
break;
case '=': // A local definition found like $4=$3/2
// At this point, one defered parameter is expected to be read.
// this parameter value (the index) is stored in m_index.
// The list of items is cleared
aText++;
m_paramStack.clear();
found = false;
break;
default:
dvalue = ReadDouble( aText, false );
PushOperator( PUSHVALUE, dvalue );
found = true;
break;
}
}
return found;
}
|