From 2267aefca5eb9399829eeef369c469b0f2d79800 Mon Sep 17 00:00:00 2001 From: Lloyd Dilley Date: Fri, 18 Aug 2023 22:31:44 -0400 Subject: [PATCH 1/5] Remove unnecessary cast --- src/vm/instructions.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vm/instructions.c b/src/vm/instructions.c index bcc82e6..ceb77ed 100644 --- a/src/vm/instructions.c +++ b/src/vm/instructions.c @@ -207,7 +207,7 @@ RunCode op_asn(Stack* stack, ConcoctHashMap* map) fprintf(stderr, "Value is NULL during ASN operation.\n"); return RUN_ERROR; } - cct_hash_map_set(map, key->value.strobj.strval, (Object *)val); + cct_hash_map_set(map, key->value.strobj.strval, val); if(debug_mode) print_object_value((Object *)cct_hash_map_get(map, key->value.strobj.strval)); key->is_flagged = true; // throw away the key since we have it in the map From 170e39aa420328adc40a4068ca1ae6f537292114 Mon Sep 17 00:00:00 2001 From: Lloyd Dilley Date: Sat, 9 Sep 2023 13:22:27 -0400 Subject: [PATCH 2/5] Decouple stack from VM --- CMakeLists.txt | 8 ++++---- include/memory.h | 2 +- include/{vm => }/stack.h | 14 +++++++------- include/vm/instructions.h | 2 +- include/vm/vm.h | 2 +- src/compiler.c | 5 ++++- src/{vm => }/stack.c | 12 ++++++------ src/tests/stack_test.c | 2 +- 8 files changed, 25 insertions(+), 22 deletions(-) rename include/{vm => }/stack.h (89%) rename src/{vm => }/stack.c (93%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 82fda48..820958c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,11 +14,11 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "bin") set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD_REQUIRED ON) set(HASH_MAP_TEST_SOURCES src/debug.c src/hash_map.c src/seconds.c src/tests/hash_map_test.c) -set(INTERPRET_TEST_SOURCES src/debug.c src/hash_map.c src/memory.c src/seconds.c src/types.c - src/vm/instructions.c src/vm/opcodes.c src/vm/stack.c src/vm/vm.c src/tests/interpret_test.c) +set(INTERPRET_TEST_SOURCES src/debug.c src/hash_map.c src/memory.c src/seconds.c src/stack.c + src/types.c src/vm/instructions.c src/vm/opcodes.c src/vm/vm.c src/tests/interpret_test.c) set(OBJECT_TEST_SOURCES src/debug.c src/memory.c src/seconds.c src/types.c src/tests/object_test.c) -set(STACK_TEST_SOURCES src/debug.c src/hash_map.c src/memory.c src/seconds.c src/types.c - src/vm/instructions.c src/vm/stack.c src/tests/stack_test.c) +set(STACK_TEST_SOURCES src/debug.c src/hash_map.c src/memory.c src/seconds.c src/stack.c + src/types.c src/vm/instructions.c src/tests/stack_test.c) set(UNIT_TESTS_SOURCES src/debug.c src/memory.c src/seconds.c src/types.c src/tests/unit_tests.c) if(MSVC) diff --git a/include/memory.h b/include/memory.h index a1ce569..2c1e8d9 100644 --- a/include/memory.h +++ b/include/memory.h @@ -29,7 +29,7 @@ #define MEMORY_H #include "types.h" -#include "vm/stack.h" +#include "stack.h" // Initial free store capacity static const size_t INITIAL_STORE_CAPACITY = 128; diff --git a/include/vm/stack.h b/include/stack.h similarity index 89% rename from include/vm/stack.h rename to include/stack.h index d395ae1..62657db 100644 --- a/include/vm/stack.h +++ b/include/stack.h @@ -31,25 +31,25 @@ #include // NULL #include "types.h" -#define MAX_STACK_CAPACITY ((size_t)64) +#define MAX_STACK_CAPACITY ((size_t)128) -typedef struct vm_stack +typedef struct stack { - int top; + int16_t top; size_t count; - Object* objects[MAX_STACK_CAPACITY]; + void* objects[MAX_STACK_CAPACITY]; } Stack; // Initializes stack void init_stack(Stack* stack); // Returns object at top of stack without removal -Object* peek(const Stack* stack); +void* peek(const Stack* stack); // Returns and removes object at top of stack -Object* pop(Stack* stack); +void* pop(Stack* stack); // Pushes new object on top of stack -void push(Stack* stack, Object* object); +void push(Stack* stack, void* object); #endif // STACK_H diff --git a/include/vm/instructions.h b/include/vm/instructions.h index 6a08aa8..f70a6aa 100644 --- a/include/vm/instructions.h +++ b/include/vm/instructions.h @@ -29,7 +29,7 @@ #define INSTRUCTIONS_H #include "hash_map.h" -#include "vm/stack.h" +#include "stack.h" #include "vm/vm.h" #define OP_NOOP (void)0 diff --git a/include/vm/vm.h b/include/vm/vm.h index b15d601..a9c10fd 100644 --- a/include/vm/vm.h +++ b/include/vm/vm.h @@ -28,9 +28,9 @@ #ifndef VM_H #define VM_H +#include "stack.h" #include "types.h" // BigNum, Byte #include "vm/opcodes.h" // Opcode -#include "vm/stack.h" // Stack #define REGISTER_AMOUNT ((uint8_t)17) static const uint8_t REGISTER_EMPTY = 127; diff --git a/src/compiler.c b/src/compiler.c index 8abf930..3a62485 100644 --- a/src/compiler.c +++ b/src/compiler.c @@ -124,7 +124,10 @@ void compile(ConcoctNodeTree* tree, ConcoctHashMap* map) ic++; break; case CCT_TOKEN_ADD_ASSIGN: - // OP_ADD + OP_ASN + vm.instructions[ic] = OP_ADD; + ic++; + vm.instructions[ic] = OP_ASN; + ic++; break; case CCT_TOKEN_AND: vm.instructions[ic] = OP_AND; diff --git a/src/vm/stack.c b/src/stack.c similarity index 93% rename from src/vm/stack.c rename to src/stack.c index e2837ba..1921fad 100644 --- a/src/vm/stack.c +++ b/src/stack.c @@ -25,10 +25,10 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include // fprintf(), stderr -#include // EXIT_FAILURE +#include // fprintf(), stderr +#include // EXIT_FAILURE #include "debug.h" -#include "vm/stack.h" +#include "stack.h" void init_stack(Stack* stack) { @@ -40,7 +40,7 @@ void init_stack(Stack* stack) } // Returns object at top of stack without removal -Object* peek(const Stack* stack) +void* peek(const Stack* stack) { if(stack->top == -1) { @@ -54,7 +54,7 @@ Object* peek(const Stack* stack) } // Returns and removes object at top of stack -Object* pop(Stack* stack) +void* pop(Stack* stack) { if(stack->top == -1) { @@ -68,7 +68,7 @@ Object* pop(Stack* stack) } // Pushes new object on top of stack -void push(Stack* stack, Object* object) +void push(Stack* stack, void* object) { if(stack->top >= ((int)MAX_STACK_CAPACITY - 1)) { diff --git a/src/tests/stack_test.c b/src/tests/stack_test.c index 7d56cca..7034e50 100644 --- a/src/tests/stack_test.c +++ b/src/tests/stack_test.c @@ -28,9 +28,9 @@ #include #include "debug.h" #include "memory.h" +#include "stack.h" #include "types.h" #include "vm/instructions.h" -#include "vm/stack.h" int main() { From 11785ea454a7cf578e81ef430dfb289240646c8c Mon Sep 17 00:00:00 2001 From: Lloyd Dilley Date: Sat, 9 Sep 2023 13:42:30 -0400 Subject: [PATCH 3/5] Fix intentional fallthrough warning --- src/parser.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/parser.c b/src/parser.c index 8fa6b8d..891be2c 100644 --- a/src/parser.c +++ b/src/parser.c @@ -219,7 +219,9 @@ ConcoctNode* cct_parse_unary_expr(ConcoctParser* parser) case CCT_TOKEN_SUB: // Change the token type so the compiler knows which operation it is parser->current_token.type = CCT_TOKEN_UNARY_MINUS; + goto TOK_ADD_LBL; // portable way to handle intentional fallthrough and silence -Wimplicit-fallthrough case CCT_TOKEN_ADD: + TOK_ADD_LBL: case CCT_TOKEN_NOT: case CCT_TOKEN_INC: case CCT_TOKEN_DEC: From c623754b9f80b117539969f6e2e58db102be33fa Mon Sep 17 00:00:00 2001 From: Lloyd Dilley Date: Sat, 9 Sep 2023 13:52:36 -0400 Subject: [PATCH 4/5] Add compiler support for unary minus/negative --- src/compiler.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/compiler.c b/src/compiler.c index 3a62485..f9915cb 100644 --- a/src/compiler.c +++ b/src/compiler.c @@ -276,6 +276,10 @@ void compile(ConcoctNodeTree* tree, ConcoctHashMap* map) vm.instructions[ic] = OP_TRU; ic++; break; + case CCT_TOKEN_UNARY_MINUS: + vm.instructions[ic] = OP_NEG; + ic++; + break; default: fprintf(stderr, "Unable to handle token: %s\n", cct_token_type_to_string(current->token.type)); break; From 8ebfbeba47833b283f7874f5d8467fe2085159a9 Mon Sep 17 00:00:00 2001 From: Lloyd Dilley Date: Sat, 9 Sep 2023 14:17:40 -0400 Subject: [PATCH 5/5] Adjust style and add const in cct_next_token() --- src/lexer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lexer.c b/src/lexer.c index 9a7a0c6..932ba54 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -208,7 +208,7 @@ ConcoctToken cct_next_token(ConcoctLexer* lexer) if(cct_hash_map_has_key(lexer->keyword_map, lexer->token_text)) { void* void_pointer = cct_hash_map_get(lexer->keyword_map, lexer->token_text); - int* pointer_to_type = (int*) (&void_pointer); + const int* pointer_to_type = (int *)(&void_pointer); type = *pointer_to_type; } else