Year 2 compilers coureswork

main.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include "nodes.h"
  6. #include "C.tab.h"
  7. #include "types.h"
  8. #include "list.h"
  9. TOKEN* lookup_token(char *s);
  10. int asprintf(char **strp, const char *fmt, ...);
  11. int debug = 0;
  12. /*
  13. * Existing problems/todo:
  14. * return from control block, need a "return address" to jump to -- call levels
  15. * --multiple assignments `int x,y,z = 1`--
  16. * new block scope for if statements?
  17. * arguments for functions
  18. * difference between tilde and semicolon
  19. * while loops
  20. * control flow `continue` and `break` linked to return address sort of? call levels
  21. */
  22. char *named(int t) {
  23. static char b[100];
  24. if (isgraph(t) || t==' ') {
  25. sprintf(b, "%c", t);
  26. return b;
  27. }
  28. switch (t) {
  29. default: return "???";
  30. case IDENTIFIER:
  31. return "id";
  32. case CONSTANT:
  33. return "constant";
  34. case STRING_LITERAL:
  35. return "string";
  36. case LE_OP:
  37. return "<=";
  38. case GE_OP:
  39. return ">=";
  40. case EQ_OP:
  41. return "==";
  42. case NE_OP:
  43. return "!=";
  44. case EXTERN:
  45. return "extern";
  46. case AUTO:
  47. return "auto";
  48. case INT:
  49. return "int";
  50. case VOID:
  51. return "void";
  52. case APPLY:
  53. return "apply";
  54. case LEAF:
  55. return "leaf";
  56. case IF:
  57. return "if";
  58. case ELSE:
  59. return "else";
  60. case WHILE:
  61. return "while";
  62. case CONTINUE:
  63. return "continue";
  64. case BREAK:
  65. return "break";
  66. case RETURN:
  67. return "return";
  68. }
  69. }
  70. void print_leaf(NODE *tree, int level) {
  71. TOKEN *t = (TOKEN *)tree;
  72. int i;
  73. for (i=0; i<level; i++) putchar(' ');
  74. if (t->type == CONSTANT) printf("%d\n", t->value);
  75. else if (t->type == STRING_LITERAL) printf("\"%s\"\n", t->lexeme);
  76. else if (t) puts(t->lexeme);
  77. }
  78. void print_tree0(NODE *tree, int level) {
  79. int i;
  80. if (tree==NULL) return;
  81. if (tree->type==LEAF) {
  82. print_leaf(tree->left, level);
  83. }
  84. else {
  85. for(i=0; i<level; i++) putchar(' ');
  86. printf("%s\n", named(tree->type));
  87. /* if (tree->type=='~') { */
  88. /* for(i=0; i<level+2; i++) putchar(' '); */
  89. /* printf("%p\n", tree->left); */
  90. /* } */
  91. /* else */
  92. print_tree0(tree->left, level+2);
  93. print_tree0(tree->right, level+2);
  94. }
  95. }
  96. // forward declare because it is used in add_var_to_env
  97. BIND* recursive_interpret(NODE*, ENV*);
  98. void print_tree(NODE *tree) {
  99. print_tree0(tree, 0);
  100. }
  101. int count_args(NODE *tree) {
  102. NODE *tmp = tree;
  103. int a = 1;
  104. while (tmp != NULL) {
  105. if (tmp->type != ',') {
  106. break;
  107. }
  108. tmp = tmp->left;
  109. a++;
  110. }
  111. return a;
  112. }
  113. void populate_arg_list(NODE *tree, TOKEN** arr, int arr_len) {
  114. if (tree->type == '~') {
  115. for (int i = 0; i < arr_len; i++) {
  116. if (arr[i] == NULL) {
  117. TOKEN* name = (TOKEN*)tree->right->left;
  118. arr[i] = name;
  119. break;
  120. }
  121. }
  122. } else {
  123. populate_arg_list(tree->left, arr, arr_len);
  124. populate_arg_list(tree->right, arr, arr_len);
  125. }
  126. }
  127. TOKEN** get_argument_list(NODE *tree) {
  128. int num_args = count_args(tree->left->right->right);
  129. TOKEN** arr = (TOKEN**) malloc(sizeof(TOKEN*) * num_args);
  130. populate_arg_list(tree->left->right->right, arr, num_args);
  131. return arr;
  132. }
  133. void add_function_to_env(NODE *tree, ENV *env_ptr) {
  134. BIND* existing;
  135. NODE* func_name = tree->left->right->left->left;
  136. TOKEN* name_token = (TOKEN*) func_name;
  137. if ((existing = find_name_in_env(name_token, env_ptr)) == NULL) {
  138. if (env_ptr->bindings == NULL) {
  139. env_ptr->bindings = create_list(create_binding(name_token, tree, env_ptr), NULL);
  140. } else {
  141. append_list(env_ptr->bindings, create_binding(name_token, tree, env_ptr));
  142. }
  143. } else {
  144. printf("Error: redefinition of function with name %s\n", name_token->lexeme);
  145. }
  146. }
  147. void populate_val_list(NODE* tree, NODE** arr, int num_args, ENV* env_ptr) {
  148. if (tree->type == ',') {
  149. populate_val_list(tree->left, arr, num_args, env_ptr);
  150. populate_val_list(tree->right, arr, num_args, env_ptr);
  151. } else {
  152. for (int i = 0; i < num_args; i++) {
  153. if (arr[i] == NULL) {
  154. arr[i] = tree;
  155. break;
  156. }
  157. }
  158. }
  159. }
  160. NODE** get_val_list(NODE *tree, ENV* env_ptr) {
  161. int num_args = count_args(tree);
  162. NODE** arr = (NODE**) malloc(sizeof(NODE*) * num_args);
  163. populate_val_list(tree, arr, num_args, env_ptr);
  164. return arr;
  165. }
  166. void bind_arg(NODE* val, TOKEN* arg_name, ENV *env_ptr, ENV* src_env) {
  167. BIND* result = recursive_interpret(val, src_env);
  168. if(debug)printf("Binding argument: %s\n", arg_name->lexeme);
  169. if (env_ptr->bindings == NULL) {
  170. env_ptr->bindings = create_list(create_binding(arg_name, result->tree, result->env), NULL);
  171. } else {
  172. BIND* existing = find_name_in_list(arg_name, env_ptr->bindings);
  173. if (existing) {
  174. existing->tree = result->tree;
  175. } else {
  176. append_list(env_ptr->bindings, create_binding(arg_name, result->tree, result->env));
  177. }
  178. }
  179. }
  180. void add_var_to_env(NODE *tree, ENV *env_ptr) {
  181. BIND* existing;
  182. NODE* var_name = tree->left->left;
  183. TOKEN* name_token = (TOKEN*) var_name;
  184. if (debug) printf("assigning %s:\n", name_token->lexeme);
  185. BIND* node = recursive_interpret(tree->right, env_ptr);
  186. if ((existing = find_name_in_env(name_token, env_ptr)) == NULL) {
  187. if (env_ptr->bindings == NULL) {
  188. env_ptr->bindings = create_list(create_binding(name_token, node->tree, node->env), NULL);
  189. } else {
  190. append_list(env_ptr->bindings, create_binding(name_token, node->tree, node->env));
  191. }
  192. } else {
  193. existing->tree = node->tree;
  194. }
  195. }
  196. TOKEN* get_int_token(int value) {
  197. TOKEN* res;
  198. char buffer[20];
  199. snprintf(buffer, 20, "%d", value);
  200. res = lookup_token(buffer);
  201. if (res->type != CONSTANT) res->type = CONSTANT;
  202. if (res->value != value) res->value = value;
  203. return res;
  204. }
  205. BIND* anon_binding(NODE *tree) {
  206. return create_binding(NULL, tree, NULL);
  207. }
  208. BIND* ret_val;
  209. int returning;
  210. BIND* recursive_interpret(NODE *tree, ENV *env_ptr) {
  211. if (returning == 1) return NULL;
  212. if (tree==NULL) return NULL;
  213. if (tree->type==LEAF) {
  214. if (tree->left->type == CONSTANT) {
  215. return anon_binding(tree->left);
  216. } else if (tree->left->type == STRING_LITERAL) {
  217. printf("Not implemented\n");
  218. exit(1);
  219. } else if (tree->left->type == INT || tree->left->type == FUNCTION) {
  220. // do nothing we dont care about types for now
  221. return NULL;
  222. } else {
  223. // an identifier
  224. TOKEN* tok = (TOKEN *) tree->left;
  225. BIND* var_bind = find_name_in_env(tok, env_ptr);
  226. if (debug) {
  227. printf("looking up %s\n", tok->lexeme);
  228. }
  229. if (var_bind == NULL) {
  230. printf("Could not find variable %s\n", tok->lexeme);
  231. exit(1);
  232. }
  233. if(debug)printf("I got: ");
  234. if (var_bind->tree->type == CONSTANT) {
  235. if(debug){
  236. printf("CONSTANT\n");
  237. print_leaf(var_bind->tree, 0);
  238. }
  239. } else {
  240. if(debug)print_tree(var_bind->tree);
  241. }
  242. return var_bind;
  243. /* if (var_bind->tree->type == CONSTANT) { */
  244. /* return var_bind->tree; */
  245. /* } else { */
  246. /* print_tree(var_bind->tree); */
  247. /* printf("I GOT A %d from %s\n", var_bind->tree->type, var_bind->name->lexeme); */
  248. /* printf("Maybe got a function?\n"); */
  249. /* return NULL; */
  250. /* } */
  251. }
  252. }
  253. if (tree->type=='D') {
  254. // this is a function definition
  255. add_function_to_env(tree, env_ptr);
  256. return NULL;
  257. }
  258. if (tree->type=='=') {
  259. // this is a variable definition
  260. add_var_to_env(tree, env_ptr);
  261. return NULL;
  262. }
  263. if (tree->type==APPLY) {
  264. TOKEN* func_name = ((TOKEN*) tree->left->left);
  265. BIND* func = find_name_in_env(func_name, env_ptr);
  266. if (func == NULL) {
  267. printf("Could not find binding for function with name %s\n", func_name->lexeme);
  268. exit(1);
  269. }
  270. ENV* new_func_env = create_new_function_env(func->env);
  271. if (debug) printf("Calling function %s\n", func_name->lexeme);
  272. if (tree->right != NULL) {
  273. TOKEN **arg_list;
  274. NODE **val_list;
  275. int exp_args, num_args;
  276. exp_args = count_args(func->tree->left->right->right);
  277. num_args = count_args(tree->right);
  278. arg_list = get_argument_list(func->tree);
  279. val_list = get_val_list(tree->right, env_ptr);
  280. if (exp_args == num_args) {
  281. for (int i = 0; i < num_args; i++) {
  282. bind_arg(val_list[i], arg_list[i], new_func_env, env_ptr);
  283. }
  284. } else {
  285. printf("Incorrect arguments passed to function %s, expected %d got %d\n", func_name->lexeme, exp_args, num_args);
  286. exit(1);
  287. }
  288. }
  289. recursive_interpret(func->tree->right, new_func_env);
  290. if (returning == 1) {
  291. returning = 0;
  292. return ret_val;
  293. } else {
  294. printf("HELP called a function with no return!!\n");
  295. }
  296. }
  297. if (tree->type == IF) {
  298. if (((TOKEN*)recursive_interpret(tree->left, env_ptr)->tree)->value == 1) {
  299. ENV* scope_env = create_new_function_env(env_ptr);
  300. if (tree->right->type == ELSE) {
  301. return recursive_interpret(tree->right->left, scope_env);
  302. } else {
  303. return recursive_interpret(tree->right, scope_env);
  304. }
  305. } else {
  306. if (tree->right->type == ELSE) {
  307. ENV* scope_env = create_new_function_env(env_ptr);
  308. return recursive_interpret(tree->right->right, scope_env);
  309. }
  310. return 0;
  311. }
  312. }
  313. if (tree->type == LE_OP) {
  314. TOKEN* lhs = (TOKEN*) recursive_interpret(tree->left, env_ptr)->tree;
  315. TOKEN* rhs = (TOKEN*) recursive_interpret(tree->right, env_ptr)->tree;
  316. int result = lhs->value <= rhs->value;
  317. return anon_binding((NODE*) get_int_token(result));
  318. }
  319. if (tree->type == GE_OP) {
  320. TOKEN* lhs = (TOKEN*) recursive_interpret(tree->left, env_ptr)->tree;
  321. TOKEN* rhs = (TOKEN*) recursive_interpret(tree->right, env_ptr)->tree;
  322. int result = lhs->value >= rhs->value;
  323. return anon_binding((NODE*) get_int_token(result));
  324. }
  325. if (tree->type == EQ_OP) {
  326. TOKEN* lhs = (TOKEN*) recursive_interpret(tree->left, env_ptr)->tree;
  327. TOKEN* rhs = (TOKEN*) recursive_interpret(tree->right, env_ptr)->tree;
  328. if (debug) printf("Comparing %d == %d\n", lhs->value, rhs->value);
  329. int result = lhs->value == rhs->value;
  330. return anon_binding((NODE*) get_int_token(result));
  331. }
  332. if (tree->type == NE_OP) {
  333. TOKEN* lhs = (TOKEN*) recursive_interpret(tree->left, env_ptr)->tree;
  334. TOKEN* rhs = (TOKEN*) recursive_interpret(tree->right, env_ptr)->tree;
  335. int result = lhs->value != rhs->value;
  336. return anon_binding((NODE*) get_int_token(result));
  337. }
  338. if (tree->type == '>') {
  339. TOKEN* lhs = (TOKEN*) recursive_interpret(tree->left, env_ptr)->tree;
  340. TOKEN* rhs = (TOKEN*) recursive_interpret(tree->right, env_ptr)->tree;
  341. int result = lhs->value > rhs->value;
  342. return anon_binding((NODE*) get_int_token(result));
  343. }
  344. if (tree->type == '<') {
  345. TOKEN* lhs = (TOKEN*) recursive_interpret(tree->left, env_ptr)->tree;
  346. TOKEN* rhs = (TOKEN*) recursive_interpret(tree->right, env_ptr)->tree;
  347. int result = lhs->value < rhs->value;
  348. return anon_binding((NODE*) get_int_token(result));
  349. }
  350. if (tree->type == '+') {
  351. TOKEN* lhs = (TOKEN*) recursive_interpret(tree->left, env_ptr)->tree;
  352. TOKEN* rhs = (TOKEN*) recursive_interpret(tree->right, env_ptr)->tree;
  353. if (debug) printf("Adding %d and %d\n", lhs->value, rhs->value);
  354. int result = lhs->value + rhs->value;
  355. return anon_binding((NODE*) get_int_token(result));
  356. }
  357. if (tree->type == '-') {
  358. TOKEN* lhs = (TOKEN*) recursive_interpret(tree->left, env_ptr)->tree;
  359. TOKEN* rhs = (TOKEN*) recursive_interpret(tree->right, env_ptr)->tree;
  360. int result = lhs->value - rhs->value;
  361. return anon_binding((NODE*) get_int_token(result));
  362. }
  363. if (tree->type == '*') {
  364. TOKEN* lhs = (TOKEN*) recursive_interpret(tree->left, env_ptr)->tree;
  365. TOKEN* rhs = (TOKEN*) recursive_interpret(tree->right, env_ptr)->tree;
  366. int result = lhs->value * rhs->value;
  367. return anon_binding((NODE*) get_int_token(result));
  368. }
  369. if (tree->type == '/') {
  370. TOKEN* lhs = (TOKEN*) recursive_interpret(tree->left, env_ptr)->tree;
  371. TOKEN* rhs = (TOKEN*) recursive_interpret(tree->right, env_ptr)->tree;
  372. int result = lhs->value / rhs->value;
  373. return anon_binding((NODE*) get_int_token(result));
  374. }
  375. if (tree->type == '%') {
  376. TOKEN* lhs = (TOKEN*) recursive_interpret(tree->left, env_ptr)->tree;
  377. TOKEN* rhs = (TOKEN*) recursive_interpret(tree->right, env_ptr)->tree;
  378. int result = lhs->value % rhs->value;
  379. return anon_binding((NODE*) get_int_token(result));
  380. }
  381. if (tree->type == '~') {
  382. if (tree->left->type == INT || tree->left->type == FUNCTION) {
  383. return recursive_interpret(tree->right, env_ptr);
  384. } else {
  385. recursive_interpret(tree->left, env_ptr);
  386. return recursive_interpret(tree->right, env_ptr);
  387. }
  388. }
  389. if (tree->type == RETURN) {
  390. ret_val = recursive_interpret(tree->left, env_ptr);
  391. returning = 1;
  392. }
  393. if (tree->type == WHILE) {
  394. ENV* scope_env = create_new_function_env(env_ptr);
  395. while(((TOKEN*)recursive_interpret(tree->left, env_ptr)->tree)->value == 1) {
  396. recursive_interpret(tree->right, scope_env);
  397. }
  398. return 0;
  399. }
  400. recursive_interpret(tree->left, env_ptr);
  401. return recursive_interpret(tree->right, env_ptr);
  402. }
  403. ENV* cons_global_env(NODE *tree) {
  404. ENV* global = create_new_function_env(NULL);
  405. recursive_interpret(tree, global);
  406. return global;
  407. }
  408. void interpret_tree(NODE *tree) {
  409. ENV* global_env = cons_global_env(tree);
  410. BIND* ref_main = find_name_in_env(lookup_token("main"), global_env);
  411. if (ref_main == NULL) {
  412. printf("Could not find main, cannot run!\n");
  413. exit(1);
  414. }
  415. // print ref_main to make sure we really got it
  416. printf("Located %s, ready to run!\n", ref_main->name->lexeme);
  417. recursive_interpret(ref_main->tree->right, ref_main->env);
  418. /* printf("%d\n", ((TOKEN*)recursive_interpret(ref_main->tree->right, ref_main->env))->value); */
  419. printf("%d\n", ((TOKEN*)ret_val->tree)->value);
  420. }
  421. extern int yydebug;
  422. extern NODE* yyparse(void);
  423. extern NODE* ans;
  424. extern void init_symbtable(void);
  425. int main(int argc, char** argv)
  426. {
  427. NODE* tree;
  428. if (argc>1 && strcmp(argv[1],"-d")==0) debug = 1;
  429. init_symbtable();
  430. printf("--C COMPILER\n");
  431. yyparse();
  432. tree = ans;
  433. printf("parse finished\n");
  434. print_tree(tree);
  435. interpret_tree(tree);
  436. return 0;
  437. }