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..f9915cb 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; @@ -273,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; 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 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: 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() { 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