Skip to content

Commit

Permalink
Merge pull request #68 from ldilley/master
Browse files Browse the repository at this point in the history
Stack decouple and compiler unary minus support
  • Loading branch information
ldilley authored Sep 9, 2023
2 parents bf623e4 + 8ebfbeb commit c4c3554
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 24 deletions.
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion include/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 7 additions & 7 deletions include/vm/stack.h → include/stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,25 @@
#include <stddef.h> // 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
2 changes: 1 addition & 1 deletion include/vm/instructions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion include/vm/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion src/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions src/vm/stack.c → src/stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdio.h> // fprintf(), stderr
#include <stdlib.h> // EXIT_FAILURE
#include <stdio.h> // fprintf(), stderr
#include <stdlib.h> // EXIT_FAILURE
#include "debug.h"
#include "vm/stack.h"
#include "stack.h"

void init_stack(Stack* stack)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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))
{
Expand Down
2 changes: 1 addition & 1 deletion src/tests/stack_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
#include <stdio.h>
#include "debug.h"
#include "memory.h"
#include "stack.h"
#include "types.h"
#include "vm/instructions.h"
#include "vm/stack.h"

int main()
{
Expand Down
2 changes: 1 addition & 1 deletion src/vm/instructions.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c4c3554

Please sign in to comment.