Added the opaque data type

This commit is contained in:
2022-10-03 21:02:13 +01:00
parent 016ab9c5fe
commit ca24c4f211
5 changed files with 212 additions and 4 deletions

View File

@@ -165,6 +165,10 @@ Literal copyLiteral(Literal original) {
return lit;
}
case LITERAL_OPAQUE: {
return original; //literally a shallow copy
}
case LITERAL_DICTIONARY_INTERMEDIATE: {
LiteralArray* array = ALLOCATE(LiteralArray, 1);
initLiteralArray(array);
@@ -327,6 +331,9 @@ bool literalsAreEqual(Literal lhs, Literal rhs) {
}
return true;
case LITERAL_OPAQUE:
return false; //IDK what this is!
case LITERAL_ANY:
return true;
@@ -389,6 +396,7 @@ int hashLiteral(Literal lit) {
case LITERAL_TYPE:
return AS_TYPE(lit).typeOf; //nothing else I can do
case LITERAL_OPAQUE:
case LITERAL_ANY:
return -1;
@@ -639,6 +647,10 @@ void printLiteralCustom(Literal literal, void (printFn)(const char*)) {
printToBuffer("type");
break;
case LITERAL_OPAQUE:
printToBuffer("opaque");
break;
case LITERAL_ANY:
printToBuffer("any");
break;
@@ -676,6 +688,10 @@ void printLiteralCustom(Literal literal, void (printFn)(const char*)) {
printFn("Unprintable literal found");
break;
case LITERAL_OPAQUE:
printFn("(opaque)");
break;
case LITERAL_ANY:
printFn("(any)");
break;

View File

@@ -15,6 +15,7 @@ typedef enum {
LITERAL_FUNCTION,
LITERAL_IDENTIFIER,
LITERAL_TYPE,
LITERAL_OPAQUE,
LITERAL_ANY,
//these are meta-level types - not for general use
@@ -58,6 +59,8 @@ typedef struct {
int capacity;
int count;
} type;
void* opaque;
} as;
} Literal;
@@ -72,6 +75,7 @@ typedef struct {
#define IS_FUNCTION_NATIVE(value) ((value).type == LITERAL_FUNCTION_NATIVE)
#define IS_IDENTIFIER(value) ((value).type == LITERAL_IDENTIFIER)
#define IS_TYPE(value) ((value).type == LITERAL_TYPE)
#define IS_OPAQUE(value) ((value).type == LITERAL_OPAQUE)
#define AS_BOOLEAN(value) ((value).as.boolean)
#define AS_INTEGER(value) ((value).as.integer)
@@ -82,6 +86,7 @@ typedef struct {
#define AS_FUNCTION(value) ((value).as.function)
#define AS_IDENTIFIER(value) ((value).as.identifier.ptr)
#define AS_TYPE(value) ((value).as.type)
#define AS_OPAQUE(value) ((value).as.opaque)
#define TO_NULL_LITERAL ((Literal){LITERAL_NULL, { .integer = 0 }})
#define TO_BOOLEAN_LITERAL(value) ((Literal){LITERAL_BOOLEAN, { .boolean = value }})
@@ -93,6 +98,7 @@ typedef struct {
#define TO_FUNCTION_LITERAL(value, l) ((Literal){LITERAL_FUNCTION, { .function.bytecode = value, .function.scope = NULL, .function.length = l }})
#define TO_IDENTIFIER_LITERAL(value, l) _toIdentifierLiteral(value, l)
#define TO_TYPE_LITERAL(value, c) ((Literal){ LITERAL_TYPE, { .type.typeOf = value, .type.constant = c, .type.subtypes = NULL, .type.capacity = 0, .type.count = 0 }})
#define TO_OPAQUE_LITERAL(value) ((Literal){ LITERAL_OPAQUE, { .opaque = value }})
TOY_API void freeLiteral(Literal literal);

View File

@@ -136,13 +136,18 @@ void freeLiteralDictionary(LiteralDictionary* dictionary) {
void setLiteralDictionary(LiteralDictionary* dictionary, Literal key, Literal value) {
if (IS_NULL(key)) {
fprintf(stderr, ERROR "Dictionaries can't have null keys (get)\n" RESET);
fprintf(stderr, ERROR "Dictionaries can't have null keys (set)\n" RESET);
return;
}
//BUGFIX: Can't hash a function
if (IS_FUNCTION(key) || IS_FUNCTION_NATIVE(key)) {
fprintf(stderr, ERROR "Dictionaries can't have function keys (get)\n" RESET);
fprintf(stderr, ERROR "Dictionaries can't have function keys (set)\n" RESET);
return;
}
if (IS_OPAQUE(key)) {
fprintf(stderr, ERROR "Dictionaries can't have opaque keys (set)\n" RESET);
return;
}
@@ -156,13 +161,18 @@ void setLiteralDictionary(LiteralDictionary* dictionary, Literal key, Literal va
Literal getLiteralDictionary(LiteralDictionary* dictionary, Literal key) {
if (IS_NULL(key)) {
fprintf(stderr, ERROR "Dictionaries can't have null keys (set)\n" RESET);
fprintf(stderr, ERROR "Dictionaries can't have null keys (get)\n" RESET);
return TO_NULL_LITERAL;
}
//BUGFIX: Can't hash a function
if (IS_FUNCTION(key) || IS_FUNCTION_NATIVE(key)) {
fprintf(stderr, ERROR "Dictionaries can't have function keys (set)\n" RESET);
fprintf(stderr, ERROR "Dictionaries can't have function keys (get)\n" RESET);
return TO_NULL_LITERAL;
}
if (IS_OPAQUE(key)) {
fprintf(stderr, ERROR "Dictionaries can't have opaque keys (get)\n" RESET);
return TO_NULL_LITERAL;
}
@@ -188,6 +198,11 @@ void removeLiteralDictionary(LiteralDictionary* dictionary, Literal key) {
return;
}
if (IS_OPAQUE(key)) {
fprintf(stderr, ERROR "Dictionaries can't have opaque keys (remove)\n" RESET);
return;
}
_entry* entry = getEntryArray(dictionary->entries, dictionary->capacity, key, hashLiteral(key), true);
if (entry != NULL) {