Skip to content
This repository
Browse code

Merge pull request #117 from pyrated/master

Adds little io library and example
  • Loading branch information...
commit afa12cc878d488ed9a80dcd0c20f3ac4f04b3de6 2 parents f45b2f8 + b106372
Chris Knadler authored December 03, 2012
10  examples/io.hkl
... ...
@@ -0,0 +1,10 @@
  1
+include "src/module/io.hkl"
  2
+
  3
+file = IO.open("sometest.txt", "a+")
  4
+
  5
+puts "What to write?"
  6
+text = gets
  7
+
  8
+IO.write(file, text)
  9
+
  10
+IO.close(file)
12  src/Makefile.am
@@ -2,7 +2,7 @@ AM_CFLAGS = -Wall -Wextra -Werror -Wno-unused-parameter -Wno-unused-function -st
2 2
 AM_YFLAGS = -d -v
3 3
 AM_LFLAGS =
4 4
 
5  
-bin_PROGRAMS = hkl math
  5
+bin_PROGRAMS = hkl math io
6 6
 
7 7
 hkl_LDADD = -ldl
8 8
 
@@ -36,10 +36,14 @@ statement/hklr_statement_while.c \
36 36
 linenoise/linenoise.c
37 37
 
38 38
 math_SOURCES = module/math.c
39  
-math_SOURCES += hklr.c hkl_value.c hklr_expression.c
  39
+math_SOURCES += hkl_value.c
40 40
 math_LDFLAGS = -shared -nostartfiles
41 41
 math_LDADD = -lm
42 42
 math_CFLAGS = $(AM_CFLAGS)
43  
-math_CFLAGS +=  -fPIC
44  
-
  43
+math_CFLAGS += -fPIC
45 44
 
  45
+io_SOURCES = module/io.c
  46
+io_SOURCES += $(hkl_SOURCES)
  47
+io_LDFLAGS = -shared -nostartfiles
  48
+io_CFLAGS = $(AM_CFLAGS)
  49
+io_CFLAGS += -fPIC
4  src/expression/hklr_as_string.c
@@ -79,6 +79,10 @@ HklValue* hklr_as_string(HklValue* value)
79 79
           cast->as.string = hkl_string_new_from_utf8("cfunc");
80 80
         break;
81 81
 
  82
+        case HKL_TYPE_CDATA:
  83
+          cast->as.string = hkl_string_new_from_utf8("cdata");
  84
+        break;
  85
+
82 86
         default:
83 87
           assert(false);
84 88
         break;
8  src/hkl_value.c
@@ -50,6 +50,10 @@ HklValue* hkl_value_new(HklType type, ...)
50 50
       value->as.function = va_arg(argp, HklrFunction*);
51 51
       break;
52 52
 
  53
+    case HKL_TYPE_CDATA:
  54
+      value->as.cdata = va_arg(argp, void*);
  55
+      break;
  56
+
53 57
     default:
54 58
       break;
55 59
   }
@@ -86,9 +90,7 @@ void hkl_value_free(HklValue* value)
86 90
       hkl_hash_free(value->as.hash);
87 91
     break;
88 92
 
89  
-    case HKL_TYPE_FUNCTION:
90  
-      hklr_function_free(value->as.function);
91  
-    break;
  93
+  
92 94
 
93 95
     default:
94 96
       break;
2  src/hkl_value.h
@@ -25,6 +25,8 @@ typedef struct HklValue
25 25
     HklrObject*        object;
26 26
     HklType            type;
27 27
     HklrFunction*      function;
  28
+    struct HklValue*   (*cfunction)(HklList*);
  29
+    void*              cdata;
28 30
 
29 31
   } as;
30 32
 
3  src/hklr_expression.c
@@ -370,6 +370,9 @@ HklValue* hklr_expression_eval(HklrExpression* expr)
370 370
 
371 371
           HklValue* ret_val = object->as.cfunction(call);
372 372
 
  373
+          if (ret_val == NULL)
  374
+            ret_val = hkl_value_new(HKL_TYPE_NIL);
  375
+
373 376
           // need to free the args
374 377
           return ret_val;
375 378
         }
18  src/hklr_object.c
@@ -66,6 +66,10 @@ HklrObject* hklr_object_new(HklType type, HklFlag flags, ...)
66 66
       object->as.cfunction = va_arg(argp, HklValue* (*)(HklList*));
67 67
     break;
68 68
 
  69
+    case HKL_TYPE_CDATA:
  70
+      object->as.cdata = va_arg(argp, void*);
  71
+    break;
  72
+
69 73
     default: 
70 74
       assert(false);
71 75
     break;
