Year 2 compilers coureswork

main.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. BIND* node = recursive_interpret(tree->right, env_ptr);
  185. if ((existing = find_name_in_env(name_token, env_ptr)) == NULL) {
  186. if (env_ptr->bindings == NULL) {
  187. env_ptr->bindings = create_list(create_binding(name_token, node->tree, node->env), NULL);
  188. } else {
  189. append_list(env_ptr->bindings, create_binding(name_token, node->tree, node->env));
  190. }
  191. } else {
  192. existing->tree = node->tree;
  193. }
  194. }
  195. TOKEN* get_int_token(int value) {
  196. TOKEN* res;
  197. char buffer[20];
  198. snprintf(buffer, 20, "%d", value);
  199. res = lookup_token(buffer);
  200. if (res->type != CONSTANT) res->type = CONSTANT;
  201. if (res->value != value) res->value = value;
  202. return res;
  203. }
  204. BIND* anon_binding(NODE *tree) {
  205. return create_binding(NULL, tree, NULL);
  206. }
  207. BIND* ret_val;
  208. int returning;
  209. BIND* recursive_interpret(NODE *tree, ENV *env_ptr) {
  210. if (returning == 1) return NULL;
  211. if (tree==NULL) return NULL;
  212. if (tree->type==LEAF) {
  213. if (tree->left->type == CONSTANT) {
  214. return anon_binding(tree->left);
  215. } else if (tree->left->type == STRING_LITERAL) {
  216. printf("Not implemented\n");
  217. exit(1);
  218. } else if (tree->left->type == INT || tree->left->type == FUNCTION) {
  219. // do nothing we dont care about types for now
  220. return NULL;
  221. } else {
  222. // an identifier
  223. TOKEN* tok = (TOKEN *) tree->left;
  224. BIND* var_bind = find_name_in_env(tok, env_ptr);
  225. if (debug) {
  226. printf("looking up %s\n", tok->lexeme);
  227. }
  228. if (var_bind == NULL) {
  229. printf("Could not find variable %s\n", tok->lexeme);
  230. exit(1);
  231. }
  232. if(debug)printf("I got: ");
  233. if (var_bind->tree->type == CONSTANT) {
  234. if(debug){
  235. print_leaf(var_bind->tree, 0);
  236. }
  237. } else {
  238. if(debug)print_tree(var_bind->tree);
  239. }
  240. return var_bind;
  241. }
  242. }
  243. if (tree->type=='D') {
  244. // this is a function definition
  245. add_function_to_env(tree, env_ptr);
  246. return NULL;
  247. }
  248. if (tree->type=='~') {
  249. if (tree->right->type == '=') {
  250. printf("ASSIGNING TYPE:\n");
  251. print_tree(tree->left);
  252. add_var_to_env(tree->right, env_ptr);
  253. return NULL;
  254. } else {
  255. recursive_interpret(tree->left, env_ptr);
  256. return recursive_interpret(tree->right, env_ptr);
  257. }
  258. }
  259. if (tree->type=='=') {
  260. // this is a variable definition
  261. add_var_to_env(tree, env_ptr);
  262. return NULL;
  263. }
  264. if (tree->type==APPLY) {
  265. BIND* func;
  266. TOKEN* func_name;
  267. if (tree->left->type == APPLY) {
  268. func = recursive_interpret(tree->left, env_ptr);
  269. func_name = lookup_token("$anonFunc");
  270. } else {
  271. func_name = ((TOKEN*) tree->left->left);
  272. func = find_name_in_env(func_name, env_ptr);
  273. }
  274. if (func == NULL) {
  275. printf("Could not find binding for function with name %s\n", func_name->lexeme);
  276. exit(1);
  277. }
  278. if (func->tree->type != 'D') {
  279. printf("Tried to call non-function %s as a function\n", func_name->lexeme);
  280. exit(1);
  281. }
  282. ENV* new_func_env = create_new_function_env(func->env);
  283. if (debug) printf("Calling function %s\n", func_name->lexeme);
  284. if (tree->right != NULL) {
  285. TOKEN **arg_list;
  286. NODE **val_list;
  287. int exp_args, num_args;
  288. exp_args = count_args(func->tree->left->right->right);
  289. num_args = count_args(tree->right);
  290. arg_list = get_argument_list(func->tree);
  291. val_list = get_val_list(tree->right, env_ptr);
  292. if (exp_args == num_args) {
  293. for (int i = 0; i < num_args; i++) {
  294. bind_arg(val_list[i], arg_list[i], new_func_env, env_ptr);
  295. }
  296. } else {
  297. printf("Incorrect arguments passed to function %s, expected %d got %d\n", func_name->lexeme, exp_args, num_args);
  298. exit(1);
  299. }
  300. }
  301. recursive_interpret(func->tree->right, new_func_env);
  302. if (returning == 1) {
  303. returning = 0;
  304. return ret_val;
  305. } else {
  306. printf("HELP called a function with no return!!\n");
  307. exit(1);
  308. }
  309. }
  310. if (tree->type == IF) {
  311. if (((TOKEN*)recursive_interpret(tree->left, env_ptr)->tree)->value == 1) {
  312. ENV* scope_env = create_new_function_env(env_ptr);
  313. if (tree->right->type == ELSE) {
  314. return recursive_interpret(tree->right->left, scope_env);
  315. } else {
  316. return recursive_interpret(tree->right, scope_env);
  317. }
  318. } else {
  319. if (tree->right->type == ELSE) {
  320. ENV* scope_env = create_new_function_env(env_ptr);
  321. return recursive_interpret(tree->right->right, scope_env);
  322. }
  323. return 0;
  324. }
  325. }
  326. if (tree->type == LE_OP) {
  327. TOKEN* lhs = (TOKEN*) recursive_interpret(tree->left, env_ptr)->tree;
  328. TOKEN* rhs = (TOKEN*) recursive_interpret(tree->right, env_ptr)->tree;
  329. int result = lhs->value <= rhs->value;
  330. return anon_binding((NODE*) get_int_token(result));
  331. }
  332. if (tree->type == GE_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 == EQ_OP) {
  339. TOKEN* lhs = (TOKEN*) recursive_interpret(tree->left, env_ptr)->tree;
  340. TOKEN* rhs = (TOKEN*) recursive_interpret(tree->right, env_ptr)->tree;
  341. if (debug) printf("Comparing %d == %d\n", lhs->value, rhs->value);
  342. int result = lhs->value == rhs->value;
  343. return anon_binding((NODE*) get_int_token(result));
  344. }
  345. if (tree->type == NE_OP) {
  346. TOKEN* lhs = (TOKEN*) recursive_interpret(tree->left, env_ptr)->tree;
  347. TOKEN* rhs = (TOKEN*) recursive_interpret(tree->right, env_ptr)->tree;
  348. int result = lhs->value != rhs->value;
  349. return anon_binding((NODE*) get_int_token(result));
  350. }
  351. if (tree->type == '>') {
  352. TOKEN* lhs = (TOKEN*) recursive_interpret(tree->left, env_ptr)->tree;
  353. TOKEN* rhs = (TOKEN*) recursive_interpret(tree->right, env_ptr)->tree;
  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. if (debug) printf("Adding %d and %d\n", lhs->value, rhs->value);
  367. int result = lhs->value + rhs->value;
  368. return anon_binding((NODE*) get_int_token(result));
  369. }
  370. if (tree->type == '-') {
  371. TOKEN* lhs = (TOKEN*) recursive_interpret(tree->left, env_ptr)->tree;
  372. TOKEN* rhs = (TOKEN*) recursive_interpret(tree->right, env_ptr)->tree;
  373. int result = lhs->value - rhs->value;
  374. return anon_binding((NODE*) get_int_token(result));
  375. }
  376. if (tree->type == '*') {
  377. TOKEN* lhs = (TOKEN*) recursive_interpret(tree->left, env_ptr)->tree;
  378. TOKEN* rhs = (TOKEN*) recursive_interpret(tree->right, env_ptr)->tree;
  379. int result = lhs->value * rhs->value;
  380. return anon_binding((NODE*) get_int_token(result));
  381. }
  382. if (tree->type == '/') {
  383. TOKEN* lhs = (TOKEN*) recursive_interpret(tree->left, env_ptr)->tree;
  384. TOKEN* rhs = (TOKEN*) recursive_interpret(tree->right, env_ptr)->tree;
  385. int result = lhs->value / rhs->value;
  386. return anon_binding((NODE*) get_int_token(result));
  387. }
  388. if (tree->type == '%') {
  389. TOKEN* lhs = (TOKEN*) recursive_interpret(tree->left, env_ptr)->tree;
  390. TOKEN* rhs = (TOKEN*) recursive_interpret(tree->right, env_ptr)->tree;
  391. int result = lhs->value % rhs->value;
  392. return anon_binding((NODE*) get_int_token(result));
  393. }
  394. if (tree->type == '~') {
  395. if (tree->left->type == INT || tree->left->type == FUNCTION) {
  396. return recursive_interpret(tree->right, env_ptr);
  397. } else {
  398. recursive_interpret(tree->left, env_ptr);
  399. return recursive_interpret(tree->right, env_ptr);
  400. }
  401. }
  402. if (tree->type == RETURN) {
  403. ret_val = recursive_interpret(tree->left, env_ptr);
  404. returning = 1;
  405. }
  406. if (tree->type == WHILE) {
  407. ENV* scope_env = create_new_function_env(env_ptr);
  408. while(((TOKEN*)recursive_interpret(tree->left, env_ptr)->tree)->value == 1) {
  409. recursive_interpret(tree->right, scope_env);
  410. }
  411. return 0;
  412. }
  413. recursive_interpret(tree->left, env_ptr);
  414. return recursive_interpret(tree->right, env_ptr);
  415. }
  416. ENV* cons_global_env(NODE *tree) {
  417. ENV* global = create_new_function_env(NULL);
  418. recursive_interpret(tree, global);
  419. return global;
  420. }
  421. void interpret_tree(NODE *tree) {
  422. ENV* global_env = cons_global_env(tree);
  423. BIND* ref_main = find_name_in_env(lookup_token("main"), global_env);
  424. if (ref_main == NULL) {
  425. printf("Could not find main, cannot run!\n");
  426. exit(1);
  427. }
  428. // print ref_main to make sure we really got it
  429. printf("Located %s, ready to run!\n", ref_main->name->lexeme);
  430. ENV* main_env = create_new_function_env(ref_main->env);
  431. recursive_interpret(ref_main->tree->right, main_env);
  432. printf("%d\n", ((TOKEN*)ret_val->tree)->value);
  433. }
  434. extern int yydebug;
  435. extern NODE* yyparse(void);
  436. extern NODE* ans;
  437. extern void init_symbtable(void);
  438. int main(int argc, char** argv)
  439. {
  440. NODE* tree;
  441. if (argc>1 && strcmp(argv[1],"-d")==0) debug = 1;
  442. init_symbtable();
  443. printf("--C COMPILER\n");
  444. yyparse();
  445. tree = ans;
  446. printf("parse finished\n");
  447. print_tree(tree);
  448. interpret_tree(tree);
  449. return 0;
  450. }