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 ;