Added preprocessor
This commit is contained in:
parent
7f753d0954
commit
21198a8282
5 changed files with 67 additions and 6 deletions
6
include/tvm/tvm_preprocessor.h
Normal file
6
include/tvm/tvm_preprocessor.h
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
#ifndef TVM_PREPROCESSOR_H_
|
||||
#define TVM_PREPROCESSOR_H_
|
||||
|
||||
int tvm_preprocess(char *src, int *src_len);
|
||||
|
||||
#endif
|
||||
|
|
@ -32,7 +32,4 @@ int program_interpret(tvm_program_t *p, char *filename, tvm_memory_t *pMemory);
|
|||
|
||||
void program_destroy(tvm_program_t *p);
|
||||
|
||||
//int *tvm_add_value(tvm_program_t *p, const int val);
|
||||
//int tvm_parse_value(const char *str);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
#include <tvm/tvm_parser.h>
|
||||
#include <tvm/tvm_file.h>
|
||||
#include <tvm/tvm_lexer.h>
|
||||
|
||||
const char *tvm_opcode_map[] = {"nop", "int", "mov", "push", "pop", "pushf", "popf", "inc", "dec", "add", "sub", "mul", "div", "mod", "rem",
|
||||
|
|
|
|||
54
libtvm/tvm_preprocesor.c
Normal file
54
libtvm/tvm_preprocesor.c
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
#include <tvm/tvm_preprocessor.h>
|
||||
#include <tvm/tvm_file.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
int tvm_preprocess(char *src, int *src_len)
|
||||
{
|
||||
char* pp_directive_delimiter = NULL;
|
||||
if((pp_directive_delimiter = strstr(src, "%include")))
|
||||
{
|
||||
char *strbegin = pp_directive_delimiter, *strend = strchr(strbegin, '\n');
|
||||
|
||||
if(!strend || !strbegin) goto pp_ret;
|
||||
|
||||
int linelen = strend - strbegin;
|
||||
char* temp_str = calloc(linelen + 1, sizeof(char));
|
||||
memcpy(temp_str, strbegin, linelen);
|
||||
|
||||
char *filename = (strchr(temp_str, ' ') + 1);
|
||||
|
||||
FILE* pFile = tvm_fopen(filename, ".vm", "r");
|
||||
if(!pFile)
|
||||
{
|
||||
printf("Unable to open file \"%s\"\n", filename);
|
||||
goto pp_ret;
|
||||
}
|
||||
|
||||
free(temp_str);
|
||||
|
||||
size_t addition_len = tvm_flength(pFile);
|
||||
char *addition_str = calloc(addition_len, sizeof(char));
|
||||
tvm_fcopy(addition_str, addition_len, pFile);
|
||||
fclose(pFile);
|
||||
|
||||
size_t first_block_len = (strbegin - src);
|
||||
size_t second_block_len = ((src + *src_len) - strend);
|
||||
size_t new_src_len = (first_block_len + addition_len + second_block_len);
|
||||
|
||||
src = (char *)realloc((char *)src, sizeof(char) * new_src_len);
|
||||
src[new_src_len] = 0;
|
||||
|
||||
memmove(&src[first_block_len + addition_len], strend, second_block_len);
|
||||
memcpy(&src[first_block_len], addition_str, addition_len);
|
||||
|
||||
// Fuckin' hack
|
||||
for(int i = 0; i < new_src_len; i++) if(src[i] == 0) src[i] = ' ';
|
||||
|
||||
*src_len = strlen(src);
|
||||
return 1;
|
||||
}
|
||||
|
||||
pp_ret: return 0;
|
||||
|
||||
}
|
||||
|
|
@ -1,7 +1,8 @@
|
|||
#include <tvm/tvm_file.h>
|
||||
#include <tvm/tvm_program.h>
|
||||
#include <tvm/tvm_preprocessor.h>
|
||||
#include <tvm/tvm_lexer.h>
|
||||
#include "tvm/tvm_parser.h"
|
||||
#include <tvm/tvm_parser.h>
|
||||
|
||||
tvm_program_t *program_create()
|
||||
{
|
||||
|
|
@ -41,18 +42,20 @@ int program_interpret(tvm_program_t *p, char *filename, tvm_memory_t *pMemory)
|
|||
for(int i = 0; i < 2; i++)
|
||||
if(!pFile) pFile = tvm_fopen(filename, ".vm", "r");
|
||||
|
||||
if(pFile) goto pi_lex;
|
||||
if(pFile) goto pi_interpret;
|
||||
|
||||
printf("File was not found, or does not exist. Unable to interpret.\n");
|
||||
return 1;
|
||||
|
||||
pi_lex:
|
||||
pi_interpret:
|
||||
source_length = tvm_flength(pFile);
|
||||
char *source = calloc(source_length, sizeof(char));
|
||||
|
||||
tvm_fcopy(source, source_length, pFile);
|
||||
fclose(pFile);
|
||||
|
||||
while(tvm_preprocess(source, &source_length));
|
||||
|
||||
tvm_lexer_t *lexer_ctx = lexer_create();
|
||||
lex(lexer_ctx, source);
|
||||
free(source);
|
||||
|
|
|
|||
Loading…
Reference in a new issue