started work on assignment
This commit is contained in:
@@ -100,6 +100,8 @@ ResultType(voidPtr, charptr) compile(CometCompiler* c, CometASTNode* node) {
|
||||
|
||||
case AST_EXPRESSION_STATEMENT:
|
||||
return visitExpressionStatement(c, node);
|
||||
case AST_ASSIGN_STATEMENT:
|
||||
return visitAssignStatement(c, node);
|
||||
|
||||
case AST_INFIX_EXPRESSION: {
|
||||
ResultType(CometOperand, charptr) value = visitInfixExpression(c, node);
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#include "environment.h"
|
||||
#include <llvm-c/Types.h>
|
||||
|
||||
|
||||
CometEnvironment* newEnvironment(char* name, CometEnvironment* parent) {
|
||||
@@ -9,25 +8,32 @@ CometEnvironment* newEnvironment(char* name, CometEnvironment* parent) {
|
||||
env->name = name;
|
||||
env->parent = parent;
|
||||
env->records = NULL;
|
||||
env->recordIdx = 0;
|
||||
return env;
|
||||
}
|
||||
|
||||
void defineVar(CometEnvironment* env, char* name, bool isMutable) {
|
||||
uint32_t defineVar(CometEnvironment* env, char* name, CometOperand value, bool isMutable) {
|
||||
Record* record;
|
||||
HASH_FIND_STR(env->records, name, record);
|
||||
|
||||
// avoid duplication of keys
|
||||
if (record != NULL) {
|
||||
printf("Redeclaration of %s!\n", name);
|
||||
return;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// create a new record and save it to the hash map (or dictionary or whatever you wanna call it)
|
||||
record = malloc(sizeof(Record));
|
||||
record->name = strdup(name);
|
||||
record->value = value;
|
||||
record->isMutable = isMutable;
|
||||
|
||||
record->recordIdx = env->recordIdx;
|
||||
env->recordIdx++;
|
||||
|
||||
HASH_ADD_KEYPTR(hh, env->records, record->name, strlen(record->name), record);
|
||||
|
||||
return record->recordIdx;
|
||||
}
|
||||
|
||||
Record* lookup(CometEnvironment* env, char* name) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
#include "ast.h"
|
||||
#include "lexer.h"
|
||||
#include "operand.h"
|
||||
#include "token.h"
|
||||
#include <uthash.h>
|
||||
#include <stdlib.h>
|
||||
@@ -11,16 +12,19 @@ typedef struct {
|
||||
char* name;
|
||||
UT_hash_handle hh;
|
||||
bool isMutable;
|
||||
CometOperand value;
|
||||
uint32_t recordIdx;
|
||||
} Record;
|
||||
|
||||
typedef struct CometEnvironment CometEnvironment;
|
||||
struct CometEnvironment {
|
||||
CometEnvironment* parent;
|
||||
char* name;
|
||||
uint32_t recordIdx;
|
||||
Record* records;
|
||||
};
|
||||
|
||||
|
||||
CometEnvironment* newEnvironment(char* name, CometEnvironment* parent);
|
||||
void defineVar(CometEnvironment* env, char* name, bool isMutable);
|
||||
uint32_t defineVar(CometEnvironment* env, char* name, CometOperand value, bool isMutable);
|
||||
Record* lookup(CometEnvironment* env, char* name);
|
||||
33
src/inst.c
33
src/inst.c
@@ -1,4 +1,6 @@
|
||||
#include "inst.h"
|
||||
#include "environment.h"
|
||||
#include "operand.h"
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -85,8 +87,8 @@ char* cometInstructionToCStr(CometCompiler* c, CometInst inst) {
|
||||
sprintf(
|
||||
extra,
|
||||
"; consts[%d] = %s",
|
||||
inst.dest.imm.intVal,
|
||||
cometOperandToCStr(c->consts[inst.dest.imm.intVal])
|
||||
inst.a.imm.intVal,
|
||||
cometOperandToCStr(c->consts[inst.a.imm.intVal])
|
||||
);
|
||||
break;
|
||||
default: extra = "";
|
||||
@@ -96,9 +98,9 @@ char* cometInstructionToCStr(CometCompiler* c, CometInst inst) {
|
||||
buffer,
|
||||
"%s%s, %s, %s %s",
|
||||
cometInstOpcodeToCStr(inst.opcode),
|
||||
cometOperandToCStr(inst.dest),
|
||||
cometOperandToCStr(inst.a),
|
||||
cometOperandToCStr(inst.b),
|
||||
cometOperandToCStr(inst.c),
|
||||
extra
|
||||
);
|
||||
|
||||
@@ -106,19 +108,19 @@ char* cometInstructionToCStr(CometCompiler* c, CometInst inst) {
|
||||
}
|
||||
|
||||
void buildInst(
|
||||
CometCompiler* c,
|
||||
CometCompiler* compiler,
|
||||
CometInstType opcode,
|
||||
CometOperand dest,
|
||||
CometOperand a,
|
||||
CometOperand b
|
||||
CometOperand b,
|
||||
CometOperand c
|
||||
) {
|
||||
c->outputProgram[c->programIdx] = (CometInst){
|
||||
compiler->outputProgram[compiler->programIdx] = (CometInst){
|
||||
.opcode = opcode,
|
||||
.dest = dest,
|
||||
.a = a,
|
||||
.b = b,
|
||||
.c = c
|
||||
};
|
||||
c->programIdx++;
|
||||
compiler->programIdx++;
|
||||
}
|
||||
|
||||
ResultType(cometCompilerPtr, charptr) newCompiler() {
|
||||
@@ -126,6 +128,7 @@ ResultType(cometCompilerPtr, charptr) newCompiler() {
|
||||
|
||||
newCompiler->programIdx = 0;
|
||||
newCompiler->stackIdx = 0;
|
||||
newCompiler->env = newEnvironment("root", NULL);
|
||||
|
||||
return Success(cometCompilerPtr, charptr, newCompiler);
|
||||
}
|
||||
@@ -216,6 +219,18 @@ void buildPushConst(CometCompiler* c, CometOperand idx) {
|
||||
|
||||
buildInst(c, INST_PUSH_CONST, idx, NO_OPERAND, NO_OPERAND);
|
||||
}
|
||||
void buildStore(CometCompiler* c, uint32_t idx) {
|
||||
popVal(c);
|
||||
|
||||
|
||||
CometOperand value = createOperand(CO_IMMEDIATE);
|
||||
value.imm = (CometImmediate){
|
||||
.typeKind = COMET_INT,
|
||||
.intVal = idx
|
||||
};
|
||||
|
||||
buildInst(c, INST_STORE, value, NO_OPERAND, NO_OPERAND);
|
||||
}
|
||||
CometOperand buildAdd(CometCompiler* c) {
|
||||
popVal(c);
|
||||
popVal(c);
|
||||
|
||||
43
src/inst.h
43
src/inst.h
@@ -3,47 +3,17 @@
|
||||
|
||||
#include "../include/error.h"
|
||||
#include "environment.h"
|
||||
#include "operand.h"
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef enum {
|
||||
COMET_VOID,
|
||||
COMET_SMALL,
|
||||
COMET_INT,
|
||||
COMET_BIG,
|
||||
COMET_FLOAT,
|
||||
COMET_DOUBLE,
|
||||
COMET_BOOL
|
||||
} CometValueTypeKind;
|
||||
|
||||
typedef enum {
|
||||
CO_IMMEDIATE,
|
||||
CO_STACK
|
||||
} CometOperandKind;
|
||||
|
||||
typedef struct {
|
||||
CometValueTypeKind typeKind;
|
||||
union {
|
||||
int8_t smallVal;
|
||||
int32_t intVal;
|
||||
int64_t bigVal;
|
||||
float floatVal;
|
||||
double doubleVal;
|
||||
bool boolVal;
|
||||
};
|
||||
} CometImmediate;
|
||||
|
||||
typedef struct {
|
||||
CometOperandKind type;
|
||||
union {
|
||||
uint32_t stackIdx;
|
||||
CometImmediate imm;
|
||||
};
|
||||
} CometOperand;
|
||||
|
||||
typedef enum {
|
||||
INST_PUSH_CONST,
|
||||
INST_STORE,
|
||||
INST_LOAD,
|
||||
INST_ADD,
|
||||
INST_SUB,
|
||||
INST_MUL
|
||||
@@ -51,9 +21,9 @@ typedef enum {
|
||||
|
||||
typedef struct {
|
||||
CometInstType opcode;
|
||||
CometOperand dest;
|
||||
CometOperand a;
|
||||
CometOperand b;
|
||||
CometOperand c;
|
||||
} CometInst;
|
||||
|
||||
#define NO_OPERAND ((CometOperand){0})
|
||||
@@ -64,6 +34,7 @@ typedef struct {
|
||||
uint32_t constIdx;
|
||||
CometOperand consts[256];
|
||||
CometInst outputProgram[256];
|
||||
CometEnvironment* env;
|
||||
} CometCompiler;
|
||||
|
||||
typedef CometCompiler* cometCompilerPtr;
|
||||
@@ -88,8 +59,10 @@ void buildInst(
|
||||
CometOperand a,
|
||||
CometOperand b
|
||||
);
|
||||
void buildPushConst(CometCompiler* c, CometOperand idx);
|
||||
CometOperand storeConst(CometCompiler* c, CometOperand value);
|
||||
void buildPushConst(CometCompiler* c, CometOperand idx);
|
||||
void buildStore(CometCompiler* c, uint32_t idx);
|
||||
CometOperand buildLoad(CometCompiler* c);
|
||||
CometOperand buildAdd(CometCompiler* c);
|
||||
CometOperand buildSub(CometCompiler* c);
|
||||
CometOperand buildMul(CometCompiler* c);
|
||||
|
||||
41
src/operand.h
Normal file
41
src/operand.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef OPERAND_H
|
||||
#define OPERAND_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum {
|
||||
COMET_VOID,
|
||||
COMET_SMALL,
|
||||
COMET_INT,
|
||||
COMET_BIG,
|
||||
COMET_FLOAT,
|
||||
COMET_DOUBLE,
|
||||
COMET_BOOL
|
||||
} CometValueTypeKind;
|
||||
|
||||
typedef enum {
|
||||
CO_IMMEDIATE,
|
||||
CO_STACK
|
||||
} CometOperandKind;
|
||||
|
||||
typedef struct {
|
||||
CometValueTypeKind typeKind;
|
||||
union {
|
||||
int8_t smallVal;
|
||||
int32_t intVal;
|
||||
int64_t bigVal;
|
||||
float floatVal;
|
||||
double doubleVal;
|
||||
bool boolVal;
|
||||
};
|
||||
} CometImmediate;
|
||||
|
||||
typedef struct {
|
||||
CometOperandKind type;
|
||||
union {
|
||||
uint32_t stackIdx;
|
||||
CometImmediate imm;
|
||||
};
|
||||
} CometOperand;
|
||||
|
||||
#endif
|
||||
@@ -1 +1 @@
|
||||
2 + 5 * (4 - 1)
|
||||
int x = 5
|
||||
Reference in New Issue
Block a user