Hallo,
ich erhalte immer einen segfault bei printAST.
Ziel ist es, einen AST aufstellen, und diesen durchsuchen.
Hier den aktuellen Code:
Danke für Hilfe
ich erhalte immer einen segfault bei printAST.
Ziel ist es, einen AST aufstellen, und diesen durchsuchen.
Hier den aktuellen Code:
Danke für Hilfe
Code:
enum enumStaticType {
CHAR,
SHORT,
INT,
LONG,
stFLOAT,
DOUBLE,
ARRAY
};
class dBaseClass;
class Statement;
class StaticType {
public:
StaticType() {}
~StaticType() {}
enumStaticType staticType;
QVariant value;
};
class Expression: public StaticType {
public:
Expression() { }
~Expression() { }
Expression * left;
Expression * right;
dBaseClass * nextStmt;
};
class Assignment {
public:
Assignment() { }
~Assignment() { }
QString variableName;
Expression * rvalue;
};
class Statement {
public:
Statement() { }
~Statement() { }
Assignment * op_assign;
};
class dBaseClass {
public:
dBaseClass() { }
dBaseClass * next;
Assignment * op_assign;
};
void printAST(dBaseClass * head)
{
dBaseClass * current = head;
while (current != nullptr) {
current = current->next;
qDebug() << current->op_assign->variableName;
if (current == nullptr)
break;
}
}
void pushAST(
dBaseClass * head,
enumClassType type,
QString vname,
float vfloat)
{
dBaseClass * current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = new dBaseClass;
if (type == enumClassType::ctAssign) {
auto *ptr = current->next->op_assign = new Assignment;
ptr->variableName = vname;
auto *num = ptr->rvalue = new Expression;
num->staticType = enumStaticType::stFLOAT;
num->value = vfloat;
num->left = nullptr;
}
current->next->next = nullptr;
}
void testAST()
{
// -----------------------------------
// init rootAST ...
// -----------------------------------
dBaseClass * rootASTstmt = nullptr;
rootASTstmt = new dBaseClass;
rootASTstmt->next = new dBaseClass;
rootASTstmt->next->next = nullptr;
pushAST(rootASTstmt,ctAssign,"Test1", 21.44);
// pushAST(rootASTstmt,ctAssign,"22Test22", 121.144);
printAST(rootASTstmt);
}