Types are being read into the variable declaration, not yet used

This commit is contained in:
2022-08-12 15:06:41 +01:00
parent 998b913fc9
commit 53f0996fd1
9 changed files with 150 additions and 100 deletions

View File

@@ -1,2 +1 @@
print [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; var a : int const = 42;
print [["foo":1]:["bar":2]];

View File

@@ -128,6 +128,35 @@ static int writeNodeCompoundToCache(Compiler* compiler, Node* node) {
return index; return index;
} }
static int writeLiteralTypeToCache(Compiler* compiler, Literal literal) {
int index = -1;
//for now, stored as an array
LiteralArray* store = ALLOCATE(LiteralArray, 1);
initLiteralArray(store);
//save the mask to the store
int maskIndex = findLiteralIndex(store, TO_INTEGER_LITERAL(AS_TYPE(literal).mask));
if (maskIndex < 0) {
maskIndex = pushLiteralArray(store, TO_INTEGER_LITERAL(AS_TYPE(literal).mask));
}
pushLiteralArray(store, TO_INTEGER_LITERAL(maskIndex));
//if it's a compound type, recurse
if (AS_TYPE(literal).mask & (MASK_ARRAY|MASK_DICTIONARY)) {
for (int i = 0; i < AS_TYPE(literal).count; i++) {
int subIndex = writeLiteralTypeToCache(compiler, ((Literal*)(AS_TYPE(literal).subtypes))[i]);
pushLiteralArray(store, TO_INTEGER_LITERAL(subIndex));
}
}
//push the store to the compiler
index = pushLiteralArray(&compiler->literalCache, TO_ARRAY_LITERAL(store));
return index;
}
void writeCompiler(Compiler* compiler, Node* node) { void writeCompiler(Compiler* compiler, Node* node) {
//grow if the bytecode space is too small //grow if the bytecode space is too small
if (compiler->capacity < compiler->count + 1) { if (compiler->capacity < compiler->count + 1) {
@@ -221,17 +250,28 @@ void writeCompiler(Compiler* compiler, Node* node) {
fprintf(stderr, "[Internal] NODE_PAIR encountered in writeCompiler()"); fprintf(stderr, "[Internal] NODE_PAIR encountered in writeCompiler()");
break; break;
// case NODE_VAR_TYPES: case NODE_VAR_TYPES: {
// //TODO: OP_TYPE_DECL int index = writeLiteralTypeToCache(compiler, node->varTypes.typeLiteral);
// //find the type declaration in the cache, or create it if it doesn't exist //embed the info into the bytecode
// break; if (index >= 256) {
//push a "long" index
compiler->bytecode[compiler->count++] = OP_TYPE_DECL_LONG; //1 byte
*((unsigned short*)(compiler->bytecode + compiler->count)) = (unsigned short)index; //2 bytes
compiler->count += sizeof(unsigned short);
}
else {
//push the index
compiler->bytecode[compiler->count++] = OP_TYPE_DECL; //1 byte
compiler->bytecode[compiler->count++] = (unsigned char)index; //1 byte
}
}
break;
// case NODE_VAR_DECL: // case NODE_VAR_DECL:
// //TODO: OP_VAR_DECL + OP_VAR_ASSIGN // //TODO: OP_VAR_DECL + OP_VAR_ASSIGN
// break; // break;
//TODO: more
} }
} }
@@ -371,7 +411,7 @@ unsigned char* collateCompiler(Compiler* compiler, int* size) {
} }
break; break;
case LITERAL_DICTIONARY: case LITERAL_DICTIONARY: {
emitByte(&collation, &capacity, &count, LITERAL_DICTIONARY); emitByte(&collation, &capacity, &count, LITERAL_DICTIONARY);
LiteralArray* ptr = AS_ARRAY(compiler->literalCache.literals[i]); //used an array for storage above LiteralArray* ptr = AS_ARRAY(compiler->literalCache.literals[i]); //used an array for storage above
@@ -385,6 +425,24 @@ unsigned char* collateCompiler(Compiler* compiler, int* size) {
} }
freeLiteralArray(ptr); freeLiteralArray(ptr);
}
break;
case LITERAL_TYPE: {
emitByte(&collation, &capacity, &count, LITERAL_TYPE);
LiteralArray* ptr = AS_ARRAY(compiler->literalCache.literals[i]); //used an array for storage above
//length of the array, as a short
emitShort(&collation, &capacity, &count, ptr->count); //count is the array size
//each element of the array
for (int i = 0; i < ptr->count; i++) {
emitShort(&collation, &capacity, &count, (unsigned short)AS_INTEGER(ptr->literals[i])); //shorts representing the indexes of the values
}
freeLiteralArray(ptr);
}
break; break;
default: default:

