Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

basic integer values

  • Loading branch information...
commit b3271acaeb4a291323fada90d7693d100d24a940 1 parent 6d218f0
@txus authored
View
18 src/forkix/gc.c
@@ -0,0 +1,18 @@
+#include <forkix/gc.h>
+#include <stdlib.h>
+
+VALUE
+gc_alloc(size_t size)
+{
+ VALUE val = calloc(1, size);
+ val->gc.refcount = 0;
+ return val;
+}
+
+void
+gc_dealloc(VALUE obj)
+{
+ free(obj);
+ obj = NULL;
+}
+
View
11 src/forkix/gc.h
@@ -0,0 +1,11 @@
+#ifndef _fx_gc_h_
+#define _fx_gc_h_
+
+#include <stdlib.h>
+#include <forkix/value.h>
+
+VALUE gc_alloc(size_t size);
+void gc_dealloc(VALUE obj);
+
+#endif
+
View
10 src/forkix/gc_header.h
@@ -0,0 +1,10 @@
+#ifndef _fx_gc_header_h_
+#define _fx_gc_header_h_
+
+struct gc_header_s {
+ int refcount;
+};
+
+typedef struct gc_header_s GCHeader;
+
+#endif
View
25 src/forkix/value.c
@@ -0,0 +1,25 @@
+#include <forkix/value.h>
+#include <forkix/gc.h>
+
+VALUE
+Value_new(ValueType type)
+{
+ VALUE val = gc_alloc(sizeof(val_t));
+ val->type = type;
+ return val;
+};
+
+void
+Value_destroy(VALUE o)
+{
+ gc_dealloc(o);
+}
+
+VALUE
+Integer_new(int num)
+{
+ VALUE val = Value_new(IntegerType);
+ val->data.as_int = num;
+ return val;
+}
+
View
27 src/forkix/value.h
@@ -0,0 +1,27 @@
+#ifndef _fx_value_h_
+#define _fx_value_h_
+
+#include <forkix/value_type.h>
+#include <forkix/gc_header.h>
+
+struct val_s {
+ GCHeader gc;
+ ValueType type;
+ union {
+ int as_int;
+ char *as_str;
+ void *as_data;
+ } data;
+};
+
+typedef struct val_s val_t;
+#define VALUE val_t*
+
+VALUE Value_new(ValueType);
+void Value_destroy(VALUE);
+
+VALUE Integer_new(int);
+#define VAL2INT(o) (o->data.as_int)
+
+#endif
+
View
15 src/forkix/value_type.h
@@ -0,0 +1,15 @@
+#ifndef _fx_value_type_h_
+#define _fx_value_type_h_
+
+typedef enum {
+ IntegerType = 0,
+ StringType,
+ ArrayType,
+ HashType,
+ ClosureType,
+ TrueType,
+ FalseType,
+ NilType
+} ValueType;
+
+#endif
View
1  src/forkix/vm.c
@@ -3,4 +3,5 @@
void
foo()
{
+
}
View
34 tests/value_tests.c
@@ -0,0 +1,34 @@
+#include "minunit.h"
+#include <forkix/value.h>
+#include <assert.h>
+
+VALUE val = NULL;
+
+char *test_integer_new()
+{
+ val = Integer_new(123);
+
+ mu_assert(val->type == IntegerType, "failed assigning type");
+ mu_assert(VAL2INT(val) == 123, "failed assigning integer value");
+
+ return NULL;
+}
+
+char *test_destroy()
+{
+ Value_destroy(val);
+ mu_assert(!val->type, "failed destroying value")
+
+ return NULL;
+}
+
+char *all_tests() {
+ mu_suite_start();
+
+ mu_run_test(test_integer_new);
+ mu_run_test(test_destroy);
+
+ return NULL;
+}
+
+RUN_TESTS(all_tests);
Please sign in to comment.
Something went wrong with that request. Please try again.