Advertisement

Memory Allocating Error ( I'm Massed Up )

Started by July 25, 2016 03:41 PM
4 comments, last by zozzaloka 8 years, 4 months ago

hi I'm very exhausted for an allocating problem in my codeblocks project.

when I was running some mallocs in lvm_read app was dead.

I caught some problems looks like logical error in lvm_utility_getword. but It's still stopped running

I really don't know why ... ( I uploaded my project )

You'll probably get more/better help if you explain in more detail what your problem is and provide some of the relevant code inline instead of in a .zip file. Lots of people will just ignore your thread if you're expecting them to download, unzip, build, run and debug you entire project for you.

Advertisement

Have you tried running it in debug mode? It should be F8.

Remember to add debug symbols in the Settings > Compiler > Compiler settings, mark the "Produce debugging symbols [-g]" option.

You can also run it in valgrind (use "-v --track-origins=yes" options).

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).

thanks for your advices :)

my source structure is

main.c

- lvm_core.h , lvm_core.c

- - lvm_utility.h , lvm_utility.c

ptype.h pstring.h pstring.c pdata.h pdata.c pmath.h pmath.c <- my global includes

all headers include ptype.h and some global includes share each their codes.

main.c

==================================

#include <stdio.h>
#include <string.h>
#include "lvm/lvm_core.h"

int main( )
{
// local data
FILE *FPtr = fopen( "test_lvm.lvasm" , "r" ) ;
char TmpStr[ 64 ] = { 0 } , SrcStr[ 256 ] = { 0 } ;
int Cnt ;

// read source code
for( Cnt = 0 ; !( fgets( TmpStr , 50 , FPtr ) == NULL ) ; Cnt ++ )
strcat( SrcStr , TmpStr ) ;
fclose( FPtr ) ;

// call lvm
lvm_read( SrcStr , "main" ) ; < error occured in this !
lvm_unread( ) ;
return 0 ;
}

==================================

lvm_core.h

==================================

// core
#pragma once

// includes
#include <stdlib.h>
#include "lvm_utility/lvm_utility.h"

void lvm_read( BTYE *SrcCodeStr , BYTE *ProcMainStr ) ;
void lvm_unread( ) ;

==================================

lvm_core.c

==================================

// includes
#include "lvm_core.h"
#include <stdio.h>
// data
struct lvm_FLAG lvm_Flag ;
struct lvm_DIRECTOR lvm_Director ;
struct lvm_REGISTER lvm_Register ;
struct lvm_RAM lvm_Ram ;

