From 488f7efc276de69011ea81442353b50a00446970 Mon Sep 17 00:00:00 2001 From: Jeremy Hughes Date: Wed, 8 Jul 2009 14:52:07 +1200 Subject: [PATCH 1/3] alien.inline: added C-TYPEDEF: --- basis/alien/inline/inline-tests.factor | 15 +++++++++++++-- basis/alien/inline/inline.factor | 11 ++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/basis/alien/inline/inline-tests.factor b/basis/alien/inline/inline-tests.factor index 837f27ceb7..2092800c7d 100644 --- a/basis/alien/inline/inline-tests.factor +++ b/basis/alien/inline/inline-tests.factor @@ -1,20 +1,31 @@ ! Copyright (C) 2009 Jeremy Hughes. ! See http://factorcode.org/license.txt for BSD license. USING: alien.inline alien.inline.private io.directories io.files -kernel namespaces tools.test ; +kernel namespaces tools.test alien.c-types ; IN: alien.inline.tests -C-LIBRARY: const +C-LIBRARY: test C-FUNCTION: const-int add ( int a, int b ) return a + b; ; +C-TYPEDEF: double bigfloat + +C-FUNCTION: bigfloat smaller ( bigfloat a ) + return a / 10; +; + ;C-LIBRARY { 2 1 } [ add ] must-infer-as [ 5 ] [ 2 3 add ] unit-test +{ 1 1 } [ smaller ] must-infer-as +[ 1.0 ] [ 10 smaller ] unit-test + +[ t ] [ "double" "bigfloat" [ resolve-typedef ] bi@ = ] unit-test + DELETE-C-LIBRARY: const diff --git a/basis/alien/inline/inline.factor b/basis/alien/inline/inline.factor index 07f4693446..0f05488917 100644 --- a/basis/alien/inline/inline.factor +++ b/basis/alien/inline/inline.factor @@ -5,7 +5,8 @@ alien.libraries alien.parser arrays assocs effects fry generalizations grouping io.directories io.files io.files.info io.files.temp kernel lexer math math.order math.ranges multiline namespaces sequences source-files -splitting strings system vocabs.loader vocabs.parser words ; +splitting strings system vocabs.loader vocabs.parser words +alien.c-types make ; IN: alien.inline : define-c-include ( str -- ) "#include " prepend c-strings get push ; +: define-c-typedef ( old new -- ) + [ typedef ] [ + [ swap "typedef " % % " " % % ";" % ] + "" make c-strings get push + ] 2bi ; + : delete-inline-library ( str -- ) library-path dup exists? [ delete-file ] [ drop ] if ; @@ -104,6 +111,8 @@ SYNTAX: C-INCLUDE: scan define-c-include ; SYNTAX: C-FUNCTION: function-types-effect define-c-function ; +SYNTAX: C-TYPEDEF: scan scan define-c-typedef ; + SYNTAX: ;C-LIBRARY compile-c-library ; SYNTAX: DELETE-C-LIBRARY: scan delete-inline-library ; From 7d218b0263a10f98995ac8b8fce1c3bf61b36854 Mon Sep 17 00:00:00 2001 From: Jeremy Hughes Date: Wed, 8 Jul 2009 15:14:51 +1200 Subject: [PATCH 2/3] alien.inline: added C-STRUCTURE: --- basis/alien/inline/inline-tests.factor | 20 ++++++++++++++++++-- basis/alien/inline/inline.factor | 15 ++++++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/basis/alien/inline/inline-tests.factor b/basis/alien/inline/inline-tests.factor index 2092800c7d..19920a58ba 100644 --- a/basis/alien/inline/inline-tests.factor +++ b/basis/alien/inline/inline-tests.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2009 Jeremy Hughes. ! See http://factorcode.org/license.txt for BSD license. USING: alien.inline alien.inline.private io.directories io.files -kernel namespaces tools.test alien.c-types ; +kernel namespaces tools.test alien.c-types alien.structs ; IN: alien.inline.tests C-LIBRARY: test @@ -16,15 +16,31 @@ C-FUNCTION: bigfloat smaller ( bigfloat a ) return a / 10; ; +C-STRUCTURE: rectangle + { "int" "width" } + { "int" "height" } ; + +C-FUNCTION: int area ( rectangle c ) + return c.width * c.height; +; + ;C-LIBRARY { 2 1 } [ add ] must-infer-as [ 5 ] [ 2 3 add ] unit-test +[ t ] [ "double" "bigfloat" [ resolve-typedef ] bi@ = ] unit-test { 1 1 } [ smaller ] must-infer-as [ 1.0 ] [ 10 smaller ] unit-test -[ t ] [ "double" "bigfloat" [ resolve-typedef ] bi@ = ] unit-test +[ t ] [ "rectangle" resolve-typedef struct-type? ] unit-test +{ 1 1 } [ area ] must-infer-as +[ 20 ] [ + "rectangle" + 4 over set-rectangle-width + 5 over set-rectangle-height + area +] unit-test DELETE-C-LIBRARY: const diff --git a/basis/alien/inline/inline.factor b/basis/alien/inline/inline.factor index 0f05488917..68da8b50f9 100644 --- a/basis/alien/inline/inline.factor +++ b/basis/alien/inline/inline.factor @@ -6,7 +6,7 @@ generalizations grouping io.directories io.files io.files.info io.files.temp kernel lexer math math.order math.ranges multiline namespaces sequences source-files splitting strings system vocabs.loader vocabs.parser words -alien.c-types make ; +alien.c-types alien.structs make parser ; IN: alien.inline "" make c-strings get push ] 2bi ; +: define-c-struct ( name vocab fields -- ) + [ define-struct ] [ + nip over + [ + "typedef struct " % "_" % % " {\n" % + [ first2 swap % " " % % ";\n" % ] each + "} " % % ";\n" % + ] "" make c-strings get push + ] 3bi ; + : delete-inline-library ( str -- ) library-path dup exists? [ delete-file ] [ drop ] if ; @@ -113,6 +123,9 @@ SYNTAX: C-FUNCTION: SYNTAX: C-TYPEDEF: scan scan define-c-typedef ; +SYNTAX: C-STRUCTURE: + scan current-vocab parse-definition define-c-struct ; + SYNTAX: ;C-LIBRARY compile-c-library ; SYNTAX: DELETE-C-LIBRARY: scan delete-inline-library ; From 8814e2b308e98fc93d40f50ec9779cb77a3f12c1 Mon Sep 17 00:00:00 2001 From: Jeremy Hughes Date: Wed, 8 Jul 2009 15:15:45 +1200 Subject: [PATCH 3/3] alien.inline.tests: delete libraries before compilation --- basis/alien/inline/inline-tests.factor | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/basis/alien/inline/inline-tests.factor b/basis/alien/inline/inline-tests.factor index 19920a58ba..09b76a4bb5 100644 --- a/basis/alien/inline/inline-tests.factor +++ b/basis/alien/inline/inline-tests.factor @@ -4,6 +4,7 @@ USING: alien.inline alien.inline.private io.directories io.files kernel namespaces tools.test alien.c-types alien.structs ; IN: alien.inline.tests +DELETE-C-LIBRARY: test C-LIBRARY: test C-FUNCTION: const-int add ( int a, int b ) @@ -42,9 +43,8 @@ C-FUNCTION: int area ( rectangle c ) area ] unit-test -DELETE-C-LIBRARY: const - +DELETE-C-LIBRARY: cpplib C-LIBRARY: cpplib COMPILE-AS-C++ @@ -61,9 +61,8 @@ C-FUNCTION: const-char* hello ( ) { 0 1 } [ hello ] must-infer-as [ "hello world" ] [ hello ] unit-test -DELETE-C-LIBRARY: cpplib - +DELETE-C-LIBRARY: compile-error C-LIBRARY: compile-error C-FUNCTION: char* breakme ( ) @@ -71,5 +70,3 @@ C-FUNCTION: char* breakme ( ) ; << [ compile-c-library ] must-fail >> - -DELETE-C-LIBRARY: compile-error