Skip to content

Commit

Permalink
Began working on the value and stack systems
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandMarchand committed Jul 17, 2022
1 parent d816924 commit e6918cd
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
40 changes: 40 additions & 0 deletions src/vm/value.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2022, Roland Marchand <roland.marchand@protonmail.com>
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*/

#pragma once

#include <stdint.h>

enum value_type {
VALUE_NUMBER = 0,
VALUE_BOOL,
VALUE_NIL
};

struct value {
union {
double number;
uint8_t bool;
} as;
enum value_type type;
};
20 changes: 19 additions & 1 deletion src/vm/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ enum interpret_result interpret(struct source *src) {

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

enum interpret_result result = run();

Expand Down Expand Up @@ -72,6 +73,23 @@ static enum interpret_result run()
break;
}
}

#undef READ_BYTE
}

void vm_push(struct value v)
{
if (vm.stack_top >= vm.stack + VM_STACK_SIZE)
return;

*vm.stack_top = v;
vm.stack_top++;
}

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

vm.stack_top--;
return vm.stack_top + 1;
}
9 changes: 7 additions & 2 deletions src/vm/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@

#include "opcode.h"
#include "lump.h"
#include "value.h"
#include "src/scanner/scanner.h"

#include <stdint.h>

#define VM_STACK_SIZE 256

struct vm {
struct lump *lump;
struct value stack[VM_STACK_SIZE];
struct value *stack_top;
uint8_t *pc;
};

Expand All @@ -40,6 +45,6 @@ enum interpret_result {
INTERPRET_RUNTIME_ERROR
};

/* void vm_init(); */
/* void vm_free(); */
enum interpret_result interpret(struct source *src);
void vm_push(struct value v);
struct value *vm_pop();

0 comments on commit e6918cd

Please sign in to comment.