Initial commit, save progress so far
This commit is contained in:
93
src/lexer/SolsType.c
Normal file
93
src/lexer/SolsType.c
Normal file
@@ -0,0 +1,93 @@
|
||||
#include "lexer.h"
|
||||
#include "../include/error.h"
|
||||
#include "../include/estr.h"
|
||||
|
||||
ResultType(SolsType, charptr) createSolsType(SolsTypeType in) {
|
||||
SolsTypeField* ptr = malloc(sizeof(SolsTypeField) * 32);
|
||||
if (ptr == NULL) {
|
||||
return Error(SolsType, charptr, "Couldn't allocate memory (in createSolsType() function)");
|
||||
}
|
||||
SolsType type = { .type = in, .children.capacity = 32, .children.at = ptr };
|
||||
return Success(SolsType, charptr, type);
|
||||
}
|
||||
|
||||
ResultType(SolsType, charptr) copySolsType(SolsType* type) {
|
||||
SolsType ret = { .type = type->type, .children.count = type->children.count, .children.capacity = type->children.capacity};
|
||||
|
||||
// Allocate memory
|
||||
SolsTypeField* ptr = malloc(sizeof(SolsTypeField) * type->children.capacity);
|
||||
if (ptr == NULL) {
|
||||
return Error(SolsType, charptr, "Couldn't allocate memory (in copySolsType() function)");
|
||||
}
|
||||
ret.children.at = ptr;
|
||||
|
||||
// Deep copy values
|
||||
for (size_t i = 0; i < type->children.count; i++) {
|
||||
// Copy the SolsType value
|
||||
ResultType(SolsType, charptr) copied = copySolsType(&type->children.at[i].type);
|
||||
if (copied.error) {
|
||||
Estr err = CREATE_ESTR(copied.as.error);
|
||||
APPEND_ESTR(err, " (in addChildToSolsType() function)");
|
||||
return Error(SolsType, charptr, err.str);
|
||||
}
|
||||
ret.children.at[i].type = copied.as.success;
|
||||
|
||||
// Copy the name
|
||||
if (type->children.at[i].name == NULL) {
|
||||
ret.children.at[i].name = NULL;
|
||||
} else {
|
||||
ret.children.at[i].name = malloc(strlen(type->children.at[i].name) + 1);
|
||||
if (ret.children.at[i].name == NULL) {
|
||||
return Error(SolsType, charptr, "Couldn't allocate memory (in copySolsType() function)");
|
||||
}
|
||||
strcpy(ret.children.at[i].name, type->children.at[i].name);
|
||||
}
|
||||
}
|
||||
return Success(SolsType, charptr, ret);
|
||||
}
|
||||
|
||||
ResultType(voidptr, charptr) addChildToSolsType(SolsType* type, SolsType child, const char* name) {
|
||||
if (type->children.capacity < type->children.count + 1) {
|
||||
type->children.capacity *= 2;
|
||||
SolsTypeField* ptr = realloc(type->children.at, sizeof(SolsTypeField) * type->children.capacity);
|
||||
if (ptr == NULL) {
|
||||
return Error(voidptr, charptr, "Couldn't allocate memory (in addChildToSolsType() function)");
|
||||
}
|
||||
type->children.at = ptr;
|
||||
}
|
||||
ResultType(SolsType, charptr) copied = copySolsType(&child);
|
||||
if (copied.error) {
|
||||
Estr err = CREATE_ESTR(copied.as.error);
|
||||
APPEND_ESTR(err, " (in addChildToSolsType() function)");
|
||||
return Error(voidptr, charptr, err.str);
|
||||
}
|
||||
type->children.at[type->children.count].type = copied.as.success;
|
||||
if (name == NULL) {
|
||||
type->children.at[type->children.count].name = NULL;
|
||||
} else {
|
||||
type->children.at[type->children.count].name = malloc(strlen(name) + 1);
|
||||
strcpy(type->children.at[type->children.count].name, name);
|
||||
}
|
||||
type->children.count++;
|
||||
|
||||
return Success(voidptr, charptr, NULL);
|
||||
}
|
||||
|
||||
void freeSolsType(SolsType* type) {
|
||||
for (size_t i = 0; i < type->children.count; i++) {
|
||||
// Free the name
|
||||
if (type->children.at[i].name != NULL) {
|
||||
free(type->children.at[i].name);
|
||||
}
|
||||
|
||||
// Free the child SolsTypes
|
||||
freeSolsType(&type->children.at[i].type);
|
||||
}
|
||||
// Free the field itself
|
||||
free(type->children.at);
|
||||
type->children.at = NULL;
|
||||
|
||||
// Set count and capacity to zero
|
||||
type->children.count = 0;
|
||||
type->children.capacity = 0;
|
||||
}
|
||||
80
src/lexer/SolsType.h
Normal file
80
src/lexer/SolsType.h
Normal file
@@ -0,0 +1,80 @@
|
||||
#ifndef SOLSTYPE_H
|
||||
#define SOLSTYPE_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "../include/error.h"
|
||||
|
||||
typedef enum SolsTypeType {
|
||||
STT_INT, STT_STRING, STT_DOUBLE, STT_BOOL, STT_CHAR, STT_FUN, STT_TEMPLATE, STT_OBJECT
|
||||
} SolsTypeType;
|
||||
|
||||
// Definition of charptr for Result() and ResultType() macros
|
||||
typedef char* charptr;
|
||||
// Definition of voidptr for Result() and ResultType() macros
|
||||
typedef void* voidptr;
|
||||
|
||||
struct SolsTypeField;
|
||||
|
||||
// Holds type information for a struct, object or function.
|
||||
// Say, for example, your type signature looks like this:
|
||||
// object(string x, fun(int) y)
|
||||
// This is stored like this:
|
||||
// SolsType {
|
||||
// type: STT_OBJECT
|
||||
// children: [
|
||||
// {
|
||||
// type: {
|
||||
// type: STT_STRING
|
||||
// }
|
||||
// name: "x"
|
||||
// }
|
||||
// {
|
||||
// type: {
|
||||
// type: STT_FUN
|
||||
// children: [
|
||||
// {
|
||||
// type: {
|
||||
// type: STT_INT
|
||||
// }
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
// name: "y"
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
//
|
||||
// (Sorry for the long explaination, but it's worth it so you know how the type system works.)
|
||||
//
|
||||
typedef struct SolsType {
|
||||
SolsTypeType type;
|
||||
struct {
|
||||
struct SolsTypeField* at;
|
||||
size_t count;
|
||||
size_t capacity;
|
||||
} children;
|
||||
} SolsType;
|
||||
|
||||
// Assists with holding child types in the SolsType struct.
|
||||
typedef struct SolsTypeField {
|
||||
SolsType type;
|
||||
char* name;
|
||||
} SolsTypeField;
|
||||
|
||||
|
||||
// Creates a SolsType, with the provided type type.
|
||||
// Use the "addChildToSolsType()" function to add children, in case this type has children.
|
||||
Result(SolsType, charptr);
|
||||
ResultType(SolsType, charptr) createSolsType(SolsTypeType in);
|
||||
|
||||
// Adds a child SolsType to a given SolsType.
|
||||
Result(voidptr, charptr);
|
||||
ResultType(voidptr, charptr) addChildToSolsType(SolsType* type, SolsType child, const char* name);
|
||||
|
||||
// Makes a deep copy of a SolsType.
|
||||
ResultType(SolsType, charptr) copySolsType(SolsType* type);
|
||||
|
||||
// Frees a SolsType
|
||||
void freeSolsType(SolsType* type);
|
||||
|
||||
#endif
|
||||
0
src/lexer/lexer.c
Normal file
0
src/lexer/lexer.c
Normal file
77
src/lexer/lexer.h
Normal file
77
src/lexer/lexer.h
Normal file
@@ -0,0 +1,77 @@
|
||||
#ifndef LEXER_H
|
||||
#define LEXER_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../include/error.h"
|
||||
|
||||
#include "SolsType.h"
|
||||
|
||||
typedef enum SolsTokenType {
|
||||
STT_IDENTIFIER, STT_LITERAL, STT_TYPE, STT_OPEN_CURLY, STT_CLOSE_CURLY, STT_OPEN_PAREN, STT_CLOSE_PAREN, STT_OP_ADD, STT_OP_SUB, STT_OP_MUL, STT_OP_DIV, STT_OP_SET, STT_OP_GREATER, STT_OP_LESSER, STT_OP_EQUAL, STT_OP_INEQUAL, STT_OP_EQGREATER, STT_OP_EQLESSER, STT_KW_DEF, STT_KW_STRUCT, STT_KW_PUTS, STT_KW_GROUND
|
||||
} SolsTokenType;
|
||||
|
||||
typedef enum SolsLiteralType {
|
||||
SLT_INT, SLT_STRING, SLT_DOUBLE, SLT_BOOL, SLT_CHAR
|
||||
} SolsLiteralType;
|
||||
|
||||
// Stores literal values which will be added to the Ground code.
|
||||
// Not much explaining needed here.
|
||||
typedef struct SolsLiteral {
|
||||
SolsLiteralType type;
|
||||
union {
|
||||
int64_t intv;
|
||||
char* stringv;
|
||||
double doublev;
|
||||
bool boolv;
|
||||
char charv;
|
||||
} as;
|
||||
} SolsLiteral;
|
||||
|
||||
// Represents a token lexed by the lex() function.
|
||||
// Most token types exclusively use the .type field, however some tokens require storing
|
||||
// more data, inside the .as union.
|
||||
// Those tokens are:
|
||||
// STT_LITERAL: A literal value. Uses field .as.literal
|
||||
// STT_TYPE: A type descriptor. Uses field .as.type
|
||||
// STT_IDENTIFIER: An identifier. Uses field .as.idName
|
||||
// STT_KW_GROUND: Ground code embedded inside Solstice. Uses field .as.inlineGround
|
||||
typedef struct SolsToken {
|
||||
SolsTokenType type;
|
||||
union {
|
||||
SolsLiteral literal;
|
||||
SolsType type;
|
||||
char* idName;
|
||||
char* inlineGround;
|
||||
} as;
|
||||
} SolsToken;
|
||||
|
||||
// Represents a Solstice program, seperated into tokens.
|
||||
// .at is a pointer to the tokens
|
||||
// .count is how many tokens are currently being stored
|
||||
// .capacity is how many tokens worth of memory is allocated
|
||||
typedef struct SolsTokens {
|
||||
SolsToken* at;
|
||||
size_t count;
|
||||
size_t capacity;
|
||||
} SolsTokens;
|
||||
|
||||
// Represents the current state of the lexer.
|
||||
// .input is the Solstice program as written by the user.
|
||||
// .output is the lexed Solstice program, which is constructed by the lex() function.
|
||||
// .current represents the current character from .input being lexed.
|
||||
typedef struct SolsLexer {
|
||||
char* input;
|
||||
SolsTokens output;
|
||||
size_t current;
|
||||
} SolsLexer;
|
||||
|
||||
// Creates a lexer for use by the lex() function.
|
||||
SolsLexer createLexer(char* input);
|
||||
|
||||
// Uses the provided lexer to scan the code, and create tokens.
|
||||
void lex(SolsLexer* lexer);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user