Year 2 compilers coureswork

main.c 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. void add_function_to_env(NODE *tree, ENV *env_ptr) {
  101. BIND* existing;
  102. NODE* func_name = tree->left->right->left->left;
  103. TOKEN* name_token = (TOKEN*) func_name;
  104. if (tree->left->right->right == NULL) {
  105. printf("%s has no arguments\n", name_token->lexeme);
  106. }
  107. if ((existing = find_name_in_env(name_token, env_ptr)) == NULL) {
  108. /* printf("Added function name %s to environment with value: \n", name_token->lexeme); */
  109. /* print_tree(tree); */
  110. if (env_ptr->bindings == NULL) {
  111. env_ptr->bindings = create_list(create_binding(name_token, tree, env_ptr), NULL);
  112. } else {
  113. append_list(env_ptr->bindings, create_binding(name_token, tree, env_ptr));
  114. }
  115. } else {
  116. printf("Error: redefinition of function with name %s\n", name_token->lexeme);
  117. }
  118. }
  119. void add_var_to_env(NODE *tree, ENV *env_ptr) {
  120. BIND* existing;
  121. NODE* var_name = tree->left->left;
  122. TOKEN* name_token = (TOKEN*) var_name;
  123. TOKEN* tok = new_token(INT);
  124. tok->value = recursive_interpret(tree->right, env_ptr);
  125. asprintf(&tok->lexeme, "%d", tok->value);
  126. if ((existing = find_name_in_env(name_token, env_ptr)) == NULL) {
  127. /* printf("Added variable name %s to environment with value: %s\n", name_token->lexeme, tok->lexeme); */
  128. if (env_ptr->bindings == NULL) {
  129. env_ptr->bindings = create_list(create_binding(name_token, (NODE*) tok, NULL), NULL);
  130. } else {
  131. append_list(env_ptr->bindings, create_binding(name_token, (NODE*) tok, NULL));
  132. }
  133. } else {
  134. /* printf("Updating variable name %s with value: %s\n", name_token->lexeme, tok->lexeme); */
  135. existing->tree = (NODE *) tok;
  136. }
  137. }
  138. int recursive_interpret(NODE *tree, ENV *env_ptr) {
  139. if (tree==NULL) return 0;
  140. if (tree->type==LEAF) {
  141. if (tree->left->type == CONSTANT) {
  142. return ((TOKEN *) tree->left)->value;
  143. } else if (tree->left->type == STRING_LITERAL) {
  144. printf("Not implemented\n");
  145. exit(1);
  146. } else if (tree->left->type == INT || tree->left->type == FUNCTION) {
  147. // do nothing we dont care about types for now
  148. return 0;
  149. } else {
  150. // an identifier
  151. TOKEN* tok = (TOKEN *) tree->left;
  152. BIND* var_bind = find_name_in_env(tok, env_ptr);
  153. if (var_bind == NULL) {
  154. printf("Could not find variable %s\n", tok->lexeme);
  155. exit(1);
  156. }
  157. if (var_bind->tree->type == INT) {
  158. TOKEN* var_tok = (TOKEN *) var_bind->tree;
  159. return var_tok->value;
  160. } else {
  161. printf("Maybe got a function?\n");
  162. return 0;
  163. }
  164. }
  165. }
  166. if (tree->type=='D') {
  167. // this is a function definition
  168. add_function_to_env(tree, env_ptr);
  169. return 0;
  170. }
  171. if (tree->type=='=') {
  172. // this is a variable definition
  173. add_var_to_env(tree, env_ptr);
  174. return 0;
  175. }
  176. if (tree->type==APPLY) {
  177. TOKEN* func_name = ((TOKEN*) tree->left->left);
  178. BIND* func = find_name_in_env(func_name, env_ptr);
  179. if (func == NULL) {
  180. printf("Could not find binding for function with name %s\n", func_name->lexeme);
  181. exit(1);
  182. }
  183. return recursive_interpret(func->tree->right, func->env);
  184. }
  185. if (tree->type == IF) {
  186. if (recursive_interpret(tree->left, env_ptr) == 1) {
  187. if (tree->right->type == ELSE) {
  188. return recursive_interpret(tree->right->left, env_ptr);
  189. } else {
  190. return recursive_interpret(tree->right, env_ptr);
  191. }
  192. } else {
  193. if (tree->right->type == ELSE) {
  194. return recursive_interpret(tree->right->right, env_ptr);
  195. }
  196. return 0;
  197. }
  198. }
  199. if (tree->type == LE_OP) {
  200. return (int)recursive_interpret(tree->left, env_ptr) <= recursive_interpret(tree->right, env_ptr);
  201. }
  202. if (tree->type == GE_OP) {
  203. return (int)recursive_interpret(tree->left, env_ptr) >= recursive_interpret(tree->right, env_ptr);
  204. }
  205. if (tree->type == EQ_OP) {
  206. return (int)recursive_interpret(tree->left, env_ptr) == recursive_interpret(tree->right, env_ptr);
  207. }
  208. if (tree->type == NE_OP) {
  209. return (int)recursive_interpret(tree->left, env_ptr) != recursive_interpret(tree->right, env_ptr);
  210. }
  211. if (tree->type == '>') {
  212. return (int)recursive_interpret(tree->left, env_ptr) > recursive_interpret(tree->right, env_ptr);
  213. }
  214. if (tree->type == '<') {
  215. return (int)recursive_interpret(tree->left, env_ptr) < recursive_interpret(tree->right, env_ptr);
  216. }
  217. if (tree->type == '+') {
  218. return recursive_interpret(tree->left, env_ptr) + recursive_interpret(tree->right, env_ptr);
  219. }
  220. if (tree->type == '-') {
  221. return recursive_interpret(tree->left, env_ptr) - recursive_interpret(tree->right, env_ptr);
  222. }
  223. if (tree->type == '*') {
  224. return recursive_interpret(tree->left, env_ptr) * recursive_interpret(tree->right, env_ptr);
  225. }
  226. if (tree->type == '/') {
  227. return recursive_interpret(tree->left, env_ptr) / recursive_interpret(tree->right, env_ptr);
  228. }
  229. if (tree->type == '%') {
  230. return recursive_interpret(tree->left, env_ptr) % recursive_interpret(tree->right, env_ptr);
  231. }
  232. if (tree->type == '~') {
  233. if (tree->left->type == INT || tree->left->type == FUNCTION) {
  234. return recursive_interpret(tree->right, env_ptr);
  235. } else {
  236. recursive_interpret(tree->left, env_ptr);
  237. return recursive_interpret(tree->right, env_ptr);
  238. }
  239. }
  240. if (tree->type == RETURN) {
  241. return recursive_interpret(tree->left, env_ptr);
  242. }
  243. if (tree->type == WHILE) {
  244. while (recursive_interpret(tree->left, env_ptr)) {
  245. recursive_interpret(tree->right, env_ptr);
  246. }
  247. return 0;
  248. }
  249. recursive_interpret(tree->left, env_ptr);
  250. return recursive_interpret(tree->right, env_ptr);
  251. }
  252. ENV* cons_global_env(NODE *tree) {
  253. ENV* global = create_new_function_env(NULL);
  254. recursive_interpret(tree, global);
  255. return global;
  256. }
  257. void interpret_tree(NODE *tree) {
  258. ENV* global_env = cons_global_env(tree);
  259. BIND* ref_main = find_name_in_env(lookup_token("main"), global_env);
  260. if (ref_main == NULL) {
  261. printf("Could not find main, cannot run!\n");
  262. exit(1);
  263. }
  264. // print ref_main to make sure we really got it
  265. printf("Located %s, ready to run!\n", ref_main->name->lexeme);
  266. printf("%d\n", recursive_interpret(ref_main->tree->right, ref_main->env));
  267. }
  268. extern int yydebug;
  269. extern NODE* yyparse(void);
  270. extern NODE* ans;
  271. extern void init_symbtable(void);
  272. int main(int argc, char** argv)
  273. {
  274. NODE* tree;
  275. if (argc>1 && strcmp(argv[1],"-d")==0) yydebug = 1;
  276. init_symbtable();
  277. printf("--C COMPILER\n");
  278. yyparse();
  279. tree = ans;
  280. printf("parse finished\n");
  281. print_tree(tree);
  282. interpret_tree(tree);
  283. return 0;
  284. }