comptut
changeset 4:ae1c30fa39f3
working on the control structures of tutorial5
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 16 Mar 2015 00:13:54 +0200 |
parents | f167a743f348 |
children | bebbe71ba949 |
files | src/main.c |
diffstat | 1 files changed, 108 insertions(+), 6 deletions(-) [+] |
line diff
1.1 --- a/src/main.c Sun Mar 15 13:48:04 2015 +0200 1.2 +++ b/src/main.c Mon Mar 16 00:13:54 2015 +0200 1.3 @@ -12,11 +12,16 @@ 1.4 }; 1.5 1.6 void init(void); 1.7 +void program(void); 1.8 +void block(void); 1.9 +void doif(void); 1.10 +void dowhile(void); 1.11 +void condition(void); 1.12 void expression(void); 1.13 void assignment(void); 1.14 void term(void); 1.15 void factor(void); 1.16 -void ident(); 1.17 +void ident(void); 1.18 void add(void); 1.19 void sub(void); 1.20 void mul(void); 1.21 @@ -33,6 +38,10 @@ 1.22 void emitln(const char *fmt, ...); 1.23 int is_addsub(char c); 1.24 int add_symbol(char c, int type); 1.25 +int newlabel(void); 1.26 +const char *labelstr(int lb); 1.27 +void postlabel(int lb); 1.28 +void other(void); 1.29 1.30 FILE *infile, *outfile; 1.31 char look; 1.32 @@ -60,10 +69,7 @@ 1.33 /* output prologue */ 1.34 fputs(prologue, outfile); 1.35 1.36 - assignment(); 1.37 - if(look != '\n') { 1.38 - expected("newline"); 1.39 - } 1.40 + program(); 1.41 1.42 /* output epilogue */ 1.43 fputs(epilogue, outfile); 1.44 @@ -86,6 +92,84 @@ 1.45 skip_white(); 1.46 } 1.47 1.48 +void program(void) 1.49 +{ 1.50 + block(); 1.51 + if(look != 'e') { 1.52 + expected("End"); 1.53 + } 1.54 + emitln("# END"); 1.55 +} 1.56 + 1.57 +void other(void) 1.58 +{ 1.59 + emitln("%c", get_name()); 1.60 +} 1.61 + 1.62 +void block(void) 1.63 +{ 1.64 + while(look != 'e' && look != 'l') { 1.65 + switch(look) { 1.66 + case 'i': 1.67 + doif(); 1.68 + break; 1.69 + 1.70 + case 'w': 1.71 + dowhile(); 1.72 + break; 1.73 + 1.74 + default: 1.75 + other(); 1.76 + break; 1.77 + } 1.78 + } 1.79 +} 1.80 + 1.81 +void doif(void) 1.82 +{ 1.83 + int lb1, lb2; 1.84 + 1.85 + match('i'); 1.86 + condition(); 1.87 + 1.88 + lb1 = newlabel(); 1.89 + lb2 = lb1; 1.90 + 1.91 + emitln("jz %s", labelstr(lb1)); 1.92 + block(); 1.93 + 1.94 + if(look == 'l') { 1.95 + match('l'); 1.96 + lb2 = newlabel(); 1.97 + emitln("jmp %s", labelstr(lb2)); 1.98 + postlabel(lb1); 1.99 + block(); 1.100 + } 1.101 + match('e'); 1.102 + postlabel(lb2); 1.103 +} 1.104 + 1.105 +void dowhile(void) 1.106 +{ 1.107 + int lbtop, lbend; 1.108 + 1.109 + match('w'); 1.110 + lbtop = newlabel(); 1.111 + lbend = newlabel(); 1.112 + postlabel(lbtop); 1.113 + condition(); 1.114 + emitln("jz %s", labelstr(lbend)); 1.115 + block(); 1.116 + match('e'); 1.117 + emitln("jmp %s", labelstr(lbtop)); 1.118 + postlabel(lbend); 1.119 +} 1.120 + 1.121 +void condition(void) 1.122 +{ 1.123 + emitln("<condition>"); 1.124 +} 1.125 + 1.126 void expression(void) 1.127 { 1.128 if(is_addsub(look)) { 1.129 @@ -149,7 +233,7 @@ 1.130 } 1.131 } 1.132 1.133 -void ident() 1.134 +void ident(void) 1.135 { 1.136 char name = get_name(); 1.137 if(look == '(') { 1.138 @@ -320,3 +404,21 @@ 1.139 symlist = sym; 1.140 return 0; 1.141 } 1.142 + 1.143 +int newlabel(void) 1.144 +{ 1.145 + static int n; 1.146 + return n++; 1.147 +} 1.148 + 1.149 +const char *labelstr(int lb) 1.150 +{ 1.151 + static char buf[16]; 1.152 + sprintf(buf, "L%02d", lb); 1.153 + return buf; 1.154 +} 1.155 + 1.156 +void postlabel(int lb) 1.157 +{ 1.158 + fprintf(outfile, "%s:\n", labelstr(lb)); 1.159 +}