Permalink
Browse files

Merge pull request #117 from pyrated/master

Adds little io library and example
  • Loading branch information...
2 parents f45b2f8 + b106372 commit afa12cc878d488ed9a80dcd0c20f3ac4f04b3de6 @cknadler cknadler committed Dec 4, 2012
View
@@ -0,0 +1,10 @@
+include "src/module/io.hkl"
+
+file = IO.open("sometest.txt", "a+")
+
+puts "What to write?"
+text = gets
+
+IO.write(file, text)
+
+IO.close(file)
View
@@ -2,7 +2,7 @@ AM_CFLAGS = -Wall -Wextra -Werror -Wno-unused-parameter -Wno-unused-function -st
AM_YFLAGS = -d -v
AM_LFLAGS =
-bin_PROGRAMS = hkl math
+bin_PROGRAMS = hkl math io
hkl_LDADD = -ldl
@@ -36,10 +36,14 @@ statement/hklr_statement_while.c \
linenoise/linenoise.c
math_SOURCES = module/math.c
-math_SOURCES += hklr.c hkl_value.c hklr_expression.c
+math_SOURCES += hkl_value.c
math_LDFLAGS = -shared -nostartfiles
math_LDADD = -lm
math_CFLAGS = $(AM_CFLAGS)
-math_CFLAGS += -fPIC
-
+math_CFLAGS += -fPIC
+io_SOURCES = module/io.c
+io_SOURCES += $(hkl_SOURCES)
+io_LDFLAGS = -shared -nostartfiles
+io_CFLAGS = $(AM_CFLAGS)
+io_CFLAGS += -fPIC
@@ -79,6 +79,10 @@ HklValue* hklr_as_string(HklValue* value)
cast->as.string = hkl_string_new_from_utf8("cfunc");
break;
+ case HKL_TYPE_CDATA:
+ cast->as.string = hkl_string_new_from_utf8("cdata");
+ break;
+
default:
assert(false);
break;
View
@@ -50,6 +50,10 @@ HklValue* hkl_value_new(HklType type, ...)
value->as.function = va_arg(argp, HklrFunction*);
break;
+ case HKL_TYPE_CDATA:
+ value->as.cdata = va_arg(argp, void*);
+ break;
+
default:
break;
}
@@ -86,9 +90,7 @@ 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
@@ -25,6 +25,8 @@ typedef struct HklValue
HklrObject* object;
HklType type;
HklrFunction* function;
+ struct HklValue* (*cfunction)(HklList*);
+ void* cdata;
} as;
@@ -370,6 +370,9 @@ HklValue* hklr_expression_eval(HklrExpression* expr)
HklValue* ret_val = object->as.cfunction(call);
+ if (ret_val == NULL)
+ ret_val = hkl_value_new(HKL_TYPE_NIL);
+
// need to free the args
return ret_val;
}
View
@@ -66,6 +66,10 @@ HklrObject* hklr_object_new(HklType type, HklFlag flags, ...)
object->as.cfunction = va_arg(argp, HklValue* (*)(HklList*));
break;
+ case HKL_TYPE_CDATA:
+ object->as.cdata = va_arg(argp, void*);
+ break;
+
default:
assert(false);
break;
@@ -117,6 +121,10 @@ HklValue* hklr_object_dereference(HklrObject* object)
case HKL_TYPE_CFUNC:
return hkl_value_new(HKL_TYPE_CFUNC, object->as.cfunction);
break;
+
+ case HKL_TYPE_CDATA:
+ return hkl_value_new(HKL_TYPE_CDATA, object->as.cdata);
+ break;
// A composite object reference
case HKL_TYPE_REF:
@@ -244,6 +252,16 @@ void hklr_object_assign(HklrObject* object, HklrExpression* expression)
break;
+ case HKL_TYPE_CFUNC:
+ object->type = HKL_TYPE_CFUNC;
+ object->as.cfunction = value->as.cfunction;
+ break;
+
+ case HKL_TYPE_CDATA:
+ object->type = HKL_TYPE_CDATA;
+ object->as.cdata = value->as.cdata;
+ break;
+
default:
assert(false);
break;
View
@@ -79,6 +79,7 @@ typedef struct HklrObject {
HklType type;
struct HklrFunction* function;
struct HklValue* (*cfunction)(HklList*);
+ void* cdata;
} as;
View
Binary file not shown.
View
Binary file not shown.
View
@@ -0,0 +1,42 @@
+#include <stdio.h>
+#include <assert.h>
+
+#include "hkl_value.h"
+#include "hklr_object.h"
+#include "hkl_string.h"
+
+HklValue* hklapi_open(HklList* args)
+{
+ HklValue* path = args->head->data;
+ HklValue* mode = args->head->next->data;
+
+ FILE* fd = fopen(path->as.string->utf8_data, mode->as.string->utf8_data);
+ assert(fd);
+
+ return hkl_value_new(HKL_TYPE_CDATA, fd);
+}
+
+HklValue* hklapi_write(HklList* args)
+{
+ HklValue* fd = args->head->data;
+
+ assert(fd->type == HKL_TYPE_CDATA);
+
+ HklValue* string = args->head->next->data;
+ assert(string->type == HKL_TYPE_STRING);
+
+ fprintf(fd->as.cdata, "%s", string->as.string->utf8_data);
+
+ return NULL;
+}
+
+HklValue* hklapi_close(HklList* args)
+{
+ HklValue* fd = args->head->data;
+
+ assert(fd->type == HKL_TYPE_CDATA);
+
+ fclose(fd->as.cdata);
+
+ return NULL;
+}
View
@@ -0,0 +1,7 @@
+IO = interface "src/io"
+
+ open: cfunc
+ close: cfunc
+ write: cfunc
+
+end
View
@@ -3,7 +3,6 @@
#include <math.h>
#include "hkl_value.h"
-#include "hklr_expression.h"
double hklapi_PI = 3.1415926;
double hklapi_TAU = 2.0*3.1415926;
@@ -58,6 +57,15 @@ HklValue* hklapi_log10(HklList* args) {
return hkl_value_new(HKL_TYPE_REAL, log10(d));
}
+HklValue* hklapi_sqrt(HklList* args) {
+
+ HklValue* value = args->head->data;
+ double d = value->as.real;
+ hkl_value_free(value);
+
+ return hkl_value_new(HKL_TYPE_REAL, sqrt(d));
+}
+
HklValue* hklapi_pow(HklList* args) {
HklValue* value = args->head->data;
View
@@ -7,6 +7,7 @@ Math = interface "src/math"
tan: cfunc
log: cfunc
log10: cfunc
+ sqrt: cfunc
pow: cfunc
end

0 comments on commit afa12cc

Please sign in to comment.