KiCad PCB EDA Suite
LIBEVAL Namespace Reference

Classes

class  COMPILER
 
class  CONTEXT
 
struct  ERROR_STATUS
 
struct  T_TOKEN
 
struct  T_TOKEN_VALUE
 
class  TOKENIZER
 
class  TREE_NODE
 
class  UCODE
 
class  UNIT_RESOLVER
 
class  UOP
 
class  VALUE
 
class  VAR_REF
 

Typedefs

typedef std::function< void(CONTEXT *, void *)> FUNC_CALL_REF
 

Enumerations

enum  COMPILATION_STAGE { CST_PARSE = 0, CST_CODEGEN, CST_RUNTIME }
 
enum  VAR_TYPE_T { VT_STRING = 1, VT_NUMERIC, VT_UNDEFINED, VT_PARSE_ERROR }
 
enum  TOKEN_TYPE_T {
  TR_UNDEFINED = 0, TR_NUMBER = 1, TR_IDENTIFIER = 2, TR_ASSIGN = 3,
  TR_STRUCT_REF = 4, TR_STRING = 5, TR_UNIT = 6, TR_ARG_LIST = 7,
  TR_NULL = 8
}
 

Functions

TREE_NODEnewNode (LIBEVAL::COMPILER *compiler, int op, const T_TOKEN_VALUE &value)
 
static const wxString formatOpName (int op)
 
const wxString formatNode (TREE_NODE *node)
 
void dumpNode (wxString &buf, TREE_NODE *tok, int depth=0)
 
static void prepareTree (LIBEVAL::TREE_NODE *node)
 
static std::vector< TREE_NODE * > squashParamList (TREE_NODE *root)
 

Variables

constexpr T_TOKEN_VALUE defaultTokenValue = { nullptr, 0.0, 0 }
 
constexpr T_TOKEN defaultToken = { TR_UNDEFINED, defaultTokenValue }
 

Typedef Documentation

◆ FUNC_CALL_REF

typedef std::function<void( CONTEXT*, void* )> LIBEVAL::FUNC_CALL_REF

Definition at line 106 of file libeval_compiler.h.

Enumeration Type Documentation

◆ COMPILATION_STAGE

Enumerator
CST_PARSE 
CST_CODEGEN 
CST_RUNTIME 

Definition at line 65 of file libeval_compiler.h.

◆ TOKEN_TYPE_T

Enumerator
TR_UNDEFINED 
TR_NUMBER 
TR_IDENTIFIER 
TR_ASSIGN 
TR_STRUCT_REF 
TR_STRING 
TR_UNIT 
TR_ARG_LIST 
TR_NULL 

Definition at line 90 of file libeval_compiler.h.

◆ VAR_TYPE_T

Enumerator
VT_STRING 
VT_NUMERIC 
VT_UNDEFINED 
VT_PARSE_ERROR 

Definition at line 82 of file libeval_compiler.h.

Function Documentation

◆ dumpNode()

void LIBEVAL::dumpNode ( wxString &  buf,
TREE_NODE tok,
int  depth = 0 
)

Definition at line 586 of file libeval_compiler.cpp.

587 {
588  wxString str;
589 
590  if( !tok )
591  return;
592 
593  str.Printf( "\n[%p L0:%-20p L1:%-20p] ", tok, tok->leaf[0], tok->leaf[1] );
594  buf += str;
595 
596  for( int i = 0; i < 2 * depth; i++ )
597  buf += " ";
598 
599  if( tok->op & TR_OP_BINARY_MASK )
600  {
601  buf += formatOpName( tok->op );
602  dumpNode( buf, tok->leaf[0], depth + 1 );
603  dumpNode( buf, tok->leaf[1], depth + 1 );
604  }
605 
606  switch( tok->op )
607  {
608  case TR_NUMBER:
609  buf += "NUMERIC: ";
610  buf += formatNode( tok );
611 
612  if( tok->leaf[0] )
613  dumpNode( buf, tok->leaf[0], depth + 1 );
614 
615  break;
616 
617  case TR_ARG_LIST:
618  buf += "ARG_LIST: ";
619  buf += formatNode( tok );
620 
621  if( tok->leaf[0] )
622  dumpNode( buf, tok->leaf[0], depth + 1 );
623  if( tok->leaf[1] )
624  dumpNode( buf, tok->leaf[1], depth + 1 );
625 
626  break;
627 
628  case TR_STRING:
629  buf += "STRING: ";
630  buf += formatNode( tok );
631  break;
632 
633  case TR_IDENTIFIER:
634  buf += "ID: ";
635  buf += formatNode( tok );
636  break;
637 
638  case TR_STRUCT_REF:
639  buf += "SREF: ";
640  dumpNode( buf, tok->leaf[0], depth + 1 );
641  dumpNode( buf, tok->leaf[1], depth + 1 );
642  break;
643 
644  case TR_OP_FUNC_CALL:
645  buf += "CALL '";
646  buf += *tok->leaf[0]->value.str;
647  buf += "': ";
648  dumpNode( buf, tok->leaf[1], depth + 1 );
649  break;
650 
651  case TR_UNIT:
652  str.Printf( "UNIT: %d ", tok->value.idx );
653  buf += str;
654  break;
655  }
656 }
#define TR_OP_BINARY_MASK
#define TR_OP_FUNC_CALL
static void formatNode(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const std::string &aKey, const CPTREE &aTree)
Definition: ptree.cpp:166
static const wxString formatOpName(int op)
void dumpNode(wxString &buf, TREE_NODE *tok, int depth=0)

