Skip to content

Commit 570c000

Browse files
committed
refactor: simplify imports
1 parent 03ef655 commit 570c000

File tree

10 files changed

+174
-180
lines changed

10 files changed

+174
-180
lines changed

src/Rzig.zig

Lines changed: 144 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ const r = @import("r.zig");
77
const std = @import("std");
88
const 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
1511
pub const heap = @import("allocator.zig");
1612

@@ -35,3 +31,147 @@ pub const strings = @import("strings.zig");
3531
test {
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;

src/allocator.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ const std = @import("std");
44
const mem = std.mem;
55
const testing = std.testing;
66

7-
const RAssert = @import("errors.zig").RAssert;
7+
const r = @import("r.zig");
8+
const rzig = @import("Rzig.zig");
89

10+
const RAssert = rzig.errors.RAssert;
911
const Allocator = mem.Allocator;
1012

11-
const r = @import("r.zig");
12-
1313
/// Points to start of allocated region for free and resize.
1414
/// Following implementation of `std.heap.c_allocator`.
1515
fn getHeader(ptr: [*]u8) *[*]u8 {

src/constants.zig

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/errors.zig

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
//! R control flow affecting error handling and warnings
22

3-
const r = @import("r.zig");
4-
const Robject = @import("types.zig").Robject;
5-
63
const std = @import("std");
74
const fmt = std.fmt;
85
const testing = std.testing;
96

7+
const r = @import("r.zig");
8+
const rzig = @import("Rzig.zig");
9+
10+
const Robject = rzig.Robject;
1011
pub const ERR_BUF_SIZE = 1000; // undocumented. Seems like errors are truncated to this value from testing.
1112

1213
/// Asserts expression. If false prints error to R managed stderr and returns

src/eval.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Related to evaluation of R expressions
2-
const rzig = @import("Rzig.zig");
2+
33
const r = @import("r.zig");
4+
const rzig = @import("Rzig.zig");
45
const Robject = rzig.Robject;
56

67
pub fn eval(expr: Robject, envir: Robject) Robject {

src/gc.zig

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
//! GC related
22

3-
const r = @import("r.zig");
4-
const Robject = @import("Rzig.zig").Robject;
5-
const RAssert = @import("errors.zig").RAssert;
6-
73
const std = @import("std");
84
const math = std.math;
95
const testing = std.testing;
106

7+
const r = @import("r.zig");
8+
const rzig = @import("Rzig.zig");
9+
10+
const Robject = rzig.Robject;
11+
const RAssert = rzig.errors.RAssert;
12+
1113
const ProtectError = error{
1214
StackOverflow,
1315
UnprotectTooMany,

src/io.zig

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
//! R printing utilities
2-
const r = @import("r.zig");
32
const std = @import("std");
43
const io = std.io;
54
const math = std.math;
65
const testing = std.testing;
76
const fmt = std.fmt;
87

9-
const errors = @import("errors.zig");
10-
const Robject = @import("types.zig").Robject;
8+
const r = @import("r.zig");
9+
const rzig = @import("Rzig.zig");
10+
const errors = rzig.errors;
1111

12+
const Robject = rzig.Robject;
1213
const BUFFER_SIZE = r.BUFSIZE;
1314

1415
const PrintError = error{

src/strings.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ const mem = std.mem;
33
const math = std.math;
44

55
const r = @import("r.zig");
6-
const types = @import("types.zig");
7-
const errors = @import("errors.zig");
8-
const vec = @import("vectors.zig");
6+
const rzig = @import("Rzig.zig");
7+
const errors = rzig.errors;
8+
const vec = rzig.vec;
99

10-
const Robject = types.Robject;
10+
const Robject = rzig.Robject;
1111

1212
pub const Encoding = enum(c_uint) {
1313
native = 0,

0 commit comments

Comments
 (0)