libeval_compiler: fixed segfault for "(number)" expressions.

This commit is contained in:
Tomasz Wlostowski 2020-08-10 16:19:18 +02:00
parent a5a06e3c89
commit c88c8102e5
3 changed files with 7 additions and 28 deletions

View File

@ -172,7 +172,7 @@ nt(A) ::= nt(B) G_STRUCT_REF nt(C).
nt(A) ::= G_PARENL nt(B) G_PARENR. nt(A) ::= G_PARENL nt(B) G_PARENR.
{ {
A = newNode( pEval, B->op ); A = newNode( pEval, B->op, B->value );
A->leaf[0] = B->leaf[0]; A->leaf[0] = B->leaf[0];
A->leaf[1] = B->leaf[1]; A->leaf[1] = B->leaf[1];
} }

View File

@ -68,6 +68,8 @@ TREE_NODE* newNode( LIBEVAL::COMPILER* compiler, int op, const T_TOKEN_VALUE& va
t2->srcPos = compiler->GetSourcePos(); t2->srcPos = compiler->GetSourcePos();
t2->uop = nullptr; t2->uop = nullptr;
libeval_dbg(10, " ostr %p nstr %p nnode %p op %d", value.str, t2->value.str, t2, t2->op );
if(t2->value.str) if(t2->value.str)
compiler->GcItem( t2->value.str ); compiler->GcItem( t2->value.str );
@ -145,7 +147,6 @@ UCODE::~UCODE()
{ {
for ( auto op : m_ucode ) for ( auto op : m_ucode )
{ {
printf("destroy uop %p\n", op );
delete op; delete op;
} }
} }
@ -169,7 +170,6 @@ wxString TOKENIZER::GetChars( std::function<bool( wxUniChar )> cond ) const
{ {
wxString rv; wxString rv;
size_t p = m_pos; size_t p = m_pos;
// printf("p %d len %d\n", p, str.length() );
while( p < m_str.length() && cond( m_str[p] ) ) while( p < m_str.length() && cond( m_str[p] ) )
{ {
@ -287,7 +287,7 @@ bool COMPILER::Compile( const wxString& aString, UCODE* aCode, CONTEXT* aPreflig
if( tok.value.str ) if( tok.value.str )
GcItem( tok.value.str ); GcItem( tok.value.str );
libeval_dbg(10, "parse: tok %d\n", tok.token ); libeval_dbg(10, "parse: tok %d valstr %p\n", tok.token, tok.value.str );
Parse( m_parser, tok.token, tok, this ); Parse( m_parser, tok.token, tok, this );
if ( m_errorStatus.pendingError ) if ( m_errorStatus.pendingError )
@ -332,7 +332,6 @@ T_TOKEN COMPILER::getToken()
done = lexString( rv ); done = lexString( rv );
break; break;
} }
//printf( "-> lstate %d done %d\n", m_lexerState, !!done );
} while( !done ); } while( !done );
return rv; return rv;
@ -342,8 +341,7 @@ T_TOKEN COMPILER::getToken()
bool COMPILER::lexString( T_TOKEN& aToken ) bool COMPILER::lexString( T_TOKEN& aToken )
{ {
wxString str = m_tokenizer.GetChars( []( int c ) -> bool { return c != '\''; } ); wxString str = m_tokenizer.GetChars( []( int c ) -> bool { return c != '\''; } );
//printf("STR LIT '%s'\n", (const char *)str.c_str() );
aToken.token = G_STRING; aToken.token = G_STRING;
aToken.value.str = new wxString( str ); aToken.value.str = new wxString( str );
@ -383,7 +381,6 @@ bool COMPILER::lexDefault( T_TOKEN& aToken )
retval.value.str = nullptr; retval.value.str = nullptr;
retval.token = G_ENDS; retval.token = G_ENDS;
//printf( "tokdone %d\n", !!m_tokenizer.Done() );
if( m_tokenizer.Done() ) if( m_tokenizer.Done() )
{ {
aToken = retval; aToken = retval;
@ -423,8 +420,6 @@ bool COMPILER::lexDefault( T_TOKEN& aToken )
if( isDecimalSeparator( current[i - 1] ) ) if( isDecimalSeparator( current[i - 1] ) )
current[i - 1] = m_localeDecimalSeparator; current[i - 1] = m_localeDecimalSeparator;
} }
//printf("-> NUM: '%s'\n", (const char *) current.c_str() );
}; };
@ -456,7 +451,6 @@ bool COMPILER::lexDefault( T_TOKEN& aToken )
} }
else if( ( convertFrom = resolveUnits() ) >= 0 ) else if( ( convertFrom = resolveUnits() ) >= 0 )
{ {
//printf("unit\n");
// UNIT // UNIT
// Units are appended to a VALUE. // Units are appended to a VALUE.
// Determine factor to default unit if unit for value is given. // Determine factor to default unit if unit for value is given.
@ -468,18 +462,13 @@ bool COMPILER::lexDefault( T_TOKEN& aToken )
} }
else if( ch == '\'' ) // string literal else if( ch == '\'' ) // string literal
{ {
//printf( "MATCH STRING LITERAL\n" );
m_lexerState = LS_STRING; m_lexerState = LS_STRING;
m_tokenizer.NextChar(); m_tokenizer.NextChar();
return false; return false;
} }
else if( isalpha( ch ) || ch == '_' ) else if( isalpha( ch ) || ch == '_' )
{ {
//printf("ALPHA\n");
current = m_tokenizer.GetChars( []( int c ) -> bool { return isalnum( c ) || c == '_'; } ); current = m_tokenizer.GetChars( []( int c ) -> bool { return isalnum( c ) || c == '_'; } );
//printf("Len: %d\n", current.length() );
//printf("id '%s'\n", (const char *) current.c_str() );
//fflush( stdout );
retval.token = G_IDENTIFIER; retval.token = G_IDENTIFIER;
retval.value.str = new wxString( current ); retval.value.str = new wxString( current );
m_tokenizer.NextChar( current.length() ); m_tokenizer.NextChar( current.length() );
@ -546,7 +535,7 @@ bool COMPILER::lexDefault( T_TOKEN& aToken )
const wxString formatNode( TREE_NODE* node ) const wxString formatNode( TREE_NODE* node )
{ {
return *(node->value.str); return node->value.str ? *(node->value.str) : "";
} }
@ -655,8 +644,6 @@ void COMPILER::setRoot( TREE_NODE *root )
void COMPILER::freeTree( LIBEVAL::TREE_NODE *tree ) void COMPILER::freeTree( LIBEVAL::TREE_NODE *tree )
{ {
printf("->FreeTre %p\n", tree );
if ( tree->leaf[0] ) if ( tree->leaf[0] )
freeTree( tree->leaf[0] ); freeTree( tree->leaf[0] );
if ( tree->leaf[1] ) if ( tree->leaf[1] )
@ -664,14 +651,8 @@ void COMPILER::freeTree( LIBEVAL::TREE_NODE *tree )
if( tree->uop ) if( tree->uop )
{ {
printf("Free uop %p\n", tree->uop );
delete tree->uop; delete tree->uop;
} }
/* if( tree->value.str )
delete tree->value.str;
delete tree;*/
} }
void TREE_NODE::SetUop( int aOp, double aValue ) void TREE_NODE::SetUop( int aOp, double aValue )
@ -856,7 +837,6 @@ bool COMPILER::generateUCode( UCODE* aCode, CONTEXT* aPreflightContext )
if( son && son->op == TR_UNIT ) if( son && son->op == TR_UNIT )
{ {
//printf( "HandleUnit: %s unit %d\n", node->value.str, son->value.type );
int units = son->value.idx; int units = son->value.idx;
value = m_unitResolver->Convert( *node->value.str, units ); value = m_unitResolver->Convert( *node->value.str, units );
visitedNodes.insert( son ); visitedNodes.insert( son );
@ -946,7 +926,6 @@ void UOP::Exec( CONTEXT* ctx )
return; return;
case TR_OP_METHOD_CALL: case TR_OP_METHOD_CALL:
//printf("CALL METHOD %s\n" );
m_func( ctx, m_ref.get() ); m_func( ctx, m_ref.get() );
return; return;

View File

@ -1136,7 +1136,7 @@ static YYACTIONTYPE yy_reduce(
case 18: /* nt ::= G_PARENL nt G_PARENR */ case 18: /* nt ::= G_PARENL nt G_PARENR */
#line 174 "grammar.lemon" #line 174 "grammar.lemon"
{ {
yymsp[-2].minor.yy31 = newNode( pEval, yymsp[-1].minor.yy31->op ); yymsp[-2].minor.yy31 = newNode( pEval, yymsp[-1].minor.yy31->op, yymsp[-1].minor.yy31->value );
yymsp[-2].minor.yy31->leaf[0] = yymsp[-1].minor.yy31->leaf[0]; yymsp[-2].minor.yy31->leaf[0] = yymsp[-1].minor.yy31->leaf[0];
yymsp[-2].minor.yy31->leaf[1] = yymsp[-1].minor.yy31->leaf[1]; yymsp[-2].minor.yy31->leaf[1] = yymsp[-1].minor.yy31->leaf[1];
} }