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 108 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.

66{
67 CST_PARSE = 0,
70};

◆ 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.

91{
92 TR_UNDEFINED = 0,
93 TR_NUMBER = 1,
94 TR_IDENTIFIER = 2,
95 TR_ASSIGN = 3,
96 TR_STRUCT_REF = 4,
97 TR_STRING = 5,
98 TR_UNIT = 6,
99 TR_ARG_LIST = 7,
100 TR_NULL = 8
101};

◆ 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 587 of file libeval_compiler.cpp.

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

References dumpNode(), 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 dumpNode(), and LIBEVAL::COMPILER::generateUCode().

◆ formatNode()

const wxString LIBEVAL::formatNode ( TREE_NODE node)

Definition at line 581 of file libeval_compiler.cpp.

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

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 87 of file libeval_compiler.cpp.

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

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 61 of file libeval_compiler.cpp.

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

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 734 of file libeval_compiler.cpp.

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

References LIBEVAL::TREE_NODE::isVisited, LIBEVAL::TREE_NODE::leaf, LIBEVAL::TREE_NODE::op, prepareTree(), and TR_OP_FUNC_CALL.

Referenced by LIBEVAL::COMPILER::generateUCode(), and prepareTree().

◆ squashParamList()

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

Definition at line 753 of file libeval_compiler.cpp.

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

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 }
constexpr

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 }
constexpr

Definition at line 118 of file libeval_compiler.h.