From 1d646ef21b040e2385efb91227c34b44df70a2a6 Mon Sep 17 00:00:00 2001 From: SpookyDervish Date: Tue, 26 May 2026 07:57:21 +1000 Subject: [PATCH] started work on assignment --- src/compiler_vm.c | 2 ++ src/environment.c | 12 +++++++++--- src/environment.h | 6 +++++- src/inst.c | 33 ++++++++++++++++++++++++--------- src/inst.h | 43 ++++++++----------------------------------- src/operand.h | 41 +++++++++++++++++++++++++++++++++++++++++ test.comet | 2 +- 7 files changed, 90 insertions(+), 49 deletions(-) create mode 100644 src/operand.h diff --git a/src/compiler_vm.c b/src/compiler_vm.c index c87577a..fec20d6 100644 --- a/src/compiler_vm.c +++ b/src/compiler_vm.c @@ -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); diff --git a/src/environment.c b/src/environment.c index 4927654..10397ab 100644 --- a/src/environment.c +++ b/src/environment.c @@ -1,5 +1,4 @@ #include "environment.h" -#include 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) { diff --git a/src/environment.h b/src/environment.h index 4b19ff4..6b485ea 100644 --- a/src/environment.h +++ b/src/environment.h @@ -1,6 +1,7 @@ #pragma once #include "ast.h" #include "lexer.h" +#include "operand.h" #include "token.h" #include #include @@ -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); \ No newline at end of file diff --git a/src/inst.c b/src/inst.c index fdeccdb..27b0908 100644 --- a/src/inst.c +++ b/src/inst.c @@ -1,4 +1,6 @@ #include "inst.h" +#include "environment.h" +#include "operand.h" #include #include #include @@ -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); diff --git a/src/inst.h b/src/inst.h index ec7ad22..8609a30 100644 --- a/src/inst.h +++ b/src/inst.h @@ -3,47 +3,17 @@ #include "../include/error.h" #include "environment.h" +#include "operand.h" #include #include #include -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); diff --git a/src/operand.h b/src/operand.h new file mode 100644 index 0000000..00d8a90 --- /dev/null +++ b/src/operand.h @@ -0,0 +1,41 @@ +#ifndef OPERAND_H +#define OPERAND_H + +#include + +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 \ No newline at end of file diff --git a/test.comet b/test.comet index 6ec5db5..71a7d0b 100644 --- a/test.comet +++ b/test.comet @@ -1 +1 @@ -2 + 5 * (4 - 1) \ No newline at end of file +int x = 5 \ No newline at end of file