虎书chap01编程练习
时间:2010-10-10 来源:Faircoala
main.c:
1 #include <stdio.h>
2 #include "util.h"
3 #include "slp.h"
4
5 typedef struct table *Table_;
6 struct table {string id; int value; Table_ tail;};
7
8 Table_ Table(string id, int value, struct table *tail);
9 Table_ interpStm(A_stm s, Table_ t);
10
11 struct IntAndTable {int i; Table_ t;};
12 struct IntAndTable interpExp(A_exp e, Table_ t);
13
14 Table_ update(Table_, string, int);
15 int lookup(Table_, string);
16
17 int maxargs(A_stm stm);
18 void interp(A_stm stm);
19
20 int main() {
21 int n = maxargs(prog());
22 printf("maxargs: %d\n", n);
23
24 interp(prog());
25
26 return 0;
27 }
28
29 int maxargs(A_stm s) {
30 if(s->kind == A_compoundStm) {
31 A_stm stm1,stm2;
32
33 stm1 = s->u.compound.stm1;
34 stm2 = s->u.compound.stm2;
35
36 return maxargs(stm1) + maxargs(stm2);
37 } else if(s->kind == A_printStm) {
38 int cnt = 1;
39 A_expList es;
40 es = s->u.print.exps;
41 while(es->kind != A_lastExpList) {
42 A_exp e = es->u.pair.head;
43 if(e->kind == A_eseqExp) {
44 A_stm s = e->u.eseq.stm;
45 cnt += maxargs(s);
46 }
47 es = es->u.pair.tail;
48 }
49
50 return cnt;
51 } else {
52 A_exp e = s->u.assign.exp;
53 if(e->kind == A_eseqExp) {
54 A_stm s = e->u.eseq.stm;
55 return maxargs(s);
56 }
57
58 return 0;
59 }
60 }
61
62 void interp(A_stm stm) {
63 interpStm(stm, NULL);
64 }
65
66 Table_ Table(string id, int value, struct table *tail) {
67 Table_ t = checked_malloc(sizeof(*t));
68 t->id = id;
69 t->value = value;
70 t->tail = tail;
71
72 return t;
73 }
74
75 Table_ interpStm(A_stm s, Table_ t) {
76 struct IntAndTable iat;
77
78 if(s->kind == A_compoundStm) {
79 return interpStm(s->u.compound.stm2, \
80 interpStm(s->u.compound.stm1, t));
81 } else if(s->kind == A_assignStm) {
82 // interpExp and update table.
83 iat = interpExp(s->u.assign.exp, t);
84 iat.t = update(iat.t, s->u.assign.id, iat.i);
85
86 return iat.t;
87 } else {
88 // interpExp and print.
89 A_expList exps = s->u.print.exps;
90 while(exps->kind != A_lastExpList) {
91 iat = interpExp(exps->u.pair.head, t);
92 printf("%d ", iat.i);
93
94 exps = exps->u.pair.tail;
95 t = iat.t;
96 }
97 iat = interpExp(exps->u.last, t);
98 printf("%d\n", iat.i);
99
100 return iat.t;
101 }
102 }
103
104 struct IntAndTable interpExp(A_exp e, Table_ t) {
105 struct IntAndTable iat;
106 struct IntAndTable iat1,iat2;
107 iat.i = 0;
108 iat.t = NULL;
109
110 switch(e->kind) {
111 case A_idExp:
112 iat.i = lookup(t, e->u.id);
113 iat.t = t;
114 break;
115 case A_numExp:
116 iat.i = e->u.num;
117 iat.t = t;
118 break;
119 case A_opExp:
120 iat1 = interpExp(e->u.op.left, t);
121 iat2 = interpExp(e->u.op.right, iat1.t);
122 switch(e->u.op.oper) {
123 case A_plus:
124 iat.i = iat1.i + iat2.i;
125 break;
126 case A_minus:
127 iat.i = iat1.i - iat2.i;
128 break;
129 case A_times:
130 iat.i = iat1.i * iat2.i;
131 break;
132 case A_div:
133 iat.i = iat1.i / iat2.i;
134 break;
135 }
136 iat.t = iat2.t;
137 break;
138 case A_eseqExp:
139 iat = interpExp(e->u.eseq.exp, t);
140 iat.t = interpStm(e->u.eseq.stm, iat.t);
141 break;
142 default:
143 break;
144 }
145
146 return iat;
147 }
148
149 Table_ update(Table_ t, string id, int value) {
150 return Table(id, value, t);
151 }
152
153 int lookup(Table_ t, string id) {
154 while(t != NULL) {
155 if(t->id == id)
156 return t->value;
157 t = t->tail;
158 }
159
160 return 0;
161 }
试试SyntaxHighlighter效果:
#include <stdio.h> #include "util.h" #include "slp.h" typedef struct table *Table_; struct table {string id; int value; Table_ tail;}; Table_ Table(string id, int value, struct table *tail); Table_ interpStm(A_stm s, Table_ t); struct IntAndTable {int i; Table_ t;}; struct IntAndTable interpExp(A_exp e, Table_ t); Table_ update(Table_, string, int); int lookup(Table_, string); int maxargs(A_stm stm); void interp(A_stm stm); int main() { int n = maxargs(prog()); printf("maxargs: %d\n", n); interp(prog()); return 0; } int maxargs(A_stm s) { if(s->kind == A_compoundStm) { A_stm stm1,stm2; stm1 = s->u.compound.stm1; stm2 = s->u.compound.stm2; return maxargs(stm1) + maxargs(stm2); } else if(s->kind == A_printStm) { int cnt = 1; A_expList es; es = s->u.print.exps; while(es->kind != A_lastExpList) { A_exp e = es->u.pair.head; if(e->kind == A_eseqExp) { A_stm s = e->u.eseq.stm; cnt += maxargs(s); } es = es->u.pair.tail; } return cnt; } else { A_exp e = s->u.assign.exp; if(e->kind == A_eseqExp) { A_stm s = e->u.eseq.stm; return maxargs(s); } return 0; } } void interp(A_stm stm) { interpStm(stm, NULL); } Table_ Table(string id, int value, struct table *tail) { Table_ t = checked_malloc(sizeof(*t)); t->id = id; t->value = value; t->tail = tail; return t; } Table_ interpStm(A_stm s, Table_ t) { struct IntAndTable iat; if(s->kind == A_compoundStm) { return interpStm(s->u.compound.stm2, \ interpStm(s->u.compound.stm1, t)); } else if(s->kind == A_assignStm) { // interpExp and update table. iat = interpExp(s->u.assign.exp, t); iat.t = update(iat.t, s->u.assign.id, iat.i); return iat.t; } else { // interpExp and print. A_expList exps = s->u.print.exps; while(exps->kind != A_lastExpList) { iat = interpExp(exps->u.pair.head, t); printf("%d ", iat.i); exps = exps->u.pair.tail; t = iat.t; } iat = interpExp(exps->u.last, t); printf("%d\n", iat.i); return iat.t; } } struct IntAndTable interpExp(A_exp e, Table_ t) { struct IntAndTable iat; struct IntAndTable iat1,iat2; iat.i = 0; iat.t = NULL; switch(e->kind) { case A_idExp: iat.i = lookup(t, e->u.id); iat.t = t; break; case A_numExp: iat.i = e->u.num; iat.t = t; break; case A_opExp: iat1 = interpExp(e->u.op.left, t); iat2 = interpExp(e->u.op.right, iat1.t); switch(e->u.op.oper) { case A_plus: iat.i = iat1.i + iat2.i; break; case A_minus: iat.i = iat1.i - iat2.i; break; case A_times: iat.i = iat1.i * iat2.i; break; case A_div: iat.i = iat1.i / iat2.i; break; } iat.t = iat2.t; break; case A_eseqExp: iat = interpExp(e->u.eseq.exp, t); iat.t = interpStm(e->u.eseq.stm, iat.t); break; default: break; } return iat; } Table_ update(Table_ t, string id, int value) { return Table(id, value, t); } int lookup(Table_ t, string id) { while(t != NULL) { if(t->id == id) return t->value; t = t->tail; } return 0; }
相关阅读 更多 +