Substitute defines with their strings.
Before allocating space for a token, the lexer will first check to see if that token is a defined name. If it is, it will allocate space for the defined string instead. A test file is included in programs/tinyvm/preprocessor to demonstrate the behavior. When functioning, the program will print the fibonacci sequence.
This commit is contained in:
parent
23e72a8ca7
commit
c80d6974cb
4 changed files with 31 additions and 5 deletions
|
|
@ -4,6 +4,8 @@
|
|||
#define MAX_ARGS 2
|
||||
#define MAX_TOKENS 4
|
||||
|
||||
#include "tvm_tree.h"
|
||||
|
||||
typedef struct tvm_lexer_s
|
||||
{
|
||||
char **source_lines;
|
||||
|
|
@ -14,6 +16,6 @@ tvm_lexer_t *lexer_create();
|
|||
void lexer_destroy(tvm_lexer_t *l);
|
||||
|
||||
/* Tokenize the character array "source" into lines and tokens */
|
||||
void lex(tvm_lexer_t *lexer, char *source);
|
||||
void lex(tvm_lexer_t *lexer, char *source, tvm_tree_t **node);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ void lexer_destroy(tvm_lexer_t *lexer)
|
|||
free(lexer);
|
||||
}
|
||||
|
||||
void lex(tvm_lexer_t *lexer, char *source)
|
||||
void lex(tvm_lexer_t *lexer, char *source, tvm_tree_t **node)
|
||||
{
|
||||
int i, j;
|
||||
char *pToken, *pLine = strtok(source, "\n");
|
||||
|
|
@ -60,8 +60,12 @@ void lex(tvm_lexer_t *lexer, char *source)
|
|||
|
||||
for(j = 0; (pToken && j < MAX_TOKENS); j++)
|
||||
{
|
||||
lexer->tokens[i][j] = (char *)calloc(1, (strlen(pToken) + 1));
|
||||
strcpy(lexer->tokens[i][j], pToken);
|
||||
/* Check if this token is a define. */
|
||||
char *token = (char *)tvm_tree_find(*node, pToken);
|
||||
if(!token) token = pToken;
|
||||
|
||||
lexer->tokens[i][j] = (char *)calloc(1, (strlen(token) + 1));
|
||||
strcpy(lexer->tokens[i][j], token);
|
||||
|
||||
pToken = strtok(NULL, " \t,");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ pi_interpret:
|
|||
while(tvm_preprocess(source, &source_length, &p->defines));
|
||||
|
||||
tvm_lexer_t *lexer_ctx = lexer_create();
|
||||
lex(lexer_ctx, source);
|
||||
lex(lexer_ctx, source, &p->defines);
|
||||
free(source);
|
||||
|
||||
if(parse_labels(p, (const char ***)lexer_ctx->tokens) != 0) return 1;
|
||||
|
|
|
|||
20
programs/tinyvm/preprocessor/define.vm
Normal file
20
programs/tinyvm/preprocessor/define.vm
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
%define ONE 1
|
||||
%define ZERO 0
|
||||
|
||||
start:
|
||||
mov eax, ONE
|
||||
mov ebx, ZERO
|
||||
|
||||
loop: add eax, ebx
|
||||
add ebx, eax
|
||||
|
||||
prn eax
|
||||
prn ebx
|
||||
|
||||
cmp eax, ZERO
|
||||
jl end
|
||||
|
||||
cmp ebx, ZERO
|
||||
jg loop
|
||||
|
||||
end:
|
||||
Loading…
Reference in a new issue