// lvm functions
void lvm_read( BYTE *SrcCodeStr , BYTE *ProcMainStr )
{
// local data
BYTE *TmpStr , *TmpStrPtr , WordStr[ PSTRING_CHAR_LEN ] ;
INT Cnt , ProcCnt , OpCnt , MemCnt ;

// initialize flag
lvm_Flag.Interrupt = LVM_FLAG_INTON ;

// initialize info
lvm_Director.ProcProf = PTYPE_PTR_NULL ;
lvm_Director.MainProcProf.Hash = 0 ;
lvm_Director.MainProcProf.Index = 0 ;
lvm_Director.OpArr = PTYPE_PTR_NULL ;
lvm_Director.ProcQty = 0 ;
lvm_Director.OpQty = 0 ;
lvm_Ram.MemProf = PTYPE_PTR_NULL ;
lvm_Ram.MemQty = 0 ;

// get source length
for( Cnt = 0 ; !( SrcCodeStr[ Cnt ] == PSTRING_CHAR_NULL ) ; Cnt ++ ) ;
Cnt ++ ;

// allocate temp string and lex
TmpStr = ( BYTE* )malloc( sizeof( BYTE ) * Cnt ) ;
pstring_lexer( SrcCodeStr , Cnt , TmpStr ) ;

// initialize
TmpStrPtr = TmpStr ;

printf( "what?\n" ) ;

// pre-read source
ProcCnt = 0 , OpCnt = 0 , MemCnt = 0 ;
for( Cnt = 0 ; !( TmpStrPtr == PTYPE_PTR_NULL ) ; Cnt ++ )
{
// get word
TmpStrPtr = lvm_utility_getword( TmpStrPtr , WordStr ) ;

// check word
if( WordStr[ 0 ] == LVM_ASM_DIRECTOR_PROCCHAR )
{
// get next word
TmpStrPtr = lvm_utility_getword( TmpStrPtr , WordStr ) ;

// check main
if( pdata_hash_atoi( WordStr ) == pdata_hash_atoi( ProcMainStr ) )
{
// store main procedure profile
lvm_Director.MainProcProf.Hash = pdata_hash_atoi( WordStr ) ;
lvm_Director.MainProcProf.Index = OpCnt ;
}
else
{
// count procedure profile
ProcCnt ++ ;
}
}
else if( lvm_utility_judgeopcode( WordStr ) == LVM_ASM_OPCODE_ALLOC )
MemCnt ++ ;

// count op
OpCnt ++ ;
}

// check count
printf( "%d %d %d\n" , ProcCnt , OpCnt , MemCnt ) ;

// allocate
if( ProcCnt > 0 )
lvm_Director.ProcProf = ( struct lvm_PROCPROFILE* )malloc( sizeof( struct lvm_PROCPROFILE ) * ProcCnt ) ;
if( OpCnt > 0 )
lvm_Director.OpArr = ( void** )malloc( sizeof( void* ) * OpCnt ) ;
if( MemCnt > 0 )
lvm_Ram.MemProf = ( struct lvm_MEMPROFILE* )malloc( sizeof( struct lvm_MEMPROFILE ) * MemCnt ) ;

// store info
lvm_Director.ProcQty = ProcCnt ;
lvm_Director.OpQty = OpCnt ;
lvm_Ram.MemQty = MemCnt ;

// deallocate temp string
free( TmpStr ) ;
}
void lvm_unread( )
{
// local data
INT Cnt ;

// deallocate
if( lvm_Director.ProcQty > 0 )
free( lvm_Director.ProcProf ) ;
if( lvm_Director.OpQty > 0 )
{
for( Cnt = 0 ; Cnt < lvm_Director.OpQty ; Cnt ++ )
free( lvm_Director.OpArr[ Cnt ] ) ;
free( lvm_Director.OpArr ) ;
}
if( lvm_Ram.MemQty > 0 )
free( lvm_Ram.MemProf ) ;

// initialize info
lvm_Director.MainProcProf.Hash = 0 ;
lvm_Director.MainProcProf.Index = 0 ;
lvm_Director.ProcQty = 0 ;
lvm_Director.OpQty = 0 ;
lvm_Director.OpCurrent = 0 ;
lvm_Ram.MemQty = 0 ;

// initialize flag
lvm_Flag.Interrupt = LVM_FLAG_INTOFF ;
}

==================================

lvm_utility.h

==================================

#pragma once

// includes
#include <ptype.h>
#include <pstring/pstring.h>
#include <pdata/pdata.h>
#include <pmath/pmath.h>

