mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Added constant folding for strings, tweaked some error messages
This commit is contained in:
@@ -59,7 +59,7 @@ static void consume(Toy_Parser* parser, Toy_TokenType tokenType, const char* msg
|
||||
static void synchronize(Toy_Parser* parser) {
|
||||
#ifndef TOY_EXPORT
|
||||
if (Toy_commandLine.verbose) {
|
||||
fprintf(stderr, TOY_CC_ERROR "synchronizing\n" TOY_CC_RESET);
|
||||
fprintf(stderr, TOY_CC_ERROR "Synchronizing input\n" TOY_CC_RESET);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -982,6 +982,21 @@ static bool calcStaticBinaryArithmetic(Toy_Parser* parser, Toy_ASTNode** nodeHan
|
||||
Toy_Literal rhs = (*nodeHandle)->binary.right->atomic.literal;
|
||||
Toy_Literal result = TOY_TO_NULL_LITERAL;
|
||||
|
||||
//special case for string concatenation ONLY
|
||||
if (TOY_IS_STRING(lhs) && TOY_IS_STRING(rhs)) {
|
||||
//check for overflow
|
||||
int totalLength = TOY_AS_STRING(lhs)->length + TOY_AS_STRING(rhs)->length;
|
||||
if (totalLength > TOY_MAX_STRING_LENGTH) {
|
||||
error(parser, parser->previous, "Can't concatenate these strings, result is too long (error found in constant folding)\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
//concat the strings
|
||||
char buffer[TOY_MAX_STRING_LENGTH];
|
||||
snprintf(buffer, TOY_MAX_STRING_LENGTH, "%s%s", Toy_toCString(TOY_AS_STRING(lhs)), Toy_toCString(TOY_AS_STRING(rhs)));
|
||||
result = TOY_TO_STRING_LITERAL(Toy_createRefStringLength(buffer, totalLength));
|
||||
}
|
||||
|
||||
//type coersion
|
||||
if (TOY_IS_FLOAT(lhs) && TOY_IS_INTEGER(rhs)) {
|
||||
rhs = TOY_TO_FLOAT_LITERAL(TOY_AS_INTEGER(rhs));
|
||||
|
||||
Reference in New Issue
Block a user