.
This commit is contained in:
@@ -40,14 +40,14 @@ void code_destroy(Code* code)
|
||||
free(code);
|
||||
}
|
||||
|
||||
TYC_RESULT code_load_bytecode(Code* code, uint8_t* bytecode, size_t bytecode_sz)
|
||||
TYC_RESULT code_load_bytecode(Code* code, uint8_t const* bytecode, size_t bytecode_sz)
|
||||
{
|
||||
// TODO - linking
|
||||
|
||||
if (bytecode_sz < 24)
|
||||
return T_ERR_BYTECODE_TOO_SMALL;
|
||||
|
||||
uint32_t magic = *(uint32_t*) &bytecode[0];
|
||||
uint32_t magic = *(uint32_t const*) &bytecode[0];
|
||||
if (magic != MAGIC)
|
||||
return T_ERR_BYTECODE_INVALID_MAGIC;
|
||||
|
||||
|
||||
@@ -214,7 +214,7 @@ TYC_RESULT code_assemble(const char* code, uint8_t** bytecode, size_t* bytec
|
||||
Code* code_new(void);
|
||||
void code_destroy(Code* code);
|
||||
|
||||
TYC_RESULT code_load_bytecode(Code* code, uint8_t* bytecode, size_t bytecode_sz);
|
||||
TYC_RESULT code_load_bytecode(Code* code, uint8_t const* bytecode, size_t bytecode_sz);
|
||||
|
||||
uint32_t code_n_consts(Code const* code);
|
||||
TYC_CONST_TYPE code_const_type(Code const* code, size_t n);
|
||||
|
||||
24
lib/tyche.h
24
lib/tyche.h
@@ -1,6 +1,9 @@
|
||||
#ifndef TYCHE_TYCHE_H
|
||||
#define TYCHE_TYCHE_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
typedef enum {
|
||||
TT_NIL, TT_INTEGER, TT_REAL, TT_STRING, TT_STRING_CONST, TT_ARRAY, TT_TABLE, TT_FUNCTION, TT_NATIVE_PTR,
|
||||
} TYC_TYPE;
|
||||
@@ -12,8 +15,29 @@ typedef enum {
|
||||
T_ERR_TABLE_KEY_NOT_FOUND = -20,
|
||||
T_ERR_ASSEMBLER_SYNTAX_ERROR = -30,
|
||||
T_ERR_BYTECODE_TOO_SMALL = -40, T_ERR_BYTECODE_INVALID_MAGIC = -41,
|
||||
T_ERR_TYPE_UNEXPECTED = -50,
|
||||
} TYC_RESULT;
|
||||
|
||||
typedef enum {
|
||||
TX_SUM,
|
||||
} TYC_EXPR;
|
||||
|
||||
#define T_REAL float
|
||||
|
||||
typedef struct TycheVM TycheVM;
|
||||
|
||||
// create/destroy VM
|
||||
TycheVM* tyc_new(void);
|
||||
void tyc_destroy(TycheVM* t);
|
||||
|
||||
// code loading and execution
|
||||
TYC_RESULT tyc_load_bytecode(TycheVM* T, uint8_t const* bytecode, size_t bytecode_sz);
|
||||
TYC_RESULT tyc_call(TycheVM* t, uint16_t n_pars);
|
||||
|
||||
// stack manipulation and query
|
||||
void tyc_pushinteger(TycheVM* T, int32_t value);
|
||||
TYC_RESULT tyc_type(TycheVM* T, int idx, TYC_TYPE* type);
|
||||
TYC_RESULT tyc_tointeger(TycheVM* T, int idx, int32_t* value);
|
||||
TYC_RESULT tyc_expr(TycheVM* T, TYC_EXPR expr);
|
||||
|
||||
#endif //TYCHE_TYCHE_H
|
||||
|
||||
73
lib/vm.c
73
lib/vm.c
@@ -1 +1,72 @@
|
||||
#include "priv.h"
|
||||
#include "priv.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
struct TycheVM {
|
||||
Stack* stack;
|
||||
Heap* heap;
|
||||
Code* code;
|
||||
};
|
||||
|
||||
TycheVM* tyc_new(void)
|
||||
{
|
||||
TycheVM* t = xcalloc(1, sizeof(TycheVM));
|
||||
t->stack = stack_new();
|
||||
t->heap = heap_new();
|
||||
t->code = code_new();
|
||||
return t;
|
||||
}
|
||||
|
||||
void tyc_destroy(TycheVM* t)
|
||||
{
|
||||
code_destroy(t->code);
|
||||
heap_destroy(t->heap);
|
||||
stack_destroy(t->stack);
|
||||
free(t);
|
||||
}
|
||||
|
||||
TYC_RESULT tyc_load_bytecode(TycheVM* T, uint8_t const* bytecode, size_t bytecode_sz)
|
||||
{
|
||||
return code_load_bytecode(T->code, bytecode, bytecode_sz);
|
||||
}
|
||||
|
||||
TYC_RESULT tyc_call(TycheVM* T, uint16_t n_pars)
|
||||
{
|
||||
abort(); // TODO
|
||||
}
|
||||
|
||||
void tyc_pushinteger(TycheVM* T, int32_t value)
|
||||
{
|
||||
stack_push(T->stack, create_value_integer(value));
|
||||
}
|
||||
|
||||
TYC_RESULT tyc_type(TycheVM* T, int idx, TYC_TYPE* type)
|
||||
{
|
||||
VALUE v;
|
||||
TYC_RESULT r = stack_at(T->stack, idx, &v);
|
||||
if (r == T_OK)
|
||||
*type = v.type;
|
||||
return r;
|
||||
}
|
||||
|
||||
TYC_RESULT tyc_tointeger(TycheVM* T, int idx, int32_t* value)
|
||||
{
|
||||
VALUE v;
|
||||
TYC_RESULT r = stack_at(T->stack, idx, &v);
|
||||
if (r == T_OK) {
|
||||
if (v.type != TT_INTEGER)
|
||||
return T_ERR_TYPE_UNEXPECTED;
|
||||
*value = value_integer(v);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
TYC_RESULT tyc_expr(TycheVM* T, TYC_EXPR expr)
|
||||
{
|
||||
// TODO
|
||||
VALUE v1, v2;
|
||||
stack_pop(T->stack, &v2);
|
||||
stack_pop(T->stack, &v1);
|
||||
stack_push(T->stack, create_value_integer(value_integer(v1) + value_integer(v2)));
|
||||
return T_OK;
|
||||
}
|
||||
Reference in New Issue
Block a user