Matt Coles před 8 roky
rodič
revize
4927f524ee
7 změnil soubory, kde provedl 119 přidání a 14 odebrání
  1. 16 9
      cw/main.c
  2. 15 5
      cw/simple.c
  3. 25 0
      cw/tests/cond_loop.c
  4. 9 0
      cw/tests/func_call.c
  5. 3 0
      cw/tests/return_only.c
  6. 46 0
      cw/tests/test.sh
  7. 5 0
      cw/tests/var_maths.c

+ 16 - 9
cw/main.c

8
 #include "list.h"
8
 #include "list.h"
9
 
9
 
10
 TOKEN* lookup_token(char *s);
10
 TOKEN* lookup_token(char *s);
11
+int asprintf(char **strp, const char *fmt, ...);
11
 
12
 
12
 /*
13
 /*
13
  * Existing problems/todo:
14
  * Existing problems/todo:
111
   BIND* existing;
112
   BIND* existing;
112
   NODE* func_name = tree->left->right->left->left;
113
   NODE* func_name = tree->left->right->left->left;
113
   TOKEN* name_token = (TOKEN*) func_name;
114
   TOKEN* name_token = (TOKEN*) func_name;
115
+  if (tree->left->right->right == NULL) {
116
+    printf("%s has no arguments\n", name_token->lexeme);
117
+  }
114
   if ((existing = find_name_in_env(name_token, env_ptr)) == NULL) {
118
   if ((existing = find_name_in_env(name_token, env_ptr)) == NULL) {
115
-    printf("Added function name %s to environment with value: \n", name_token->lexeme);
116
-    print_tree(tree);
119
+    /* printf("Added function name %s to environment with value: \n", name_token->lexeme); */
120
+    /* print_tree(tree); */
117
     if (env_ptr->bindings == NULL) {
121
     if (env_ptr->bindings == NULL) {
118
       env_ptr->bindings = create_list(create_binding(name_token, tree, env_ptr), NULL);
122
       env_ptr->bindings = create_list(create_binding(name_token, tree, env_ptr), NULL);
119
     } else {
123
     } else {
120
       append_list(env_ptr->bindings, create_binding(name_token, tree, env_ptr));
124
       append_list(env_ptr->bindings, create_binding(name_token, tree, env_ptr));
121
     }
125
     }
122
   } else {
126
   } else {
123
-    printf("Updating function name %s with value: \n", name_token->lexeme);
124
-    print_tree(tree);
125
-    existing->tree = tree;
126
-    existing->env = env_ptr;
127
+    printf("Error: redefinition of function with name %s\n", name_token->lexeme);
127
   }
128
   }
128
 }
129
 }
129
 
130
 
136
   tok->value = recursive_interpret(tree->right, env_ptr);
137
   tok->value = recursive_interpret(tree->right, env_ptr);
137
   asprintf(&tok->lexeme, "%d", tok->value);
138
   asprintf(&tok->lexeme, "%d", tok->value);
138
   if ((existing = find_name_in_env(name_token, env_ptr)) == NULL) {
139
   if ((existing = find_name_in_env(name_token, env_ptr)) == NULL) {
139
-    printf("Added variable name %s to environment with value: %s\n", name_token->lexeme, tok->lexeme);
140
+    /* printf("Added variable name %s to environment with value: %s\n", name_token->lexeme, tok->lexeme); */
140
     if (env_ptr->bindings == NULL) {
141
     if (env_ptr->bindings == NULL) {
141
       env_ptr->bindings = create_list(create_binding(name_token, (NODE*) tok, NULL), NULL);
142
       env_ptr->bindings = create_list(create_binding(name_token, (NODE*) tok, NULL), NULL);
142
     } else {
143
     } else {
143
       append_list(env_ptr->bindings, create_binding(name_token, (NODE*) tok, NULL));
144
       append_list(env_ptr->bindings, create_binding(name_token, (NODE*) tok, NULL));
144
     }
145
     }
145
   } else {
146
   } else {
146
-    printf("Updating variable name %s with value: %s\n", name_token->lexeme, tok->lexeme);
147
+    /* printf("Updating variable name %s with value: %s\n", name_token->lexeme, tok->lexeme); */
147
     existing->tree = (NODE *) tok;
148
     existing->tree = (NODE *) tok;
148
   } 
149
   } 
149
 }
150
 }
157
       printf("Not implemented\n");
158
       printf("Not implemented\n");
158
       exit(1);
159
       exit(1);
159
     } else if (tree->left->type == INT || tree->left->type == FUNCTION) {
160
     } else if (tree->left->type == INT || tree->left->type == FUNCTION) {
160
-      // do nothing we dont care about types
161
+      // do nothing we dont care about types for now
161
       return 0;
162
       return 0;
162
     } else {
163
     } else {
163
       // an identifier
164
       // an identifier
253
   if (tree->type == RETURN) {
254
   if (tree->type == RETURN) {
254
     return recursive_interpret(tree->left, env_ptr);
255
     return recursive_interpret(tree->left, env_ptr);
255
   }
256
   }
257
+  if (tree->type == WHILE) {
258
+    while (recursive_interpret(tree->left, env_ptr)) {
259
+      recursive_interpret(tree->right, env_ptr);
260
+    }
261
+    return 0;
262
+  }
256
   recursive_interpret(tree->left, env_ptr);
263
   recursive_interpret(tree->left, env_ptr);
257
   return recursive_interpret(tree->right, env_ptr);
264
   return recursive_interpret(tree->right, env_ptr);
258
 }
265
 }

+ 15 - 5
cw/simple.c

1
-int main() {
1
+int y = 0;
2
+
3
+int incr_y(int a) {
4
+  y = y + 2;
5
+}
2
 
6
 
3
-  int foo = 1 + 4;
4
-  int bar = 2 + foo;
7
+int blah() {
8
+}
9
+
10
+int main() {
5
 
11
 
6
-  int baz = foo + bar;
12
+  int x = 0;
13
+  while (x < 10) {
14
+    incr_y();
15
+    x = x + 1;
16
+  }
7
 
17
 
8
-  return baz;
18
+  return y;
9
 }
19
 }

+ 25 - 0
cw/tests/cond_loop.c

1
+int y = 0;
2
+
3
+int incr_one() {
4
+  y = y + 1;
5
+}
6
+
7
+int incr_two() {
8
+  y = y + 2;
9
+}
10
+
11
+int main() {
12
+
13
+  int x = 0;
14
+
15
+  while (x <= 10) {
16
+    if (y < 5) {
17
+      incr_one();
18
+    } else {
19
+      incr_two();
20
+    }
21
+    x = x + 1;
22
+  }
23
+  
24
+  return y;
25
+}

+ 9 - 0
cw/tests/func_call.c

1
+int foo() {
2
+  return 5;
3
+}
4
+
5
+int main() {
6
+  int x = foo() + 3;
7
+
8
+  return x % 17;
9
+}

+ 3 - 0
cw/tests/return_only.c

1
+int main() {
2
+  return 1 + 2;
3
+}

+ 46 - 0
cw/tests/test.sh

1
+#/bin/bash
2
+ret_only=`../mycc < ./return_only.c | tail -n 1`
3
+var_maths=`../mycc < ./var_maths.c | tail -n 1`
4
+func_call=`../mycc < ./func_call.c | tail -n 1`
5
+cond_loop=`../mycc < ./cond_loop.c | tail -n 1`
6
+
7
+ret_ans='3'
8
+var_ans='11'
9
+func_ans='8'
10
+cond_ans='17'
11
+
12
+overall='0'
13
+
14
+pass="\033[32mPASSED\033[0m"
15
+fail="\033[31mFAILED\033[0m"
16
+
17
+if [ "$ret_only" -eq $ret_ans ]; then
18
+  echo "return_only ... $pass"
19
+else
20
+  echo -n "return_only ... $fail"
21
+  echo " expected $ret_ans but got $ret_only"
22
+  overall='1'
23
+fi
24
+if [ "$var_maths" -eq $var_ans ]; then
25
+  echo "var_maths ..... $pass"
26
+else
27
+  echo "var_maths ..... $fail"
28
+  echo " expected $var_ans but got $var_maths"
29
+  overall='1'
30
+fi
31
+if [ "$func_call" -eq $func_ans ]; then
32
+  echo "func_call ..... $pass"
33
+else
34
+  echo -n "func_call ..... $fail"
35
+  echo " expected $func_ans but got $func_call"
36
+  overall='1'
37
+fi
38
+if [ "$cond_loop" -eq $cond_ans ]; then
39
+  echo "cond_loop ..... $pass"
40
+else
41
+  echo -n "cond_loop ..... $fail"
42
+  echo " expected $cond_ans but got $cond_loop"
43
+  overall='1'
44
+fi
45
+
46
+exit $overall

+ 5 - 0
cw/tests/var_maths.c

1
+int main() {
2
+  int x = 5;
3
+
4
+  return x + 3 * 2;
5
+}