Skip to content
Browse files

Allows lambdas to free themselves

  • Loading branch information...
1 parent 528f672 commit ecb4a3c61f218a2f0d1ad237f5f74727583a618b @pyrated pyrated committed Nov 29, 2012
Showing with 35 additions and 8 deletions.
  1. +8 −4 src/expression/hklr_op_typeof.c
  2. +2 −0 src/hkl.c
  3. +4 −0 src/hkl_value.c
  4. +3 −0 src/hklr_expression.c
  5. +18 −4 src/hklr_function.c
View
12 src/expression/hklr_op_typeof.c
@@ -4,8 +4,7 @@
HklValue* hklr_op_typeof(HklValue* value)
{
-
- //bool temporary = true;
+ bool temporary = true;
// Dereference
if (value->type == HKL_TYPE_REF)
@@ -15,9 +14,9 @@ HklValue* hklr_op_typeof(HklValue* value)
value = hklr_object_dereference(value->as.object);
// Don't free the deque or hash since it can't be a temporary
- if (value->type == HKL_TYPE_ARRAY)
+ if (value->type == HKL_TYPE_ARRAY || value->type == HKL_TYPE_HASH || value->type == HKL_TYPE_FUNCTION)
{
- //temporary = false;
+ temporary = false;
// simply spoof the value
temp->type = HKL_TYPE_NIL;
}
@@ -26,6 +25,11 @@ HklValue* hklr_op_typeof(HklValue* value)
}
HklType type = value->type;
+
+ // Spoof the value if it not temporary
+ if (temporary == false)
+ value->type = HKL_TYPE_NIL;
+
hkl_value_free(value);
value = hkl_value_new(HKL_TYPE_TYPE, type);
View
2 src/hkl.c
@@ -119,6 +119,7 @@ int main(int argc, const char* argv[])
keywords_map = hkl_tree_new();
hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("if", NULL));
hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("while", NULL));
+ hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("break", NULL));
hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("end", NULL));
hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("include", NULL));
hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("nil", NULL));
@@ -138,6 +139,7 @@ int main(int argc, const char* argv[])
hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("collect", NULL));
hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("typeof", NULL));
hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("function", NULL));
+ hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("return", NULL));
// If there is a filename
if (argv[1])
View
4 src/hkl_value.c
@@ -86,6 +86,10 @@ void hkl_value_free(HklValue* value)
hkl_hash_free(value->as.hash);
break;
+ case HKL_TYPE_FUNCTION:
+ hklr_function_free(value->as.function);
+ break;
+
default:
break;
}
View
3 src/hklr_expression.c
@@ -334,6 +334,9 @@ HklValue* hklr_expression_eval(HklrExpression* expr)
case HKL_EXPR_FUNCTION:
{
+ // The function can't own the lists and trees.
+ // Like a string, it needs to make a copy.
+
return hkl_value_new(HKL_TYPE_FUNCTION, hklr_function_new(expr->arg[0].list, expr->arg[1].tree, expr->arg[2].list));
}
break;
View
22 src/hklr_function.c
@@ -6,7 +6,7 @@
#include "hklr_object.h"
#include "hklr.h"
-static bool process_closures(HklPair* pair, void* closure_list)
+static bool process_closures(HklPair* pair, void* data)
{
// Search in our scope for the closed variables.
HklrObject* object = hklr_exists(pair->key);
@@ -33,17 +33,31 @@ static bool free_names(void* key, void* data)
return false;
}
+/*static bool copy_statement_list(void* statement, void* list)
+{
+ hkl_list_push_back((HklList*) list, hklr_statement_new_from_statement((HklrStatement*) statement));
+
+ return false;
+}
+
+static bool copy_args_list(void* arg, void* list)
+{
+ hkl_list_push_back((HklList*) list, hkl_string_new_from_string((HklString*) arg));
+
+ return false;
+}*/
+
HklrFunction* hklr_function_new(HklList* args_list, HklTree* closure_list, HklList* stmt_list)
{
HklrFunction* function = hkl_alloc_object(HklrFunction);
function->stmt_list = stmt_list;
+ //hkl_list_traverse(stmt_list, copy_statement_list, function->stmt_list);
+
function->args_list = args_list;
function->closure_list = closure_list;
- hkl_tree_traverse(closure_list, process_closures, closure_list);
-
- // free the closure names, as we don't need them anymore
+ hkl_tree_traverse(closure_list, process_closures, NULL);
return function;
}

0 comments on commit ecb4a3c

Please sign in to comment.
Something went wrong with that request. Please try again.