Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Adds little io library and example #117

Merged
merged 2 commits into from

2 participants

@pyrated
Owner

No description provided.

@cknadler cknadler merged commit afa12cc into from
@cknadler
Owner

<3 <3 <3 <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
10 examples/io.hkl
@@ -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
12 src/Makefile.am
@@ -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
View
4 src/expression/hklr_as_string.c
@@ -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
8 src/hkl_value.c
@@ -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
2  src/hkl_value.h
@@ -25,6 +25,8 @@ typedef struct HklValue
HklrObject* object;
HklType type;
HklrFunction* function;
+ struct HklValue* (*cfunction)(HklList*);
+ void* cdata;
} as;
View
3  src/hklr_expression.c
@@ -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
18 src/hklr_object.c
@@ -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
1  src/hklr_object.h
@@ -79,6 +79,7 @@ typedef struct HklrObject {
HklType type;
struct HklrFunction* function;
struct HklValue* (*cfunction)(HklList*);
+ void* cdata;
} as;
View
BIN  src/io
Binary file not shown
View
BIN  src/math
Binary file not shown
View
42 src/module/io.c
@@ -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
7 src/module/io.hkl
@@ -0,0 +1,7 @@
+IO = interface "src/io"
+
+ open: cfunc
+ close: cfunc
+ write: cfunc
+
+end
View
10 src/module/math.c
@@ -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
1  src/module/math.hkl
@@ -7,6 +7,7 @@ Math = interface "src/math"
tan: cfunc
log: cfunc
log10: cfunc
+ sqrt: cfunc
pow: cfunc
end
Something went wrong with that request. Please try again.