Skip to content

Commit b85adf7

Browse files
committed
Fix memory and other issues with procedures
1 parent b4940bd commit b85adf7

File tree

5 files changed

+41
-15
lines changed

5 files changed

+41
-15
lines changed

dist/catto.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,11 +1161,13 @@ CATTO_FN_PREFIX void catto_removeScopedVariables(catto_Context* context) {
11611161
if (variable->scope >= context->statementStackCount) {
11621162
if (previousVariable) {
11631163
previousVariable->nextVariable = nextVariable;
1164-
} else {
1164+
}
1165+
1166+
if (variable == context->firstVariable) {
11651167
context->firstVariable = nextVariable;
11661168
}
11671169

1168-
if (context->lastVariable == variable) {
1170+
if (variable == context->lastVariable) {
11691171
context->lastVariable = previousVariable;
11701172
}
11711173

@@ -2759,9 +2761,9 @@ CATTO_FN_PREFIX catto_AstNode* catto_parseStatement(catto_Token** currentTokenPt
27592761
parsedAccessor = CATTO_TRUE;
27602762
}
27612763

2762-
catto_Token* assignmentOperatorToken = catto_eatIfType(currentTokenPtr, CATTO_TOKEN_TYPE_OPERATOR);
2764+
catto_Token* assignmentOperatorToken = catto_eatIfKeyword(currentTokenPtr, "=");
27632765

2764-
if (assignmentOperatorToken && catto_stringsEqualCaseInsensitive(assignmentOperatorToken->value.asString, "=")) {
2766+
if (assignmentOperatorToken) {
27652767
catto_AstNode* value = CATTO_NULL;
27662768

27672769
if (!catto_parseExpression(currentTokenPtr, &value)) {
@@ -3067,6 +3069,17 @@ CATTO_FN_PREFIX void catto_debugAstNodes(catto_AstNode* firstAstNode) {
30673069

30683070
break;
30693071

3072+
case CATTO_AST_NODE_TYPE_PROCEDURE_STATEMENT:
3073+
CATTO_LOG(currentAstNode->value.asStatement.attributes.asProcedure.name);
3074+
3075+
CATTO_LOG_CHAR('(');
3076+
3077+
catto_debugAstNodes(currentAstNode->value.asStatement.firstArgument);
3078+
3079+
CATTO_LOG_CHAR(')');
3080+
3081+
break;
3082+
30703083
case CATTO_AST_NODE_TYPE_EXPRESSION_LEAF:
30713084
if (currentAstNode->value.asExpressionLeaf.subjectVariable) {
30723085
CATTO_LOG(currentAstNode->value.asExpressionLeaf.subjectVariable);

src/contexts.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,11 +657,13 @@ void catto_removeScopedVariables(catto_Context* context) {
657657
if (variable->scope >= context->statementStackCount) {
658658
if (previousVariable) {
659659
previousVariable->nextVariable = nextVariable;
660-
} else {
660+
}
661+
662+
if (variable == context->firstVariable) {
661663
context->firstVariable = nextVariable;
662664
}
663665

664-
if (context->lastVariable == variable) {
666+
if (variable == context->lastVariable) {
665667
context->lastVariable = previousVariable;
666668
}
667669

src/numbers.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ catto_Char* catto_numberToString(catto_Float number) {
9595
number -= integralPart; // Now fractional part
9696

9797
do {
98-
catto_appendCharToString(string, (catto_Char)('0' + (integralPart % 10)));
98+
string = catto_appendCharToString(string, (catto_Char)('0' + (integralPart % 10)));
9999

100100
integralPart /= 10;
101101
precisionLeft--;
102102
} while (integralPart > 0);
103103

104104
if (isNegative) {
105-
catto_appendCharToString(string, '-');
105+
string = catto_appendCharToString(string, '-');
106106
}
107107

108108
catto_reverseString(string);
@@ -111,7 +111,7 @@ catto_Char* catto_numberToString(catto_Float number) {
111111
catto_Bool anyDigitsInFractionalPart = CATTO_FALSE;
112112

113113
if (number > CATTO_EPSILON && precisionLeft > 0) {
114-
catto_appendCharToString(string, '.');
114+
string = catto_appendCharToString(string, '.');
115115

116116
while (number > CATTO_EPSILON && precisionLeft > 0) {
117117
number *= 10;
@@ -125,7 +125,7 @@ catto_Char* catto_numberToString(catto_Float number) {
125125
anyDigitsInFractionalPart = CATTO_TRUE;
126126
}
127127

128-
catto_appendCharToString(string, (catto_Char)('0' + digit));
128+
string = catto_appendCharToString(string, (catto_Char)('0' + digit));
129129

130130
number -= digit;
131131
precisionLeft--;
@@ -144,10 +144,10 @@ catto_Char* catto_numberToString(catto_Float number) {
144144
}
145145

146146
if (exponent != 0) {
147-
catto_appendCharToString(string, 'E');
147+
string = catto_appendCharToString(string, 'E');
148148

149149
if (exponent > 0) {
150-
catto_appendCharToString(string, '+');
150+
string = catto_appendCharToString(string, '+');
151151
}
152152

153153
catto_Char* exponentString = catto_numberToString(exponent);

src/parser.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,9 @@ catto_AstNode* catto_parseStatement(catto_Token** currentTokenPtr, catto_AstNode
446446
parsedAccessor = CATTO_TRUE;
447447
}
448448

449-
catto_Token* assignmentOperatorToken = catto_eatIfType(currentTokenPtr, CATTO_TOKEN_TYPE_OPERATOR);
449+
catto_Token* assignmentOperatorToken = catto_eatIfKeyword(currentTokenPtr, "=");
450450

451-
if (assignmentOperatorToken && catto_stringsEqualCaseInsensitive(assignmentOperatorToken->value.asString, "=")) {
451+
if (assignmentOperatorToken) {
452452
catto_AstNode* value = CATTO_NULL;
453453

454454
if (!catto_parseExpression(currentTokenPtr, &value)) {
@@ -754,6 +754,17 @@ void catto_debugAstNodes(catto_AstNode* firstAstNode) {
754754

755755
break;
756756

757+
case CATTO_AST_NODE_TYPE_PROCEDURE_STATEMENT:
758+
CATTO_LOG(currentAstNode->value.asStatement.attributes.asProcedure.name);
759+
760+
CATTO_LOG_CHAR('(');
761+
762+
catto_debugAstNodes(currentAstNode->value.asStatement.firstArgument);
763+
764+
CATTO_LOG_CHAR(')');
765+
766+
break;
767+
757768
case CATTO_AST_NODE_TYPE_EXPRESSION_LEAF:
758769
if (currentAstNode->value.asExpressionLeaf.subjectVariable) {
759770
CATTO_LOG(currentAstNode->value.asExpressionLeaf.subjectVariable);

src/stdlib/functions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ catto_TypedValue catto_function_chr(catto_Context* context, catto_DataType retur
5858
catto_Char* string = catto_copyString("");
5959

6060
if (codepoint > 0) {
61-
catto_appendCharToString(string, codepoint);
61+
string = catto_appendCharToString(string, codepoint);
6262
}
6363

6464
catto_TypedValue returnValue = catto_asTypedString(string);

0 commit comments

Comments
 (0)