Pol  Revision:4b29d2b
eprog3.cpp
Go to the documentation of this file.
1 
7 #include <cstring>
8 #include <iomanip>
9 
10 #include "eprog.h"
11 #include "symcont.h"
12 #include "token.h"
13 #include "tokens.h"
14 
15 namespace Pol
16 {
17 namespace Bscript
18 {
19 void EScriptProgram::dump( std::ostream& os )
20 {
21  Token token;
22  unsigned PC;
23  if ( !exported_functions.empty() )
24  {
25  os << "Exported Functions:" << std::endl;
26  os << " PC Args Name" << std::endl;
27  for ( auto& elem : exported_functions )
28  {
29  os << std::setw( 5 ) << elem.PC << std::setw( 6 ) << elem.nargs << " " << elem.name
30  << std::endl;
31  }
32  }
33  unsigned nLines = tokens.length() / sizeof( StoredToken );
34  for ( PC = 0; PC < nLines; PC++ )
35  {
36  if ( _readToken( token, PC ) )
37  {
38  return;
39  }
40  else
41  {
42  if ( fileline.size() > PC && !fileline[PC].empty() )
43  os << fileline[PC] << std::endl;
44  if ( program_PC == PC && !program_decl.empty() )
45  os << program_decl << std::endl;
46  if ( function_decls.size() > PC && !function_decls[PC].empty() )
47  os << function_decls[PC] << std::endl;
48  if ( sourcelines.size() > PC && !sourcelines[PC].empty() )
49  os << sourcelines[PC] << std::endl;
50  os << PC << ": " << token << std::endl;
51  if ( token.id == INS_CASEJMP )
52  {
53  dump_casejmp( os, token );
54  }
55  }
56  }
57 }
58 
59 void EScriptProgram::dump_casejmp( std::ostream& os, const Token& token )
60 {
61  const unsigned char* dataptr = token.dataptr;
62  for ( ;; )
63  {
64  unsigned short offset;
65  std::memcpy( &offset, dataptr, sizeof( unsigned short ) );
66  dataptr += 2;
67  unsigned char type = *dataptr;
68  dataptr += 1;
69  if ( type == CASE_TYPE_LONG )
70  {
71  int lval;
72  std::memcpy( &lval, dataptr, sizeof( int ) );
73  dataptr += 4;
74  os << "\t" << lval << ": @" << offset << std::endl;
75  }
76  else if ( type == CASE_TYPE_DEFAULT )
77  {
78  os << "\tdefault: @" << offset << std::endl;
79  break;
80  }
81  else
82  { // type is the length of the string, otherwise
83  os << "\t\"" << std::string( (const char*)dataptr, type ) << "\": @" << offset << std::endl;
84  dataptr += type;
85  }
86  }
87 }
88 }
89 }
void dump_casejmp(std::ostream &os, const Token &token)
Definition: eprog3.cpp:59
BTokenId id
Definition: token.h:39
std::vector< EPExportedFunction > exported_functions
Definition: eprog.h:117
int _readToken(Token &token, unsigned position) const
Definition: eprog_read.cpp:198
std::vector< std::string > fileline
Definition: eprog.h:114
std::vector< std::string > sourcelines
Definition: eprog.h:113
unsigned length(void) const
Definition: symcont.h:67
void dump(std::ostream &os)
Definition: eprog3.cpp:19
const unsigned char * dataptr
Definition: token.h:50
std::vector< std::string > function_decls
Definition: eprog.h:115
StoredTokenContainer tokens
Definition: eprog.h:93
Definition: berror.cpp:12
std::string program_decl
Definition: eprog.h:112