@@ -117,6 +121,10 @@ HklValue* hklr_object_dereference(HklrObject* object)
117 121
     case HKL_TYPE_CFUNC:
118 122
       return hkl_value_new(HKL_TYPE_CFUNC, object->as.cfunction);
119 123
     break;
  124
+
  125
+    case HKL_TYPE_CDATA:
  126
+      return hkl_value_new(HKL_TYPE_CDATA, object->as.cdata);
  127
+    break;
120 128
     
121 129
     // A composite object reference
122 130
     case HKL_TYPE_REF:
@@ -244,6 +252,16 @@ void hklr_object_assign(HklrObject* object, HklrExpression* expression)
244 252
 
245 253
     break;
246 254
 
  255
+    case HKL_TYPE_CFUNC:
  256
+      object->type = HKL_TYPE_CFUNC;
  257
+      object->as.cfunction = value->as.cfunction;
  258
+    break;
  259
+
  260
+    case HKL_TYPE_CDATA:
  261
+      object->type = HKL_TYPE_CDATA;
  262
+      object->as.cdata = value->as.cdata;
  263
+    break;
  264
+
247 265
     default:
248 266
     assert(false);
249 267
     break;
1  src/hklr_object.h
@@ -79,6 +79,7 @@ typedef struct HklrObject {
79 79
     HklType              type;
80 80
     struct HklrFunction* function;
81 81
     struct HklValue* (*cfunction)(HklList*);
  82
+    void*                cdata;
82 83
   
83 84
   } as;
84 85
 
BIN  src/io
Binary file not shown
BIN  src/math
Binary file not shown
42  src/module/io.c
... ...
@@ -0,0 +1,42 @@
  1
+#include <stdio.h>
  2
+#include <assert.h>
  3
+
  4
+#include "hkl_value.h"
  5
+#include "hklr_object.h"
  6
+#include "hkl_string.h"
  7
+
  8
+HklValue* hklapi_open(HklList* args)
  9
+{
  10
+  HklValue* path = args->head->data;
  11
+  HklValue* mode = args->head->next->data;
  12
+
  13
+  FILE* fd = fopen(path->as.string->utf8_data, mode->as.string->utf8_data);
  14
+  assert(fd);
  15
+
  16
+  return hkl_value_new(HKL_TYPE_CDATA, fd);
  17
+}
  18
+
  19
+HklValue* hklapi_write(HklList* args)
  20
+{
  21
+  HklValue* fd = args->head->data;
  22
+
  23
+  assert(fd->type == HKL_TYPE_CDATA);
  24
+
  25
+  HklValue* string = args->head->next->data;
  26
+  assert(string->type == HKL_TYPE_STRING);
  27
+
  28
+  fprintf(fd->as.cdata, "%s", string->as.string->utf8_data);
  29
+
  30
+  return NULL;
  31
+}
  32
+
  33
+HklValue* hklapi_close(HklList* args)
  34
+{
  35
+  HklValue* fd = args->head->data;
  36
+
  37
+  assert(fd->type == HKL_TYPE_CDATA);
  38
+
  39
+  fclose(fd->as.cdata);
  40
+
  41
+  return NULL;
  42
+}
7  src/module/io.hkl
... ...
@@ -0,0 +1,7 @@
  1
+IO = interface "src/io"
  2
+
  3
+  open: cfunc
  4
+  close: cfunc
  5
+  write: cfunc
  6
+
  7
+end
10  src/module/math.c
@@ -3,7 +3,6 @@
3 3
 #include <math.h>
4 4
 
5 5
 #include "hkl_value.h"
6  
-#include "hklr_expression.h"
7 6
 
8 7
 double hklapi_PI = 3.1415926;
9 8
 double hklapi_TAU = 2.0*3.1415926;
@@ -58,6 +57,15 @@ HklValue* hklapi_log10(HklList* args) {
58 57
   return hkl_value_new(HKL_TYPE_REAL, log10(d));
59 58
 }
60 59
 
  60
+HklValue* hklapi_sqrt(HklList* args) {
  61
+  
  62
+  HklValue* value = args->head->data;
  63
+  double d = value->as.real;
  64
+  hkl_value_free(value);
  65
+
  66
+  return hkl_value_new(HKL_TYPE_REAL, sqrt(d));
  67
+}
  68
+
61 69
 HklValue* hklapi_pow(HklList* args) {
62 70
   
63 71
   HklValue* value = args->head->data;
1  src/module/math.hkl
@@ -7,6 +7,7 @@ Math = interface "src/math"
7 7
   tan: cfunc
8 8
   log: cfunc
9 9
   log10: cfunc
  10
+  sqrt: cfunc
10 11
   pow: cfunc
11 12
 
12 13
 end

0 notes on commit afa12cc

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