Added scopes using '{}' symbols, read more

I've also added a new literal type called 'identifier'. This will be
used for variable names, and has a type mask embedded in it.
This commit is contained in:
2022-08-08 15:02:12 +01:00
parent 08ce270e06
commit 6d5549fc8e
15 changed files with 335 additions and 68 deletions

View File

@@ -13,6 +13,7 @@ typedef enum {
// LITERAL_ARRAY,
// LITERAL_DICTIONARY,
// LITERAL_FUNCTION,
LITERAL_IDENTIFIER,
} LiteralType;
typedef struct {
@@ -23,53 +24,84 @@ typedef struct {
float number;
struct {
char* ptr;
int length; //could possibly cut it down further by removing this
int length;
} string;
// //experimental
// void* array;
// void* dictionary;
// void* function;
struct { //for variable names
char* ptr;
int length;
unsigned char types;
} identifier;
} as;
} Literal;
#define IS_NULL(value) ((value).type == LITERAL_NULL)
#define IS_BOOLEAN(value) ((value).type == LITERAL_BOOLEAN)
#define IS_INTEGER(value) ((value).type == LITERAL_INTEGER)
#define IS_FLOAT(value) ((value).type == LITERAL_FLOAT)
#define IS_STRING(value) ((value).type == LITERAL_STRING)
#define IS_ARRAY(value) ((value).type == LITERAL_ARRAY)
#define IS_DICTIONARY(value) ((value).type == LITERAL_DICTIONARY)
#define IS_FUNCTION(value) ((value).type == LITERAL_FUNCTION)
#define IS_NULL(value) ((value).type == LITERAL_NULL)
#define IS_BOOLEAN(value) ((value).type == LITERAL_BOOLEAN)
#define IS_INTEGER(value) ((value).type == LITERAL_INTEGER)
#define IS_FLOAT(value) ((value).type == LITERAL_FLOAT)
#define IS_STRING(value) ((value).type == LITERAL_STRING)
#define IS_ARRAY(value) ((value).type == LITERAL_ARRAY)
#define IS_DICTIONARY(value) ((value).type == LITERAL_DICTIONARY)
#define IS_FUNCTION(value) ((value).type == LITERAL_FUNCTION)
#define IS_IDENTIFIER(value) ((value).type == LITERAL_IDENTIFIER)
#define AS_BOOLEAN(value) ((value).as.boolean)
#define AS_INTEGER(value) ((value).as.integer)
#define AS_FLOAT(value) ((value).as.number)
#define AS_STRING(value) ((value).as.string.ptr)
// #define AS_ARRAY_PTR(value)
// #define AS_DICTIONARY_PTR(value)
// #define AS_FUNCTION_PTR(value) ((Function*)((value).as.function))
#define AS_BOOLEAN(value) ((value).as.boolean)
#define AS_INTEGER(value) ((value).as.integer)
#define AS_FLOAT(value) ((value).as.number)
#define AS_STRING(value) ((value).as.string.ptr)
// #define AS_ARRAY(value)
// #define AS_DICTIONARY(value)
// #define AS_FUNCTION(value)
#define AS_IDENTIFIER(value) ((value).as.identifier.ptr)
#define TO_NULL_LITERAL ((Literal){LITERAL_NULL, { .integer = 0 }})
#define TO_BOOLEAN_LITERAL(value) ((Literal){LITERAL_BOOLEAN, { .boolean = value }})
#define TO_INTEGER_LITERAL(value) ((Literal){LITERAL_INTEGER, { .integer = value }})
#define TO_FLOAT_LITERAL(value) ((Literal){LITERAL_FLOAT, { .number = value }})
#define TO_STRING_LITERAL(value) _toStringLiteral(value)
// #define TO_ARRAY_PTR
// #define TO_DICTIONARY_PTR
// #define TO_FUNCTION_PTR(value) ((Literal){LITERAL_FUNCTION, { .function = (Function*)value }})
#define TO_NULL_LITERAL ((Literal){LITERAL_NULL, { .integer = 0 }})
#define TO_BOOLEAN_LITERAL(value) ((Literal){LITERAL_BOOLEAN, { .boolean = value }})
#define TO_INTEGER_LITERAL(value) ((Literal){LITERAL_INTEGER, { .integer = value }})
#define TO_FLOAT_LITERAL(value) ((Literal){LITERAL_FLOAT, { .number = value }})
#define TO_STRING_LITERAL(value) _toStringLiteral(value)
// #define TO_ARRAY_LITERAL
// #define TO_DICTIONARY_LITERAL
// #define TO_FUNCTION_LITERAL
#define TO_IDENTIFIER_LITERAL(value, types) _toIdentifierLiteral(value, types)
#define MASK(x) (1 >> (x))
#define TYPE_CONST 0
#define TYPE_BOOLEAN 1
#define TYPE_INTEGER 2
#define TYPE_FLOAT 3
#define TYPE_STRING 4
#define TYPE_ARRAY 5
#define TYPE_DICTIONARY 6
#define TYPE_FUNCTION 7
#define MASK_CONST (MASK(TYPE_CONST))
#define MASK_BOOLEAN (MASK(TYPE_BOOLEAN))
#define MASK_INTEGER (MASK(TYPE_INTEGER))
#define MASK_FLOAT (MASK(TYPE_FLOAT))
#define MASK_STRING (MASK(TYPE_STRING))
#define MASK_ARRAY (MASK(TYPE_ARRAY))
#define MASK_DICTIONARY (MASK(TYPE_DICTIONARY))
#define MASK_FUNCTION (MASK(TYPE_FUNCTION))
//utils
void printLiteral(Literal literal);
void printLiteralCustom(Literal literal, void (printFn)(const char*));
void freeLiteral(Literal literal);
#define IS_TRUTHY(x) _isTruthy(x)
#define STRLEN(lit) ((lit).as.string.length)
#define STRLEN(lit) ((lit).as.string.length)
#define STRLEN_I(lit) ((lit).as.identifier.length)
#define TYPES(lit) ((lit).as.identifier.types)
//BUGFIX: macros are not functions
bool _isTruthy(Literal x);
Literal _toStringLiteral(char* cstr);
Literal _toStringLiteral(char* str);
Literal _toIdentifierLiteral(char* str, unsigned char types);
//utils
char* copyString(char* original, int length);