References formatNode(), formatOpName(), LIBEVAL::T_TOKEN_VALUE::idx, LIBEVAL::TREE_NODE::leaf, LIBEVAL::TREE_NODE::op, LIBEVAL::T_TOKEN_VALUE::str, TR_ARG_LIST, TR_IDENTIFIER, TR_NUMBER, TR_OP_BINARY_MASK, TR_OP_FUNC_CALL, TR_STRING, TR_STRUCT_REF, TR_UNIT, and LIBEVAL::TREE_NODE::value.

Referenced by LIBEVAL::COMPILER::generateUCode().

◆ formatNode()

const wxString LIBEVAL::formatNode ( TREE_NODE node)

Definition at line 580 of file libeval_compiler.cpp.

581 {
582  return node->value.str ? *(node->value.str) : "";
583 }

References LIBEVAL::T_TOKEN_VALUE::str, and LIBEVAL::TREE_NODE::value.

Referenced by dumpNode().

◆ formatOpName()

static const wxString LIBEVAL::formatOpName ( int  op)
static

Definition at line 86 of file libeval_compiler.cpp.

87 {
88  static const struct
89  {
90  int op;
91  wxString mnemonic;
92  }
93  simpleOps[] =
94  {
95  { TR_OP_MUL, "MUL" }, { TR_OP_DIV, "DIV" }, { TR_OP_ADD, "ADD" },
96  { TR_OP_SUB, "SUB" }, { TR_OP_LESS, "LESS" }, { TR_OP_GREATER, "GREATER" },
97  { TR_OP_LESS_EQUAL, "LESS_EQUAL" }, { TR_OP_GREATER_EQUAL, "GREATER_EQUAL" },
98  { TR_OP_EQUAL, "EQUAL" }, { TR_OP_NOT_EQUAL, "NEQUAL" }, { TR_OP_BOOL_AND, "AND" },
99  { TR_OP_BOOL_OR, "OR" }, { TR_OP_BOOL_NOT, "NOT" }, { -1, "" }
100  };
101 
102  for( int i = 0; simpleOps[i].op >= 0; i++ )
103  {
104  if( simpleOps[i].op == op )
105  return simpleOps[i].mnemonic;
106  }
107 
108  return "???";
109 }
#define TR_OP_BOOL_OR
#define TR_OP_BOOL_AND
#define TR_OP_GREATER_EQUAL
#define TR_OP_NOT_EQUAL
#define TR_OP_GREATER
#define TR_OP_MUL
#define TR_OP_DIV
#define TR_OP_SUB
#define TR_OP_LESS_EQUAL
#define TR_OP_ADD
#define TR_OP_BOOL_NOT
#define TR_OP_LESS
#define TR_OP_EQUAL

References TR_OP_ADD, TR_OP_BOOL_AND, TR_OP_BOOL_NOT, TR_OP_BOOL_OR, TR_OP_DIV, TR_OP_EQUAL, TR_OP_GREATER, TR_OP_GREATER_EQUAL, TR_OP_LESS, TR_OP_LESS_EQUAL, TR_OP_MUL, TR_OP_NOT_EQUAL, and TR_OP_SUB.

Referenced by dumpNode(), and LIBEVAL::UOP::Format().

◆ newNode()

