diff --git a/include/mgba/internal/debugger/parser.h b/include/mgba/internal/debugger/parser.h index 0b8f09d4c..1b06b7f60 100644 --- a/include/mgba/internal/debugger/parser.h +++ b/include/mgba/internal/debugger/parser.h @@ -67,10 +67,11 @@ struct ParseTree { }; 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); + +struct ParseTree* parseTreeCreate(void); void parseFree(struct ParseTree* tree); +bool parseLexedExpression(struct ParseTree* tree, struct LexVector* lv); struct mDebugger; bool mDebuggerEvaluateParseTree(struct mDebugger* debugger, struct ParseTree* tree, int32_t* value, int* segment); diff --git a/src/debugger/cli-debugger.c b/src/debugger/cli-debugger.c index dbc10fe89..e3897cd3b 100644 --- a/src/debugger/cli-debugger.c +++ b/src/debugger/cli-debugger.c @@ -588,7 +588,7 @@ static struct ParseTree* _parseTree(const char** string) { } struct ParseTree* tree = NULL; if (!error) { - tree = malloc(sizeof(*tree)); + tree = parseTreeCreate(); parseLexedExpression(tree, &lv); } lexFree(&lv); @@ -796,17 +796,16 @@ struct CLIDebugVector* CLIDVParse(struct CLIDebugger* debugger, const char* stri dvTemp.type = CLIDV_ERROR_TYPE; } - struct ParseTree tree; - parseLexedExpression(&tree, &lv); - if (tree.token.type == TOKEN_ERROR_TYPE) { + struct ParseTree* tree = parseTreeCreate(); + if (!parseLexedExpression(tree, &lv)) { dvTemp.type = CLIDV_ERROR_TYPE; } else { - if (!mDebuggerEvaluateParseTree(&debugger->d, &tree, &dvTemp.intValue, &dvTemp.segmentValue)) { + if (!mDebuggerEvaluateParseTree(&debugger->d, tree, &dvTemp.intValue, &dvTemp.segmentValue)) { dvTemp.type = CLIDV_ERROR_TYPE; } } - parseFree(&tree); + parseFree(tree); lexFree(&lv); LexVectorDeinit(&lv); diff --git a/src/debugger/parser.c b/src/debugger/parser.c index 4af61a14e..0d4924ea9 100644 --- a/src/debugger/parser.c +++ b/src/debugger/parser.c @@ -495,7 +495,7 @@ static const int _operatorPrecedence[] = { [OP_DEREFERENCE] = 2, }; -static struct ParseTree* _parseTreeCreate(void) { +struct ParseTree* parseTreeCreate(void) { struct ParseTree* tree = malloc(sizeof(struct ParseTree)); tree->token.type = TOKEN_ERROR_TYPE; tree->p = NULL; @@ -529,12 +529,12 @@ static size_t _parseExpression(struct ParseTree* tree, struct LexVector* lv, int } break; case TOKEN_SEGMENT_TYPE: - tree->lhs = _parseTreeCreate(); + tree->lhs = parseTreeCreate(); tree->lhs->token.type = TOKEN_UINT_TYPE; tree->lhs->token.uintValue = token->uintValue; tree->lhs->p = tree; tree->lhs->precedence = precedence; - tree->rhs = _parseTreeCreate(); + tree->rhs = parseTreeCreate(); tree->rhs->p = tree; tree->rhs->precedence = precedence; 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]; if (newPrecedence < precedence) { - newTree = _parseTreeCreate(); + newTree = parseTreeCreate(); memcpy(newTree, tree, sizeof(*tree)); if (newTree->lhs) { newTree->lhs->p = newTree; @@ -582,7 +582,7 @@ static size_t _parseExpression(struct ParseTree* tree, struct LexVector* lv, int } newTree->p = tree; tree->lhs = newTree; - tree->rhs = _parseTreeCreate(); + tree->rhs = parseTreeCreate(); tree->rhs->p = tree; tree->rhs->precedence = newPrecedence; precedence = newPrecedence; @@ -617,9 +617,9 @@ static size_t _parseExpression(struct ParseTree* tree, struct LexVector* lv, int return i; } -void parseLexedExpression(struct ParseTree* tree, struct LexVector* lv) { +bool parseLexedExpression(struct ParseTree* tree, struct LexVector* lv) { if (!tree) { - return; + return false; } tree->token.type = TOKEN_ERROR_TYPE; @@ -636,6 +636,7 @@ void parseLexedExpression(struct ParseTree* tree, struct LexVector* lv) { } tree->token.type = TOKEN_ERROR_TYPE; } + return tree->token.type != TOKEN_ERROR_TYPE; } void lexFree(struct LexVector* lv) {