View File

@@ -210,6 +210,12 @@ void printLiteralCustom(Literal literal, void (printFn)(const char*)) {
} }
for (int i = 1; i < 8; i ++) { //0th bit is const for (int i = 1; i < 8; i ++) { //0th bit is const
//zero mask = any type, anys can't be const
if (AS_TYPE(literal).mask == 0) {
printToBuffer("any");
break;
}
if (AS_TYPE(literal).mask & MASK(i)) { if (AS_TYPE(literal).mask & MASK(i)) {
//pretty print //pretty print
if (iterations++ > 0) { if (iterations++ > 0) {

View File

@@ -97,7 +97,7 @@ typedef struct {
#define MASK_ARRAY (MASK(TYPE_ARRAY)) #define MASK_ARRAY (MASK(TYPE_ARRAY))
#define MASK_DICTIONARY (MASK(TYPE_DICTIONARY)) #define MASK_DICTIONARY (MASK(TYPE_DICTIONARY))
#define MASK_FUNCTION (MASK(TYPE_FUNCTION)) #define MASK_FUNCTION (MASK(TYPE_FUNCTION))
#define MASK_ANY (MASK_BOOLEAN|MASK_INTEGER|MASK_FLOAT|MASK_STRING|MASK_ARRAY|MASK_DICTIONARY|MASK_FUNCTION) #define MASK_ANY 0
//utils //utils
void printLiteral(Literal literal); void printLiteral(Literal literal);

View File

@@ -53,15 +53,12 @@ void freeNode(Node* node) {
break; break;
case NODE_VAR_TYPES: case NODE_VAR_TYPES:
for (int i = 0; i < node->varTypes.count; i++) { freeLiteral(node->varTypes.typeLiteral);
freeNode(node->varTypes.nodes + 1);
}
FREE_ARRAY(Node, node->varTypes.nodes, node->varTypes.capacity);
break; break;
case NODE_VAR_DECL: case NODE_VAR_DECL:
freeLiteral(node->varDecl.identifier); freeLiteral(node->varDecl.identifier);
freeNode(node->varDecl.varType); freeLiteral(node->varDecl.typeLiteral);
freeNode(node->varDecl.expression); freeNode(node->varDecl.expression);
break; break;
} }
@@ -137,24 +134,21 @@ void emitNodePair(Node** nodeHandle, Node* left, Node* right) {
*nodeHandle = tmp; *nodeHandle = tmp;
} }
void emitNodeVarTypes(Node** nodeHandle, unsigned char mask) { void emitNodeVarTypes(Node** nodeHandle, Literal literal) {
Node* tmp = ALLOCATE(Node, 1); Node* tmp = ALLOCATE(Node, 1);
tmp->type = NODE_VAR_TYPES; tmp->type = NODE_VAR_TYPES;
tmp->varTypes.mask = mask; tmp->varTypes.typeLiteral = literal;
tmp->varTypes.nodes = NULL;
tmp->varTypes.capacity = 0;
tmp->varTypes.count = 0;
*nodeHandle = tmp; *nodeHandle = tmp;
} }
void emitNodeVarDecl(Node** nodeHandle, Literal identifier, Node* varType, Node* expression) { void emitNodeVarDecl(Node** nodeHandle, Literal identifier, Literal type, Node* expression) {
Node* tmp = ALLOCATE(Node, 1); Node* tmp = ALLOCATE(Node, 1);
tmp->type = NODE_VAR_DECL; tmp->type = NODE_VAR_DECL;
tmp->varDecl.identifier = identifier; tmp->varDecl.identifier = identifier;
tmp->varDecl.varType = varType; tmp->varDecl.typeLiteral = type;
tmp->varDecl.expression = expression; tmp->varDecl.expression = expression;
*nodeHandle = tmp; *nodeHandle = tmp;
@@ -223,20 +217,14 @@ void printNode(Node* node) {
break; break;
case NODE_VAR_TYPES: case NODE_VAR_TYPES:
printf("type[\n"); printLiteral(node->varTypes.typeLiteral);
for (int i = 0; i < node->varTypes.count; i++) {
printNode(&(node->varTypes.nodes[i]));
}
printf("]\n");
break; break;
case NODE_VAR_DECL: case NODE_VAR_DECL:
printf("vardecl("); printf("vardecl(");
printLiteral(node->varDecl.identifier); printLiteral(node->varDecl.identifier);
printf("; "); printf("; ");
printNode(node->varDecl.varType); printLiteral(node->varDecl.typeLiteral);
printf("; "); printf("; ");
printNode(node->varDecl.expression); printNode(node->varDecl.expression);
printf(")"); printf(")");

View File

@@ -66,16 +66,13 @@ typedef struct NodePair {
typedef struct NodeVarTypes { typedef struct NodeVarTypes {
NodeType type; NodeType type;
unsigned char mask; //the type mask Literal typeLiteral;
Node* nodes;
int capacity;
int count;
} NodeVarTypes; } NodeVarTypes;
typedef struct NodeVarDecl { typedef struct NodeVarDecl {
NodeType type; NodeType type;
Literal identifier; Literal identifier;
Node* varType; Literal typeLiteral;
Node* expression; Node* expression;
} NodeVarDecl; } NodeVarDecl;
@@ -100,8 +97,8 @@ void emitNodeGrouping(Node** nodeHandle);
void emitNodeBlock(Node** nodeHandle); void emitNodeBlock(Node** nodeHandle);
void emitNodeCompound(Node** nodeHandle, LiteralType literalType); void emitNodeCompound(Node** nodeHandle, LiteralType literalType);
void emitNodePair(Node** nodeHandle, Node* left, Node* right); void emitNodePair(Node** nodeHandle, Node* left, Node* right);
void emitNodeVarTypes(Node** nodeHandle, unsigned char mask); void emitNodeVarTypes(Node** nodeHandle, Literal literal);
void emitNodeVarDecl(Node** nodeHandle, Literal identifier, Node* varType, Node* expression); void emitNodeVarDecl(Node** nodeHandle, Literal identifier, Literal type, Node* expression);
void printNode(Node* node); void printNode(Node* node);

View File

@@ -25,9 +25,11 @@ typedef enum Opcode {
OP_SCOPE_BEGIN, OP_SCOPE_BEGIN,
OP_SCOPE_END, OP_SCOPE_END,
OP_TYPE_DECL, //declare a type to be used OP_TYPE_DECL, //declare a type to be used (as a literal)
OP_VAR_DECL, //stack: literal name, literal type (referenced by array index) OP_TYPE_DECL_LONG, //declare a type to be used (as a long literal)
OP_VAR_ASSIGN, //stack: literal name, literal value
// OP_VAR_DECL, //stack: literal name, literal type (referenced by array index)
// OP_VAR_ASSIGN, //stack: literal name, literal value
//meta //meta
OP_SECTION_END, OP_SECTION_END,

View File

@@ -731,83 +731,76 @@ static void statement(Parser* parser, Node* node) {
} }
//declarations and definitions //declarations and definitions
static void readVarType(Parser* parser, Node** nodeHandle) { static Literal readTypeToLiteral(Parser* parser) {
//TODO: custom types with the "type" keyword
advance(parser); advance(parser);
unsigned char typeMask = 0; Literal literal = TO_TYPE_LITERAL(MASK_ANY);
Node* left = NULL;
Node* right = NULL;
switch(parser->previous.type) { switch(parser->previous.type) {
case TOKEN_BOOLEAN: case TOKEN_BOOLEAN:
typeMask |= MASK_BOOLEAN; AS_TYPE(literal).mask |= MASK_BOOLEAN;
break; break;
case TOKEN_INTEGER: case TOKEN_INTEGER:
typeMask |= MASK_INTEGER; AS_TYPE(literal).mask |= MASK_INTEGER;
break; break;
case TOKEN_FLOAT: case TOKEN_FLOAT:
typeMask |= MASK_FLOAT; AS_TYPE(literal).mask |= MASK_FLOAT;
break; break;
case TOKEN_STRING: case TOKEN_STRING:
typeMask |= MASK_STRING; AS_TYPE(literal).mask |= MASK_STRING;
break; break;
//array, dictionary - read the sub-types //array, dictionary - read the sub-types
case TOKEN_BRACKET_LEFT: case TOKEN_BRACKET_LEFT: {
//at least 1 type required Literal l = readTypeToLiteral(parser);
readVarType(parser, &left);
if (match(parser, TOKEN_COMMA)) { if (match(parser, TOKEN_COMMA)) {
//if there's 2 types, it's a dictionary Literal r = readTypeToLiteral(parser);
readVarType(parser, &right);
typeMask |= MASK_DICTIONARY; //dictionary
Literal* dict = TYPE_PUSH_SUBTYPE(&literal, MASK_DICTIONARY);
((Literal*)(AS_TYPE(*dict).subtypes))[0] = l;
((Literal*)(AS_TYPE(*dict).subtypes))[1] = r;
} }
else { else {
//else it's just an array //array
typeMask |= MASK_ARRAY; Literal* arr = TYPE_PUSH_SUBTYPE(&literal, MASK_ARRAY);
}
consume(parser, TOKEN_BRACKET_RIGHT, "Expected ']' at end of type definition");
break;
case TOKEN_ANY: //append the "l" literal
typeMask |= MASK_ANY; ((Literal*)(AS_TYPE(*arr).subtypes))[0] = l;
}
consume(parser, TOKEN_BRACKET_RIGHT, "Expected ']' at end of type definition");
}
break; break;
//TODO: function //TODO: function
default: default:
error(parser, parser->previous, "Bad type"); error(parser, parser->previous, "Bad type signature");
return; return TO_NULL_LITERAL;
} }
//const follows the type //const follows the type
if (match(parser, TOKEN_CONST)) { if (match(parser, TOKEN_CONST)) {
typeMask |= MASK_CONST; AS_TYPE(literal).mask |= MASK_CONST;
} }
return literal;
}
static void readVarType(Parser* parser, Node** nodeHandle) {
//TODO: custom types with the "type" keyword
//get the type literal
Literal type = readTypeToLiteral(parser);
//generate the node //generate the node
emitNodeVarTypes(nodeHandle, typeMask); emitNodeVarTypes(nodeHandle, type);
//check for sub-nodes
if (left) {
int oldCapacity = (*nodeHandle)->varTypes.capacity;
(*nodeHandle)->varTypes.capacity = GROW_CAPACITY(oldCapacity);
(*nodeHandle)->varTypes.nodes = GROW_ARRAY(Node, (*nodeHandle)->varTypes.nodes, oldCapacity, (*nodeHandle)->varTypes.capacity);
//push left to the array
*((*nodeHandle)->varTypes.nodes) = *left;
//append the other one too
if (right) {
*((*nodeHandle)->varTypes.nodes + 1) = *right;
}
}
} }
static void varDecl(Parser* parser, Node** nodeHandle) { static void varDecl(Parser* parser, Node** nodeHandle) {
@@ -827,10 +820,10 @@ static void varDecl(Parser* parser, Node** nodeHandle) {
expression(parser, &expressionNode); expression(parser, &expressionNode);
} }
//TODO: compile-time static type check? //TODO: static type checking?
//finally //finally
emitNodeVarDecl(nodeHandle, TO_IDENTIFIER_LITERAL(identifierToken.lexeme), typeNode, expressionNode); // emitNodeVarDecl(nodeHandle, TO_IDENTIFIER_LITERAL(identifierToken.lexeme), typeNode, expressionNode);
consume(parser, TOKEN_SEMICOLON, "Expected ';' at end of var declaration"); consume(parser, TOKEN_SEMICOLON, "Expected ';' at end of var declaration");
} }

View File

@@ -247,27 +247,34 @@ int main(int argc, const char* argv[]) {
return 0; return 0;
} }
// repl(); repl();
//testing the types, improving on them as I go // //testing the types, improving on them as I go
Literal root = TO_TYPE_LITERAL( MASK_ARRAY | MASK_DICTIONARY ); // Literal root = TO_TYPE_LITERAL( MASK_ARRAY | MASK_DICTIONARY | MASK_INTEGER | MASK_FLOAT );
Literal* arr = TYPE_PUSH_SUBTYPE(&root, MASK_ARRAY ); // Literal* arr = TYPE_PUSH_SUBTYPE(&root, MASK_ARRAY );
Literal* dict = TYPE_PUSH_SUBTYPE(&root, MASK_DICTIONARY ); // Literal* dict = TYPE_PUSH_SUBTYPE(&root, MASK_DICTIONARY );
TYPE_PUSH_SUBTYPE(arr, MASK_INTEGER ); // TYPE_PUSH_SUBTYPE(arr, MASK_INTEGER );
TYPE_PUSH_SUBTYPE(arr, MASK_FLOAT ); // TYPE_PUSH_SUBTYPE(arr, MASK_FLOAT );
TYPE_PUSH_SUBTYPE(arr, MASK_INTEGER | MASK_FLOAT ); // TYPE_PUSH_SUBTYPE(arr, MASK_INTEGER | MASK_FLOAT );
TYPE_PUSH_SUBTYPE(dict, MASK_STRING | MASK_CONST ); // TYPE_PUSH_SUBTYPE(dict, MASK_STRING | MASK_CONST );
TYPE_PUSH_SUBTYPE(dict, MASK_BOOLEAN ); // TYPE_PUSH_SUBTYPE(dict, MASK_FLOAT );
TYPE_PUSH_SUBTYPE(dict, MASK_STRING | MASK_CONST ); // TYPE_PUSH_SUBTYPE(dict, MASK_STRING | MASK_CONST );
TYPE_PUSH_SUBTYPE(dict, MASK_INTEGER ); // TYPE_PUSH_SUBTYPE(dict, MASK_INTEGER );
printLiteral(root); // TYPE_PUSH_SUBTYPE(dict, MASK_STRING | MASK_CONST );
printf("\n"); // TYPE_PUSH_SUBTYPE(dict, MASK_INTEGER | MASK_FLOAT );
//output: <[int] | [float] | [int | float] | [string const:bool] | [string const:int]> // printLiteral(root);
// printf("\n");
// Literal any = TO_TYPE_LITERAL(MASK_ANY);
// printLiteral(any);
//output: <int | float | [int] | [float] | [int | float] | [string const:float] | [string const:int] | [string const:int | float]>
return 0; return 0;
} }