Year 2 compilers coureswork

main.c 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. /*
  11. * Existing problems/todo:
  12. * return from control block, need a "return address" to jump to -- call levels
  13. * --multiple assignments `int x,y,z = 1`--
  14. * new block scope for if statements?
  15. * arguments for functions
  16. * difference between tilde and semicolon
  17. * while loops
  18. * control flow `continue` and `break` linked to return address sort of? call levels
  19. */
  20. char *named(int t) {
  21. static char b[100];
  22. if (isgraph(t) || t==' ') {
  23. sprintf(b, "%c", t);
  24. return b;
  25. }
  26. switch (t) {
  27. default: return "???";
  28. case IDENTIFIER:
  29. return "id";
  30. case CONSTANT:
  31. return "constant";
  32. case STRING_LITERAL:
  33. return "string";
  34. case LE_OP:
  35. return "<=";
  36. case GE_OP:
  37. return ">=";
  38. case EQ_OP:
  39. return "==";
  40. case NE_OP:
  41. return "!=";
  42. case EXTERN:
  43. return "extern";
  44. case AUTO:
  45. return "auto";
  46. case INT:
  47. return "int";
  48. case VOID:
  49. return "void";
  50. case APPLY:
  51. return "apply";
  52. case LEAF:
  53. return "leaf";
  54. case IF:
  55. return "if";
  56. case ELSE:
  57. return "else";
  58. case WHILE:
  59. return "while";
  60. case CONTINUE:
  61. return "continue";
  62. case BREAK:
  63. return "break";
  64. case RETURN:
  65. return "return";
  66. }
  67. }
  68. void print_leaf(NODE *tree, int level) {
  69. TOKEN *t = (TOKEN *)tree;
  70. int i;
  71. for (i=0; i<level; i++) putchar(' ');
  72. if (t->type == CONSTANT) printf("%d\n", t->value);
  73. else if (t->type == STRING_LITERAL) printf("\"%s\"\n", t->lexeme);
  74. else if (t) puts(t->lexeme);
  75. }
  76. void print_tree0(NODE *tree, int level) {
  77. int i;
  78. if (tree==NULL) return;
  79. if (tree->type==LEAF) {
  80. print_leaf(tree->left, level);
  81. }
  82. else {
  83. for(i=0; i<level; i++) putchar(' ');
  84. printf("%s\n", named(tree->type));
  85. /* if (tree->type=='~') { */
  86. /* for(i=0; i<level+2; i++) putchar(' '); */
  87. /* printf("%p\n", tree->left); */
  88. /* } */
  89. /* else */
  90. print_tree0(tree->left, level+2);
  91. print_tree0(tree->right, level+2);
  92. }
  93. }
  94. // forward declare because it is used in add_var_to_env
  95. int recursive_interpret(NODE*, ENV*);
  96. void print_tree(NODE *tree) {
  97. print_tree0(tree, 0);
  98. }
  99. void add_function_to_env(NODE *tree, ENV *env_ptr) {
  100. BIND* existing;
  101. NODE* func_name = tree->left->right->left->left;
  102. TOKEN* name_token = (TOKEN*) func_name;
  103. if ((existing = find_name_in_env(name_token, env_ptr)) == NULL) {
  104. printf("Added function name %s to environment with value: \n", name_token->lexeme);
  105. print_tree(tree);
  106. if (env_ptr->bindings == NULL) {
  107. env_ptr->bindings = create_list(create_binding(name_token, tree, env_ptr), NULL);
  108. } else {
  109. append_list(env_ptr->bindings, create_binding(name_token, tree, env_ptr));
  110. }
  111. } else {
  112. printf("Updating function name %s with value: \n", name_token->lexeme);
  113. print_tree(tree);
  114. existing->tree = tree;
  115. existing->env = env_ptr;
  116. }
  117. }
  118. void add_var_to_env(NODE *tree, ENV *env_ptr) {
  119. BIND* existing;
  120. NODE* var_name = tree->left->left;
  121. TOKEN* name_token = (TOKEN*) var_name;
  122. TOKEN* tok = new_token(INT);
  123. tok->value = recursive_interpret(tree->right, env_ptr);
  124. asprintf(&tok->lexeme, "%d", tok->value);
  125. if ((existing = find_name_in_env(name_token, env_ptr)) == NULL) {
  126. printf("Added variable name %s to environment with value: %s\n", name_token->lexeme, tok->lexeme);
  127. if (env_ptr->bindings == NULL) {
  128. env_ptr->bindings = create_list(create_binding(name_token, (NODE*) tok, NULL), NULL);
  129. } else {
  130. append_list(env_ptr->bindings, create_binding(name_token, (NODE*) tok, NULL));
  131. }
  132. } else {
  133. printf("Updating variable name %s with value: %s\n", name_token->lexeme, tok->lexeme);
  134. existing->tree = (NODE *) tok;
  135. }
  136. }
  137. int recursive_interpret(NODE *tree, ENV *env_ptr) {
  138. if (tree==NULL) return 0;
  139. if (tree->type==LEAF) {
  140. if (tree->left->type == CONSTANT) {
  141. return ((TOKEN *) tree->left)->value;
  142. } else if (tree->left->type == STRING_LITERAL) {
  143. printf("Not implemented\n");
  144. exit(1);
  145. } else if (tree->left->type == INT || tree->left->type == FUNCTION) {
  146. // do nothing we dont care about types
  147. return 0;
  148. } else {
  149. // an identifier
  150. TOKEN* tok = (TOKEN *) tree->left;
  151. BIND* var_bind = find_name_in_env(tok, env_ptr);
  152. if (var_bind == NULL) {
  153. printf("Could not find variable %s\n", tok->lexeme);
  154. exit(1);
  155. }
  156. if (var_bind->tree->type == INT) {
  157. TOKEN* var_tok = (TOKEN *) var_bind->tree;
  158. return var_tok->value;
  159. } else {
  160. printf("Maybe got a function?\n");
  161. return 0;
  162. }
  163. }
  164. }
  165. if (tree->type=='D') {
  166. // this is a function definition
  167. add_function_to_env(tree, env_ptr);
  168. return 0;
  169. }
  170. if (tree->type=='=') {
  171. // this is a variable definition
  172. add_var_to_env(tree, env_ptr);
  173. return 0;
  174. }
  175. if (tree->type==APPLY) {
  176. TOKEN* func_name = ((TOKEN*) tree->left->left);
  177. BIND* func = find_name_in_env(func_name, env_ptr);
  178. if (func == NULL) {
  179. printf("Could not find binding for function with name %s\n", func_name->lexeme);
  180. exit(1);
  181. }
  182. return recursive_interpret(func->tree->right, func->env);
  183. }
  184. if (tree->type == IF) {
  185. if (recursive_interpret(tree->left, env_ptr) == 1) {
  186. if (tree->right->type == ELSE) {
  187. return recursive_interpret(tree->right->left, env_ptr);
  188. } else {
  189. return recursive_interpret(tree->right, env_ptr);
  190. }
  191. } else {
  192. if (tree->right->type == ELSE) {
  193. return recursive_interpret(tree->right->right, env_ptr);
  194. }
  195. return 0;
  196. }
  197. }
  198. if (tree->type == LE_OP) {
  199. return (int)recursive_interpret(tree->left, env_ptr) <= recursive_interpret(tree->right, env_ptr);
  200. }
  201. if (tree->type == GE_OP) {
  202. return (int)recursive_interpret(tree->left, env_ptr) >= recursive_interpret(tree->right, env_ptr);
  203. }
  204. if (tree->type == EQ_OP) {
  205. return (int)recursive_interpret(tree->left, env_ptr) == recursive_interpret(tree->right, env_ptr);
  206. }
  207. if (tree->type == NE_OP) {
  208. return (int)recursive_interpret(tree->left, env_ptr) != recursive_interpret(tree->right, env_ptr);
  209. }
  210. if (tree->type == '>') {
  211. return (int)recursive_interpret(tree->left, env_ptr) > recursive_interpret(tree->right, env_ptr);
  212. }
  213. if (tree->type == '<') {
  214. return (int)recursive_interpret(tree->left, env_ptr) < recursive_interpret(tree->right, env_ptr);
  215. }
  216. if (tree->type == '+') {
  217. return recursive_interpret(tree->left, env_ptr) + recursive_interpret(tree->right, env_ptr);
  218. }
  219. if (tree->type == '-') {
  220. return recursive_interpret(tree->left, env_ptr) - recursive_interpret(tree->right, env_ptr);
  221. }
  222. if (tree->type == '*') {
  223. return recursive_interpret(tree->left, env_ptr) * recursive_interpret(tree->right, env_ptr);
  224. }
  225. if (tree->type == '/') {
  226. return recursive_interpret(tree->left, env_ptr) / recursive_interpret(tree->right, env_ptr);
  227. }
  228. if (tree->type == '%') {
  229. return recursive_interpret(tree->left, env_ptr) % recursive_interpret(tree->right, env_ptr);
  230. }
  231. if (tree->type == '~') {
  232. if (tree->left->type == INT || tree->left->type == FUNCTION) {
  233. return recursive_interpret(tree->right, env_ptr);
  234. } else {
  235. recursive_interpret(tree->left, env_ptr);
  236. return recursive_interpret(tree->right, env_ptr);
  237. }
  238. }
  239. if (tree->type == RETURN) {
  240. return recursive_interpret(tree->left, env_ptr);
  241. }
  242. recursive_interpret(tree->left, env_ptr);
  243. return recursive_interpret(tree->right, env_ptr);
  244. }
  245. ENV* cons_global_env(NODE *tree) {
  246. ENV* global = create_new_function_env(NULL);
  247. recursive_interpret(tree, global);
  248. return global;
  249. }
  250. void interpret_tree(NODE *tree) {
  251. ENV* global_env = cons_global_env(tree);
  252. BIND* ref_main = find_name_in_env(lookup_token("main"), global_env);
  253. if (ref_main == NULL) {
  254. printf("Could not find main, cannot run!\n");
  255. exit(1);
  256. }
  257. // print ref_main to make sure we really got it
  258. printf("Located %s, ready to run!\n", ref_main->name->lexeme);
  259. printf("%d\n", recursive_interpret(ref_main->tree->right, ref_main->env));
  260. }
  261. extern int yydebug;
  262. extern NODE* yyparse(void);
  263. extern NODE* ans;
  264. extern void init_symbtable(void);
  265. int main(int argc, char** argv)
  266. {
  267. NODE* tree;
  268. if (argc>1 && strcmp(argv[1],"-d")==0) yydebug = 1;
  269. init_symbtable();
  270. printf("--C COMPILER\n");
  271. yyparse();
  272. tree = ans;
  273. printf("parse finished\n");
  274. print_tree(tree);
  275. interpret_tree(tree);
  276. return 0;
  277. }