Bläddra i källkod

push from mac

Matt Coles 8 år sedan
förälder
incheckning
dc825ca537
3 ändrade filer med 43 tillägg och 36 borttagningar
  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
   NODE* func_name = tree->left->right->left->left;
169
   NODE* func_name = tree->left->right->left->left;
170
   TOKEN* name_token = (TOKEN*) func_name;
170
   TOKEN* name_token = (TOKEN*) func_name;
171
   if ((existing = find_name_in_env(name_token, env_ptr)) == NULL) {
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
     ENV* new_func_env = create_new_function_env(env_ptr);
172
     ENV* new_func_env = create_new_function_env(env_ptr);
175
     if (tree->left->right->right != NULL) {
173
     if (tree->left->right->right != NULL) {
176
       add_args_to_env(tree->left->right->right, new_func_env);
174
       add_args_to_env(tree->left->right->right, new_func_env);
210
 void bind_arg(TOKEN* val, TOKEN* arg_name, ENV *env_ptr) {
208
 void bind_arg(TOKEN* val, TOKEN* arg_name, ENV *env_ptr) {
211
   BIND* existing = find_name_in_list(arg_name, env_ptr->bindings);
209
   BIND* existing = find_name_in_list(arg_name, env_ptr->bindings);
212
   if (existing == NULL) {
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
     exit(1);
212
     exit(1);
215
   }
213
   }
216
   existing->tree = (NODE*) val;
214
   existing->tree = (NODE*) val;
224
   tok->value = recursive_interpret(tree->right, env_ptr);
222
   tok->value = recursive_interpret(tree->right, env_ptr);
225
   asprintf(&tok->lexeme, "%d", tok->value);
223
   asprintf(&tok->lexeme, "%d", tok->value);
226
   if ((existing = find_name_in_env(name_token, env_ptr)) == NULL) {
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
     if (env_ptr->bindings == NULL) {
225
     if (env_ptr->bindings == NULL) {
229
       env_ptr->bindings = create_list(create_binding(name_token, (NODE*) tok, NULL), NULL);
226
       env_ptr->bindings = create_list(create_binding(name_token, (NODE*) tok, NULL), NULL);
230
     } else {
227
     } else {
231
       append_list(env_ptr->bindings, create_binding(name_token, (NODE*) tok, NULL));
228
       append_list(env_ptr->bindings, create_binding(name_token, (NODE*) tok, NULL));
232
     }
229
     }
233
   } else {
230
   } else {
234
-    /* printf("Updating variable name %s with value: %s\n", name_token->lexeme, tok->lexeme); */
235
     existing->tree = (NODE *) tok;
231
     existing->tree = (NODE *) tok;
236
   } 
232
   } 
237
 }
233
 }
282
       exit(1);
278
       exit(1);
283
     }
279
     }
284
     if (tree->right != NULL) {
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
       arg_list = get_argument_list(func->tree);
285
       arg_list = get_argument_list(func->tree);
290
       val_list = get_val_list(tree->right);
286
       val_list = get_val_list(tree->right);
291
       if (exp_args == num_args) {
287
       if (exp_args == num_args) {
293
           bind_arg(val_list[i], arg_list[i], func->env);
289
           bind_arg(val_list[i], arg_list[i], func->env);
294
         }
290
         }
295
       } else {
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
         exit(1);
293
         exit(1);
298
       }
294
       }
299
     }
295
     }
301
   }
297
   }
302
   if (tree->type == IF) {
298
   if (tree->type == IF) {
303
     if (recursive_interpret(tree->left, env_ptr) == 1) {
299
     if (recursive_interpret(tree->left, env_ptr) == 1) {
300
+      ENV* scope_env = create_new_function_env(env_ptr);
304
       if (tree->right->type == ELSE) {
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
       } else {
303
       } else {
307
-        return recursive_interpret(tree->right, env_ptr);
304
+        return recursive_interpret(tree->right, scope_env);
308
       }
305
       }
309
     } else {
306
     } else {
310
       if (tree->right->type == ELSE) {
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
       return 0;
311
       return 0;
314
     }
312
     }
358
     return recursive_interpret(tree->left, env_ptr);
356
     return recursive_interpret(tree->left, env_ptr);
359
   }
357
   }
360
   if (tree->type == WHILE) {
358
   if (tree->type == WHILE) {
359
+    ENV* scope_env = create_new_function_env(env_ptr);
361
     while (recursive_interpret(tree->left, env_ptr)) {
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
     return 0;
363
     return 0;
365
   }
364
   }

+ 14 - 14
cw/simple.c

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