Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %option noyywrap
- %{
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdint.h>
- int token_count;
- int memory = 0;
- struct inst {
- int optcode;
- int memory;
- void * next; // make this pointer null was **
- };
- typedef struct inst instruction; // also need to calculate the size of this.
- struct label{
- char name[79];
- int memory;
- };
- typedef struct label ls;
- ls labels[100];
- instruction I[10000]; // 12 bit address 4k instructions
- int count = 0;
- int labelCount = 0;
- struct branch{
- char label[79];
- int instructionCount;
- };
- typedef struct branch br;
- br branchs[100];
- int branchCount = 0;
- char msg [70];
- // you need a specific spot to store the msg text that needs to be initiallized as 0s.
- // then set to the string using memcpy
- void insertInto(int optcode, int memory)
- {
- if (count > 10000)
- {
- printf("Program too large.");
- exit(-1);
- }
- else
- {
- // sets the new memory and optcode
- I[count].optcode = optcode;
- I[count].memory = memory;
- //printf("InstInsert {%d,%d}\n", I[count].optcode, I[count].memory);
- count++;
- }
- }
- void storeBranch(char *p, int ic)
- {
- strcpy(branchs[branchCount].label, p);
- branchs[branchCount].instructionCount = ic;
- branchCount++;
- }
- %}
- %%
- "left"[\n] { /* left */
- printf("T_Left\n");
- insertInto(7, 0);
- memory++;
- }
- "right"[\n] { /* right */
- printf("T_Right\n");
- insertInto(8, 0);
- memory++;
- }
- "left"[ \t]+[0-9]+[^\n]* {
- yytext = yytext+4;
- while((int)yytext == ' ' || (int)yytext == '\t')
- {
- yytext++;
- }
- for(int i = 0; i < atoi(yytext); i++)
- {
- printf("T_Left\n");
- insertInto(7, 0);
- memory++;
- }
- }
- "right"[ \t]+[0-9]+[^\n]* {
- yytext = yytext+5;
- while((int)yytext == ' ' || (int)yytext == '\t')
- {
- yytext++;
- }
- for(int i = 0; i < atoi(yytext); i++)
- {
- printf("T_Right\n");
- insertInto(8, 0);
- memory++;
- }
- }
- "cmp"[ \t]+[']{1}[a-zA-Z0-9\ ]{1}[']{1}[\n]* {
- yytext = yytext+5;
- while((int)yytext == ' ' || (int)yytext == '\t')
- {
- yytext++;
- }
- yytext++;
- yytext[strlen(yytext)-1] = 0;
- printf("T_CMP `%s`, %d\n", yytext, (char)yytext[0]);
- insertInto(2, (char)yytext[0] );
- memory++;
- }
- "be"[ \t]+[!]+[a-zA-Z0-9]+[^\n]* { // 3
- yytext = yytext+3;
- while((int)yytext == ' ' || (int)yytext == '\t')
- {
- yytext++;
- }
- yytext++;
- printf("T_BE %s\n", yytext);
- insertInto(3, 0);
- storeBranch(yytext, memory);
- memory++;
- }
- "bne"[ \t]+[!]+[a-zA-Z0-9]+[^\n]* { // 4
- yytext = yytext+4;
- while((int)yytext == ' ' || (int)yytext == '\t')
- {
- yytext++;
- }
- yytext++;
- printf("T_BNE %s\n", yytext);
- insertInto(4, 0);
- storeBranch(yytext, memory);
- memory++;
- }
- "bra"[ \t]+[!]+[a-zA-Z0-9]+[^\n]* { // 5
- yytext = yytext+4;
- while((int)yytext == ' ' || (int)yytext == '\t')
- {
- yytext++;
- }
- yytext++;
- printf("T_BRA %s\n", yytext);
- insertInto(5, 0);
- storeBranch(yytext,memory);
- memory++;
- }
- "draw"[ \t]+[']{1}[_a-zA-Z0-9.\s]{1}[']{1}[^\n]* { /* draw 'x' */ // 6
- yytext = yytext+4;
- while((int)yytext == ' ' || (int)yytext == '\t')
- {
- yytext++;
- }
- yytext++;
- yytext++;
- yytext[strlen(yytext)-1] = 0;
- printf("T_Draw `%s`, %d\n", yytext, (char)yytext[0] );
- insertInto(6, (char)yytext[0] );
- memory++;
- }
- "halt"[\n] { /* left */ // 1
- printf("T_Halt\n");
- insertInto(1, 0);
- memory++;
- }
- "fail"[\n] { /* left */ // 0
- printf("T_Fail\n");
- insertInto(0, 0);
- memory++;
- }
- ^".msg"[ \t]+[.]{0,70}[^\n]* { /* message */
- yytext = yytext+5;
- printf("T_MSG %s\n", yytext);
- strcpy(msg, yytext);
- }
- ^"!"[a-zA-Z0-9@_]+[^\n]* { /* Label */
- yytext++;
- printf("T_Label %s\n", yytext);
- strcpy(labels[labelCount].name, yytext);
- labels[labelCount].memory = memory;
- labelCount++;
- }
- ";"[^\n\r]* { /* Comment */ printf("Comment %s\n", yytext); }
- [\s\n\b\t\v\f\r]+ { /* Whitespace */printf("WhiteSpace\n"); }
- . { /* Unexpected Character */
- printf("BAD_CHAR %s\n", yytext);
- exit(1);
- }
- %%
- int main (int argc, char **argv) {
- char * filename;
- if(argc == 1) {
- fprintf(stderr, "no input file given\n");
- exit(EXIT_FAILURE);
- }
- if(argc > 2) {
- fprintf(stderr, "Usage:\t%s <input_file>\n", argv[0]);
- exit(EXIT_FAILURE);
- }
- filename = (char*)malloc(sizeof(char) * (strlen(argv[1]) + 1));
- if(filename == NULL) {
- fprintf (stderr, "Error - could not allocate memory sufficient for this application\n");
- exit(EXIT_FAILURE);
- }
- strcpy(filename, argv[1]);
- strcpy(strrchr(filename, '.'), ".rom");
- yyin = fopen(argv [1], "r");
- if(yyin != NULL) {
- yylex();
- fclose (yyin);
- for(int i= 0; i < branchCount; i++)
- {
- for(int j= 0; j < labelCount; j++)
- {
- if (!strcmp(labels[j].name, branchs[i].label) )
- {
- I[ branchs[i].instructionCount ].memory = labels[j].memory;
- break;
- }
- }
- }
- FILE *fp;
- fp=fopen("test.rom", "wb");
- //char msg [70] = "LOL";
- fwrite(&msg, sizeof(char), 70, fp); // for when its a .msg
- // depending on what you are pulling it from some of that could be garbage
- for(int i= 0; i < count; i++)
- {
- uint16_t word = (I[i].optcode << 12) | I[i].memory;
- fwrite(&word, sizeof(uint16_t), 1, fp);
- }
- fclose(fp);
- printf("\n\n");
- }
- else {
- fprintf(stderr, "Could not open file, \"%s\"\n", argv[1]);
- }
- free (filename);
- exit(EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement