| 1 | #include "r.h" |
| 2 | |
| 3 | int swlevel = -1; |
| 4 | int swexit[5]; |
| 5 | int nextcase[5]; |
| 6 | |
| 7 | swcode() { |
| 8 | transfer = 0; |
| 9 | putcom("switch"); |
| 10 | swlevel++; |
| 11 | if (swlevel >= 5) |
| 12 | error("Switches nested > 5"); |
| 13 | swexit[swlevel] = yyval = genlab(1); |
| 14 | outcode("\tI"); |
| 15 | outnum(yyval); |
| 16 | outcode(" = "); |
| 17 | balpar(); |
| 18 | outdon(); |
| 19 | nextcase[swlevel] = 0; |
| 20 | indent++; |
| 21 | } |
| 22 | |
| 23 | getcase() { |
| 24 | int t, lpar; |
| 25 | char token[100]; |
| 26 | |
| 27 | if (nextcase[swlevel] != 0) { |
| 28 | outgoto(swexit[swlevel]); |
| 29 | outcont(nextcase[swlevel]); |
| 30 | } |
| 31 | indent--; |
| 32 | outcode("\tif(.not.("); |
| 33 | do { |
| 34 | outcode("I"); |
| 35 | outnum(swexit[swlevel]); |
| 36 | outcode(".eq.("); |
| 37 | lpar = 0; |
| 38 | do { |
| 39 | if ((t=gtok(token)) == ':') |
| 40 | break; |
| 41 | if (t == '(') |
| 42 | lpar++; |
| 43 | else if (t == ')') |
| 44 | lpar--; |
| 45 | else if (t == ',') { |
| 46 | if (lpar == 0) |
| 47 | break; |
| 48 | } |
| 49 | outcode(token); |
| 50 | } while (lpar >= 0); |
| 51 | if (lpar < 0) |
| 52 | error("Missing left parenthesis in case"); |
| 53 | if (t == ',') |
| 54 | outcode(").or."); |
| 55 | } while (t != ':'); |
| 56 | if (lpar != 0) |
| 57 | error("Missing parenthesis in case"); |
| 58 | outcode(")))"); |
| 59 | nextcase[swlevel] = genlab(1); |
| 60 | outgoto(nextcase[swlevel]); |
| 61 | indent++; |
| 62 | } |
| 63 | |
| 64 | getdefault() { |
| 65 | char token[20]; |
| 66 | if (gnbtok(token) != ':') |
| 67 | error("Missing colon after default"); |
| 68 | outgoto(swexit[swlevel]); |
| 69 | outcont(nextcase[swlevel]); |
| 70 | indent--; |
| 71 | putcom("default"); |
| 72 | indent++; |
| 73 | } |
| 74 | |
| 75 | endsw(n, def) { |
| 76 | if (def == 0) |
| 77 | outcont(nextcase[swlevel]); |
| 78 | swlevel--; |
| 79 | if (swlevel < -1) |
| 80 | error("Switches unwound too far"); |
| 81 | indent--; |
| 82 | outcont(n); |
| 83 | } |