// lvm constants
#define LVM_MEMORY_TBITCHAR "bit"
#define LVM_MEMORY_TNIBBLECHAR "nibble"
#define LVM_MEMORY_TBYTECHAR "byte"
#define LVM_MEMORY_TWORDCHAR "word"
#define LVM_MEMORY_TFLOATCHAR "float"
#define LVM_MEMORY_TUNIBBLECHAR "unibble"
#define LVM_MEMORY_TUBYTECHAR "ubyte"
#define LVM_MEMORY_TUWORDCHAR "uword"
#define LVM_MEMORY_TDBITCHAR "dbit"
#define LVM_MEMORY_TDNIBBLECHAR "dnibble"
#define LVM_MEMORY_TDBYTECHAR "dbyte"
#define LVM_MEMORY_TDWORDCHAR "dword"
#define LVM_MEMORY_TDFLOATCHAR "dfloat"
#define LVM_MEMORY_TUDNIBBLECHAR "udnibble"
#define LVM_MEMORY_TUDBYTECHAR "udbyte"
#define LVM_MEMORY_TUDWORDCHAR "udword"
enum
{
// register
LVM_REGISTER_INT = 0 ,
LVM_REGISTER_UINT ,
LVM_REGISTER_POINT ,
LVM_REGISTER_MAIN = 0 ,
LVM_REGISTER_SUB ,
LVM_REGISTER_AC ,

// memory - bit 1 , nibble 4 , byte 8 = char , word 16 = short , dword 32 = long
LVM_MEMORY_TNULL = 0 ,
LVM_MEMORY_TBIT ,
LVM_MEMORY_TNIBBLE ,
LVM_MEMORY_TBYTE ,
LVM_MEMORY_TWORD ,
LVM_MEMORY_TFLOAT ,

LVM_MEMORY_TUNIBBLE ,
LVM_MEMORY_TUBYTE ,
LVM_MEMORY_TUWORD ,

LVM_MEMORY_TDBIT ,
LVM_MEMORY_TDNIBBLE ,
LVM_MEMORY_TDBYTE ,
LVM_MEMORY_TDWORD ,
LVM_MEMORY_TDFLOAT ,

LVM_MEMORY_TUDNIBBLE ,
LVM_MEMORY_TUDBYTE ,
LVM_MEMORY_TUDWORD ,

// flag
LVM_FLAG_CMPNOTEQUAL = 0 ,
LVM_FLAG_CMPEQUAL ,
LVM_FLAG_CMPGREATER ,
LVM_FLAG_CMPLESSER ,
LVM_FLAG_CMPGREATEREQUAL ,
LVM_FLAG_CMPLESSEREQUAL ,
LVM_FLAG_INTOFF = 0 ,
LVM_FLAG_INTON ,
LVM_FLAG_INTMEMFALSE ,
LVM_FLAG_INTMEMTRUE ,
LVM_FLAG_INTDIVZERO
} ;

// lvasm constants
#define LVM_ASM_DIRECTOR_PROCCHAR 64
#define LVM_ASM_OPCODE_ALLOCSTR "allocate"
#define LVM_ASM_OPCODE_DEALLOCSTR "deallocate"
#define LVM_ASM_OPCODE_LOADSTR "load"
#define LVM_ASM_OPCODE_STORESTR "store"
#define LVM_ASM_OPCODE_MOVESTR "move"
#define LVM_ASM_OPCODE_ADDSTR "add"
#define LVM_ASM_OPCODE_SUBSTR "sub"
#define LVM_ASM_OPCODE_MULSTR "mul"
#define LVM_ASM_OPCODE_DIVSTR "div"
#define LVM_ASM_OPCODE_MODSTR "mod"
#define LVM_ASM_OPCODE_NOTSTR "not"
#define LVM_ASM_OPCODE_ANDSTR "and"
#define LVM_ASM_OPCODE_ORSTR "or"
#define LVM_ASM_OPCODE_XORSTR "xor"
#define LVM_ASM_OPCODE_LSSTR "ls"
#define LVM_ASM_OPCODE_RSSTR "rs"
#define LVM_ASM_OPCODE_CMPSTR "cmp"
#define LVM_ASM_OPCODE_JUMPSTR "jump"
#define LVM_ASM_OPCODE_RETURNSTR "return"
#define LVM_ASM_OPCODE_PROCSTR "@"
#define LVM_ASM_OPERAND_IRSTR "IR"
#define LVM_ASM_OPERAND_UIRSTR "UIR"
#define LVM_ASM_OPERAND_PRSTR "PR"
enum
{
LVM_ASM_OPCODE_ALLOC = 0 ,
LVM_ASM_OPCODE_DEALLOC ,
LVM_ASM_OPCODE_LOAD ,
LVM_ASM_OPCODE_STORE ,
LVM_ASM_OPCODE_MOVE ,
LVM_ASM_OPCODE_ADD ,
LVM_ASM_OPCODE_SUB ,
LVM_ASM_OPCODE_MUL ,
LVM_ASM_OPCODE_DIV ,
LVM_ASM_OPCODE_MOD ,
LVM_ASM_OPCODE_NOT ,
LVM_ASM_OPCODE_AND ,
LVM_ASM_OPCODE_OR ,
LVM_ASM_OPCODE_XOR ,
LVM_ASM_OPCODE_LS ,
LVM_ASM_OPCODE_RS ,
LVM_ASM_OPCODE_CMP ,
LVM_ASM_OPCODE_JUMP ,
LVM_ASM_OPCODE_RETURN ,
LVM_ASM_OPCODE_PROC ,
LVM_ASM_OPCODE_QTY ,
LVM_ASM_OPCODE_NONE ,
LVM_ASM_OPERAND_IR = 0 ,
LVM_ASM_OPERAND_UIR ,
LVM_ASM_OPERAND_PR ,
LVM_ASM_OPERAND_QTY = 4
} ;

// structures
// profiles
struct lvm_MEMPROFILE
{
void *Adr ;
BYTE Type ;
INT EleSize ;
BIT Ptr ;
} ;
struct lvm_PROCPROFILE
{
INT Hash ;
INT Index ;
} ;

// flag
struct lvm_FLAG
{
BIT Overflow ;
INT Compare : 3 , Interrupt : 3 ;
} ;

// director
struct lvm_DIRECTOR
{
struct lvm_PROCPROFILE MainProcProf ;
struct lvm_PROCPROFILE *ProcProf ;
void **OpArr ;
INT ProcQty , OpQty , OpCurrent , OpBefore , OpNext ;
} ;

// register
struct lvm_REGISTER
{
INT Integer[ 3 ] ;
unsigned INT UInteger[ 3 ] ;
DOUBLE Point[ 3 ] ;
} ;

// ram
struct lvm_RAM
{
struct lvm_MEMPROFILE *MemProf ;
INT MemQty ;
} ;

// memory data type - default / modified : bit->u x , dnibble->byte , dbyte->word
struct lvm_MEMORYBIT
{
INT Data : 1 ;
} ;
struct lvm_MEMORYNIBBLE
{
INT Data : 4 ;
} ;
struct lvm_MEMORYUNIBBLE
{
unsigned INT Data : 4 ;
} ;
struct lvm_MEMORYDBIT
{
INT Data : 2 ;
} ;

// functions
// syntax
BYTE* lvm_utility_getword( BYTE *FromStr , BYTE *ToStr ) ;
BYTE lvm_utility_judgeopcode( BYTE *CodeStr ) ;

==================================

lvm_utility.c

==================================

// includes
#include "lvm_utility.h"

// functions
// syntax
BYTE* lvm_utility_getword( BYTE *FromStr , BYTE *ToStr )
{
// local data
INT Cnt ;

// check null
if( FromStr[ 0 ] == PSTRING_CHAR_NULL )
{
ToStr[ 0 ] = PSTRING_CHAR_NULL ;
return PTYPE_PTR_NULL ;
}

// get a word
for( Cnt = 0 ; !( FromStr[ Cnt ] == PSTRING_CHAR_SPACE ) ; Cnt ++ )
ToStr[ Cnt ] = FromStr[ Cnt ] ;
ToStr[ Cnt ] = PSTRING_CHAR_NULL ;

// return next position of word-end
return &FromStr[ Cnt + 1 ] ;
}
BYTE lvm_utility_judgeopcode( BYTE *Str )
{
// judge
if( pdata_hash_atoi( Str ) == pdata_hash_atoi( LVM_ASM_OPCODE_ALLOCSTR ) )
return LVM_ASM_OPCODE_ALLOC ;
else if( pdata_hash_atoi( Str ) == pdata_hash_atoi( LVM_ASM_OPCODE_DEALLOCSTR ) )
return LVM_ASM_OPCODE_DEALLOC ;
else if( pdata_hash_atoi( Str ) == pdata_hash_atoi( LVM_ASM_OPCODE_LOADSTR ) )
return LVM_ASM_OPCODE_LOAD ;
else if( pdata_hash_atoi( Str ) == pdata_hash_atoi( LVM_ASM_OPCODE_STORESTR ) )
return LVM_ASM_OPCODE_STORE ;
else if( pdata_hash_atoi( Str ) == pdata_hash_atoi( LVM_ASM_OPCODE_MOVESTR ) )
return LVM_ASM_OPCODE_MOVE ;
else if( pdata_hash_atoi( Str ) == pdata_hash_atoi( LVM_ASM_OPCODE_ADDSTR ) )
return LVM_ASM_OPCODE_ADD ;
else if( pdata_hash_atoi( Str ) == pdata_hash_atoi( LVM_ASM_OPCODE_SUBSTR ) )
return LVM_ASM_OPCODE_SUB ;
else if( pdata_hash_atoi( Str ) == pdata_hash_atoi( LVM_ASM_OPCODE_MULSTR ) )
return LVM_ASM_OPCODE_MUL ;
else if( pdata_hash_atoi( Str ) == pdata_hash_atoi( LVM_ASM_OPCODE_DIVSTR ) )
return LVM_ASM_OPCODE_DIV ;
else if( pdata_hash_atoi( Str ) == pdata_hash_atoi( LVM_ASM_OPCODE_MODSTR ) )
return LVM_ASM_OPCODE_MOD ;
else if( pdata_hash_atoi( Str ) == pdata_hash_atoi( LVM_ASM_OPCODE_NOTSTR ) )
return LVM_ASM_OPCODE_NOT ;
else if( pdata_hash_atoi( Str ) == pdata_hash_atoi( LVM_ASM_OPCODE_ANDSTR ) )
return LVM_ASM_OPCODE_AND ;
else if( pdata_hash_atoi( Str ) == pdata_hash_atoi( LVM_ASM_OPCODE_ORSTR ) )
return LVM_ASM_OPCODE_OR ;
else if( pdata_hash_atoi( Str ) == pdata_hash_atoi( LVM_ASM_OPCODE_XORSTR ) )
return LVM_ASM_OPCODE_XOR ;
else if( pdata_hash_atoi( Str ) == pdata_hash_atoi( LVM_ASM_OPCODE_LSSTR ) )
return LVM_ASM_OPCODE_LS ;
else if( pdata_hash_atoi( Str ) == pdata_hash_atoi( LVM_ASM_OPCODE_RSSTR ) )
return LVM_ASM_OPCODE_RS ;
else if( pdata_hash_atoi( Str ) == pdata_hash_atoi( LVM_ASM_OPCODE_CMPSTR ) )
return LVM_ASM_OPCODE_CMP ;
else if( pdata_hash_atoi( Str ) == pdata_hash_atoi( LVM_ASM_OPCODE_JUMPSTR ) )
return LVM_ASM_OPCODE_JUMP ;
else if( pdata_hash_atoi( Str ) == pdata_hash_atoi( LVM_ASM_OPCODE_RETURNSTR ) )
return LVM_ASM_OPCODE_RETURN ;
else if( pdata_hash_atoi( Str ) == pdata_hash_atoi( LVM_ASM_OPCODE_PROCSTR ) )
return LVM_ASM_OPCODE_PROC ;
else
return LVM_ASM_OPCODE_NONE ;
}

==================================

ptype.h

==================================
#pragma once

// types
typedef enum { False , True } BIT ;
#define BYTE char
#define SHORT short // double byte
#define INT int // double short
#define LONG long // double short
#define FLOAT float // 32 bit
#define DOUBLE double // 64 bit

// constants
#define PTYPE_PTR_NULL NULL

==================================

pstring.h pstring.c

==================================

// includes
#include <stdlib.h>
#include "../ptype.h"
#include "../pmath/pmath.h"

#define PSTRING_CHAR_LEN 32

// junk
#define PSTRING_CHAR_NULL 0
#define PSTRING_CHAR_SPACE 32
#define PSTRING_CHAR_TAB 9
#define PSTRING_CHAR_LINEFEED 10
#define PSTRING_CHAR_CARRIAGERETURN 13

// name
#define PSTRING_CHAR_ZERO 48
#define PSTRING_CHAR_SMALLA 97
#define PSTRING_CHAR_LARGEA 65
#define PSTRING_CHAR_SMALLZ 122
#define PSTRING_CHAR_LARGEZ 90
#define PSTRING_CHAR_UNDERBAR 95

// spacer (){}[]<>''"".,
#define PSTRING_CHAR_PARENTHESISLEFT 40
#define PSTRING_CHAR_PARENTHESISRIGHT 41
#define PSTRING_CHAR_BRACELEFT 123
#define PSTRING_CHAR_BRACERIGHT 125
#define PSTRING_CHAR_SQUAREBRACKETLEFT 91
#define PSTRING_CHAR_SQUAREBRACKETRIGHT 93
#define PSTRING_CHAR_ANGLEBRACKETLEFT 60
#define PSTRING_CHAR_ANGLEBRACKETRIGHT 62
#define PSTRING_CHAR_SINGLEQUOTE 39
#define PSTRING_CHAR_DOUBLEQUOTE 34
#define PSTRING_CHAR_DOT 46
#define PSTRING_CHAR_COMMA 44

