mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 23:04:08 +10:00
Added string concatenation
This commit is contained in:
@@ -10,8 +10,8 @@ DONE: while-then
|
|||||||
DONE: for-then
|
DONE: for-then
|
||||||
DONE: break and continue statements
|
DONE: break and continue statements
|
||||||
DONE: truthiness rethink
|
DONE: truthiness rethink
|
||||||
|
DONE: string concat with the + operator
|
||||||
|
|
||||||
TODO: string concat with the + operator
|
|
||||||
TODO: increment & decrement operators
|
TODO: increment & decrement operators
|
||||||
TODO: a = b = c = 1;
|
TODO: a = b = c = 1;
|
||||||
TODO: are compounds shallow or deep copies?
|
TODO: are compounds shallow or deep copies?
|
||||||
|
|||||||
@@ -357,7 +357,7 @@ export hello as world;
|
|||||||
|
|
||||||
## String
|
## String
|
||||||
|
|
||||||
Strings are a series of characters, and are created by surrounding said characters with a pair of double quotation marks `"`. Strings cannot exceed 4096 bytes in length.
|
Strings are a series of characters, and are created by surrounding said characters with a pair of double quotation marks `"`. Strings cannot exceed 4096 bytes in length (this amount can be tweaked in the source).
|
||||||
|
|
||||||
```
|
```
|
||||||
var greeting: string = "Hello world";
|
var greeting: string = "Hello world";
|
||||||
|
|||||||
@@ -230,6 +230,22 @@ static bool execArithmetic(Interpreter* interpreter, Opcode opcode) {
|
|||||||
parseIdentifierToValue(interpreter, &rhs);
|
parseIdentifierToValue(interpreter, &rhs);
|
||||||
parseIdentifierToValue(interpreter, &lhs);
|
parseIdentifierToValue(interpreter, &lhs);
|
||||||
|
|
||||||
|
//special case for string concatenation ONLY
|
||||||
|
if (IS_STRING(lhs) && IS_STRING(rhs)) {
|
||||||
|
//check for overflow
|
||||||
|
if (STRLEN(lhs) + STRLEN(rhs) > MAX_STRING_LENGTH) {
|
||||||
|
printf("Can't concatenate these strings (result is too long)\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//concat the strings
|
||||||
|
char buffer[MAX_STRING_LENGTH];
|
||||||
|
snprintf(buffer, MAX_STRING_LENGTH, "%s%s", AS_STRING(lhs), AS_STRING(rhs));
|
||||||
|
pushLiteralArray(&interpreter->stack, TO_STRING_LITERAL(buffer));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
//type coersion
|
//type coersion
|
||||||
if (IS_FLOAT(lhs) && IS_INTEGER(rhs)) {
|
if (IS_FLOAT(lhs) && IS_INTEGER(rhs)) {
|
||||||
rhs = TO_FLOAT_LITERAL(AS_INTEGER(rhs));
|
rhs = TO_FLOAT_LITERAL(AS_INTEGER(rhs));
|
||||||
|
|||||||
@@ -81,12 +81,12 @@ void printLiteralCustom(Literal literal, void (printFn)(const char*)) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case LITERAL_STRING: {
|
case LITERAL_STRING: {
|
||||||
char buffer[4096];
|
char buffer[MAX_STRING_LENGTH];
|
||||||
if (!quotes) {
|
if (!quotes) {
|
||||||
snprintf(buffer, 4096, "%.*s", STRLEN(literal), AS_STRING(literal));
|
snprintf(buffer, MAX_STRING_LENGTH, "%.*s", STRLEN(literal), AS_STRING(literal));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
snprintf(buffer, 4096, "%c%.*s%c", quotes, STRLEN(literal), AS_STRING(literal), quotes);
|
snprintf(buffer, MAX_STRING_LENGTH, "%c%.*s%c", quotes, STRLEN(literal), AS_STRING(literal), quotes);
|
||||||
}
|
}
|
||||||
printFn(buffer);
|
printFn(buffer);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,6 +92,7 @@ void freeLiteral(Literal literal);
|
|||||||
|
|
||||||
#define IS_TRUTHY(x) _isTruthy(x)
|
#define IS_TRUTHY(x) _isTruthy(x)
|
||||||
|
|
||||||
|
#define MAX_STRING_LENGTH 4096
|
||||||
#define STRLEN(lit) ((lit).as.string.length)
|
#define STRLEN(lit) ((lit).as.string.length)
|
||||||
#define STRLEN_I(lit) ((lit).as.identifier.length)
|
#define STRLEN_I(lit) ((lit).as.identifier.length)
|
||||||
#define HASH_I(lit) ((lit).as.identifier.hash)
|
#define HASH_I(lit) ((lit).as.identifier.hash)
|
||||||
|
|||||||
@@ -241,9 +241,11 @@ static Opcode string(Parser* parser, Node** nodeHandle) {
|
|||||||
int length = parser->previous.length;
|
int length = parser->previous.length;
|
||||||
|
|
||||||
//for safety
|
//for safety
|
||||||
if (length > 4096) {
|
if (length > MAX_STRING_LENGTH) {
|
||||||
length = 4096;
|
length = MAX_STRING_LENGTH;
|
||||||
error(parser, parser->previous, "Strings can only be a maximum of 4096 characters long");
|
char buffer[256];
|
||||||
|
snprintf(buffer, 256, "Strings can only be a maximum of %d characters long", MAX_STRING_LENGTH);
|
||||||
|
error(parser, parser->previous, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
emitNodeLiteral(nodeHandle, TO_STRING_LITERAL(copyString(parser->previous.lexeme, length)));
|
emitNodeLiteral(nodeHandle, TO_STRING_LITERAL(copyString(parser->previous.lexeme, length)));
|
||||||
|
|||||||
Reference in New Issue
Block a user