aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCamil Staps2016-02-01 17:47:57 +0100
committerCamil Staps2016-02-01 17:47:57 +0100
commit921200f7d3790ba5a1f61d750b6d84bf917db966 (patch)
tree3409353cfe827abdc0b9fabacf8bef787d1912ed
parentBetter practices string copying (diff)
Void return typeHEADmaster
-rw-r--r--CleanC.dcl10
-rw-r--r--CleanC.icl7
-rw-r--r--interface.c10
-rw-r--r--test.c5
-rw-r--r--test.icl6
5 files changed, 31 insertions, 7 deletions
diff --git a/CleanC.dcl b/CleanC.dcl
index a385a50..d0257c6 100644
--- a/CleanC.dcl
+++ b/CleanC.dcl
@@ -23,12 +23,16 @@ SOFTWARE. */
definition module CleanC
from StdClass import class toInt, class toString, class toReal,
- class fromInt, class fromString, class fromReal
+ class fromInt, class fromString, class fromReal,
+ class ==
:: State
-:: CType = Int | String | Real
-:: CParam = CI Int | CS String | CR Real
+:: CType = Int | String | Real | Void
+:: CParam = CI Int | CS String | CR Real | CV
+
+instance == CType
+instance == CParam
cNewState :: *State
diff --git a/CleanC.icl b/CleanC.icl
index ed1f6b1..1e1e8ce 100644
--- a/CleanC.icl
+++ b/CleanC.icl
@@ -28,6 +28,9 @@ from GenEq import generic gEq
:: State :== Int
derive gEq CType
+derive gEq CParam
+instance == CType where (==) x y = gEq{|*|} x y
+instance == CParam where (==) x y = gEq{|*|} x y
cNewState :: *State
cNewState = 42
@@ -99,6 +102,7 @@ cSetReturnType :: !CType -> *State -> *State
cSetReturnType Int = cSetReturnType_ 0
cSetReturnType String = cSetReturnType_ 1
cSetReturnType Real = cSetReturnType_ 2
+cSetReturnType Void = cSetReturnType_ 3
cCall_ :: !String !*State -> *State
cCall_ f s = code inline {
@@ -138,7 +142,8 @@ where
cCall` t f [] s
# s = cSetReturnType t s
# s = cCall_ f s
- = cGetParam t s
+ | t == Void = (CV, s)
+ | otherwise = cGetParam t s
cCall` t f [p:ps] s
# s = cPutParam p s
= cCall` t f ps s
diff --git a/interface.c b/interface.c
index 2a9a106..e40ed86 100644
--- a/interface.c
+++ b/interface.c
@@ -68,17 +68,21 @@ void cleanPutr(double r) {
void cleanSetReturnType(long i) {
switch (i) {
case 0:
- return_type = &ffi_type_slong;
+ return_type = &ffi_type_slong;
return_val = calloc(sizeof(long), 1);
break;
case 1:
- return_type = &ffi_type_pointer;
+ return_type = &ffi_type_pointer;
return_val = calloc(sizeof(char*), 1);
break;
case 2:
- return_type = &ffi_type_double;
+ return_type = &ffi_type_double;
return_val = calloc(sizeof(double), 1);
break;
+ case 3:
+ return_type = &ffi_type_void;
+ return_val = calloc(sizeof(void), 1);
+ break;
}
}
diff --git a/test.c b/test.c
index 9653d96..0396e7b 100644
--- a/test.c
+++ b/test.c
@@ -22,6 +22,7 @@ SOFTWARE. */
#include <stdlib.h>
#include <string.h>
+#include <stdio.h>
double test_double(double x) {
return x * 0.5;
@@ -45,3 +46,7 @@ int test_two_params(int a, int b) {
return a + b;
}
+void test_void(char* s) {
+ printf("%s", s);
+}
+
diff --git a/test.icl b/test.icl
index a452945..de40c75 100644
--- a/test.icl
+++ b/test.icl
@@ -33,6 +33,7 @@ Start w
# (t1,s) = test_string s
# (t2,s) = test_double s
# (t3,s) = test_two_params s
+# (t4,s) = test_void s
# tests = [t0,t1,t2,t3]
# passed = and tests
# (io, w) = stdio w
@@ -74,3 +75,8 @@ test_two_params s
# (i, s) = cCall Int "test_two_params" (13,29) s
= (fromCParam i == 42, s)
+test_void :: *State -> (Bool, *State)
+test_void s
+# (i, s) = cCall Void "test_void" "Testing void function\n" s
+= (i == CV, s)
+