// rule
#define PSTRING_CHAR_BINSYM 98
#define PSTRING_CHAR_OCTSYM 111
#define PSTRING_CHAR_HEXSYM 104

// operation
#define PSTRING_CHAR_ADD 43
#define PSTRING_CHAR_SUB 45
#define PSTRING_CHAR_MUL 42
#define PSTRING_CHAR_DIV 47
#define PSTRING_CHAR_MOD 37
#define PSTRING_CHAR_NOT 126
#define PSTRING_CHAR_AND 38
#define PSTRING_CHAR_OR 124
#define PSTRING_CHAR_XOR 94

// sentence
#define PSTRING_CHAR_EXCLAMATION 33
#define PSTRING_CHAR_QUESTION 63
#define PSTRING_CHAR_SEMICOLON 59
#define PSTRING_CHAR_COLON 58
#define PSTRING_CHAR_AT 64
#define PSTRING_CHAR_SHARP 35
#define PSTRING_CHAR_DOLLAR 36

// special symbol
#define PSTRING_CHAR_DIR 92
#define PSTRING_CHAR_PROMPT 96
#define PSTRING_CHAR_SMALLORLARGEKEY 32

enum
{
PSTRING_TYPE_NULL = 0 ,
PSTRING_TYPE_JUNK ,
PSTRING_TYPE_NAME ,
PSTRING_TYPE_SPECIALSYMBOL ,
PSTRING_TYPE_RANGESYMBOL ,
PSTRING_TYPE_OTHER ,
PSTRING_TYPE_ALL ,
PSTRING_METHOD_NULL = 0 ,
PSTRING_METHOD_NUM ,
PSTRING_METHOD_SMALL ,
PSTRING_METHOD_LARGE ,
PSTRING_METHOD_UNDERBAR ,
PSTRING_METHOD_SPECIAL
} ;

BYTE pstring_judge( BYTE Char ) ;

void pstring_lexer( BYTE *FromStr , INT FromLen , BYTE *ToStr ) ;

...

BYTE pstring_judge( BYTE Char )
{
// check and judge character type
if( Char == PSTRING_CHAR_NULL )
return PSTRING_TYPE_NULL ;
else if
(
( Char == PSTRING_CHAR_SPACE )
|
( Char == PSTRING_CHAR_TAB )
|
( Char == PSTRING_CHAR_LINEFEED )
|
( Char == PSTRING_CHAR_CARRIAGERETURN )
)
return PSTRING_TYPE_JUNK ;
else if
(
( ( Char == PSTRING_CHAR_PARENTHESISLEFT ) | ( Char == PSTRING_CHAR_PARENTHESISRIGHT ) )
|
( ( Char == PSTRING_CHAR_BRACELEFT ) | ( Char == PSTRING_CHAR_BRACERIGHT ) )
|
( ( Char == PSTRING_CHAR_SQUAREBRACKETLEFT ) | ( Char == PSTRING_CHAR_SQUAREBRACKETRIGHT ) )
|
( ( Char == PSTRING_CHAR_ANGLEBRACKETLEFT ) | ( Char == PSTRING_CHAR_ANGLEBRACKETRIGHT ) )
|
( Char == PSTRING_CHAR_DOT )
|
( Char == PSTRING_CHAR_COMMA )
|
( Char == PSTRING_CHAR_ADD )
|
( Char == PSTRING_CHAR_SUB )
|
( Char == PSTRING_CHAR_MUL )
|
( Char == PSTRING_CHAR_DIV )
|
( Char == PSTRING_CHAR_MOD )
|
( Char == PSTRING_CHAR_NOT )
|
( Char == PSTRING_CHAR_AND )
|
( Char == PSTRING_CHAR_OR )
|
( Char == PSTRING_CHAR_XOR )
|
( Char == PSTRING_CHAR_EXCLAMATION )
|
( Char == PSTRING_CHAR_QUESTION )
|
( Char == PSTRING_CHAR_SEMICOLON )
|
( Char == PSTRING_CHAR_COLON )
|
( Char == PSTRING_CHAR_AT )
|
( Char == PSTRING_CHAR_SHARP )
|
( Char == PSTRING_CHAR_DOLLAR )
|
( Char == PSTRING_CHAR_DIR )
|
( Char == PSTRING_CHAR_PROMPT )
)
return PSTRING_TYPE_SPECIALSYMBOL ;
else if
(
( Char == PSTRING_CHAR_SINGLEQUOTE )
|
( Char == PSTRING_CHAR_DOUBLEQUOTE )
)
return PSTRING_TYPE_RANGESYMBOL ;
else if
(
( ( Char >= PSTRING_CHAR_ZERO ) & ( Char <= PSTRING_CHAR_ZERO + 9 ) )
|
( ( Char >= PSTRING_CHAR_SMALLA ) & ( Char <= PSTRING_CHAR_SMALLZ ) )
|
( ( Char >= PSTRING_CHAR_LARGEA ) & ( Char <= PSTRING_CHAR_LARGEZ ) )
|
( Char == PSTRING_CHAR_UNDERBAR )
)
return PSTRING_TYPE_NAME ;
else
return PSTRING_TYPE_OTHER ;
}

