Start work on type checker (uh oh)
This commit is contained in:
21
src/codegen/SolsScope.c
Normal file
21
src/codegen/SolsScope.c
Normal file
@@ -0,0 +1,21 @@
|
||||
#include "SolsScope.h"
|
||||
|
||||
#include "../include/uthash.h"
|
||||
#include "../lexer/SolsType.h"
|
||||
|
||||
void addVariableToScope(SolsScope* scope, const char* name, SolsType* type) {
|
||||
SolsVariable* s = malloc(sizeof(SolsVariable));
|
||||
|
||||
strncpy(s->id, name, sizeof(s->id) - 1);
|
||||
s->id[sizeof(s->id) - 1] = '\0';
|
||||
|
||||
s->typeinfo = *type;
|
||||
|
||||
HASH_ADD_STR(scope->variables, id, s);
|
||||
}
|
||||
|
||||
SolsVariable* findVariable(SolsScope* scope, const char* name) {
|
||||
SolsVariable* s;
|
||||
HASH_FIND_STR(scope->variables, name, s);
|
||||
return s;
|
||||
}
|
||||
24
src/codegen/SolsScope.h
Normal file
24
src/codegen/SolsScope.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef SOLSSCOPE_H
|
||||
#define SOLSSCOPE_H
|
||||
|
||||
#include "../include/uthash.h"
|
||||
#include "../lexer/SolsType.h"
|
||||
|
||||
// Stores type information for variables in a UTHash table.
|
||||
typedef struct SolsVariable {
|
||||
char id[256];
|
||||
UT_hash_handle hh;
|
||||
SolsType typeinfo;
|
||||
} SolsVariable;
|
||||
|
||||
typedef struct SolsScope {
|
||||
SolsVariable* variables;
|
||||
} SolsScope;
|
||||
|
||||
// Adds a variable to the SolsScope.
|
||||
void addVariableToScope(SolsScope* scope, const char* name, SolsType* type);
|
||||
|
||||
// Finds a variable in the SolsScope.
|
||||
SolsVariable* findVariable(SolsScope* scope, const char* name);
|
||||
|
||||
#endif
|
||||
@@ -1,8 +1,12 @@
|
||||
#include "codegen.h"
|
||||
|
||||
#include "SolsScope.h"
|
||||
|
||||
#include <groundvm.h>
|
||||
|
||||
#include "../parser/SolsNode.h"
|
||||
#include "../include/estr.h"
|
||||
#include "../include/uthash.h"
|
||||
|
||||
// FIXME add proper erroring function
|
||||
char* createCodegenError(char* what) {
|
||||
@@ -11,12 +15,12 @@ char* createCodegenError(char* what) {
|
||||
return estr.str;
|
||||
}
|
||||
|
||||
static inline ResultType(GroundProgram, charptr) generateLiteralNode(SolsNode* node) {
|
||||
static inline ResultType(GroundProgram, charptr) generateLiteralNode(SolsNode* node, SolsScope* scope) {
|
||||
// We don't even need to do anything lmao
|
||||
return Success(GroundProgram, charptr, groundCreateProgram());
|
||||
}
|
||||
|
||||
static inline ResultType(GroundProgram, charptr) generatePutsNode(SolsNode* node) {
|
||||
static inline ResultType(GroundProgram, charptr) generatePutsNode(SolsNode* node, SolsScope* scope) {
|
||||
if (node->children.count < 1) {
|
||||
return Error(GroundProgram, charptr, "puts requires arguments");
|
||||
}
|
||||
@@ -29,13 +33,13 @@ static inline ResultType(GroundProgram, charptr) generatePutsNode(SolsNode* node
|
||||
return Success(GroundProgram, charptr, program);
|
||||
}
|
||||
|
||||
ResultType(GroundProgram, charptr) generateCode(SolsNode* node) {
|
||||
ResultType(GroundProgram, charptr) generateCode(SolsNode* node, SolsScope* scope) {
|
||||
|
||||
GroundProgram program = groundCreateProgram();
|
||||
|
||||
// Generate code for all children before generating this node's code
|
||||
for (size_t i = 0; i < node->children.count; i++) {
|
||||
ResultType(GroundProgram, charptr) generated = generateCode(&node->children.at[i]);
|
||||
ResultType(GroundProgram, charptr) generated = generateCode(&node->children.at[i], scope);
|
||||
if (generated.error) {
|
||||
return Error(GroundProgram, charptr, createCodegenError(generated.as.error));
|
||||
}
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
#define CODEGEN_H
|
||||
|
||||
#include <groundvm.h>
|
||||
|
||||
#include "SolsScope.h"
|
||||
|
||||
#include "../parser/SolsNode.h"
|
||||
|
||||
Result(GroundProgram, charptr);
|
||||
@@ -11,11 +14,11 @@ Result(GroundProgram, charptr);
|
||||
// Returns:
|
||||
// Success: Generated GroundProgram
|
||||
// Failure: charptr detailing what happened
|
||||
ResultType(GroundProgram, charptr) generateCode(SolsNode* node);
|
||||
ResultType(GroundProgram, charptr) generateCode(SolsNode* node, SolsScope* scope);
|
||||
|
||||
// Macro to help with code generation (and soon error handling)
|
||||
#define generate(nodetype) {\
|
||||
ResultType(GroundProgram, charptr) __result = generate##nodetype##Node(node);\
|
||||
ResultType(GroundProgram, charptr) __result = generate##nodetype##Node(node, scope);\
|
||||
if (__result.error) {\
|
||||
return Error(GroundProgram, charptr, __result.as.error);\
|
||||
}\
|
||||
|
||||
Reference in New Issue
Block a user