Advertisement
dark-s0ul

kursova

Dec 10th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 57.93 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "malloc.h"
  3. #include "string.h"
  4. #include "stdlib.h"
  5. #include "ctype.h"
  6.  
  7. enum : unsigned char {
  8.     program_,
  9.     nameprogram,
  10.     data,
  11.     integer_16,
  12.     start_block,
  13.     stop_block,
  14.     label_,
  15.     for_,
  16.     to_,
  17.     minus,
  18.     next_,
  19.     ternary,
  20.     result,
  21.  
  22.     put,
  23.     get,
  24.  
  25.     symbol,
  26.     number,
  27.  
  28.     semicolon,
  29.     koma,
  30.     lparen,
  31.     rparen,
  32.  
  33.     add_,
  34.     sub_,
  35.     not_,
  36.     and_,
  37.     or_,
  38.     mul_,
  39.     div_,
  40.     mod_,
  41.  
  42.     assign,
  43.     equal,
  44.     not_equal,
  45.     ng,
  46.     nl,
  47.  
  48.     eof,
  49.  
  50.     unknown
  51. };
  52. struct Token {
  53.     char name[50];
  54.     int value;
  55.     unsigned char type;
  56. };
  57.  
  58. struct Symbol {
  59.     char name[50];
  60.     int val;
  61. };
  62.  
  63. struct Segment {
  64.     Token tokens[1000];
  65.     int Len;
  66.  
  67.     Symbol symbols[50];
  68.     int idnum;
  69.  
  70.     int LTable[50];
  71.     int TopL;
  72.  
  73.     bool put_is_used;
  74.     bool get_is_used;
  75.  
  76.     int errors;
  77.  
  78.     char input_file_name[50];
  79.     char output_file_name[50];
  80.  
  81.     int expr[200];
  82. } segment;
  83.  
  84. struct StackType {
  85.     int st[200];
  86.     int top;
  87. };
  88.  
  89. class Stack {
  90. public:
  91.     StackType S;
  92.     void Init(StackType* s) {
  93.         s->top = -1;
  94.     }
  95.  
  96.     void Push(int i, StackType* s) {
  97.         if(IsFull(s)) {
  98.             puts("Stack error (assign full)");
  99.             exit(0);
  100.         } else {
  101.             ++s->top;
  102.             s->st[s->top] = i;
  103.         }
  104.     }
  105.     int Pop(StackType* s) {
  106.         int i;
  107.         if(IsEmpty(s)) {
  108.             puts("Stack error (assign empty)");
  109.             exit(0);
  110.         } else {
  111.             i = s->st[s->top];
  112.             --s->top;
  113.         }
  114.         return i;
  115.     }
  116.     bool IsEmpty(StackType* s) {
  117.         if(s->top == -1) {
  118.             return true;
  119.         } else {
  120.             return false;
  121.         }
  122.     }
  123.     bool IsFull(StackType* s) {
  124.         if(s->top == 199)return true;
  125.         else return false;
  126.     }
  127.     void prints(StackType s) {
  128.         int i=0;
  129.         for(; i<10; ++i) {
  130.             printf("%d_",s.st[i]);
  131.         }
  132.     }
  133. } s;
  134.  
  135. FILE *input_file, *output_file, *error_file;
  136. Token next_token(FILE*);
  137. int analyze(FILE*);
  138. void print_analyze();
  139. int create_identifier_table(FILE*);
  140. void look_for_get_and_put();
  141. void print_get_instructions(FILE*);
  142. void print_put_instructions(FILE*);
  143. void print_title(FILE*, char*);
  144. void print_segment(FILE*);
  145. void print_initialization(FILE*);
  146. void print_ending(FILE*);
  147. void generate_code(FILE*, char*);
  148. int infix_to_postfix(int);
  149. bool is_operation(unsigned char);
  150. bool priority(unsigned char, StackType);
  151. void print_code(FILE*);
  152. void generate_asm_code(const char *, FILE*);
  153.  
  154. void print_mod_instructions(FILE*);
  155. void print_and_instructions(FILE*);
  156. void print_or_instructions(FILE*);
  157. void print_not_instructions(FILE*);
  158. void print_eq_instructions(FILE*);
  159. void print_nl_instructions(FILE*);
  160. void print_ng_instructions(FILE*);
  161. int check_program(FILE*);
  162.  
  163. int check_program(FILE* e) {
  164.     char ids[50][50];
  165.     int idnum = 0;
  166.     int er = 0, i = 0, idn = 0;
  167.  
  168.     int max = 65535;
  169.     int min = -65535;
  170.  
  171.     int label =0;
  172.  
  173.     fputs("S12 compiler\nDesigned by Anton Stoyan \nError file:\n",e);
  174.     fputs("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n\n",e);
  175.  
  176.     if(segment.tokens[i].type == program_) {
  177.         fputs("program_\n",e);
  178.         ++i;
  179.     } else {
  180.         fprintf(e,"%s  (Expected \"program_\")\n",segment.tokens[i].name);
  181.         ++er;
  182.     }
  183.     if(segment.tokens[i].type == nameprogram) {
  184.         fputs("nameprogram\n",e);
  185.         ++i;
  186.     } else {
  187.         fprintf(e,"%s  (Expected \"nameprogram_\")\n",segment.tokens[i].name);
  188.         ++er;
  189.     }
  190.     if(segment.tokens[i].type == data) {
  191.         fputs("variable\n",e);
  192.         ++i;
  193.     } else {
  194.         fprintf(e,"%s  (Expected \"variable\")\n",segment.tokens[i].name);
  195.         ++er;
  196.     }
  197.  
  198.     if(segment.tokens[i].type == integer_16) {
  199.         fputs("integer_16\n",e);
  200.         ++i;
  201.     } else {
  202.         fprintf(e,"%s  (Expected \"integer_16\")\n",segment.tokens[i].name);
  203.         ++er;
  204.     }
  205.  
  206.     for(; segment.tokens[i].type != symbol; ++i) {
  207.         fprintf(e,"%s  (Expected variable)\n",segment.tokens[i].name);
  208.         ++er;
  209.         if(segment.tokens[i].type == eof) return er;
  210.         if(segment.tokens[i].type == start_block) {
  211.             fputs("start_block\n",e);
  212.             break;
  213.         }
  214.         if((segment.tokens[i].type == for_)||(segment.tokens[i].type == to_)||(segment.tokens[i].type == next_)||
  215.                 (is_operation(segment.tokens[i].type))) {
  216.             fputs("%s (Expected \"start_block\"\n)",e);
  217.             ++er;
  218.         }
  219.     }
  220.     for(; segment.tokens[i].type != start_block; ++i) {
  221.         fprintf(e,"%s",segment.tokens[i].name);
  222.         if(segment.tokens[i].type == semicolon) {
  223.             fputs("\n",e);
  224.             continue;
  225.         } else if(segment.tokens[i].type == symbol) {
  226.             strncpy(ids[idnum],segment.tokens[i].name,49);
  227.             ++idnum;
  228.  
  229.             if(segment.tokens[i+1].type == koma) {
  230.                 fputs(",\n",e);
  231.                 ++i;
  232.             } else if(segment.tokens[i+1].type == assign) {
  233.                 fputs(" >> ",e);
  234.                 ++i;
  235.  
  236.                 if(segment.tokens[i+1].type == number) {
  237.                     fprintf(e,"%s",segment.tokens[i+1].name);
  238.                     if((atoi(segment.tokens[i+1].name) >= max)||(atoi(segment.tokens[i+1].name) <= min)) {
  239.                         ++er;
  240.                         fputs(" (Segment assign too long)",e);
  241.                     }
  242.  
  243.                     ++i;
  244.                     if(segment.tokens[i+1].type == koma) {
  245.                         fputs(",\n",e);
  246.                         ++i;
  247.                     } else {
  248.                         fputs("  (Expected , or ;)\n",e);
  249.                         ++er;
  250.                     }
  251.                 } else {
  252.                     fputs(" (unexpected ::)\n",e);
  253.                     ++er;
  254.                 }
  255.             }
  256.         } else if(segment.tokens[i].type == eof) {
  257.             fputs(" Unexpected eof\n",e);
  258.             ++er;
  259.             return er;
  260.         } else if(segment.tokens[i].type == unknown) {
  261.             fprintf(e,"%s  (Unknown symbol)\n",segment.tokens[i+1].name);
  262.             ++er;
  263.         }
  264.     }
  265.  
  266.     if(segment.tokens[i].type == start_block) {
  267.         fputs("start_block\n",e);
  268.         ++i;
  269.     } else {
  270.         for(; segment.tokens[i].type != start_block; ++i) {
  271.             fprintf(e,"%s",segment.tokens[i].name);
  272.             ++er;
  273.             if(segment.tokens[i].type == eof)
  274.                 return er;
  275.         }
  276.     }
  277.  
  278.     for(; segment.tokens[i].type != eof; ++i) {
  279.         fprintf(e,"%s",segment.tokens[i].name);
  280.         if(segment.tokens[i].type == semicolon) {
  281.             fputs(";",e);
  282.             continue;
  283.         }
  284.  
  285.         if(segment.tokens[i].type == stop_block) {
  286.             return er;
  287.         } else if((segment.tokens[i].type == unknown)||(segment.tokens[i].type == koma)) {
  288.             fputs("  (Unexpected)\n",e);
  289.             ++er;
  290.         } else if(segment.tokens[i].type == symbol) {
  291.  
  292.             bool flag = true;
  293.             int ind=0;
  294.             for(; ind<idnum; ++ind) {
  295.                 if((strcmp(ids[ind],segment.tokens[i].name))==0) {
  296.                     flag = false;
  297.                     break;
  298.                 }
  299.             }
  300.             if(flag) {
  301.                 fputs("  (Undeclared id)\n",e);
  302.                 ++er;
  303.                 continue;
  304.             }
  305.             if(segment.tokens[i+1].type == assign) {
  306.                 ++i;
  307.                 ++i;
  308.                 int d=0;
  309.                 int f = 0;
  310.                 bool flag = true;
  311.                 fputs(" >> ",e);
  312.                 if(segment.tokens[i+1].type == unknown) {
  313.                     er++;
  314.                     fputs("unexpected",e);
  315.                 }
  316.                 for(; segment.tokens[i].type != semicolon; ++i) {
  317.                     fprintf(e,"%s",segment.tokens[i].name);
  318.                     if(f) {
  319.                         continue;
  320.                     }
  321.                     if(segment.tokens[i].type == lparen) {
  322.                         ++d;
  323.                     } else if(segment.tokens[i].type == rparen) {
  324.                         if(d == 0) {
  325.                             ++f;
  326.                             continue;
  327.                         } else {
  328.                             --d;
  329.                         }
  330.                     } else if(segment.tokens[i].type == symbol) {
  331.                         int ind=0;
  332.                         for(; ind<idnum; ++ind) {
  333.                             if((strncmp(ids[ind],segment.tokens[i].name,49))==0) {
  334.                                 flag = false;
  335.                                 break;
  336.                             }
  337.                         }
  338.                         if(f||flag) {
  339.                             ++i;
  340.                             for(; segment.tokens[i].type != semicolon; ++i) {
  341.                                 fprintf(e,"%s",segment.tokens[i].name);
  342.                             }
  343.                             fputs(" (Ill expr)",e);
  344.                             continue;
  345.                         }
  346.                     }
  347.  
  348.                     if(is_operation(segment.tokens[i].type)) {
  349.                         if(((segment.tokens[i-1].type != symbol)&&(segment.tokens[i-1].type != rparen)&&
  350.                                 (segment.tokens[i-1].type != number)&&(segment.tokens[i-1].type != not_))||
  351.                                 ((segment.tokens[i+1].type != lparen)&&(segment.tokens[i+1].type != symbol)&&
  352.                                  (segment.tokens[i+1].type != number))) {
  353.                             ++i;
  354.                             for(; segment.tokens[i].type != semicolon; ++i) {
  355.                                 fprintf(e,"%s",segment.tokens[i].name);
  356.                             }
  357.                             ++f;
  358.                         }
  359.                     }
  360.                 }
  361.                 if(f||(d != 0)) {
  362.                     fputs("; (illegal expression2)\n",e);
  363.                     ++er;
  364.                 } else fputs(";\n",e);
  365.             }
  366.  
  367.         }
  368.  
  369.         if(segment.tokens[i].type == get) {
  370.             if((segment.tokens[i+1].type == lparen)&&(segment.tokens[i+2].type == symbol)&&
  371.                     (segment.tokens[i+3].type == rparen)) {
  372.                 fprintf(e," ( %s );\n",segment.tokens[i+2].name);
  373.                 i+=4;
  374.             } else {
  375.                 ++er;
  376.                 ++i;
  377.                 for(; segment.tokens[i].type != semicolon; ++i) {
  378.                     fprintf(e,"%s",segment.tokens[i].name);
  379.                 }
  380.                 fputs("  (illegal using of get() funtion)\n",e);
  381.             }
  382.         }
  383.  
  384.         if(segment.tokens[i].type == put) {
  385.             int d=0;
  386.             int f = 0;
  387.             ++i;
  388.             for(; segment.tokens[i].type != semicolon; ++i) {
  389.                 fprintf(e," %s",segment.tokens[i].name);
  390.                 if(f) {
  391.                     continue;
  392.                 }
  393.                 if(segment.tokens[i].type == lparen) {
  394.                     ++d;
  395.                 }
  396.                 if(segment.tokens[i].type == rparen) {
  397.                     if(d == 0) {
  398.                         ++f;
  399.                         continue;
  400.                     } else {
  401.                         --d;
  402.                     }
  403.                 }
  404.                 if(segment.tokens[i].type == symbol) {
  405.                     int ind=0;
  406.                     bool flag = true;
  407.                     for(; ind<idnum; ++ind) {
  408.                         if((strcmp(ids[ind],segment.tokens[i].name))==0) {
  409.                             flag = false;
  410.                             break;
  411.                         }
  412.                     }
  413.                     if(flag) {
  414.                         for(; segment.tokens[i].type == semicolon; ++i) {
  415.                             fprintf(e,"%s",segment.tokens[i].name);
  416.                         }
  417.                     }
  418.                 }
  419.                 if(is_operation(segment.tokens[i].type)) {
  420.                     if(((segment.tokens[i-1].type != symbol)&&(segment.tokens[i-1].type != rparen)&&
  421.                             (segment.tokens[i-1].type != not_)&&(segment.tokens[i-1].type != number))||
  422.                             ((segment.tokens[i+1].type != lparen)&&(segment.tokens[i+1].type != symbol)&&
  423.                              (segment.tokens[i+1].type != number))) {
  424.                         for(; segment.tokens[i].type != semicolon; ++i) {
  425.                             fprintf(e,"%s",segment.tokens[i].name);
  426.                         }
  427.                         ++f;
  428.                     }
  429.                 }
  430.             }
  431.             if(f||(d != 0)) {
  432.                 fputs("; (illegal expression1)\n",e);
  433.                 ++er;
  434.             }
  435.  
  436.             fputs(";\n",e);
  437.         } else if(segment.tokens[i].type == for_) {
  438.             if(segment.tokens[i+1].type != number && segment.tokens[i+1].type != symbol ) {
  439.  
  440.                 fputs(" Must be number or idetuficator\n",e);
  441.                 ++er;
  442.                 goto S;
  443.             }
  444.  
  445.             if(segment.tokens[i+2].type == number) {
  446.                 fputs(" There assign no to\n",e);
  447.                 ++er;
  448.             }
  449. S:
  450.             int u;
  451.         } else if(segment.tokens[i].type == to_) {
  452.             if(segment.tokens[i-2].type!=for_) {
  453.                 fputs(" There assign no for\n",e);
  454.                 ++er;
  455.             }
  456.  
  457.             if(segment.tokens[i+1].type != number && segment.tokens[i+1].type != symbol) {
  458.                 fputs(" Must be number or identuficator\n",e);
  459.                 ++er;
  460.                 goto R;
  461.             }
  462.             if(segment.tokens[i+2].type ==label_ ) {
  463.                 fputs(" There assign no next \n",e);
  464.                 ++er;
  465.             }
  466. R:
  467.             int uu;
  468.  
  469.         } else if(segment.tokens[i].type == next_) {
  470.             if(segment.tokens[i+1].type != label_) {
  471.                 fputs(" There assign no label \n",e);
  472.                 ++er;
  473.             }
  474.  
  475.             ++i;
  476.  
  477.         } else if(segment.tokens[i].type == label_) {
  478.  
  479.             ++i;
  480.  
  481.         }
  482.     }
  483.     fputs("stop_block\n",e);
  484.     return er;
  485. }
  486. int create_identifier_table(FILE* e) {
  487.     int i;
  488.     int ir;
  489.  
  490.     ir = segment.Len;
  491.     segment.idnum = 0;
  492.     int ErrorFlag=0;
  493.  
  494.     for(i=3; i<ir; ++i) {
  495.         sprintf(segment.symbols[segment.idnum].name,"%s",segment.tokens[i].name);
  496.         if((segment.tokens[i+2].type == number)&&(segment.tokens[i+1].type == assign)) {
  497.             segment.symbols[segment.idnum].val = segment.tokens[i+2].value;
  498.             if(segment.tokens[i+3].type == semicolon) {
  499.                 return ErrorFlag;
  500.             } else if(segment.tokens[i+3].type == koma) {
  501.                 i+=3;
  502.             }
  503.             ++segment.idnum;
  504.         } else if (segment.tokens[i+1].type == semicolon) {
  505.             ++segment.idnum;
  506.         } else if(segment.tokens[i+1].type == koma) {
  507.             ++segment.idnum;
  508.             ++i;
  509.             continue;
  510.         } else if(segment.tokens[i].type == start_block) return ErrorFlag;
  511.         else if(segment.tokens[i].type == eof) return ErrorFlag;
  512.     }
  513.     return ErrorFlag;
  514. }
  515.  
  516. Token next_token(FILE* f) {
  517.     Token token;
  518.     memset(&token, 0, sizeof(Token));
  519.     char buf[50] {0};
  520.  
  521.     for(;;) {
  522.         char ch = getc(f);
  523.         if(ch == ';') {
  524.             strncpy(token.name, ";",2);
  525.             token.type = semicolon;
  526.             token.value = 0;
  527.             break;
  528.         } else if(ch == EOF) {
  529.             strncpy(token.name,"EOF",4);
  530.             token.type = eof;
  531.             token.value = 0;
  532.             break;
  533.         } else if(isspace(ch)) continue;
  534.         else if(ch == '/') {
  535.             char c = getc(f);
  536.  
  537.             if(c == '/') {
  538.                 while ((c != EOF) && (c != '\n')) {
  539.                     c = getc(f);
  540.                 }
  541.                 ungetc(c, f);
  542.             } else {
  543.                 ungetc(c, f);
  544.                 strncpy(token.name,"/",2);
  545.                 token.type = unknown;
  546.                 token.value = 0;
  547.                 break;
  548.             }
  549.         } else if(ch=='(') {
  550.             strncpy(token.name,"(",2);
  551.             token.type = lparen;
  552.             token.value = 0;
  553.             break;
  554.         } else if(ch==')') {
  555.             strncpy(token.name,")",2);
  556.             token.type = rparen;
  557.             token.value = 0;
  558.             break;
  559.         } else if(ch==',') {
  560.             strncpy(token.name,",",2);
  561.             token.type = koma;
  562.             token.value = 0;
  563.             break;
  564.         } else if(ch=='?') {
  565.             strncpy(token.name,"?",2);
  566.             token.type = ternary;
  567.             token.value = 0;
  568.             break;
  569.         } else if(ch=='>') {
  570.             char c;
  571.             c = getc(f);
  572.             if(c==ch) {
  573.                 strncpy(token.name,">>",3);
  574.                 token.type = assign;
  575.                 token.value = 0;
  576.                 break;
  577.             } else {
  578.                 ungetc(c,f);
  579.                 strncpy(token.name,">",2);
  580.                 token.type = unknown;
  581.                 token.value = 0;
  582.                 break;
  583.             }
  584.         } else if(ch=='<') {
  585.             char c;
  586.             c = getc(f);
  587.  
  588.             if(c=='>') {
  589.                 strncpy(token.name,"<>",3);
  590.                 token.type = not_equal;
  591.                 token.value = 0;
  592.                 break;
  593.             } else {
  594.                 char buferr[6];
  595.                 int i=0;
  596.                 buferr[0]=ch;
  597.  
  598.                 for (i=1;; i++) {
  599.                     buferr[i]=getc(f);
  600.                     if ((buferr[i]==' ')||(buferr[i]=='\n')||(buferr[i]=='\t')||(buferr[i]==';')) {
  601.                         if (buferr[i]=='\n') {
  602.                             break;
  603.                         }
  604.                         else break;
  605.                     }
  606.                 }
  607.                 ungetc(buferr[i],f);
  608.                 buferr[i]='\0';
  609.  
  610.                 strncpy(token.name,buferr,i);
  611.                 token.type=nameprogram;
  612.                 token.value=0;
  613.  
  614.                 break;
  615.  
  616.             }
  617.         } else if(ch=='+') {
  618.             char c;
  619.             c = getc(f);
  620.             if(c==ch) {
  621.                 strncpy(token.name,"++",3);
  622.                 token.type = add_;
  623.                 token.value = 0;
  624.                 break;
  625.             } else {
  626.                 ungetc(c,f);
  627.                 strncpy(token.name,"+",2);
  628.                 token.type = unknown;
  629.                 token.value = 0;
  630.                 break;
  631.             }
  632.         } else if(ch=='*') {
  633.             char c;
  634.             c = getc(f);
  635.             if(c==ch) {
  636.                 strncpy(token.name,"**",3);
  637.                 token.type = mul_;
  638.                 token.value = 0;
  639.                 break;
  640.             } else {
  641.                 ungetc(c,f);
  642.                 strncpy(token.name,"*",2);
  643.                 token.type = unknown;
  644.                 token.value = 0;
  645.                 break;
  646.             }
  647.         } else if(ch=='=') {
  648.             strncpy(token.name,"=",3);
  649.             token.type = equal;
  650.             token.value = 0;
  651.             break;
  652.         } else if(isalpha(ch)) {
  653.             // if(ch=='L') {
  654.             //     char bufer[10];
  655.             //     int j=0;
  656.             //     bufer[0]=ch;
  657.             //     for (j=1;; j++) {
  658.             //         bufer[j]=getc(f);
  659.             //         if ((bufer[j]==' ')||(bufer[j]=='\n')||(bufer[j]=='\t')||(bufer[j]==';')) {
  660.             //             if (bufer[j]=='\n') {
  661.             //                 break;
  662.             //             }
  663.             //             else break;
  664.             //         }
  665.             //     }
  666.             //     ungetc(bufer[j],f);
  667.             //     bufer[j]='\0';
  668.  
  669.             //     strncpy(token.name,bufer,j);
  670.             //     token.type=label_;
  671.             //     token.value=0;
  672.  
  673.             //     break;
  674.  
  675.             // }
  676.             int i = 0;
  677.             while (isalpha(ch) || isdigit(ch) || (ch == '_')) {
  678.                 buf[i++] = ch;
  679.                 ch = getc(f);
  680.             }
  681.             ungetc(ch, f);
  682.             buf[i] = 0;
  683.  
  684.             strncpy(token.name, buf, i);
  685.  
  686.             if((strcmp(buf,"Get"))== 0) token.type = get;
  687.             else if((strcmp(buf,"Put"))== 0) token.type = put;
  688.             else if((strcmp(buf,"Mod"))== 0) token.type = mod_;
  689.             else if((strcmp(buf,"Div"))== 0) token.type = div_;
  690.             //else if((strcmp(buf,"For"))== 0) token.type = for_;
  691.            // else if((strcmp(buf,"To"))== 0) token.type = to_;
  692.             //else if((strcmp(buf,"Next"))== 0) token.type = next_;
  693.             else if((strcmp(buf,"Variable"))== 0) token.type = data;
  694.             else if((strcmp(buf,"StartBlock"))== 0) token.type = start_block;
  695.             else if((strcmp(buf,"StopBlock"))== 0) token.type = stop_block;
  696.             else if((strcmp(buf,"Integer_16"))== 0) token.type = integer_16;
  697.             else if((strcmp(buf,"Not"))== 0) token.type = not_;
  698.             else if((strcmp(buf,"And"))== 0) token.type = and_;
  699.             else if((strcmp(buf,"Or"))== 0) token.type = or_;
  700.             else if((strcmp(buf,"Lt"))== 0) token.type = nl;
  701.             else if((strcmp(buf,"Gt"))== 0) token.type = ng;
  702.             else if (i <= 8) {
  703.                 token.type = symbol;
  704.                 token.value = 0;
  705.             } else {
  706.                 token.type = unknown;
  707.                 token.value = 0;
  708.             }
  709.             break;
  710.         } else if (ch=='#') {
  711.             char bufe[10];
  712.             int ii=0;
  713.             bufe[0]=ch;
  714.             for (ii=1;; ii++) {
  715.                 bufe[ii]=getc(f);
  716.                 if ((bufe[ii]==' ')||(bufe[ii]=='\n')||(bufe[ii]=='\t')||(bufe[ii]==';')) {
  717.                     if (bufe[ii]=='\n') {
  718.                         break;
  719.                     }
  720.                     else break;
  721.                 }
  722.             }
  723.             ungetc(bufe[ii],f);
  724.             bufe[ii]='\0';
  725.             if (strcmp(bufe,"#Program")==0) {
  726.                 strncpy(token.name,"#Program",9);
  727.                 token.type=program_;
  728.                 token.value=0;
  729.                 break;
  730.             } else {
  731.                 strncpy(token.name,bufe,ii);
  732.                 token.type = unknown;
  733.                 token.value = 0;
  734.                 break;
  735.             }
  736.         } else if(ch=='-') {
  737.             char c;
  738.             c = getc(f);
  739.             if(isdigit(c)) {
  740.                 int i=2;
  741.                 buf[0] = ch;
  742.                 buf[1] = c;
  743.                 for(; isdigit(ch = getc(f)); ++i) {
  744.                     buf[i] = ch;
  745.                 }
  746.                 ungetc(ch,f);
  747.                 buf[i] = '\0';
  748.                 strncpy(token.name,buf,i);
  749.                 token.type = number;
  750.                 token.value = atoi(buf);
  751.                 break;
  752.             } else if(c==ch) {
  753.                 strncpy(token.name,"-",3);
  754.                 token.type = sub_;
  755.                 token.value = 0;
  756.                 break;
  757.             } else {
  758.                 ungetc(c,f);
  759.                 strncpy(token.name,"-",2);
  760.                 token.type = unknown;
  761.                 token.value = 0;
  762.                 break;
  763.             }
  764.         } else if(isdigit(ch)) {
  765.             int i=1;
  766.             buf[0] = ch;
  767.             for(; isdigit(ch = getc(f)); ++i) {
  768.                 buf[i] = ch;
  769.             }
  770.             ungetc(ch,f);
  771.             buf[i] = '\0';
  772.             strncpy(token.name,buf,i);
  773.             token.type = number;
  774.             token.value = atoi(buf);
  775.             break;
  776.         } else {
  777.             sprintf(token.name,"%s",ch);
  778.             token.type = unknown;
  779.             token.value = 0;
  780.             break;
  781.         }
  782.     }
  783.     return token;
  784. }
  785. int analyze(FILE* In) {
  786.     int i = 0;
  787.     while (true) {
  788.         segment.tokens[i] = next_token(In);
  789.         //printf("%s\n", segment.tokens[i].name);
  790.         if (segment.tokens[i++].type == eof) break;
  791.     }
  792.     return i;
  793. }
  794.  
  795. void print_analyze() {
  796.     FILE* f = fopen("LexArray.txt","w");
  797.     puts("Results of lexical analis:");
  798.     puts("  Lex file assign LexArray.txt");
  799.  
  800.     fputs("S12 compiler\nDesigned by Anton Stoyan \n",f);
  801.     fprintf(f,"Input file: %s\n", segment.input_file_name);
  802.     fputs("Lex file\n",f);
  803.     fputs(" №   | Name       | Type       | Value\n\n",f);
  804.  
  805.     for(int i = 0; i<segment.Len; ++i) {
  806.         char type[50] {0};
  807.         switch(segment.tokens[i].type) {
  808.         case add_ :
  809.             strncpy(type,"add",4);
  810.             break;
  811.         case and_ :
  812.             strncpy(type,"and",4);
  813.             break;
  814.         case rparen :
  815.             strncpy(type,"rparen",7);
  816.             break;
  817.         case minus:
  818.             strncpy(type,"minus",6);
  819.             break;
  820.         case data :
  821.             strncpy(type,"data",5);
  822.             break;
  823.         case div_ :
  824.             strncpy(type,"div",4);
  825.             break;
  826.         case for_ :
  827.             strncpy(type,"for",4);
  828.             break;
  829.         case to_ :
  830.             strncpy(type,"to",3);
  831.             break;
  832.         case next_ :
  833.             strncpy(type,"next",5);
  834.             break;
  835.         case stop_block :
  836.             strncpy(type,"stop_block",11);
  837.             break;
  838.         case eof :
  839.             strncpy(type,"KeyWord",8);
  840.             break;
  841.         case get :
  842.             strncpy(type,"get",4);
  843.             break;
  844.         case program_ :
  845.             strncpy(type,"program_",9);
  846.             break;
  847.         case mod_ :
  848.             strncpy(type,"mod",4);
  849.             break;
  850.         case mul_ :
  851.             strncpy(type,"mul",4);
  852.             break;
  853.         case not_ :
  854.             strncpy(type,"not",4);
  855.             break;
  856.         case number :
  857.             strncpy(type,"number",7);
  858.             break;
  859.         case nameprogram :
  860.             strncpy(type,"name",5);
  861.             break;
  862.         case lparen :
  863.             strncpy(type,"lparen",7);
  864.             break;
  865.         case or_ :
  866.             strncpy(type,"or",3);
  867.             break;
  868.         case put :
  869.             strncpy(type,"put",4);
  870.             break;
  871.         case semicolon :
  872.             strncpy(type,"semicolon",10);
  873.             break;
  874.         case start_block :
  875.             strncpy(type,"start_block",12);
  876.             break;
  877.         case sub_ :
  878.             strncpy(type,"sub",4);
  879.             break;
  880.         case symbol :
  881.             strncpy(type,"variable",9);
  882.             break;
  883.         case ternary :
  884.             strncpy(type,"ternary",8);
  885.             break;
  886.         case result :
  887.             strncpy(type,"result",7);
  888.             break;
  889.         case koma :
  890.             strncpy(type,"koma",5);
  891.             break;
  892.         case assign :
  893.             strncpy(type,"assign",7);
  894.             break;
  895.         case equal :
  896.             strncpy(type,"equal",6);
  897.             break;
  898.         case not_equal :
  899.             strncpy(type,"not_equal",10);
  900.             break;
  901.         case nl :
  902.             strncpy(type,"less",5);
  903.             break;
  904.         case ng :
  905.             strncpy(type,"greather",9);
  906.             break;
  907.         case unknown :
  908.             strncpy(type,"unknown",15);
  909.             break;
  910.         case integer_16 :
  911.             strncpy(type,"integer_16",10);
  912.             break;
  913.         case label_:
  914.             strncpy(type,"label",6);
  915.             break;
  916.         }
  917.         fprintf(f," %-3d | %-10.10s | %-10.10s | %d\n",i+1,segment.tokens[i].name,type,segment.tokens[i].value);
  918.     }
  919.     fclose(f);
  920. }
  921. void look_for_get_and_put() {
  922.     int i=0;
  923.     do {
  924.         ++i;
  925.     } while(segment.tokens[i-1].type != start_block);
  926.  
  927.     for(; segment.tokens[i].type != eof; ++i) {
  928.         if(segment.tokens[i].type == get) segment.get_is_used = true;
  929.         if(segment.tokens[i].type == put) segment.put_is_used = true;
  930.         if(segment.get_is_used && segment.put_is_used) break;
  931.     }
  932. }
  933. void print_get_instructions(FILE* f) {
  934.     fputs("\n;====Input procedure Get()=============\n",f);
  935.     fputs("get PROC\n\n",f);
  936.  
  937.     fputs("\tpush ax\n",f);
  938.     fputs("\tpush bx\n",f);
  939.     fputs("\tpush cx\n",f);
  940.     fputs("\tpush dx\n",f);
  941.     fputs("\tpush di\n",f);
  942.     fputs("\tpush si\n",f);
  943.  
  944.     fputs("\n\tlea dx,In_Str\n",f);
  945.     fputs("\tmov ah,09\n",f);
  946.     fputs("\tint 21h\n",f);
  947.     fputs("\tmov di,offset buf\n",f);
  948.     fputs("\tmov MaxLen,10\n",f);
  949.     fputs("\tmov cx,MaxLen\n\n",f);
  950.     fputs("\tmov si,0\n",f);
  951.     fputs("\n  In_00:\n",f);
  952.     fputs("\tmov ah,01\n",f);
  953.     fputs("\tint 21h\n",f);
  954.     fputs("\tcmp al,0Dh\n",f);
  955.     fputs("\tje In_1\n",f);
  956.     fputs("\tcmp al,'-'\n",f);
  957.     fputs("\tjne In_0\n",f);
  958.     fputs("\tmov FlagS,1\n",f);
  959.     fputs("\tjmp In_00\n",f);
  960.     fputs("\n  In_0:\n",f);
  961.     fputs("\tmov dl,al\n",f);
  962.     fputs("\tcall CHECK_BYTE\n",f);
  963.     fputs("\tmov TStr[si],dl\n",f);
  964.     fputs("\tinc si\n",f);
  965.     fputs("\tloop In_00\n",f);
  966.     fputs("\n  In_1:\n",f);
  967.     fputs("\tpush si\n",f);
  968.     fputs("\tdec si\n",f);
  969.     fputs("\tcmp cx,MaxLen\n",f);
  970.     fputs("\tjne In_2\n",f);
  971.     fputs("\tlea dx,erStr1\n",f);
  972.     fputs("\tmov ah,09\n",f);
  973.     fputs("\tint 21h\n",f);
  974.     fputs("\tmov erFlag,1\n",f);
  975.     fputs("\tjmp In_5\n",f);
  976.     fputs("\n  In_2:\n",f);
  977.     fputs("\tmov bh,0\n",f);
  978.     fputs("\tmov bl,TStr[si]\n",f);
  979.     fputs("\tMY_MUL Mul10,bx,my_z\n",f);
  980.     fputs("\tadd TBin,ax\n",f);
  981.     fputs("\tadc TBin+2,dx\n",f);
  982.     fputs("\tmov bh,0\n",f);
  983.     fputs("\tmov bl,10\n",f);
  984.     fputs("\tMY_MUL Mul10,bx,my_z\n",f);
  985.     fputs("\tmov Mul10,ax\n",f);
  986.     fputs("\tmov Mul10+2,dx\n",f);
  987.     fputs("\tdec si\n",f);
  988.     fputs("\tcmp si,0\n",f);
  989.     fputs("\tjge In_2\n",f);
  990.     fputs("\tmov ax,TBin\n",f);
  991.     fputs("\tmov dx,TBin+2\n",f);
  992.     fputs("\tpop si\n",f);
  993.     fputs("\tcmp si,MaxLen\n",f);
  994.     fputs("\tjl In_3\n",f);
  995.     fputs("\tcmp MaxLen,10\n",f);
  996.     fputs("\tjl In_2_1\n",f);
  997.     fputs("\tjs In_Err\n",f);
  998.     fputs("\tcmp dx,7FFFh\n",f);
  999.     fputs("\tja In_Err\n",f);
  1000.     fputs("\tjmp In_3\n",f);
  1001.     fputs("\n  In_2_1:\n",f);
  1002.     fputs("\tcmp MaxLen,5\n",f);
  1003.     fputs("\tjl In_2_2\n",f);
  1004.     fputs("\tcmp dx,00\n",f);
  1005.     fputs("\tja In_Err\n",f);
  1006.     fputs("\tcmp ah,7fh\n",f);
  1007.     fputs("\tja In_Err\n",f);
  1008.     fputs("\tjmp In_3\n",f);
  1009.     fputs("\n  In_2_2:\n",f);
  1010.     fputs("\tcmp ax,007Fh\n",f);
  1011.     fputs("\tjbe In_3\n",f);
  1012.     fputs("\n  In_Err:\n",f);
  1013.     fputs("\tlea dx,erStr3\n",f);
  1014.     fputs("\tmov ah,09\n",f);
  1015.     fputs("\tint 21h\n",f);
  1016.     fputs("\tmov erFlag,1\n",f);
  1017.     fputs("\tjmp In_5\n",f);
  1018.     fputs("\n  In_3:\n",f);
  1019.     fputs("\tcmp FlagS,1\n",f);
  1020.     fputs("\tjne In_4\n",f);
  1021.     fputs("\tmov bx,0\n",f);
  1022.     fputs("\tsub bx,ax\n",f);
  1023.     fputs("\tmov ax,bx\n",f);
  1024.     fputs("\tmov bx,0\n",f);
  1025.     fputs("\tsbb bx,dx\n",f);
  1026.     fputs("\tmov dx,bx\n",f);
  1027.     fputs("\n  In_4:\n",f);
  1028.     fputs("\tmov [di],ax\n",f);
  1029.     fputs("\tmov [di+2],dx\n",f);
  1030.     fputs("\tmov TBin,0\n",f);
  1031.     fputs("\tmov TBin+2,0\n",f);
  1032.     fputs("\tmov Mul10,1\n",f);
  1033.     fputs("\tmov Mul10+2,0\n",f);
  1034.     fputs("\tmov FlagS,0\n",f);
  1035.     fputs("\n  In_5:\n\n",f);
  1036.  
  1037.     fputs("\tpop si\n",f);
  1038.     fputs("\tpop di\n",f);
  1039.     fputs("\tpop dx\n",f);
  1040.     fputs("\tpop cx\n",f);
  1041.     fputs("\tpop bx\n",f);
  1042.     fputs("\tpop ax\n\n",f);
  1043.  
  1044.     fputs("\tret\n",f);
  1045.     fputs("get ENDP\n\n",f);
  1046.     fputs("CHECK_BYTE  PROC\n",f);
  1047.     fputs("\tsub dl,30h\n",f);
  1048.     fputs("\tcmp dl,00\n",f);
  1049.     fputs("\tjl ErS\n",f);
  1050.     fputs("\tcmp dl,0Ah\n",f);
  1051.     fputs("\tjl GO\n",f);
  1052.     fputs("\n  ErS:\n",f);
  1053.     fputs("\tlea dx,erStr2\n",f);
  1054.     fputs("\tmov ah,09\n",f);
  1055.     fputs("\tint 21h\n",f);
  1056.     fputs("\n  GO:\n",f);
  1057.     fputs("\tret\n",f);
  1058.     fputs("CHECK_BYTE ENDP\n",f);
  1059.     fputs(";======================================\n\n",f);
  1060. }
  1061.  
  1062. void print_put_instructions(FILE* f) {
  1063.     fputs("\n;===Output procedure put()=============\n",f);
  1064.     fputs("\nput PROC\n\n",f);
  1065.  
  1066.     fputs("\tpush ax\n",f);
  1067.     fputs("\tpush bx\n",f);
  1068.     fputs("\tpush cx\n",f);
  1069.     fputs("\tpush dx\n",f);
  1070.     fputs("\tpush di\n",f);
  1071.     fputs("\tpush si\n\n",f);
  1072.  
  1073.     fputs("\tmov cl,byte ptr buf+3\n",f);
  1074.     fputs("\tand cl,80h\n",f);
  1075.     fputs("\tje m6\n",f);
  1076.     fputs("\tfild buf\n",f);
  1077.     fputs("\tfchs\n",f);
  1078.     fputs("\tfistp buf\n",f);
  1079.     fputs("\tmov MSign,\'-\'\n",f);
  1080.     fputs("\n  M6:\n",f);
  1081.     fputs("\tmov cx,10\n",f);
  1082.     fputs("\tmov di,0\n",f);
  1083.     fputs("\n  O_1:\n",f);
  1084.     fputs("\tffree st(0)\n",f);
  1085.     fputs("\tffree st(1)\n",f);
  1086.     fputs("\tfild ten\n",f);
  1087.     fputs("\tfild buf\n",f);
  1088.     fputs("\tfprem\n",f);
  1089.     fputs("\tfistp X1\n",f);
  1090.     fputs("\tmov dl,byte ptr X1\n",f);
  1091.     fputs("\tadd dl,30h\n",f);
  1092.     fputs("\tmov X_Str[di],dl\n",f);
  1093.     fputs("\tinc di\n",f);
  1094.     fputs("\tfild buf\n",f);
  1095.     fputs("\tfxch st(1)\n",f);
  1096.     fputs("\tfdiv\n",f);
  1097.     fputs("\tfrndint\n",f);
  1098.     fputs("\tfistp buf\n",f);
  1099.     fputs("\tloop O_1\n\n",f);
  1100.     fputs("\tmov dx,offset MX1\n",f);
  1101.     fputs("\tmov ah,09\n",f);
  1102.     fputs("\tint 21h\n",f);
  1103.     fputs("\tmov dl,MSign\n",f);
  1104.     fputs("\tmov ah,02\n",f);
  1105.     fputs("\tint 21h\n",f);
  1106.     fputs("\tinc di\n",f);
  1107.     fputs("\tmov cx,12\n",f);
  1108.     fputs("\n  O_2:\n",f);
  1109.     fputs("\tmov dl,X_Str[di]\n",f);
  1110.     fputs("\tmov ah,02h\n",f);
  1111.     fputs("\tint 21h\n",f);
  1112.     fputs("\tdec di\n",f);
  1113.     fputs("\tloop O_2\n\n",f);
  1114.     fputs("\tmov MSign,\'+\'\n\n",f);
  1115.  
  1116.     fputs("\tpop si\n",f);
  1117.     fputs("\tpop di\n",f);
  1118.     fputs("\tpop dx\n",f);
  1119.     fputs("\tpop cx\n",f);
  1120.     fputs("\tpop bx\n",f);
  1121.     fputs("\tpop ax\n\n",f);
  1122.  
  1123.     fputs("\tret\n",f);
  1124.     fputs("put ENDP\n",f);
  1125.     fputs(";======================================\n\n",f);
  1126. }
  1127.  
  1128. void print_title(FILE* f,char* in) {
  1129.     fprintf(f,";Translated from input file %s\n",in);
  1130.     fputs("DOSSEG\n",f);
  1131.     fputs(".MODEL SMALL\n",f);
  1132.     fputs(".STACK 100h\n\n",f);
  1133.  
  1134. }
  1135.  
  1136. void print_segment(FILE* f) {
  1137.     if(segment.get_is_used) {
  1138.         fputs("MY_MUL MACRO X,Y,Z\n",f);
  1139.         fputs("\tmov z,0\n",f);
  1140.         fputs("\tmov z+2,0\n",f);
  1141.         fputs("\tmov ax,x\n",f);
  1142.         fputs("\tmul y\n",f);
  1143.         fputs("\tmov z,ax\n",f);
  1144.         fputs("\tmov z+2,dx\n",f);
  1145.         fputs("\tmov ax,x+2\n",f);
  1146.         fputs("\tmul y\n",f);
  1147.         fputs("\tadd z+2,ax\n",f);
  1148.         fputs("\tmov ax,z\n",f);
  1149.         fputs("\tmov dx,z+2\n",f);
  1150.         fputs("ENDM\n\n",f);
  1151.     }
  1152.  
  1153.     int i;
  1154.     fputs(".DATA\n",f);
  1155.     for(i = 0; i<segment.idnum; ++i) {
  1156.         fprintf(f,"\t%s\tlabel word\n\tdd\t0%xh\n",segment.symbols[i].name,segment.symbols[i].val);
  1157.     }
  1158.     fputs("\n\tbuf\tdd\t0h\n",f);
  1159.     fputs("\n\tbuf1\tdw\t0h\n",f);
  1160.     fputs("\n\tbuf2\tdw\t0h\n",f);
  1161.     fputs("\n\tbuf3\tdw\t0h\n",f);
  1162.     fputs("\n\tcond\tdw\t0h\n",f);
  1163.     fputs("\n\trez\tdw\t0h\n",f);
  1164.     fputs("\n\tFor1\tdw\t0h\n",f);
  1165.     fputs("\tRC\tdw\t0h\n",f);
  1166.  
  1167.     fputs("\tlb1\tdd\t0h\n",f);
  1168.     fputs("\tlb2\tdd\t0h\n\n",f);
  1169.  
  1170.     if(segment.get_is_used) {
  1171.         fputs(";======Segment for get() functions======\n",f);
  1172.         fputs("\terFlag\tdb\t0\n",f);
  1173.         fputs("\tTStr\tdb\t10 dup (0)\n",f);
  1174.         fputs("\tTBin\tdw\t0,0\n",f);
  1175.         fputs("\tMaxLen\tdw\t0\n",f);
  1176.         fputs("\tFlagS\tdb\t0\n",f);
  1177.         fputs("\tMul10\tdw\t1,0\n",f);
  1178.         fputs("\tmy_z\tdw\t0,0\n\n",f);
  1179.  
  1180.         fputs("\tIn_Str\tdb\t13,10,\'<< $\'\n",f);
  1181.         fputs("\terStr1\tdb\t13,10,\'Segment not input_variable\',13,10,\'$\'\n",f);
  1182.         fputs("\terStr2\tdb\t13,10,\'Incorrectly data \',13,10,\'$\'\n",f);
  1183.         fputs("\terStr3\tdb\t13,10,\'Segment assign too long \',13,10,\'$\'\n",f);
  1184.         fputs(";======================================\n\n",f);
  1185.     }
  1186.     if(segment.put_is_used) {
  1187.         fputs(";=======Segment for put===================\n",f);
  1188.         fputs("\tMSign\tdb\t\'+\',\'$\'\n",f);
  1189.         fputs("\tX_Str\tdb\t12 dup (0)\n",f);
  1190.         fputs("\tten\tdw\t10\n",f);
  1191.         fputs("\tX1\tdw\t0h\n",f);
  1192.         fputs("\tMX1\tdb\t13,10,\'>> $\'\n",f);
  1193.         fputs(";======================================\n\n",f);
  1194.     }
  1195. }
  1196.  
  1197. void print_initialization(FILE* f) {
  1198.     fputs(".CODE\n",f);
  1199.  
  1200.     fputs(";======================================\n",f);
  1201.     fputs("\tmov ax,@data\n",f);
  1202.     fputs("\tmov ds,ax\n\n",f);
  1203.  
  1204.     fputs("\tfinit\n",f);
  1205.     fputs("\tfstcw RC\n",f);
  1206.     fputs("\tor RC,0c00h\n",f);
  1207.     fputs("\tfldcw RC\n",f);
  1208.     fputs(";======================================\n\n",f);
  1209. }
  1210.  
  1211. void print_ending(FILE* f) {
  1212.     fputs(";======================================\n",f);
  1213.     fputs("MOV AH,4Ch\nINT 21h\n",f);
  1214.  
  1215.     if(segment.get_is_used) {
  1216.         print_get_instructions(f);
  1217.     }
  1218.     if(segment.put_is_used) {
  1219.         print_put_instructions(f);
  1220.     }
  1221.  
  1222.     print_mod_instructions(f);
  1223.     print_and_instructions(f);
  1224.     print_or_instructions(f);
  1225.     print_not_instructions(f);
  1226.     print_eq_instructions(f);
  1227.     print_nl_instructions(f);
  1228.     print_ng_instructions(f);
  1229.  
  1230.     fputs(";======================================\n\n",f);
  1231.  
  1232.     fputs("END",f);
  1233. }
  1234.  
  1235. int infix_to_postfix(int i) {
  1236.     s.Init(&s.S);
  1237.  
  1238.     int n,z;
  1239.  
  1240.     n = 0;
  1241.     for(; segment.tokens[i+n].type!=semicolon; ++n);
  1242.  
  1243.     int k;
  1244.     k=i+n;
  1245.     for(z=0; i < k; ++i) {
  1246.         unsigned char in;
  1247.         in = segment.tokens[i].type;
  1248.  
  1249.         if((in == symbol)||(in == number)||(in == not_)) {
  1250.             segment.expr[z] = i;
  1251.             ++z;
  1252.         } else if(is_operation(in)) {
  1253.             if(s.IsEmpty(&s.S)||priority(in,s.S)) {
  1254.                 s.Push(i,&s.S);
  1255.             } else {
  1256.                 if(segment.tokens[s.S.st[s.S.top]].type!=lparen) {
  1257.                     do {
  1258.                         segment.expr[z] = s.Pop(&s.S);
  1259.                         ++z;
  1260.                     } while((!(priority(in,s.S)))&&(!(s.IsEmpty(&s.S)))&&
  1261.                             (segment.tokens[s.S.st[s.S.top]].type != lparen));
  1262.                 }
  1263.                 s.Push(i,&s.S);
  1264.             }
  1265.         }
  1266.  
  1267.         if(in == lparen) {
  1268.             s.Push(i,&s.S);
  1269.             segment.expr[z] = i;
  1270.             ++z;
  1271.         }
  1272.  
  1273.         if(in == rparen) {
  1274.             for(; segment.tokens[s.S.st[s.S.top]].type != lparen;) {
  1275.                 segment.expr[z] = s.Pop(&s.S);
  1276.                 ++z;
  1277.             }
  1278.             s.Pop(&s.S);
  1279.             segment.expr[z] = i;
  1280.             ++z;
  1281.         }
  1282.     }
  1283.  
  1284.     for(; !(s.IsEmpty(&s.S));) {
  1285.         segment.expr[z] = s.Pop(&s.S);
  1286.         ++z;
  1287.     }
  1288.  
  1289.     segment.expr[z] = 3000;
  1290.     z++;
  1291.  
  1292.     return k;
  1293. }
  1294.  
  1295. bool is_operation(unsigned char t) {
  1296.     return ((t==add_)||(t==sub_)||(t==div_)||(t==mul_)||(t==mod_)|| (t==and_)||(t==or_));
  1297. }
  1298.  
  1299. bool priority(unsigned char t,StackType s) {
  1300.     return
  1301.         ((t==div_)&&(segment.tokens[s.st[s.top]].type==add_))||
  1302.         ((t==div_)&&(segment.tokens[s.st[s.top]].type==sub_))||
  1303.         ((t==div_)&&(segment.tokens[s.st[s.top]].type==or_))||
  1304.         ((t==div_)&&(segment.tokens[s.st[s.top]].type==and_))||
  1305.         ((t==mul_)&&(segment.tokens[s.st[s.top]].type==add_))||
  1306.         ((t==mul_)&&(segment.tokens[s.st[s.top]].type==sub_))||
  1307.         ((t==mul_)&&(segment.tokens[s.st[s.top]].type==or_))||
  1308.         ((t==mul_)&&(segment.tokens[s.st[s.top]].type==and_))||
  1309.         ((t==mod_)&&(segment.tokens[s.st[s.top]].type==add_))||
  1310.         ((t==mod_)&&(segment.tokens[s.st[s.top]].type==sub_))||
  1311.         ((t==mod_)&&(segment.tokens[s.st[s.top]].type==or_))||
  1312.         ((t==mod_)&&(segment.tokens[s.st[s.top]].type==and_))||
  1313.         ((t==and_)&&(segment.tokens[s.st[s.top]].type==or_));
  1314. }
  1315. void print_code(FILE* f) {
  1316.     int lab=0;
  1317.     Token l;
  1318.     int i=0;
  1319.  
  1320.     do {
  1321.         ++i;
  1322.     } while(segment.tokens[i].type != start_block);
  1323.     ++i;
  1324.     for(;; ++i) {
  1325.         l.type = segment.tokens[i].type;
  1326.         strncpy(l.name,segment.tokens[i].name,50);;
  1327.         l.value = segment.tokens[i].value;
  1328.  
  1329.         if(l.type == stop_block) break;
  1330.  
  1331.         if(l.type == put) {
  1332.             if(segment.tokens[i+3].type==ng) {
  1333.                 int cond=0;
  1334.                 fprintf(f,"\tfild %s\n",segment.tokens[i+2].name);
  1335.                 fprintf(f,"\tfild %s\n",segment.tokens[i+4].name);
  1336.                 fputs("\tcall ng_\n",f);
  1337.                 fputs("\tfistp cond\n",f);
  1338.                 fputs("\tmov ax, word ptr cond\n",f);
  1339.                 fputs("\tcmp ax,1\n",f);
  1340.                 fprintf(f,"\tjne Cond%d\n",cond);
  1341.                 fprintf(f,"\tmov rez, %s\n",segment.tokens[i+8].name);
  1342.                 fprintf(f,"\tjmp Condit%d\n",cond);
  1343.                 fprintf(f,"Cond%d:\n",cond);
  1344.                 fprintf(f,"\tmov rez, %s\n",segment.tokens[i+6].name);
  1345.                 fprintf(f,"Condit%d:\n",cond);
  1346.                 fputs("\tfild rez\n",f);
  1347.                 fputs("\tfistp buf\n",f);
  1348.                 i=i+10;
  1349.                 cond++;
  1350.                 goto stop_block;
  1351.             }
  1352.             if(segment.tokens[i+3].type==nl) {
  1353.                 int cond1=0;
  1354.                 fprintf(f,"\tfild %s\n",segment.tokens[i+2].name);
  1355.                 fprintf(f,"\tfild %s\n",segment.tokens[i+4].name);
  1356.                 fputs("\tcall nl_\n",f);
  1357.                 fputs("\tfistp cond\n",f);
  1358.                 fputs("\tmov ax, word ptr cond\n",f);
  1359.                 fputs("\tcmp ax,1\n",f);
  1360.                 fprintf(f,"\tjne Cond1%d\n",cond1);
  1361.                 fprintf(f,"\tmov rez, %s\n",segment.tokens[i+8].name);
  1362.                 fprintf(f,"\tjmp Condit1%d\n",cond1);
  1363.                 fprintf(f,"Cond1%d:\n",cond1);
  1364.                 fprintf(f,"\tmov rez, %s\n",segment.tokens[i+6].name);
  1365.                 fprintf(f,"Condit1%d:\n",cond1);
  1366.                 fputs("\tfild rez\n",f);
  1367.                 fputs("\tfistp buf\n",f);
  1368.                 i=i+10;
  1369.                 cond1++;
  1370.                 goto stop_block;
  1371.             }
  1372.             if(segment.tokens[i+3].type==equal) {
  1373.                 int cond2 = 0;
  1374.                 fprintf(f,"\tfild %s\n",segment.tokens[i+2].name);
  1375.                 fprintf(f,"\tfild %s\n",segment.tokens[i+4].name);
  1376.                 fputs("\tcall eq_\n",f);
  1377.                 fputs("\tfistp cond\n",f);
  1378.                 fputs("\tmov ax, word ptr cond\n",f);
  1379.                 fputs("\tcmp ax,1\n",f);
  1380.                 fprintf(f,"\tjne Cond2%d\n",cond2);
  1381.                 fprintf(f,"\tmov rez, %s\n",segment.tokens[i+6].name);
  1382.                 fprintf(f,"\tjmp Condit2%d\n",cond2);
  1383.                 fprintf(f,"Cond2%d:\n",cond2);
  1384.                 fprintf(f,"\tmov rez, %s\n",segment.tokens[i+8].name);
  1385.                 fprintf(f,"Condit2%d:\n",cond2);
  1386.                 fputs("\tfild rez\n",f);
  1387.                 fputs("\tfistp buf\n",f);
  1388.                 i=i+10;
  1389.                 cond2++;
  1390.                 goto stop_block;
  1391.             }
  1392.             if(segment.tokens[i+3].type==not_equal) {
  1393.                 int cond3=0;
  1394.                 fprintf(f,"\tfild %s\n",segment.tokens[i+2].name);
  1395.                 fprintf(f,"\tfild %s\n",segment.tokens[i+4].name);
  1396.                 fputs("\tcall eq_\n",f);
  1397.                 fputs("\tcall not_\n",f);
  1398.                 fputs("\tfistp cond\n",f);
  1399.                 fputs("\tmov ax, word ptr cond\n",f);
  1400.                 fputs("\tcmp ax,1\n",f);
  1401.                 fprintf(f,"\tjne Cond3%d\n",cond3);
  1402.                 fprintf(f,"\tmov rez, %s\n",segment.tokens[i+6].name);
  1403.                 fprintf(f,"\tjmp Condit3%d\n",cond3);
  1404.                 fprintf(f,"Cond3%d:\n",cond3);
  1405.                 fprintf(f,"\tmov rez, %s\n",segment.tokens[i+8].name);
  1406.                 fprintf(f,"Condit3%d:\n",cond3);
  1407.                 fputs("\tfild rez\n",f);
  1408.                 fputs("\tfistp buf\n",f);
  1409.                 i=i+10;
  1410.                 cond3++;
  1411.                 goto stop_block;
  1412.             }
  1413.             i = infix_to_postfix(i+1);
  1414.  
  1415.             generate_asm_code("buf",f);
  1416. stop_block:
  1417.             fputs("\tcall put\n",f);
  1418.         }
  1419.  
  1420.         if(l.type == get) {
  1421.             fputs("\tcall get\n",f);
  1422.             fprintf(f,"\tfild buf\n");
  1423.             fprintf(f,"\tfistp %s\n",segment.tokens[i+2].name);
  1424.             i +=4;
  1425.         }
  1426.         if (l.type== for_) {
  1427.             if(segment.tokens[i+1].type==number) {
  1428.                 char bufHi[6], bufLo[5], buf[10];
  1429.                 buf[0] = '0';
  1430.                 sprintf(&buf[1],"%08x",segment.tokens[i+1].value);
  1431.                 strncpy(bufLo,&buf[5],4);
  1432.                 bufLo[4] = '\0';
  1433.                 fprintf(f,"\tmov  buf1,0%sh\n",bufLo);
  1434.  
  1435.             }
  1436.             if(segment.tokens[i+1].type==symbol) {
  1437.                 fprintf(f,"\tmov ax,%s+2\n",segment.tokens[i+1].name);
  1438.                 fputs("\tmov buf1,ax\n",f);
  1439.                 i++;
  1440.             }
  1441.         }
  1442.         if (l.type== to_) {
  1443.             if(segment.tokens[i+1].type==number) {
  1444.  
  1445.                 char bufHi[6], bufLo[5], buf[10];
  1446.  
  1447.                 buf[0] = '0';
  1448.                 sprintf(&buf[1],"%08x",segment.tokens[i+1].value);
  1449.                 strncpy(bufLo,&buf[5],4);
  1450.                 bufLo[4] = '\0';
  1451.  
  1452.                 fprintf(f,"\tmov  buf2,0%sh\n",bufLo);
  1453.                 fputs("\tmov ax,buf2\n",f);
  1454.                 fputs("\tsub ax, buf1\n",f);
  1455.                 fputs("\tmov For1,ax\n",f);
  1456.                 fputs("\tmov cx,For1\n",f);
  1457.                 fprintf(f,"\n  Label_%d:\n",lab);
  1458.             }
  1459.             if(segment.tokens[i+1].type==symbol) {
  1460.                 fprintf(f,"\tmov ax,%s\n",segment.tokens[i+1].name);
  1461.                 fputs("\tmov buf2,ax\n",f);
  1462.                 fputs("\tsub ax, buf1\n",f);
  1463.                 fputs("\tmov For1,ax\n",f);
  1464.                 fputs("\tmov cx,For1\n",f);
  1465.                 fprintf(f,"\n  Label_%d:\n",lab);
  1466.                 i++;
  1467.             }
  1468.         }
  1469.         if(l.type == next_) {
  1470.             i++;
  1471.         }
  1472.         if(l.type == label_) {
  1473.             fprintf(f,"\n loop Label_%d\n",lab);
  1474.         }
  1475.         if(l.type == symbol) {
  1476.             if(segment.tokens[i+2].type==minus) {
  1477.                 fprintf(f,"\tfild %s\n",segment.tokens[i+3].name);
  1478.                 fputs("\tFLDZ\n",f);
  1479.                 fputs("\tFSUBR\n",f);
  1480.                 fprintf(f,"\tfistp %s\n",segment.tokens[i].name);
  1481.                 i=i+2;
  1482.             }
  1483.  
  1484.             int bufi;
  1485.             bufi = i;
  1486.  
  1487.             i = infix_to_postfix(i+2);
  1488.  
  1489.             if(i<0) {
  1490.                 i = -i;
  1491.                 puts("IE\n");
  1492.                 continue;
  1493.             }
  1494.  
  1495.             generate_asm_code(segment.tokens[bufi].name,f);
  1496.         }
  1497.     }
  1498. }
  1499. void generate_asm_code(const char * str, FILE* f) {
  1500.     int n;
  1501.     for(n=0; segment.expr[n] != 3000; ++n) {
  1502.         s.Init(&s.S);
  1503.         if((!is_operation(segment.tokens[segment.expr[n]].type))&&(segment.tokens[segment.expr[n]].type != not_)) {
  1504.             if(segment.tokens[segment.expr[n]].type == symbol) {
  1505.                 fprintf(f,"\tfild %s\n",segment.tokens[segment.expr[n]].name);
  1506.             } else if(segment.tokens[segment.expr[n]].type == number) {
  1507.                 char bufHi[6], bufLo[5], buf[10];
  1508.  
  1509.                 buf[0] = '0';
  1510.                 sprintf(&buf[1],"%08x",segment.tokens[segment.expr[n]].value);
  1511.  
  1512.                 strncpy(bufHi,buf,5);
  1513.                 bufHi[5] = '\0';
  1514.  
  1515.                 strncpy(bufLo,&buf[5],4);
  1516.                 bufLo[4] = '\0';
  1517.  
  1518.                 fprintf(f,"\tmov word ptr buf,0%sh\n",bufLo);
  1519.                 fprintf(f,"\tmov word ptr buf+2,0%sh\n",bufHi);
  1520.                 fputs("\tfild buf\n",f);
  1521.             } else if((segment.tokens[segment.expr[n]].type == lparen)||(segment.tokens[segment.expr[n]].type == rparen)) {
  1522.                 continue;
  1523.             }
  1524.         } else {
  1525.             switch(segment.tokens[segment.expr[n]].type) {
  1526.             case add_:
  1527.                 fputs("\tfadd\n",f);
  1528.                 break;
  1529.             case sub_:
  1530.                 fputs("\tfsub\n",f);
  1531.                 break;
  1532.             case div_:
  1533.                 fputs("\tfdiv\n",f);
  1534.                 break;
  1535.             case mod_:
  1536.                 fputs("\tcall mod_\n",f);
  1537.                 break;
  1538.             case mul_:
  1539.                 fputs("\tfmul\n",f);
  1540.                 break;
  1541.  
  1542.             case and_:
  1543.                 fputs("\tcall and_\n",f);
  1544.                 break;
  1545.             case or_:
  1546.                 fputs("\tcall or_\n",f);
  1547.                 break;
  1548.             case not_:
  1549.                 fputs("\tcall not_\n",f);
  1550.                 break;
  1551.             case equal:
  1552.                 fputs("\tcall eq_\n",f);
  1553.                 break;
  1554.             case not_equal:
  1555.                 fputs("\tcall eq_\n",f);
  1556.                 fputs("\tcall not_\n",f);
  1557.                 break;
  1558.             case ng:
  1559.                 fputs("\tcall ng_\n",f);
  1560.                 break;
  1561.             case nl:
  1562.                 fputs("\tcall nl_\n",f);
  1563.                 break;
  1564.  
  1565.             }
  1566.         }
  1567.  
  1568.     }
  1569.     if(*str=='-') {
  1570.  
  1571.         goto f;
  1572.     }
  1573.     fprintf(f,"\tfistp %s\n",str);
  1574. f:
  1575.     ;
  1576. }
  1577. void print_mod_instructions(FILE* f) {
  1578.     fputs("\n;===Procedure mod_====================\n",f);
  1579.     fputs("\nmod_ PROC\n\n",f);
  1580.  
  1581.     fputs("\tpush ax\n",f);
  1582.     fputs("\tpush bx\n",f);
  1583.     fputs("\tpush cx\n",f);
  1584.     fputs("\tpush dx\n",f);
  1585.     fputs("\tpushf\n\n",f);
  1586.  
  1587.     fputs("\tmov word ptr buf,1h\n",f);
  1588.     fputs("\tmov word ptr buf + 2,0h\n",f);
  1589.     fputs("\tfistp lb1\n",f);
  1590.     fputs("\tfistp lb2\n",f);
  1591.  
  1592.     fputs("\tmov ax,word ptr lb2\n",f);
  1593.     fputs("\tmov bx,word ptr lb2 + 2\n",f);
  1594.  
  1595.     fputs("\tand bh,80h\n",f);
  1596.     fputs("\tjz dod1\n",f);
  1597.     fputs("\tfild lb2\n",f);
  1598.     fputs("\tfchs\n",f);
  1599.     fputs("\tfistp lb2\n",f);
  1600.     fputs("\tmov word ptr buf,0ffffh\n",f);
  1601.     fputs("\tmov word ptr buf + 2,0ffffh\n",f);
  1602.  
  1603.     fputs("\n  dod1:\n",f);
  1604.  
  1605.     fputs("\tmov ax,word ptr lb2\n",f);
  1606.     fputs("\tmov bx,word ptr lb2 + 2\n",f);
  1607.  
  1608.     fputs("\tmov cx,word ptr lb1\n",f);
  1609.     fputs("\tmov dx,word ptr lb1 + 2\n",f);
  1610.  
  1611.     fputs("\tand dh,80h\n",f);
  1612.     fputs("\tjz dod2\n",f);
  1613.     fputs("\tfild lb1\n",f);
  1614.     fputs("\tfchs\n",f);
  1615.     fputs("\tfistp lb1\n",f);
  1616.  
  1617.     fputs("\n  dod2:\n",f);
  1618.     fputs("\tmov cx,word ptr lb1\n",f);
  1619.     fputs("\tmov dx,word ptr lb1 + 2\n",f);
  1620.  
  1621.     fputs("\tcmp ax,cx\n",f);
  1622.     fputs("\tjne r\n",f);
  1623.     fputs("\tcmp bx,dx\n",f);
  1624.     fputs("\tjne r\n",f);
  1625.  
  1626.     fputs("\tmov word ptr lb1,0h\n",f);
  1627.     fputs("\tmov word ptr lb1 + 2,0h\n",f);
  1628.     fputs("\tjmp end_mod\n",f);
  1629.  
  1630.     fputs("\n  r:\n",f);
  1631.     fputs("\tcmp bx,dx\n",f);
  1632.     fputs("\tjg nb\n",f);
  1633.     fputs("\tjl b\n",f);
  1634.     fputs("\tcmp ax,cx\n",f);
  1635.     fputs("\tjb b\n",f);
  1636.     fputs("\n  nb:\n",f);
  1637.     fputs("\tsub ax,cx\n",f);
  1638.     fputs("\tsbb bx,dx\n",f);
  1639.     fputs("\tjmp r\n",f);
  1640.     fputs("\n  b:\n",f);
  1641.     fputs("\tmov word ptr lb1,ax\n",f);
  1642.     fputs("\tmov word ptr lb1 + 2,bx\n",f);
  1643.  
  1644.     fputs("\n  end_mod:\n",f);
  1645.     fputs("\tfild lb1\n",f);
  1646.     fputs("\tfimul buf\n\n",f);
  1647.  
  1648.     fputs("\tpopf\n",f);
  1649.     fputs("\tpop dx\n",f);
  1650.     fputs("\tpop cx\n",f);
  1651.     fputs("\tpop bx\n",f);
  1652.     fputs("\tpop ax\n",f);
  1653.  
  1654.     fputs("\tret\n",f);
  1655.     fputs("mod_ ENDP\n",f);
  1656.     fputs(";======================================\n\n",f);
  1657. }
  1658.  
  1659. void print_and_instructions(FILE* f) {
  1660.     fputs("\n;===Procedure and_====================\n",f);
  1661.     fputs("\nand_ PROC\n\n",f);
  1662.  
  1663.     fputs("\tpush ax\n",f);
  1664.     fputs("\tpush dx\n",f);
  1665.     fputs("\tpushf\n",f);
  1666.  
  1667.     fputs("\tfistp lb1\n",f);
  1668.     fputs("\tfistp lb2\n",f);
  1669.  
  1670.     fputs("\tmov ax,word ptr lb1\n",f);
  1671.     fputs("\tmov dx,word ptr lb1+2\n",f);
  1672.     fputs("\tcmp ax,0\n",f);
  1673.     fputs("\tjnz true_and1\n",f);
  1674.     fputs("\tcmp dx,0\n",f);
  1675.     fputs("\tjz false_and\n",f);
  1676.  
  1677.     fputs("\n  true_and1:\n",f);
  1678.     fputs("\tmov ax,word ptr lb2\n",f);
  1679.     fputs("\tmov dx,word ptr lb2+2\n",f);
  1680.     fputs("\tcmp ax,0\n",f);
  1681.     fputs("\tjnz true_and\n",f);
  1682.     fputs("\tcmp dx,0\n",f);
  1683.     fputs("\tjnz true_and\n",f);
  1684.  
  1685.     fputs("\n false_and:\n",f);
  1686.     fputs("\tfldz\n",f);
  1687.     fputs("\tjmp l_and\n",f);
  1688.  
  1689.     fputs("\n  true_and:\n",f);
  1690.     fputs("\tfld1\n",f);
  1691.  
  1692.     fputs("\n  l_and:\n\n",f);
  1693.  
  1694.     fputs("\tpopf\n",f);
  1695.     fputs("\tpop dx\n",f);
  1696.     fputs("\tpop ax\n\n",f);
  1697.  
  1698.     fputs("\tret\n",f);
  1699.     fputs("and_ ENDP\n",f);
  1700.     fputs(";======================================\n\n",f);
  1701. }
  1702.  
  1703. void print_or_instructions(FILE* f) {
  1704.     fputs("\n;===Procedure or_======================\n",f);
  1705.     fputs("\nor_ PROC\n\n",f);
  1706.  
  1707.     fputs("\tpush ax\n",f);
  1708.     fputs("\tpush dx\n",f);
  1709.     fputs("\tpushf\n",f);
  1710.  
  1711.     fputs("\tfistp lb1\n",f);
  1712.     fputs("\tfistp lb2\n",f);
  1713.  
  1714.     fputs("\tmov ax,word ptr lb1\n",f);
  1715.     fputs("\tmov dx,word ptr lb1+2\n",f);
  1716.     fputs("\tcmp ax,0\n",f);
  1717.     fputs("\tjnz true_or\n",f);
  1718.     fputs("\tcmp dx,0\n",f);
  1719.     fputs("\tjnz true_or\n",f);
  1720.  
  1721.     fputs("\tmov ax,word ptr lb2\n",f);
  1722.     fputs("\tmov dx,word ptr lb2+2\n",f);
  1723.     fputs("\tcmp ax,0\n",f);
  1724.     fputs("\tjnz true_or\n",f);
  1725.     fputs("\tcmp dx,0\n",f);
  1726.     fputs("\tjnz true_or\n",f);
  1727.  
  1728.     fputs("\tfldz\n",f);
  1729.     fputs("\tjmp l_or\n",f);
  1730.  
  1731.     fputs("\n  true_or:\n",f);
  1732.     fputs("\tfld1\n",f);
  1733.  
  1734.     fputs("\n  l_or:\n\n",f);
  1735.  
  1736.     fputs("\tpopf\n",f);
  1737.     fputs("\tpop dx\n",f);
  1738.     fputs("\tpop ax\n\n",f);
  1739.  
  1740.     fputs("\tret\n",f);
  1741.     fputs("or_ ENDP\n",f);
  1742.     fputs(";======================================\n\n",f);
  1743. }
  1744.  
  1745. void print_not_instructions(FILE* f) {
  1746.     fputs("\n;===Procedure not_====================\n",f);
  1747.     fputs("\nnot_ PROC\n\n",f);
  1748.  
  1749.     fputs("\tpush ax\n",f);
  1750.     fputs("\tpushf\n",f);
  1751.  
  1752.     fputs("\tfistp lb1\n",f);
  1753.  
  1754.     fputs("\tmov ax,word ptr lb1\n",f);
  1755.     fputs("\tcmp ax,0\n",f);
  1756.     fputs("\tjne is_true\n",f);
  1757.  
  1758.     fputs("\tmov ax,word ptr lb1+2\n",f);
  1759.     fputs("\tcmp ax,0\n",f);
  1760.     fputs("\tjne is_true\n",f);
  1761.  
  1762.     fputs("\tfld1\n",f);
  1763.     fputs("\tjmp l_not\n",f);
  1764.  
  1765.     fputs("\n  is_true:\n",f);
  1766.     fputs("\tfldz\n",f);
  1767.  
  1768.     fputs("\n  l_not:\n\n",f);
  1769.  
  1770.     fputs("\tpopf\n",f);
  1771.     fputs("\tpop ax\n\n",f);
  1772.  
  1773.     fputs("\tret\n",f);
  1774.     fputs("not_ ENDP\n",f);
  1775.     fputs(";======================================\n\n",f);
  1776. }
  1777.  
  1778. void print_eq_instructions(FILE* f) {
  1779.     fputs("\n;===Procedure eq_======================\n",f);
  1780.     fputs("\neq_ PROC\n\n",f);
  1781.  
  1782.     fputs("\tpush ax\n",f);
  1783.     fputs("\tpush bx\n",f);
  1784.     fputs("\tpush cx\n",f);
  1785.     fputs("\tpush dx\n",f);
  1786.     fputs("\tpushf\n",f);
  1787.  
  1788.     fputs("\tfistp lb1\n",f);
  1789.     fputs("\tfistp lb2\n",f);
  1790.     fputs("\tmov ax,word ptr lb1\n",f);
  1791.     fputs("\tmov bx,word ptr lb1+2\n",f);
  1792.     fputs("\tmov cx,word ptr lb2\n",f);
  1793.     fputs("\tmov dx,word ptr lb2+2\n",f);
  1794.  
  1795.     fputs("\tcmp ax,cx\n",f);
  1796.     fputs("\tjne not_eq\n",f);
  1797.  
  1798.     fputs("\tcmp bx,dx\n",f);
  1799.     fputs("\tjne not_eq\n",f);
  1800.  
  1801.     fputs("\tfld1\n",f);
  1802.     fputs("\tjmp l_eq\n",f);
  1803.  
  1804.     fputs("\n  not_eq:\n",f);
  1805.     fputs("\tfldz\n",f);
  1806.  
  1807.     fputs("\n  l_eq:\n",f);
  1808.  
  1809.     fputs("\tpopf\n",f);
  1810.     fputs("\tpop dx\n",f);
  1811.     fputs("\tpop cx\n",f);
  1812.     fputs("\tpop bx\n",f);
  1813.     fputs("\tpop ax\n\n",f);
  1814.  
  1815.     fputs("\tret\n",f);
  1816.     fputs("eq_ ENDP\n",f);
  1817.     fputs(";======================================\n\n",f);
  1818. }
  1819.  
  1820. void print_nl_instructions(FILE* f) {
  1821.     fputs("\n;===Procedure nl_======================\n",f);
  1822.     fputs("\nnl_ PROC\n\n",f);
  1823.  
  1824.     fputs("\tpush ax\n",f);
  1825.     fputs("\tpush bx\n",f);
  1826.     fputs("\tpush cx\n",f);
  1827.     fputs("\tpush dx\n",f);
  1828.     fputs("\tpushf\n",f);
  1829.  
  1830.     fputs("\tfistp lb1\n",f);
  1831.     fputs("\tfistp lb2\n",f);
  1832.  
  1833.     fputs("\tmov ax,word ptr lb1\n",f);
  1834.     fputs("\tmov bx,word ptr lb1+2\n",f);
  1835.     fputs("\tmov cx,word ptr lb2\n",f);
  1836.     fputs("\tmov dx,word ptr lb2+2\n",f);
  1837.  
  1838.     fputs("\tcmp dx,bx\n",f);
  1839.     fputs("\tjl lov\n",f);
  1840.     fputs("\tjg gre\n",f);
  1841.  
  1842.     fputs("\tcmp cx,ax\n",f);
  1843.     fputs("\tjb lov\n",f);
  1844.  
  1845.     fputs("\n  gre:\n",f);
  1846.     fputs("\tfld1\n",f);
  1847.     fputs("\tjmp l_nl\n",f);
  1848.  
  1849.     fputs("\n  lov:\n",f);
  1850.     fputs("\tfldz\n",f);
  1851.  
  1852.     fputs("\n  l_nl:\n",f);
  1853.  
  1854.     fputs("\tpopf\n",f);
  1855.     fputs("\tpop dx\n",f);
  1856.     fputs("\tpop cx\n",f);
  1857.     fputs("\tpop bx\n",f);
  1858.     fputs("\tpop ax\n\n",f);
  1859.  
  1860.     fputs("\tret\n",f);
  1861.     fputs("nl_ ENDP\n",f);
  1862.     fputs(";======================================\n\n",f);
  1863. }
  1864.  
  1865. void print_ng_instructions(FILE* f) {
  1866.     fputs("\n;===Procedure ng_======================\n",f);
  1867.     fputs("\nng_ PROC\n\n",f);
  1868.  
  1869.     fputs("\tpush ax\n",f);
  1870.     fputs("\tpush bx\n",f);
  1871.     fputs("\tpush cx\n",f);
  1872.     fputs("\tpush dx\n",f);
  1873.     fputs("\tpushf\n",f);
  1874.  
  1875.     fputs("\tfistp lb1\n",f);
  1876.     fputs("\tfistp lb2\n",f);
  1877.  
  1878.     fputs("\tmov ax,word ptr lb1\n",f);
  1879.     fputs("\tmov bx,word ptr lb1+2\n",f);
  1880.     fputs("\tmov cx,word ptr lb2\n",f);
  1881.     fputs("\tmov dx,word ptr lb2+2\n",f);
  1882.  
  1883.     fputs("\tcmp dx,bx\n",f);
  1884.     fputs("\tjg gr\n",f);
  1885.     fputs("\tjl lo\n",f);
  1886.  
  1887.     fputs("\tcmp cx,ax\n",f);
  1888.     fputs("\tja gr\n",f);
  1889.  
  1890.     fputs("\n  lo:\n",f);
  1891.     fputs("\tfld1\n",f);
  1892.     fputs("\tjmp l_ng\n",f);
  1893.  
  1894.     fputs("\n  gr:\n",f);
  1895.     fputs("\tfldz\n",f);
  1896.  
  1897.     fputs("\n  l_ng:\n",f);
  1898.  
  1899.     fputs("\tpopf\n",f);
  1900.     fputs("\tpop dx\n",f);
  1901.     fputs("\tpop cx\n",f);
  1902.     fputs("\tpop bx\n",f);
  1903.     fputs("\tpop ax\n\n",f);
  1904.  
  1905.     fputs("\tret\n",f);
  1906.     fputs("ng_ ENDP\n",f);
  1907.     fputs(";======================================\n\n",f);
  1908. }
  1909.  
  1910. void generate_code(FILE* f, char* in) {
  1911.     print_title(f,in);
  1912.     look_for_get_and_put();
  1913.     print_segment(f);
  1914.     print_initialization(f);
  1915.  
  1916.     print_code(f);
  1917.  
  1918.     print_ending(f);
  1919.  
  1920. }
  1921.  
  1922. int main(int argc, char* argv[]) {
  1923.     // if(argc != 2) {
  1924.     //     puts("Pomylka!!\nNekorektno vvedeni parametry");
  1925.     //     puts("\n\tS12 <file_name>.s138");
  1926.     //     return 0;
  1927.     // }
  1928.  
  1929.     strncpy(segment.input_file_name, "source.s128",50);
  1930.  
  1931.     input_file = fopen(segment.input_file_name,"r");
  1932.  
  1933.     if(input_file == NULL) {
  1934.         printf("Pomylka!!\nNe Vdalos\' vidkryty fajl %s\n", segment.input_file_name);
  1935.         return -1;
  1936.     }
  1937.  
  1938.     strncpy(segment.output_file_name,segment.input_file_name,50);
  1939.     strncpy(strchr(segment.output_file_name,'.'),".asm",50);
  1940.  
  1941.     puts("s138 Compiler");
  1942.     puts("Designed by Anton Stoyan \n");
  1943.  
  1944.     output_file = fopen(segment.output_file_name,"w");
  1945.     error_file = fopen("ErrorOut.txt","w");
  1946.  
  1947.     segment.Len = analyze(input_file);
  1948.  
  1949.     print_analyze();
  1950.  
  1951.     segment.errors = check_program(error_file);
  1952.  
  1953.     if(segment.errors != 0) {
  1954.          printf("Found %d errors\ncan\'t compile input program\nsee errors in ErrorFile.txt\n",segment.errors);
  1955.     } else {
  1956.          printf("Find no errors.\nOutput file assign: %s\n",segment.output_file_name);
  1957.          create_identifier_table(error_file);
  1958.          generate_code(output_file, segment.input_file_name);
  1959.     }
  1960.  
  1961.     fclose(input_file);
  1962.     fclose(output_file);
  1963.     fclose(error_file);
  1964.  
  1965.     return 0;
  1966. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement