Skip to content

Commit

Permalink
Renamed tokens and functions
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandMarchand committed Jul 20, 2022
1 parent 58bef2d commit e616cda
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/scanner/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ struct scan *scan_init(const char *filename)
return s;
}

void scan_del(struct scan *s)
void scan_free(struct scan *s)
{
source_close(s->source);
token_vector_del(s->tokens);
Expand Down Expand Up @@ -129,7 +129,7 @@ static struct token get_token()
case '.': advance(); return GET_TOKEN(TOKEN_DOT);
case '+': advance(); return GET_TOKEN(TOKEN_PLUS);
case '*': advance(); return GET_TOKEN(TOKEN_STAR);
case '%': advance(); return GET_TOKEN(TOKEN_MODULO);
case '%': advance(); return GET_TOKEN(TOKEN_PERCENT);
case '/': advance(); return GET_TOKEN(TOKEN_SLASH);
case '\t': advance(); return GET_TOKEN(TOKEN_TAB);
case '\0': return GET_TOKEN(TOKEN_END_OF_FILE);
Expand Down
2 changes: 1 addition & 1 deletion src/scanner/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ enum token_type{
/* single-character tokens */
TOKEN_ARROW = 0, TOKEN_COMMA, TOKEN_DOT, TOKEN_LEFT_BRACE,
TOKEN_LEFT_PAREN, TOKEN_LEFT_SQUARE, TOKEN_MINUS,
TOKEN_MODULO, TOKEN_NEWLINE, TOKEN_PLUS, TOKEN_RIGHT_BRACE,
TOKEN_PERCENT, TOKEN_NEWLINE, TOKEN_PLUS, TOKEN_RIGHT_BRACE,
TOKEN_RIGHT_PAREN, TOKEN_RIGHT_SQUARE, TOKEN_SEMICOLON,
TOKEN_SLASH, TOKEN_STAR, TOKEN_TAB,

Expand Down
11 changes: 9 additions & 2 deletions src/vm/constant_vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,19 @@ void constant_vector_free(struct constant_vector *ca)
ca = NULL;
}


int constant_vector_add(struct constant_vector *ca, double d)
{
if (ca->count == (ca->size / sizeof(double)))
/* No duplicates */
for (int i = 0; i < ca->count; i++) {
if (ca->array[i] == d) return i;
}

if (ca->count == (ca->size / sizeof(double))) {
constant_vector_grow(ca);
}

ca->array[ca->count] = d;

return ca->count++;
}

Expand Down
9 changes: 8 additions & 1 deletion src/vm/lump.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,14 @@ int lump_add_constant(struct lump *lmp, double d)
{
int offset = constant_vector_add(lmp->constants, d);

if (offset < 1 << 8)
/* OP_CONSTANT_LONG holds two bytes for the constant's offset
* above two bytes, the constants are dropped */
if (offset >= 0xFFFF) {
fprintf(stderr, "Max constant count reached. Dropping %f.\n", d);
return offset;
}

if (offset < 0x100)
lump_add_code_monadic(lmp, OP_CONSTANT, offset);
else
lump_add_code_dyladic(lmp, OP_CONSTANT_LONG, offset);
Expand Down
19 changes: 19 additions & 0 deletions src/vm/opcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,24 @@ enum op_code {
OP_LINE_INC,
OP_CONSTANT,
OP_CONSTANT_LONG,

OP_NOT_EQUAL,
OP_EQUAL,

OP_GREATER,
OP_GREATER_EQUAL,
OP_LESS,
OP_LESS_EQUAL,

OP_ADD,
OP_SUBSTRACT,

OP_MULTIPLY,
OP_MODULO,
OP_DIVIDE,

OP_LOGICAL_NOT,
OP_NEGATE,

OP_END_PROGRAM
};
22 changes: 17 additions & 5 deletions src/vm/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/

#include "vm.h"
#include "src/compiler/compiler.h"
#include "debug/debug.h"

#include <stdio.h>
Expand All @@ -30,15 +31,15 @@ static struct vm vm;

static enum interpret_result run();

enum interpret_result interpret(struct source *src) {
enum interpret_result interpret(char *source) {
struct lump *lmp = lump_init();
vm.lump = lmp;

if (!compile(src, lmp)) {
if (!compile(source)) {
lump_free(lmp);
return INTERPRET_COMPILE_ERROR;
}

vm.lump = lmp;
vm.pc = lmp->array;
vm.stack_top = vm.stack;

Expand Down Expand Up @@ -76,7 +77,7 @@ static enum interpret_result run()
#undef READ_BYTE
}

void vm_push(struct value v)
void vm_push_value(struct value v)
{
if (vm.stack_top >= vm.stack + VM_STACK_SIZE)
return;
Expand All @@ -85,11 +86,22 @@ void vm_push(struct value v)
vm.stack_top++;
}

struct value *vm_pop()
struct value *vm_pop_value()
{
if (vm.stack_top <= vm.stack)
return NULL;

vm.stack_top--;
return vm.stack_top + 1;
}

int vm_add_constant(double value)
{
return constant_vector_add(vm.lump->constants, value);
}

void vm_add_code(enum op_code code)
{
// printf("%d added.\n", code);
lump_add_code(vm.lump, code);
}
12 changes: 9 additions & 3 deletions src/vm/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ enum interpret_result {
INTERPRET_RUNTIME_ERROR
};

enum interpret_result interpret(struct source *src);
void vm_push(struct value v);
struct value *vm_pop();
enum interpret_result interpret(char *source);

void vm_push_value(struct value val);
struct value *vm_pop_value();

/* return the constant's index */
int vm_add_constant(double value);

void vm_add_code(enum op_code code);

0 comments on commit e616cda

Please sign in to comment.