Year 2 compilers coureswork

main.c 11KB

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