First alpha version. Added support for putting\getting from TOS int and double

This commit is contained in:
Alex Vanin 2014-01-14 22:13:31 +04:00
parent 8d09513639
commit 9d30a3f8f5
5 changed files with 109 additions and 0 deletions

12
VaninVM/IOcode.c Normal file
View file

@ -0,0 +1,12 @@
#include "IOcode.h"
char* read_bytecode(char* path)
{
FILE* input;
char* buffer = (char*)calloc(1, 1000); //Early version without bytecode file
fopen_s(&input, "bytecode", "rb");
fread_s(buffer, 1000, sizeof(char), 1000, input); //Early version without bytecode file
fclose(input);
return buffer;
}

8
VaninVM/IOcode.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef IOCODE_H
#define IOCODE_H
#include <malloc.h>
#include <stdio.h>
char* read_bytecode(char*);
#endif

36
VaninVM/Main.c Normal file
View file

@ -0,0 +1,36 @@
#include "IOcode.h"
#include "TOS.h"
int main(int argc, char** argv)
{
char* code;
int ip;
int exec_status = 1;
code = read_bytecode("bytecode");
initTOS();
ip = 0;
while (exec_status)
{
switch (code[ip])
{
case 1:
push_double(12.1);
ip++;
break;
case 78:
exec_status = 0;
break;
}
}
printf("%f", pop_double());
getchar();
return 0;
}

35
VaninVM/TOS.c Normal file
View file

@ -0,0 +1,35 @@
#include <malloc.h>
#include "TOS.h"
void push_int(int num)
{
TOS[tp++] = num;
}
int pop_int()
{
int num = TOS[--tp];
return num;
}
void push_double(double num)
{
tos_num cont = { num };
push_int(cont.num_i[0]);
push_int(cont.num_i[1]);
}
double pop_double()
{
tos_num cont;
cont.num_i[1] = pop_int();
cont.num_i[0] = pop_int();
return cont.num_d;
}
int initTOS()
{
TOS = (int*)calloc(1, 1500);
tp = 0;
return 0;
}

18
VaninVM/TOS.h Normal file
View file

@ -0,0 +1,18 @@
#ifndef TOS_H
#define TOS_H
typedef union
{
double num_d;
int num_i[2];
} tos_num;
int* TOS;
int tp;
int initTOS();
void push_int(int);
void push_double(double);
int pop_int();
double pop_double();
#endif