comptut

changeset 5:bebbe71ba949 tutor5

mostly completed tutor5.txt, without the hundred different looping constructs, which I don't care about
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 16 Mar 2015 01:06:49 +0200
parents ae1c30fa39f3
children 098d02ac0319
files src/main.c
diffstat 1 files changed, 23 insertions(+), 9 deletions(-) [+]
line diff
     1.1 --- a/src/main.c	Mon Mar 16 00:13:54 2015 +0200
     1.2 +++ b/src/main.c	Mon Mar 16 01:06:49 2015 +0200
     1.3 @@ -13,9 +13,10 @@
     1.4  
     1.5  void init(void);
     1.6  void program(void);
     1.7 -void block(void);
     1.8 -void doif(void);
     1.9 +void block(int brklb);
    1.10 +void doif(int brklb);
    1.11  void dowhile(void);
    1.12 +void dobreak(int brklb);
    1.13  void condition(void);
    1.14  void expression(void);
    1.15  void assignment(void);
    1.16 @@ -94,7 +95,7 @@
    1.17  
    1.18  void program(void)
    1.19  {
    1.20 -	block();
    1.21 +	block(-1);
    1.22  	if(look != 'e') {
    1.23  		expected("End");
    1.24  	}
    1.25 @@ -106,18 +107,22 @@
    1.26  	emitln("%c", get_name());
    1.27  }
    1.28  
    1.29 -void block(void)
    1.30 +void block(int brklb)
    1.31  {
    1.32  	while(look != 'e' && look != 'l') {
    1.33  		switch(look) {
    1.34  		case 'i':
    1.35 -			doif();
    1.36 +			doif(brklb);
    1.37  			break;
    1.38  
    1.39  		case 'w':
    1.40  			dowhile();
    1.41  			break;
    1.42  
    1.43 +		case 'b':
    1.44 +			dobreak(brklb);
    1.45 +			break;
    1.46 +
    1.47  		default:
    1.48  			other();
    1.49  			break;
    1.50 @@ -125,7 +130,7 @@
    1.51  	}
    1.52  }
    1.53  
    1.54 -void doif(void)
    1.55 +void doif(int brklb)
    1.56  {
    1.57  	int lb1, lb2;
    1.58  
    1.59 @@ -136,14 +141,14 @@
    1.60  	lb2 = lb1;
    1.61  
    1.62  	emitln("jz %s", labelstr(lb1));
    1.63 -	block();
    1.64 +	block(brklb);
    1.65  
    1.66  	if(look == 'l') {
    1.67  		match('l');
    1.68  		lb2 = newlabel();
    1.69  		emitln("jmp %s", labelstr(lb2));
    1.70  		postlabel(lb1);
    1.71 -		block();
    1.72 +		block(brklb);
    1.73  	}
    1.74  	match('e');
    1.75  	postlabel(lb2);
    1.76 @@ -159,12 +164,21 @@
    1.77  	postlabel(lbtop);
    1.78  	condition();
    1.79  	emitln("jz %s", labelstr(lbend));
    1.80 -	block();
    1.81 +	block(lbend);
    1.82  	match('e');
    1.83  	emitln("jmp %s", labelstr(lbtop));
    1.84  	postlabel(lbend);
    1.85  }
    1.86  
    1.87 +void dobreak(int brklb)
    1.88 +{
    1.89 +	match('b');
    1.90 +	if(brklb == -1) {
    1.91 +		stop("invalid break, not in loop");
    1.92 +	}
    1.93 +	emitln("jmp %s", labelstr(brklb));
    1.94 +}
    1.95 +
    1.96  void condition(void)
    1.97  {
    1.98  	emitln("<condition>");