This repository has been archived by the owner on Feb 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from KrzysztofSzewczyk/labels-c
Label preprocessor in C (yay!) All tests pass, therefore I'm merging it. In case of any issues with the new label preprocessor, I'll revert the merge.
- Loading branch information
Showing
4 changed files
with
92 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
|
||
%{ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
struct label_t { | ||
struct label_t * next; | ||
char * name; | ||
int id; | ||
}; | ||
|
||
struct label_t * main_node; | ||
|
||
struct label_t * alloc_node(void) { | ||
struct label_t * x = malloc(sizeof(struct label_t)); | ||
x->next = x->name = NULL; | ||
x->id = 0; | ||
return x; | ||
} | ||
|
||
void getlabel(char * text) { | ||
struct label_t * head; | ||
|
||
if(*text == '"') { | ||
printf("%s", text); | ||
return; | ||
} | ||
|
||
if(main_node == NULL) | ||
main_node = alloc_node(); | ||
|
||
text = strchr(text, '%') + 1; | ||
head = main_node; | ||
while(head->next != NULL) | ||
if(head->name && strcmp(text, head->name) == 0) { | ||
printf("%d", head->id); | ||
return; | ||
} else | ||
head = head->next; | ||
|
||
addlabel(text); | ||
} | ||
|
||
void addlabel(char * text) { | ||
static int curid = 1; | ||
struct label_t * head; | ||
char * cpy, surpress_label = 0; | ||
|
||
cpy = strchr(text, '@') + 1; | ||
if(cpy != (void *)1) | ||
text = cpy; | ||
else surpress_label = 1; | ||
|
||
if(main_node == NULL) | ||
main_node = alloc_node(); | ||
|
||
head = main_node; | ||
while(head->next != NULL) { | ||
if(head->name && strcmp(text, head->name) == 0) { | ||
printf("lbl %d\n", head->id); | ||
return; | ||
} else head = head->next; | ||
} | ||
|
||
head->next = alloc_node(); | ||
head->name = malloc(strlen(text) + 1); | ||
strcpy(head->name, text); | ||
!surpress_label && printf("lbl %d\n", curid); | ||
surpress_label && printf("%d", curid); | ||
head->id = curid++; | ||
} | ||
|
||
int yywrap(void) {return 1;} | ||
%} | ||
|
||
%% | ||
^[ \t]*\@([A-Za-z_][A-Za-z0-9_]*) { addlabel(yytext); } | ||
(%([A-Za-z_][A-Za-z0-9_]*)|"[^"\n]*%([A-Za-z_][A-Za-z0-9_]*)) { getlabel(yytext); } | ||
. { putchar(yytext[0]); } | ||
%% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters