Matt Coles 8 anni fa
parent
commit
dc825ca537
3 ha cambiato i file con 43 aggiunte e 36 eliminazioni
  1. 13 14
      cw/main.c
  2. 14 14
      cw/simple.c
  3. 16 8
      cw/tests/test.sh

+ 13 - 14
cw/main.c

@@ -169,8 +169,6 @@ void add_function_to_env(NODE *tree, ENV *env_ptr) {
169 169
   NODE* func_name = tree->left->right->left->left;
170 170
   TOKEN* name_token = (TOKEN*) func_name;
171 171
   if ((existing = find_name_in_env(name_token, env_ptr)) == NULL) {
172
-    /* printf("Added function name %s to environment with value: \n", name_token->lexeme); */
173
-    /* print_tree(tree); */
174 172
     ENV* new_func_env = create_new_function_env(env_ptr);
175 173
     if (tree->left->right->right != NULL) {
176 174
       add_args_to_env(tree->left->right->right, new_func_env);
@@ -210,7 +208,7 @@ TOKEN** get_val_list(NODE *tree) {
210 208
 void bind_arg(TOKEN* val, TOKEN* arg_name, ENV *env_ptr) {
211 209
   BIND* existing = find_name_in_list(arg_name, env_ptr->bindings);
212 210
   if (existing == NULL) {
213
-    printf("fatal error\n");
211
+    printf("Attempted to bind arg that doesn't exist, the compiler is broken\n");
214 212
     exit(1);
215 213
   }
216 214
   existing->tree = (NODE*) val;
@@ -224,14 +222,12 @@ void add_var_to_env(NODE *tree, ENV *env_ptr) {
224 222
   tok->value = recursive_interpret(tree->right, env_ptr);
225 223
   asprintf(&tok->lexeme, "%d", tok->value);
226 224
   if ((existing = find_name_in_env(name_token, env_ptr)) == NULL) {
227
-    /* printf("Added variable name %s to environment with value: %s\n", name_token->lexeme, tok->lexeme); */
228 225
     if (env_ptr->bindings == NULL) {
229 226
       env_ptr->bindings = create_list(create_binding(name_token, (NODE*) tok, NULL), NULL);
230 227
     } else {
231 228
       append_list(env_ptr->bindings, create_binding(name_token, (NODE*) tok, NULL));
232 229
     }
233 230
   } else {
234
-    /* printf("Updating variable name %s with value: %s\n", name_token->lexeme, tok->lexeme); */
235 231
     existing->tree = (NODE *) tok;
236 232
   } 
237 233
 }
@@ -282,10 +278,10 @@ int recursive_interpret(NODE *tree, ENV *env_ptr) {
282 278
       exit(1);
283 279
     }
284 280
     if (tree->right != NULL) {
285
-      TOKEN** arg_list;
286
-      TOKEN** val_list;
287
-      int exp_args = count_args(func->tree->left->right->right);
288
-      int num_args = count_args(tree->right);
281
+      TOKEN **arg_list, **val_list;
282
+      int exp_args, num_args;
283
+      exp_args = count_args(func->tree->left->right->right);
284
+      num_args = count_args(tree->right);
289 285
       arg_list = get_argument_list(func->tree);
290 286
       val_list = get_val_list(tree->right);
291 287
       if (exp_args == num_args) {
@@ -293,7 +289,7 @@ int recursive_interpret(NODE *tree, ENV *env_ptr) {
293 289
           bind_arg(val_list[i], arg_list[i], func->env);
294 290
         }
295 291
       } else {
296
-        printf("Error! Arguments not right!!\n");
292
+        printf("Incorrect arguments passed to function %s, expected %d got %d\n", func_name->lexeme, exp_args, num_args);
297 293
         exit(1);
298 294
       }
299 295
     }
@@ -301,14 +297,16 @@ int recursive_interpret(NODE *tree, ENV *env_ptr) {
301 297
   }
302 298
   if (tree->type == IF) {
303 299
     if (recursive_interpret(tree->left, env_ptr) == 1) {
300
+      ENV* scope_env = create_new_function_env(env_ptr);
304 301
       if (tree->right->type == ELSE) {
305
-        return recursive_interpret(tree->right->left, env_ptr);
302
+        return recursive_interpret(tree->right->left, scope_env);
306 303
       } else {
307
-        return recursive_interpret(tree->right, env_ptr);
304
+        return recursive_interpret(tree->right, scope_env);
308 305
       }
309 306
     } else {
310 307
       if (tree->right->type == ELSE) {
311
-        return recursive_interpret(tree->right->right, env_ptr);
308
+        ENV* scope_env = create_new_function_env(env_ptr);
309
+        return recursive_interpret(tree->right->right, scope_env);
312 310
       }
313 311
       return 0;
314 312
     }
@@ -358,8 +356,9 @@ int recursive_interpret(NODE *tree, ENV *env_ptr) {
358 356
     return recursive_interpret(tree->left, env_ptr);
359 357
   }
360 358
   if (tree->type == WHILE) {
359
+    ENV* scope_env = create_new_function_env(env_ptr);
361 360
     while (recursive_interpret(tree->left, env_ptr)) {
362
-      recursive_interpret(tree->right, env_ptr);
361
+      recursive_interpret(tree->right, scope_env);
363 362
     }
364 363
     return 0;
365 364
   }

+ 14 - 14
cw/simple.c

@@ -1,20 +1,20 @@
1
-int y = 0;
2
-
3
-int incr_y(int a, int b) {
4
-  y = y + a + b;
1
+int sum(int a, int b, int c) {
2
+  int y = 0;
3
+  if (a > 2) {
4
+    int x = 3;
5
+    y = x;
6
+  }
7
+  return y + b + c;
5 8
 }
6 9
 
7
-int blah() {
8
-  y = 2;
10
+int fact(int n) {
11
+  int inner_fact(int n, int a) {
12
+    if (n == 0) return a;
13
+    return inner_fact(n-1, a*n);
14
+  }
15
+  return inner_fact(n, 1);
9 16
 }
10 17
 
11
-/* int blah2(int a, int b, int c, int d) {} */
12
-
13
-/* int blah3(int a) {} */
14
-
15 18
 int main() {
16
-  incr_y(2,3);
17
-  incr_y(4, 5);
18
-
19
-  return y;
19
+  return fact(3);
20 20
 }

+ 16 - 8
cw/tests/test.sh

@@ -1,6 +1,14 @@
1 1
 #/bin/bash
2
-start=$(date +%s.%N)
3
-absdir=$(readlink -f $(dirname $0))
2
+uname=`uname`
3
+if [[ "$uname" -eq "Darwin" ]]; then
4
+  date="gdate"
5
+  readlink="greadlink"
6
+else
7
+  date="date"
8
+  readlink="readlink"
9
+fi
10
+start=$($date +%s.%N)
11
+absdir=$($readlink -f $(dirname $0))
4 12
 ret_only=`$absdir/../mycc < $absdir/return_only.c | tail -n 1`
5 13
 var_maths=`$absdir/../mycc < $absdir/var_maths.c | tail -n 1`
6 14
 func_call=`$absdir/../mycc < $absdir/func_call.c | tail -n 1`
@@ -18,35 +26,35 @@ overall='0'
18 26
 pass="\033[32mPASSED\033[0m"
19 27
 fail="\033[31mFAILED\033[0m"
20 28
 
21
-if [ "$ret_only" -eq $ret_ans ]; then
29
+if [[ "$ret_only" -eq $ret_ans ]]; then
22 30
   echo "return_only ... $pass"
23 31
 else
24 32
   echo -n "return_only ... $fail"
25 33
   echo " expected $ret_ans but got $ret_only"
26 34
   overall='1'
27 35
 fi
28
-if [ "$var_maths" -eq $var_ans ]; then
36
+if [[ "$var_maths" -eq $var_ans ]]; then
29 37
   echo "var_maths ..... $pass"
30 38
 else
31 39
   echo "var_maths ..... $fail"
32 40
   echo " expected $var_ans but got $var_maths"
33 41
   overall='1'
34 42
 fi
35
-if [ "$func_call" -eq $func_ans ]; then
43
+if [[ "$func_call" -eq $func_ans ]]; then
36 44
   echo "func_call ..... $pass"
37 45
 else
38 46
   echo -n "func_call ..... $fail"
39 47
   echo " expected $func_ans but got $func_call"
40 48
   overall='1'
41 49
 fi
42
-if [ "$cond_loop" -eq $cond_ans ]; then
50
+if [[ "$cond_loop" -eq $cond_ans ]]; then
43 51
   echo "cond_loop ..... $pass"
44 52
 else
45 53
   echo -n "cond_loop ..... $fail"
46 54
   echo " expected $cond_ans but got $cond_loop"
47 55
   overall='1'
48 56
 fi
49
-if [ "$func_args" -eq $args_ans ]; then
57
+if [[ "$func_args" -eq $args_ans ]]; then
50 58
   echo "func_args ..... $pass"
51 59
 else
52 60
   echo -n "func_args ..... $fail"
@@ -54,7 +62,7 @@ else
54 62
   overall='1'
55 63
 fi
56 64
 
57
-end=$(date +%s.%N)    
65
+end=$($date +%s.%N)    
58 66
 runtime=$(python -c "print(${end} - ${start})")
59 67
 
60 68
 echo "\nCompleted 5 tests in $runtime""s"