void pstring_lexer( BYTE *FromStr , INT FromLen , BYTE *ToStr )
{
// local data
BIT JunkFlag ;
INT FromCnt , ToCnt , RangeCnt ;

// extract word by token - junk , special character
FromCnt = 0 , ToCnt = 0 , RangeCnt = 0 , JunkFlag = False ;
while( !( FromStr[ FromCnt ] == PSTRING_CHAR_NULL ) & ( FromCnt < FromLen ) )
{
// check token
switch( pstring_judge( FromStr[ FromCnt ] ) )
{
case PSTRING_TYPE_JUNK :
// check range state
if( RangeCnt == 0 )
{
// increase index once
if( !JunkFlag )
{
// put space instead of junks
ToStr[ ToCnt ] = PSTRING_CHAR_SPACE , ToCnt ++ ;

// set flag
JunkFlag = True ;
}
}
else
{
// put junk
ToStr[ ToCnt ] = FromStr[ FromCnt ] , ToCnt ++ ;
ToStr[ ToCnt ] = PSTRING_CHAR_SPACE ;
}
break ;
case PSTRING_TYPE_SPECIALSYMBOL :
// check range state
if( RangeCnt == 0 )
{
// put space and token
ToStr[ ToCnt ] = PSTRING_CHAR_SPACE , ToCnt ++ ;
ToStr[ ToCnt ] = FromStr[ FromCnt ] , ToCnt ++ ;
ToStr[ ToCnt ] = PSTRING_CHAR_SPACE , ToCnt ++ ;

// set flag
JunkFlag = False ;
}
else
{
// put token
ToStr[ ToCnt ] = FromStr[ FromCnt ] , ToCnt ++ ;
ToStr[ ToCnt ] = PSTRING_CHAR_SPACE ;
}
break ;
case PSTRING_TYPE_RANGESYMBOL :
// put range symbol
ToStr[ ToCnt ] = PSTRING_CHAR_SPACE , ToCnt ++ ;
ToStr[ ToCnt ] = FromStr[ FromCnt ] , ToCnt ++ ;
ToStr[ ToCnt ] = PSTRING_CHAR_SPACE , ToCnt ++ ;

// set flag
JunkFlag = False ;

// count range symbol
RangeCnt ++ ;
if( RangeCnt > 2 ) RangeCnt = 0 ;
break ;
case PSTRING_TYPE_NAME :
// put character
ToStr[ ToCnt ] = FromStr[ FromCnt ] , ToCnt ++ ;
ToStr[ ToCnt ] = PSTRING_CHAR_SPACE ;

// set flag
JunkFlag = False ;
break ;
}

// count
FromCnt ++ ;
}

// put null character
ToCnt ++ , ToStr[ ToCnt ] = PSTRING_CHAR_NULL ;
}

==================================