TREE_NODE * LIBEVAL::newNode ( LIBEVAL::COMPILER compiler,
int  op,
const T_TOKEN_VALUE value 
)

Definition at line 60 of file libeval_compiler.cpp.

61 {
62  auto t2 = new TREE_NODE();
63 
64  t2->valid = true;
65  t2->value.str = value.str ? new wxString( *value.str ) : nullptr;
66  t2->value.num = value.num;
67  t2->value.idx = value.idx;
68  t2->op = op;
69  t2->leaf[0] = nullptr;
70  t2->leaf[1] = nullptr;
71  t2->isTerminal = false;
72  t2->srcPos = compiler->GetSourcePos();
73  t2->uop = nullptr;
74 
75  libeval_dbg(10, " ostr %p nstr %p nnode %p op %d", value.str, t2->value.str, t2, t2->op );
76 
77  if(t2->value.str)
78  compiler->GcItem( t2->value.str );
79 
80  compiler->GcItem( t2 );
81 
82  return t2;
83 }
#define libeval_dbg(level, fmt,...)
void GcItem(TREE_NODE *aItem)
int GetSourcePos() const

References LIBEVAL::COMPILER::GcItem(), LIBEVAL::COMPILER::GetSourcePos(), LIBEVAL::T_TOKEN_VALUE::idx, libeval_dbg, LIBEVAL::T_TOKEN_VALUE::num, and LIBEVAL::T_TOKEN_VALUE::str.

◆ prepareTree()

static void LIBEVAL::prepareTree ( LIBEVAL::TREE_NODE node)
static

Definition at line 733 of file libeval_compiler.cpp.

734 {
735  node->isVisited = false;
736 
737  // fixme: for reasons I don't understand the lemon parser isn't initializing the
738  // leaf node pointers of function name nodes. -JY
739  if( node->op == TR_OP_FUNC_CALL && node->leaf[0] )
740  {
741  node->leaf[0]->leaf[0] = nullptr;
742  node->leaf[0]->leaf[1] = nullptr;
743  }
744 
745  if ( node->leaf[0] )
746  prepareTree( node->leaf[0] );
747 
748  if ( node->leaf[1] )
749  prepareTree( node->leaf[1] );
750 }
#define TR_OP_FUNC_CALL
static void prepareTree(LIBEVAL::TREE_NODE *node)
TREE_NODE * leaf[2]

References LIBEVAL::TREE_NODE::isVisited, LIBEVAL::TREE_NODE::leaf, LIBEVAL::TREE_NODE::op, and TR_OP_FUNC_CALL.

Referenced by LIBEVAL::COMPILER::generateUCode().

◆ squashParamList()

static std::vector<TREE_NODE*> LIBEVAL::squashParamList ( TREE_NODE root)
static

Definition at line 752 of file libeval_compiler.cpp.

753 {
754  std::vector<TREE_NODE*> args;
755 
756  if( !root )
757  {
758  return args;
759  }
760 
761  if( root->op != TR_ARG_LIST && root->op != TR_NULL )
762  {
763  args.push_back( root );
764  }
765  else
766  {
767  TREE_NODE *n = root;
768  do
769  {
770  if( n->leaf[1] )
771  args.push_back(n->leaf[1]);
772 
773  n = n->leaf[0];
774  } while ( n && n->op == TR_ARG_LIST );
775 
776  if( n )
777  {
778  args.push_back( n );
779  }
780  }
781 
782  std::reverse( args.begin(), args.end() );
783 
784  for( size_t i = 0; i < args.size(); i++ )
785  libeval_dbg( 10, "squash arg%d: %s\n", int( i ), *args[i]->value.str );
786 
787  return args;
788 }
#define libeval_dbg(level, fmt,...)

References LIBEVAL::TREE_NODE::leaf, libeval_dbg, LIBEVAL::TREE_NODE::op, TR_ARG_LIST, and TR_NULL.

Referenced by LIBEVAL::COMPILER::generateUCode().

Variable Documentation

◆ defaultToken

constexpr T_TOKEN LIBEVAL::defaultToken = { TR_UNDEFINED, defaultTokenValue }

Definition at line 128 of file libeval_compiler.h.

Referenced by LIBEVAL::COMPILER::Compile().

◆ defaultTokenValue

constexpr T_TOKEN_VALUE LIBEVAL::defaultTokenValue = { nullptr, 0.0, 0 }

Definition at line 118 of file libeval_compiler.h.