Permalink
Browse files

Refactored Statements and Expressions

This should hopefully make these beasts more managable.
  • Loading branch information...
1 parent 79a80c9 commit 6f07517c083f8190469a3efcbb9d607cfed6ce49 @pyrated pyrated committed Nov 5, 2012
View
@@ -1,4 +1,4 @@
a = "Hello World\n"
puts a
b = a
-puts b
+puts b
View
@@ -7,7 +7,10 @@ bin_PROGRAMS = hkl testbed
hkl_SOURCES = y.tab.y lex.yy.l hkl_string.c hkl_hash.c hkl_tree.c hkl.c \
hkl_deque.c hklr.c hklr_object.c hklr_expression.c hklr_statement.c \
-hkl_value.c hkl_list.c
+hkl_value.c hkl_list.c \
+expression/hklr_op_plus.c \
+statement/hklr_statement_puts.c \
+statement/hklr_statement_assign.c
testbed_SOURCES = hkl_tree.c hkl_string.c hkl_test.c hklr.c hklr_object.c \
-hkl_deque.c hkl_hash.c hkl_list.c test/hash.c test/traversal.c
+hkl_deque.c hkl_hash.c hkl_list.c hkl_value.c test/hash.c test/traversal.c
@@ -0,0 +1,126 @@
+#include <assert.h>
+
+#include "hkl_value.h"
+
+HklValue* hklr_op_plus(HklValue* left_value, HklValue* right_value)
+{
+ HklValue* result = NULL;
+ bool left_updated = false, right_updated = false;
+
+ // Dereference left and right sides
+ if (left_value->type == HKL_TYPE_REF)
+ {
+ left_updated = true;
+ left_value = hklr_object_dereference(left_value->as.object);
+ }
+
+ if (right_value->type == HKL_TYPE_REF)
+ {
+ right_updated = true;
+ right_value = hklr_object_dereference(right_value->as.object);
+ }
+
+ switch (left_value->type)
+ {
+ case HKL_TYPE_INT:
+ switch(right_value->type)
+ {
+ case HKL_TYPE_INT:
+ result = hkl_value_new(HKL_TYPE_INT,
+ left_value->as.integer + right_value->as.integer);
+ break;
+
+ case HKL_TYPE_REAL:
+ result = hkl_value_new(HKL_TYPE_REAL,
+ left_value->as.integer + right_value->as.real);
+ break;
+
+ case HKL_TYPE_STRING:
+ assert(false);
+ break;
+
+ default:
+ assert(false);
+ break;
+ }
+ break; // HKL_TYPE_INT
+
+ case HKL_TYPE_REAL:
+ switch(right_value->type)
+ {
+ case HKL_TYPE_INT:
+ result = hkl_value_new(HKL_TYPE_REAL,
+ left_value->as.real + right_value->as.integer);
+ break;
+
+ case HKL_TYPE_REAL:
+ result = hkl_value_new(HKL_TYPE_REAL,
+ left_value->as.real + right_value->as.real);
+ break;
+
+ case HKL_TYPE_STRING:
+ assert(false);
+ break;
+
+ default:
+ assert(false);
+ break;
+ }
+ break; // HKL_TYPE_REAL
+
+ case HKL_TYPE_STRING:
+ switch(right_value->type)
+ {
+
+ case HKL_TYPE_INT:
+ {
+ result = hkl_value_new(HKL_TYPE_STRING,
+ hkl_string_new_from_string(left_value->as.string));
+ HklString* right_string =
+ hkl_string_new_from_integer(right_value->as.integer);
+ hkl_string_cat(result->as.string, right_string);
+ hkl_string_free(right_string);
+ }
+ break;
+
+ case HKL_TYPE_REAL:
+ {
+ result = hkl_value_new(HKL_TYPE_STRING,
+ hkl_string_new_from_string(left_value->as.string));
+ HklString* right_string =
+ hkl_string_new_from_real(right_value->as.real);
+ hkl_string_cat(result->as.string, right_string);
+ hkl_string_free(right_string);
+ }
+ break;
+
+ case HKL_TYPE_STRING:
+ result = hkl_value_new(HKL_TYPE_STRING,
+ hkl_string_new_from_string(left_value->as.string));
+
+ hkl_string_cat(result->as.string, right_value->as.string);
+ break;
+
+ default:
+ assert(false);
+ break;
+ }
+ break; // HKL_TYPE_STRING
+
+ default:
+ assert(false);
+ break;
+ }
+
+ if (left_updated)
+ {
+ hkl_value_free(left_value);
+ }
+
+ if (right_updated)
+ {
+ hkl_value_free(right_value);
+ }
+
+ return result;
+}
View
@@ -5,6 +5,8 @@
#include "hkl_alloc.h"
#include "hklr_expression.h"
+extern HklValue* hklr_op_plus(HklValue* left_value, HklValue* right_value);
+
HklrExpression* hklr_expression_new(HklExpressionType type, ...)
{
assert(type != HKL_EXPR_NONE);
@@ -60,6 +62,9 @@ HklValue* hklr_expression_eval(HklrExpression* expr)
switch (expr->type)
{
+ case HKL_EXPR_NIL:
+ return hkl_value_new(HKL_TYPE_NIL);
+ break;
case HKL_EXPR_INT:
return hkl_value_new(HKL_TYPE_INT, expr->arg[0].integer);
@@ -149,98 +154,8 @@ HklValue* hklr_expression_eval(HklrExpression* expr)
switch (expr->arg[1].op)
{
case HKL_OP_PLUS:
- switch (left_value->type)
- {
- case HKL_TYPE_INT:
- switch(right_value->type)
- {
- case HKL_TYPE_INT:
- result = hkl_value_new(HKL_TYPE_INT,
- left_value->as.integer + right_value->as.integer);
- break;
-
- case HKL_TYPE_REAL:
- result = hkl_value_new(HKL_TYPE_INT,
- left_value->as.integer + right_value->as.real);
- break;
-
- case HKL_TYPE_STRING:
- assert(false);
- break;
-
- default:
- assert(false);
- break;
- }
- break; // HKL_TYPE_INT
-
- case HKL_TYPE_REAL:
- switch(right_value->type)
- {
- case HKL_TYPE_INT:
- result = hkl_value_new(HKL_TYPE_REAL,
- left_value->as.real + right_value->as.integer);
- break;
-
- case HKL_TYPE_REAL:
- result = hkl_value_new(HKL_TYPE_REAL,
- left_value->as.real + right_value->as.real);
- break;
-
- case HKL_TYPE_STRING:
- assert(false);
- break;
-
- default:
- assert(false);
- break;
- }
- break; // HKL_TYPE_REAL
-
- case HKL_TYPE_STRING:
- switch(right_value->type)
- {
-
- case HKL_TYPE_INT:
- {
- result = hkl_value_new(HKL_TYPE_STRING,
- hkl_string_new_from_string(left_value->as.string));
- HklString* right_string =
- hkl_string_new_from_integer(right_value->as.integer);
- hkl_string_cat(result->as.string, right_string);
- hkl_string_free(right_string);
- }
- break;
-
- case HKL_TYPE_REAL:
- {
- result = hkl_value_new(HKL_TYPE_STRING,
- hkl_string_new_from_string(left_value->as.string));
- HklString* right_string =
- hkl_string_new_from_real(right_value->as.real);
- hkl_string_cat(result->as.string, right_string);
- hkl_string_free(right_string);
- }
- break;
-
- case HKL_TYPE_STRING:
- result = hkl_value_new(HKL_TYPE_STRING,
- hkl_string_new_from_string(left_value->as.string));
-
- hkl_string_cat(result->as.string, right_value->as.string);
- break;
-
- default:
- assert(false);
- break;
- }
- break; // HKL_TYPE_STRING
-
- default:
- assert(false);
- break;
- }
- break; // HKL_OP_PLUS
+ result = hklr_op_plus(left_value, right_value);
+ break;
default:
assert(false);
View
@@ -9,6 +9,7 @@
typedef enum HklExpressionType
{
HKL_EXPR_NONE,
+ HKL_EXPR_NIL,
HKL_EXPR_INT,
HKL_EXPR_REAL,
HKL_EXPR_STRING,
View
@@ -4,6 +4,7 @@
#include "hklr_object.h"
#include "hkl_alloc.h"
#include "hklr.h"
+#include "hkl_value.h"
HklrObject* hklr_object_new(HklType type, HklFlag flags, ...)
{
@@ -56,6 +57,35 @@ HklrObject* hklr_object_new(HklType type, HklFlag flags, ...)
return object;
}
+HklValue* hklr_object_dereference(HklrObject* object)
+{
+ assert(object != NULL);
+
+ switch (object->type)
+ {
+ case HKL_TYPE_NIL:
+ return hkl_value_new(HKL_TYPE_NIL);
+ break;
+
+ case HKL_TYPE_INT:
+ return hkl_value_new(HKL_TYPE_INT, object->as.integer);
+ break;
+
+ case HKL_TYPE_REAL:
+ return hkl_value_new(HKL_TYPE_REAL, object->as.real);
+ break;
+
+ case HKL_TYPE_STRING:
+ return hkl_value_new(HKL_TYPE_STRING, hkl_string_new_from_string(object->as.string));
+ break;
+
+ default:
+ assert(false);
+ return NULL;
+ break;
+ }
+}
+
void hklr_object_free(HklrObject* object)
{
assert(object != NULL);
View
@@ -89,6 +89,8 @@ garbage collected by the HKLR.
*/
HklrObject* hklr_object_new(HklType type, HklFlag flags, ...);
+struct HklValue* hklr_object_dereference(HklrObject* object);
+
/**
Free resources used by a HklObject
Oops, something went wrong.

0 comments on commit 6f07517

Please sign in to comment.