pdata.h pdata.c

==================================

#pragma once

// includes
#include <stdlib.h>
#include "../ptype.h"

INT pdata_hash_atoi( BYTE *Str ) ;

...

INT pdata_hash_atoi( BYTE *Str )
{
// local data
INT HashCode = 0 ;
INT StrCnt , DigitCnt = 1 ;

// check string
for( StrCnt = 0 ; !( Str[ StrCnt ] == PSTRING_CHAR_NULL ) ; StrCnt ++ )
{
// ascii string to integer hash - increasing digit number from left to right
HashCode = HashCode + Str[ StrCnt ] * DigitCnt ;
DigitCnt ++ ;
}

// return hash code
return HashCode ;
}

==================================

pmath.h pmath.c

==================================

#pragma once

// includes
#include "../ptype.h"

// functions
// arithm
DOUBLE pmath_arithm_exp( DOUBLE Val , INT Exponent ) ;

...

// includes
#include "pmath.h"

// functions
// arithm
DOUBLE pmath_arithm_exp( DOUBLE Val , INT Exponent )
{
// local data
DOUBLE ResVal = 1.0 ;
INT Cnt ;

// check
if( Exponent == 0 )
return ResVal ;
else if( Exponent > 0 )
{
for( Cnt = Exponent ; Cnt > 0 ; Cnt -- )
ResVal = ResVal * Val ;
return ResVal ;
}
else
{
for( Cnt = Exponent ; Cnt < 0 ; Cnt ++ )
ResVal = ResVal * ( 1 / Val ) ;
return ResVal ;
}
}

==================================

when I compiled and ran lvm_read( SrcStr , "main" ) ; fully , in code blocks it was stopped and in bin folder

it printed "what?" "%d %d %d\n" but ProcCnt , OpCnt , MemCnt had too big values... and was stopped after printing. ( I know it was caused by uninitializing but don't know why it was different between bin folder execution and codeblock running. )

if I commented out from printf( "what?\n" ) ; to above // store info comment , it was stopped....

( plus ) sorry I found a cause but I faced an error " ntdll!RtlpNtMakeTemporaryKey " at call stack in debugging

What happens here do you think? In particular when your input file is more than 256 bytes?


    // local data
    FILE *FPtr = fopen( "test_lvm.lvasm" , "r" ) ;
    char TmpStr[ 64 ] = { 0 } , SrcStr[ 256 ] = { 0 } ;
    int Cnt ;

    // read source code
    for( Cnt = 0 ; !( fgets( TmpStr , 50 , FPtr ) == NULL ) ; Cnt ++ )
        strcat( SrcStr , TmpStr ) ;
    fclose( FPtr ) ;

I would like to suggest you do smaller tests. Take a routine, and write a new main program that only calls that function, and tests whether the right result gets returned.

Do that for various routines, so you can somewhat check that the code does something useful.

That should give you a handle on locating any problem that may exist (I only briefly browsed the post, it's too much to check in detail)

btw, why are you making an assembler?

What happens here do you think? In particular when your input file is more than 256 bytes?


    // local data
    FILE *FPtr = fopen( "test_lvm.lvasm" , "r" ) ;
    char TmpStr[ 64 ] = { 0 } , SrcStr[ 256 ] = { 0 } ;
    int Cnt ;

    // read source code
    for( Cnt = 0 ; !( fgets( TmpStr , 50 , FPtr ) == NULL ) ; Cnt ++ )
        strcat( SrcStr , TmpStr ) ;
    fclose( FPtr ) ;

I would like to suggest you do smaller tests. Take a routine, and write a new main program that only calls that function, and tests whether the right result gets returned.

Do that for various routines, so you can somewhat check that the code does something useful.

That should give you a handle on locating any problem that may exist (I only briefly browsed the post, it's too much to check in detail)

btw, why are you making an assembler?

THANKS SO MUCH I was just foolish X( and after succeeding to debug it , I was faced at some logical error but fixed.

especially in your comment "Take a routine, and write a new main program that only calls that function, and tests whether the right result

gets" it's a great tip for debugging despite simplest thing thanks again! :D

I'm making a virtual machine so assembler is required finally:)

This topic is closed to new replies.

Advertisement