Skip to content

Commit

Permalink
Merge pull request #73 from ldilley/master
Browse files Browse the repository at this point in the history
Decouple queue, label functions with void parameters, remove FLS and TRU opcodes
  • Loading branch information
ldilley authored Oct 5, 2023
2 parents 33b93cf + 19978bc commit 120dc7a
Show file tree
Hide file tree
Showing 20 changed files with 229 additions and 196 deletions.
36 changes: 1 addition & 35 deletions include/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,45 +28,11 @@
#ifndef COMPILER_H
#define COMPILER_H

#include <stdbool.h> // bool
#include "hash_map.h"
#include "parser.h"
#include "queue.h"
#include "vm/opcodes.h"

#define MAX_QUEUE_CAPACITY ((size_t)256)

typedef struct node_queue
{
int front;
int back;
size_t count;
ConcoctNode* nodes[MAX_QUEUE_CAPACITY];
} Queue;

// Initializes queue
void init_queue(Queue* queue);

// Is queue empty?
bool is_empty(const Queue* queue);

// Is queue full?
bool is_full(const Queue* queue);

// Returns size of queue
size_t size(const Queue* queue);

// Returns node at the back of queue
ConcoctNode* back(const Queue* queue);

// Returns node at the front of queue
ConcoctNode* front(const Queue* queue);

// Returns and removes next node from queue
void dequeue(Queue* queue, ConcoctNode** node);

// Inserts new node into queue
void enqueue(Queue* queue, ConcoctNode* node);

// Swap last 2 stack objects to fix order if first instruction is a binary operation
void swap_last_operands(Opcode oc);

Expand Down
8 changes: 4 additions & 4 deletions include/concoct.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ void parse_file(const char* file_name);
void parse_string(const char* input_string);
void handle_options(int argc, char *argv[]);
bool compare_input(const char* input, const char* command);
void print_license();
void print_usage();
void print_version();
void print_license(void);
void print_usage(void);
void print_version(void);
void handle_sigint(int sig);
void interactive_mode();
void interactive_mode(void);

#endif // CONCOCT_H
20 changes: 10 additions & 10 deletions include/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,37 +56,37 @@ typedef struct objstore
extern ObjectStore object_store;

// Initializes object store
void init_store();
void init_store(void);

// Reallocates memory for object store
void realloc_store(size_t new_size);

// Frees object store
void free_store();
void free_store(void);

// Returns size of object store
static inline size_t get_store_capacity() { return object_store.capacity; }
static inline size_t get_store_capacity(void) { return object_store.capacity; }

// Returns free slots of object store
size_t get_store_free_slots();
size_t get_store_free_slots(void);

// Returns used slots of object store
size_t get_store_used_slots();
size_t get_store_used_slots(void);

// Returns size of object in bytes
size_t get_object_size(const Object* object);

// Returns total size of objects in object store in bytes
size_t get_store_objects_size();
size_t get_store_objects_size(void);

// Returns total size of object store in bytes
static inline size_t get_store_total_size() { return get_store_objects_size() + sizeof(ObjectStore) + sizeof(Object *) * get_store_capacity(); }
static inline size_t get_store_total_size(void) { return get_store_objects_size() + sizeof(ObjectStore) + sizeof(Object *) * get_store_capacity(); }

// Prints total size of objects in object store
void print_store_objects_size();
void print_store_objects_size(void);

// Prints total size of object store
void print_store_total_size();
void print_store_total_size(void);

// Converts bytes to kilobytes
size_t convert_kilobytes(size_t bytes);
Expand Down Expand Up @@ -134,6 +134,6 @@ void stringify(char** str, void* data, DataType datatype);
size_t flag_objects(Stack* stack);

// Collects garbage and returns number of objects collected
size_t collect_garbage();
size_t collect_garbage(void);

#endif // MEMORY_H
68 changes: 68 additions & 0 deletions include/queue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Concoct - An imperative, dynamically-typed, interpreted, general-purpose programming language
* Copyright (c) 2020-2023 BlakeTheBlock and Lloyd Dilley
* http://concoct.ist/
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef QUEUE_H
#define QUEUE_H

#include <stdbool.h> // bool
#include <stddef.h> // size_t

#define MAX_QUEUE_CAPACITY ((size_t)256)

typedef struct queue
{
int front;
int back;
size_t count;
void* objects[MAX_QUEUE_CAPACITY];
} Queue;

// Initializes queue
void init_queue(Queue* queue);

// Returns true if queue is empty
bool is_empty(const Queue* queue);

// Returns true if queue is full
bool is_full(const Queue* queue);

// Returns size of queue
size_t size(const Queue* queue);

// Returns object at the back of queue
void* back(const Queue* queue);

// Returns object at the front of queue
void* front(const Queue* queue);

// Returns and removes next object from queue
void dequeue(Queue* queue, void** object);

// Inserts new object into queue
void enqueue(Queue* queue, void* object);

#endif // QUEUE_H
2 changes: 1 addition & 1 deletion include/stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#ifndef STACK_H
#define STACK_H

#include <stddef.h> // NULL
#include <stddef.h> // NULL, size_t
#include "types.h"

#define MAX_STACK_CAPACITY ((size_t)128)
Expand Down
2 changes: 0 additions & 2 deletions include/vm/instructions.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ RunCode op_xcg(Object** rp, Byte reg1, Byte reg2);
RunCode op_pop(Stack* stack, const Object* object);
RunCode op_psh(Stack* stack, char* value);
RunCode op_asn(Stack* stack, ConcoctHashMap* map);
RunCode op_fls(Stack* stack);
RunCode op_tru(Stack* stack);
RunCode op_and(Stack* stack);
RunCode op_not(Stack* stack);
RunCode op_or(Stack* stack);
Expand Down
2 changes: 0 additions & 2 deletions include/vm/opcodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ typedef enum opcode
OP_ENT, // entry point
OP_EQL, // equal to (==)
OP_EXT, // exit
OP_FLS, // false
OP_GT, // greater than (>)
OP_GTE, // greater than or equal to (>=)
OP_HLT, // halt
Expand Down Expand Up @@ -86,7 +85,6 @@ typedef enum opcode
OP_STR, // store (to memory from register)
OP_SUB, // subtract (-)
OP_SYS, // system
OP_TRU, // true
OP_TST, // test
OP_XCG, // exchange/swap
OP_XOR // bitwise exclusive or (^)
Expand Down
8 changes: 4 additions & 4 deletions include/vm/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,19 @@ typedef enum
} RunCode;

// Clears instructions
void clear_instructions();
void clear_instructions(void);

// Reverses instructions since they should be in a LIFO arrangement
void reverse_instructions(size_t ic);

// Prints register values
void print_registers();
void print_registers(void);

// Initializes virtual machine
void init_vm();
void init_vm(void);

// Stops virtual machine
void stop_vm();
void stop_vm(void);

// Interprets code
RunCode interpret(ConcoctHashMap* map);
Expand Down
76 changes: 4 additions & 72 deletions src/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,75 +29,9 @@
#include "compiler.h"
#include "debug.h" // debug_mode, debug_print()
#include "memory.h" // new_object(), new_object_by_type()
#include "queue.h"
#include "vm/vm.h" // interpret(), reverse_instructions()

// Initializes queue
void init_queue(Queue* queue)
{
queue->count = 0;
queue->back = MAX_QUEUE_CAPACITY - 1;
queue->front = 0;
if(debug_mode)
debug_print("Queue initialized with %zu slots.", MAX_QUEUE_CAPACITY);
return;
}

// Is queue empty?
bool is_empty(const Queue* queue)
{
return queue->count == 0;
}

// Is queue full?
bool is_full(const Queue* queue)
{
return queue->count == MAX_QUEUE_CAPACITY;
}

// Returns size of queue
size_t size(const Queue* queue)
{
return queue->count;
}

// Returns node at the back of queue
ConcoctNode* back(const Queue* queue)
{
if(is_empty(queue))
return NULL;
return queue->nodes[queue->back];
}

// Returns node at the front of queue
ConcoctNode* front(const Queue* queue)
{
if(is_empty(queue))
return NULL;
return queue->nodes[queue->front];
}

// Returns and removes next node from queue
void dequeue(Queue* queue, ConcoctNode** node)
{
if(is_empty(queue))
return;
*node = queue->nodes[queue->front];
queue->front = (queue->front + 1) % MAX_QUEUE_CAPACITY;
queue->count--;
return;
}

// Inserts new node into queue
void enqueue(Queue* queue, ConcoctNode* node)
{
if(is_full(queue))
return;
queue->back = (queue->back + 1) % MAX_QUEUE_CAPACITY;
queue->nodes[queue->back] = node;
queue->count++;
return;
}

// Swap last 2 stack objects to fix order if first instruction is a binary operation
void swap_last_operands(Opcode oc)
{
Expand Down Expand Up @@ -131,7 +65,7 @@ void compile(ConcoctNodeTree* tree, ConcoctHashMap* map)
while(!is_empty(pqueue))
{
ConcoctNode* current = NULL;
dequeue(pqueue, &current);
dequeue(pqueue, (void **)&current);
// ToDo: Add remaining cases
switch(current->token.type)
{
Expand Down Expand Up @@ -191,8 +125,7 @@ void compile(ConcoctNodeTree* tree, ConcoctHashMap* map)
// OP_POW + OP_ASN
break;
case CCT_TOKEN_FALSE:
vm.instructions[ic] = OP_FLS;
ic++;
push(vm.sp, new_object("false"));
break;
case CCT_TOKEN_FLOAT:
push(vm.sp, new_object_by_type(current->text, CCT_TYPE_DECIMAL));
Expand Down Expand Up @@ -289,8 +222,7 @@ void compile(ConcoctNodeTree* tree, ConcoctHashMap* map)
// OP_SUB + OP_ASN
break;
case CCT_TOKEN_TRUE:
vm.instructions[ic] = OP_TRU;
ic++;
push(vm.sp, new_object("true"));
break;
case CCT_TOKEN_UNARY_MINUS:
vm.instructions[ic] = OP_NEG;
Expand Down
8 changes: 4 additions & 4 deletions src/concoct.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ void handle_options(int argc, char *argv[])
}

// Displays license
void print_license()
void print_license(void)
{
puts("BSD 2-Clause License\n");
puts("Copyright (c) 2020-2023 BlakeTheBlock and Lloyd Dilley");
Expand All @@ -318,7 +318,7 @@ void print_license()
}

// Displays usage
void print_usage()
void print_usage(void)
{
print_version();
printf("Usage: concoct [%c<option>] [file]\n", ARG_PREFIX);
Expand All @@ -331,7 +331,7 @@ void print_usage()
}

// Displays version
void print_version()
void print_version(void)
{
if(strlen(GIT_REV) == 0) // git not detected in path
printf("Concoct v%s (%s %s) (%s) built at %s on %s\n", VERSION, BITNESS, PLATFORM, BUILD_TYPE, BUILD_TIME, BUILD_DATE);
Expand Down Expand Up @@ -367,7 +367,7 @@ void handle_sigint(int sig)
}

// Interactive mode
void interactive_mode()
void interactive_mode(void)
{
signal(SIGINT, handle_sigint);
char input[1024];
Expand Down
Loading

0 comments on commit 120dc7a

Please sign in to comment.