@@ -7,10 +7,6 @@ const r = @import("r.zig");
77const std = @import ("std" );
88const testing = std .testing ;
99
10- // R data types
11- pub usingnamespace @import ("types.zig" );
12- pub usingnamespace @import ("constants.zig" );
13-
1410// R memory allocators
1511pub const heap = @import ("allocator.zig" );
1612
@@ -35,3 +31,147 @@ pub const strings = @import("strings.zig");
3531test {
3632 testing .refAllDecls (@This ());
3733}
34+
35+ // ---------
36+ // Types
37+ // ---------
38+
39+ /// General purpose R object (SEXP).
40+ /// Must use R API functions to access values and coerce to other types.
41+ pub const Robject = r .Sexp ;
42+
43+ pub const Rcomplex = r .Rcomplex ;
44+
45+ ///no SEXPTYPE Description
46+ ///0 NILSXP NULL
47+ ///1 SYMSXP symbols
48+ ///2 LISTSXP pairlists
49+ ///3 CLOSXP closures
50+ ///4 ENVSXP environments
51+ ///5 PROMSXP promises
52+ ///6 LANGSXP language objects
53+ ///7 SPECIALSXP special functions
54+ ///8 BUILTINSXP builtin functions
55+ ///9 CHARSXP internal character strings
56+ ///10 LGLSXP logical vectors
57+ ///13 INTSXP integer vectors
58+ ///14 REALSXP numeric vectors
59+ ///15 CPLXSXP complex vectors
60+ ///16 STRSXP character vectors
61+ ///17 DOTSXP dot-dot-dot object
62+ ///18 ANYSXP make “any” args work
63+ ///19 VECSXP list (generic vector)
64+ ///20 EXPRSXP expression vector
65+ ///21 BCODESXP byte code
66+ ///22 EXTPTRSXP external pointer
67+ ///23 WEAKREFSXP weak reference
68+ ///24 RAWSXP raw vector
69+ ///25 OBJSXP objects not of simple type
70+ ///
71+ /// More details in https://cran.r-project.org/doc/manuals/R-ints.html#The-_0027data_0027
72+ pub const Rtype = enum (c_uint ) {
73+ NULL = 0 ,
74+ Symbol = 1 ,
75+ Pairlist = 2 ,
76+ Closure = 3 ,
77+ Environment = 4 ,
78+ Promise = 5 ,
79+ LanguageObject = 6 ,
80+ SpecialFunction = 7 ,
81+ BuiltinFunction = 8 ,
82+ String = 9 , // C string
83+ LogicalVector = 10 ,
84+
85+ IntegerVector = 13 ,
86+ NumericVector = 14 ,
87+ ComplexVector = 15 ,
88+ CharacterVector = 16 ,
89+ TripleDot = 17 ,
90+ Any = 18 ,
91+ List = 19 ,
92+ Expression = 20 ,
93+ Bytecode = 21 ,
94+ ExternalPointer = 22 ,
95+ WeakReference = 23 ,
96+ RawVector = 24 ,
97+ Object = 25 , // non-vector
98+
99+ pub fn int (self : Rtype ) c_uint {
100+ return @intFromEnum (self );
101+ }
102+ };
103+
104+ test "R type checks" {
105+ const code =
106+ \\dyn.load('zig-out/tests/lib/libRtests.so')
107+ \\obj <- list()
108+ \\obj[[1]] <- quote(x)
109+ \\obj[[2]] <- pairlist(1)
110+ \\obj[[3]] <- function(x) 1 + 1
111+ \\obj[[4]] <- new.env()
112+ \\obj[[5]] <- call("any")
113+ \\obj[[6]] <- `[`
114+ \\obj[[7]] <- `+`
115+ \\obj[[8]] <- "test"
116+ \\.Call('testIsObjects', obj)
117+ ;
118+
119+ const result = try std .process .Child .run (.{
120+ .allocator = testing .allocator ,
121+ .argv = &.{
122+ "Rscript" ,
123+ "--vanilla" ,
124+ "-e" ,
125+ code ,
126+ },
127+ });
128+
129+ defer testing .allocator .free (result .stdout );
130+ defer testing .allocator .free (result .stderr );
131+
132+ const expected =
133+ \\[[1]]
134+ \\[1] TRUE
135+ \\
136+ \\[[2]]
137+ \\[1] TRUE
138+ \\
139+ \\[[3]]
140+ \\[1] TRUE
141+ \\
142+ \\[[4]]
143+ \\[1] TRUE
144+ \\
145+ \\[[5]]
146+ \\[1] TRUE
147+ \\
148+ \\[[6]]
149+ \\[1] TRUE
150+ \\
151+ \\[[7]]
152+ \\[1] TRUE
153+ \\
154+ \\[[8]]
155+ \\[1] TRUE
156+ \\
157+ \\[[9]]
158+ \\[1] TRUE
159+ \\
160+ \\[[10]]
161+ \\[1] TRUE
162+ \\
163+ \\
164+ ;
165+
166+ testing .expectEqualStrings (expected , result .stdout ) catch | err | {
167+ std .debug .print ("stderr:\n {s}\n " , .{result .stderr });
168+ return err ;
169+ };
170+
171+ try testing .expectEqualStrings ("" , result .stderr );
172+ }
173+
174+ // ---------
175+ // Constants
176+ // ---------
177+ pub const r_null : * Robject = & r .R_NilValue ;
0 commit comments