Debugger: Minor parser refactoring; fix crash

This commit is contained in:
Vicki Pfau 2022-07-06 23:55:49 -07:00
parent 5b3fea3038
commit 01c881d18d
3 changed files with 16 additions and 15 deletions

View File

@ -67,10 +67,11 @@ struct ParseTree {
}; };
size_t lexExpression(struct LexVector* lv, const char* string, size_t length, const char* eol); size_t lexExpression(struct LexVector* lv, const char* string, size_t length, const char* eol);
void parseLexedExpression(struct ParseTree* tree, struct LexVector* lv);
void lexFree(struct LexVector* lv); void lexFree(struct LexVector* lv);
struct ParseTree* parseTreeCreate(void);
void parseFree(struct ParseTree* tree); void parseFree(struct ParseTree* tree);
bool parseLexedExpression(struct ParseTree* tree, struct LexVector* lv);
struct mDebugger; struct mDebugger;
bool mDebuggerEvaluateParseTree(struct mDebugger* debugger, struct ParseTree* tree, int32_t* value, int* segment); bool mDebuggerEvaluateParseTree(struct mDebugger* debugger, struct ParseTree* tree, int32_t* value, int* segment);

View File

@ -588,7 +588,7 @@ static struct ParseTree* _parseTree(const char** string) {
} }
struct ParseTree* tree = NULL; struct ParseTree* tree = NULL;
if (!error) { if (!error) {
tree = malloc(sizeof(*tree)); tree = parseTreeCreate();
parseLexedExpression(tree, &lv); parseLexedExpression(tree, &lv);
} }
lexFree(&lv); lexFree(&lv);
@ -796,17 +796,16 @@ struct CLIDebugVector* CLIDVParse(struct CLIDebugger* debugger, const char* stri
dvTemp.type = CLIDV_ERROR_TYPE; dvTemp.type = CLIDV_ERROR_TYPE;
} }
struct ParseTree tree; struct ParseTree* tree = parseTreeCreate();
parseLexedExpression(&tree, &lv); if (!parseLexedExpression(tree, &lv)) {
if (tree.token.type == TOKEN_ERROR_TYPE) {
dvTemp.type = CLIDV_ERROR_TYPE; dvTemp.type = CLIDV_ERROR_TYPE;
} else { } else {
if (!mDebuggerEvaluateParseTree(&debugger->d, &tree, &dvTemp.intValue, &dvTemp.segmentValue)) { if (!mDebuggerEvaluateParseTree(&debugger->d, tree, &dvTemp.intValue, &dvTemp.segmentValue)) {
dvTemp.type = CLIDV_ERROR_TYPE; dvTemp.type = CLIDV_ERROR_TYPE;
} }
} }
parseFree(&tree); parseFree(tree);
lexFree(&lv); lexFree(&lv);
LexVectorDeinit(&lv); LexVectorDeinit(&lv);

View File

@ -495,7 +495,7 @@ static const int _operatorPrecedence[] = {
[OP_DEREFERENCE] = 2, [OP_DEREFERENCE] = 2,
}; };
static struct ParseTree* _parseTreeCreate(void) { struct ParseTree* parseTreeCreate(void) {
struct ParseTree* tree = malloc(sizeof(struct ParseTree)); struct ParseTree* tree = malloc(sizeof(struct ParseTree));
tree->token.type = TOKEN_ERROR_TYPE; tree->token.type = TOKEN_ERROR_TYPE;
tree->p = NULL; tree->p = NULL;
@ -529,12 +529,12 @@ static size_t _parseExpression(struct ParseTree* tree, struct LexVector* lv, int
} }
break; break;
case TOKEN_SEGMENT_TYPE: case TOKEN_SEGMENT_TYPE:
tree->lhs = _parseTreeCreate(); tree->lhs = parseTreeCreate();
tree->lhs->token.type = TOKEN_UINT_TYPE; tree->lhs->token.type = TOKEN_UINT_TYPE;
tree->lhs->token.uintValue = token->uintValue; tree->lhs->token.uintValue = token->uintValue;
tree->lhs->p = tree; tree->lhs->p = tree;
tree->lhs->precedence = precedence; tree->lhs->precedence = precedence;
tree->rhs = _parseTreeCreate(); tree->rhs = parseTreeCreate();
tree->rhs->p = tree; tree->rhs->p = tree;
tree->rhs->precedence = precedence; tree->rhs->precedence = precedence;
tree->token.type = TOKEN_SEGMENT_TYPE; tree->token.type = TOKEN_SEGMENT_TYPE;
@ -572,7 +572,7 @@ static size_t _parseExpression(struct ParseTree* tree, struct LexVector* lv, int
} }
newPrecedence = _operatorPrecedence[token->operatorValue]; newPrecedence = _operatorPrecedence[token->operatorValue];
if (newPrecedence < precedence) { if (newPrecedence < precedence) {
newTree = _parseTreeCreate(); newTree = parseTreeCreate();
memcpy(newTree, tree, sizeof(*tree)); memcpy(newTree, tree, sizeof(*tree));
if (newTree->lhs) { if (newTree->lhs) {
newTree->lhs->p = newTree; newTree->lhs->p = newTree;
@ -582,7 +582,7 @@ static size_t _parseExpression(struct ParseTree* tree, struct LexVector* lv, int
} }
newTree->p = tree; newTree->p = tree;
tree->lhs = newTree; tree->lhs = newTree;
tree->rhs = _parseTreeCreate(); tree->rhs = parseTreeCreate();
tree->rhs->p = tree; tree->rhs->p = tree;
tree->rhs->precedence = newPrecedence; tree->rhs->precedence = newPrecedence;
precedence = newPrecedence; precedence = newPrecedence;
@ -617,9 +617,9 @@ static size_t _parseExpression(struct ParseTree* tree, struct LexVector* lv, int
return i; return i;
} }
void parseLexedExpression(struct ParseTree* tree, struct LexVector* lv) { bool parseLexedExpression(struct ParseTree* tree, struct LexVector* lv) {
if (!tree) { if (!tree) {
return; return false;
} }
tree->token.type = TOKEN_ERROR_TYPE; tree->token.type = TOKEN_ERROR_TYPE;
@ -636,6 +636,7 @@ void parseLexedExpression(struct ParseTree* tree, struct LexVector* lv) {
} }
tree->token.type = TOKEN_ERROR_TYPE; tree->token.type = TOKEN_ERROR_TYPE;
} }
return tree->token.type != TOKEN_ERROR_TYPE;
} }
void lexFree(struct LexVector* lv) { void lexFree(struct LexVector* lv) {