Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.73 KB | None | 0 0
  1. %option noyywrap
  2.  
  3. %{
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <stdint.h>
  9.  
  10. int token_count;
  11. int memory = 0;
  12.  
  13. struct inst {
  14.    int optcode;
  15.    int memory;
  16.    void * next; // make this pointer null was **
  17. };
  18. typedef struct inst instruction; // also need to calculate the size of this.
  19.  
  20. struct label{
  21.    char name[79];
  22.    int memory;
  23. };
  24. typedef struct label ls;
  25.  
  26. ls labels[100];
  27.  
  28. instruction I[10000]; // 12 bit address 4k instructions
  29. int count = 0;
  30. int labelCount = 0;
  31.  
  32. struct branch{
  33.    char label[79];
  34.    int instructionCount;
  35. };
  36. typedef struct branch br;
  37.  
  38. br branchs[100];
  39. int branchCount = 0;
  40. char msg [70];
  41. // you need a specific spot to store the msg text that needs to be initiallized as 0s.
  42. // then set to the string using memcpy
  43. void insertInto(int optcode, int memory)
  44. {
  45.    if (count > 10000)
  46.    {
  47.       printf("Program too large.");
  48.       exit(-1);
  49.    }
  50.    else
  51.    {
  52.       // sets the new memory and optcode
  53.       I[count].optcode = optcode;
  54.       I[count].memory =  memory;
  55.  
  56.       //printf("InstInsert {%d,%d}\n", I[count].optcode, I[count].memory);
  57.       count++;
  58.    }
  59. }
  60.  
  61. void storeBranch(char *p, int ic)
  62. {
  63.    strcpy(branchs[branchCount].label, p);
  64.    branchs[branchCount].instructionCount = ic;
  65.    branchCount++;
  66. }
  67.  
  68. %}
  69.  
  70. %%
  71.  
  72. "left"[\n]   { /* left */
  73.    printf("T_Left\n");
  74.  
  75.    insertInto(7, 0);
  76.  
  77.    memory++;
  78. }
  79. "right"[\n]   { /* right */
  80.    printf("T_Right\n");
  81.  
  82.    insertInto(8, 0);
  83.  
  84.    memory++;
  85. }
  86. "left"[ \t]+[0-9]+[^\n]*   {
  87.    yytext = yytext+4;
  88.    while((int)yytext == ' ' || (int)yytext == '\t')
  89.    {
  90.       yytext++;
  91.    }
  92.    for(int i = 0; i < atoi(yytext); i++)
  93.    {
  94.       printf("T_Left\n");
  95.  
  96.       insertInto(7, 0);
  97.  
  98.       memory++;
  99.    }
  100. }
  101. "right"[ \t]+[0-9]+[^\n]*   {
  102.    yytext = yytext+5;
  103.    while((int)yytext == ' ' || (int)yytext == '\t')
  104.    {
  105.       yytext++;
  106.    }
  107.    for(int i = 0; i < atoi(yytext); i++)
  108.    {
  109.       printf("T_Right\n");
  110.  
  111.       insertInto(8, 0);
  112.  
  113.       memory++;
  114.    }
  115. }
  116. "cmp"[ \t]+[']{1}[a-zA-Z0-9\ ]{1}[']{1}[\n]*   {
  117.    yytext = yytext+5;
  118.    while((int)yytext == ' ' || (int)yytext == '\t')
  119.    {
  120.       yytext++;
  121.    }
  122.    yytext++;
  123.    yytext[strlen(yytext)-1] = 0;
  124.    printf("T_CMP     `%s`, %d\n", yytext, (char)yytext[0]);
  125.  
  126.    insertInto(2, (char)yytext[0] );
  127.  
  128.    memory++;
  129. }
  130. "be"[ \t]+[!]+[a-zA-Z0-9]+[^\n]*   { // 3
  131.    yytext = yytext+3;
  132.    while((int)yytext == ' ' || (int)yytext == '\t')
  133.    {
  134.    yytext++;
  135.    }
  136.    yytext++;
  137.    printf("T_BE     %s\n", yytext);
  138.  
  139.    insertInto(3, 0);
  140.    storeBranch(yytext, memory);
  141.  
  142.    memory++;
  143. }
  144. "bne"[ \t]+[!]+[a-zA-Z0-9]+[^\n]*   { // 4
  145.    yytext = yytext+4;
  146.    while((int)yytext == ' ' || (int)yytext == '\t')
  147.    {
  148.       yytext++;
  149.    }
  150.    yytext++;
  151.    printf("T_BNE     %s\n", yytext);
  152.  
  153.    insertInto(4, 0);
  154.    storeBranch(yytext, memory);
  155.  
  156.    memory++;
  157. }
  158. "bra"[ \t]+[!]+[a-zA-Z0-9]+[^\n]*   { // 5
  159.    yytext = yytext+4;
  160.    while((int)yytext == ' ' || (int)yytext == '\t')
  161.    {
  162.       yytext++;
  163.    }
  164.    yytext++;
  165.    printf("T_BRA     %s\n", yytext);
  166.  
  167.    insertInto(5, 0);
  168.    storeBranch(yytext,memory);
  169.  
  170.    memory++;
  171. }
  172. "draw"[ \t]+[']{1}[_a-zA-Z0-9.\s]{1}[']{1}[^\n]*   { /* draw 'x' */ // 6
  173.    yytext = yytext+4;
  174.    while((int)yytext == ' ' || (int)yytext == '\t')
  175.    {
  176.       yytext++;
  177.    }
  178.    yytext++;
  179.    yytext++;
  180.    yytext[strlen(yytext)-1] = 0;
  181.    printf("T_Draw     `%s`, %d\n", yytext, (char)yytext[0] );
  182.  
  183.    insertInto(6, (char)yytext[0] );
  184.  
  185.    memory++;
  186. }
  187. "halt"[\n]   { /* left */ // 1
  188.    printf("T_Halt\n");
  189.  
  190.    insertInto(1, 0);
  191.  
  192.    memory++;
  193. }
  194. "fail"[\n]   { /* left */ // 0
  195.    printf("T_Fail\n");
  196.  
  197.    insertInto(0, 0);
  198.  
  199.    memory++;
  200. }
  201. ^".msg"[ \t]+[.]{0,70}[^\n]*   { /* message */
  202.    yytext = yytext+5;
  203.    printf("T_MSG       %s\n", yytext);
  204.    strcpy(msg, yytext);
  205. }
  206. ^"!"[a-zA-Z0-9@_]+[^\n]*   { /* Label */
  207.    yytext++;
  208.    printf("T_Label       %s\n", yytext);
  209.    strcpy(labels[labelCount].name, yytext);
  210.    labels[labelCount].memory = memory;
  211.    labelCount++;
  212. }
  213. ";"[^\n\r]*       { /* Comment */ printf("Comment      %s\n", yytext); }
  214. [\s\n\b\t\v\f\r]+    { /* Whitespace */printf("WhiteSpace\n"); }
  215. .    { /* Unexpected Character */
  216.    printf("BAD_CHAR      %s\n", yytext);
  217.    exit(1);
  218. }
  219.  
  220. %%
  221.  
  222. int main (int argc, char **argv) {
  223.  
  224.     char * filename;
  225.  
  226.     if(argc == 1) {
  227.         fprintf(stderr, "no input file given\n");
  228.         exit(EXIT_FAILURE);
  229.     }
  230.  
  231.     if(argc > 2) {
  232.         fprintf(stderr, "Usage:\t%s <input_file>\n", argv[0]);
  233.         exit(EXIT_FAILURE);
  234.     }
  235.  
  236.     filename = (char*)malloc(sizeof(char) * (strlen(argv[1]) + 1));
  237.     if(filename == NULL) {
  238.         fprintf (stderr, "Error - could not allocate memory sufficient for this application\n");
  239.         exit(EXIT_FAILURE);
  240.     }
  241.     strcpy(filename, argv[1]);
  242.     strcpy(strrchr(filename, '.'), ".rom");
  243.  
  244.     yyin = fopen(argv [1], "r");
  245.     if(yyin != NULL) {
  246.         yylex();
  247.  
  248.       fclose (yyin);
  249.  
  250.       for(int i= 0; i < branchCount; i++)
  251.       {
  252.          for(int j= 0; j < labelCount; j++)
  253.          {
  254.             if (!strcmp(labels[j].name, branchs[i].label) )
  255.             {
  256.                I[ branchs[i].instructionCount ].memory = labels[j].memory;
  257.  
  258.                break;
  259.             }
  260.          }
  261.       }
  262.       FILE *fp;
  263.       fp=fopen("test.rom", "wb");
  264.       //char msg [70] = "LOL";
  265.       fwrite(&msg, sizeof(char), 70, fp); // for when its a .msg
  266.       // depending on what you are pulling it from some of that could be garbage
  267.       for(int i= 0; i < count; i++)
  268.       {
  269.          uint16_t word = (I[i].optcode << 12) | I[i].memory;
  270.          fwrite(&word, sizeof(uint16_t), 1, fp);
  271.       }
  272.       fclose(fp);
  273.       printf("\n\n");
  274.     }
  275.     else {
  276.         fprintf(stderr, "Could not open file, \"%s\"\n", argv[1]);
  277.     }
  278.     free (filename);
  279.  
  280.     exit(EXIT_SUCCESS);
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement