-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerate Function.R
205 lines (163 loc) · 4.87 KB
/
Generate Function.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
source("Documentation.R")
generate_function <- function(name, file_name, params)
{
if (is.na(file_name) || str_detect(file_name, ".*Z.f"))
{
cat(paste("Skipping complex function", file_name, "\n"))
return ()
}
output_name <- paste0(name, ".R")
param_names <- filter(params, stringr::str_detect(intent, "in") | is.na(intent)) %>% arrange(original_order) %>% pull(name)
x <- paste0(
# Roxygen
generate_docs(file_name, name),
# Function definition
name, "<- function(",
paste(param_names, collapse = ","),
")\n",
"{\n\n",
# Type coercion for in parameters
"# In Parameters", "\n",
paste0(handle_in(params), collapse = "\n"), "\n\n"
)
other_params <- dplyr::filter(params, !(stringr::str_detect(intent, "in") | is.na(intent)))
for (i in 1:nrow(other_params))
{
this_param <- other_params[i,]
if (is_out(this_param))
{
# Define out parameters
x <- paste0(x, handle_out(this_param), "\n")
}
else
{
# Define hidden parameters
x <- paste0(x, handle_hide(this_param), "\n")
}
}
# Apply checks
#"# Check dimensions of input parameters", "\n",
#paste0(check_in(params), collapse = "\n"), "\n\n",
x <- paste0(x, "\n\n",
# Call Fortran
create_call(file_name, params), "\n\n",
# Return list
create_return(params) , "\n}"
)
# Handle reserved keywords
x <- stringr::str_replace_all(x, "(?<=(\\,|\\s))na(?=(\\,|\\s))", "na_")
x <- stringr::str_replace_all(x, "(?<=(\\,|\\s|\\$))NA(?=(\\,|\\s|\\=))", "\\`NA\\`")
x <- stringr::str_remove_all(x, "(?<=complex)\\*[0-9]*")
x <- stringr::str_replace_all(x, "\\`\\?\\`", "ifelse")
file_name <- paste0("R/", name, ".R")
readr::write_file(x, file = file_name)
formatR::tidy_file(file_name)
}
check_in <- function(params)
{
filt_params <- dplyr::filter(params, stringr::str_detect(intent, "in") & !is.na(dimension))
if (nrow(filt_params) == 0)
return ("")
filt_params %>%
mutate(text = paste0(
"if (dim (", name, ") != c(", dimension, ")) stop(\"Incorrect dimensions for matrix ", name, "\")"
)) %>%
pull(text)
}
create_return <- function(params)
{
out_names <- params %>%
filter(stringr::str_detect(intent, "out")) %>%
pull(name)
return (
paste0(
"return (list(",
paste0(out_names, " = res$", stringr::str_to_upper(out_names), collapse = ","),
"))"
)
)
}
create_call <- function(file_name, params)
{
param_string <- params %>%
arrange(original_order) %>%
mutate(text = paste0(stringr::str_to_upper(name), "=", name)) %>%
pull(text) %>%
paste(collapse = ",")
return (
paste0(
"res <- suppressWarnings(.Fortran(\"",
stringr::str_split_fixed(file_name, "\\.", 2)[1,1],
"\",", param_string,
"))"
)
)
}
is_out <- function(param)
{
this_intent <- param$intent[1]
return (!is.na(this_intent) && this_intent == "out")
}
handle_out <- function(params)
{
out_params <- dplyr::filter(params, intent == "out")
if (nrow(out_params) == 0)
return ("")
out_params %>%
mutate(text = paste0(
name, " <- ",
if_else(is.na(dimension),
paste0("as.", type, "(0)"),
paste0("array(as.", type, "(0), c(", dimension, "))"))
)) %>% pull(text)
}
handle_in <- function(params)
{
in_params <- dplyr::filter(params, stringr::str_detect(intent, "in") | is.na(intent)) %>%
filter(is.na(dimension)) %>%
arrange(!is.na(depend), name, depend)
if (nrow(in_params) == 0)
return ("")
text <- in_params %>%
mutate(text = paste0(name, " <- as.", type, "(", name, ")")) %>%
pull(text)
return (text)
}
handle_hide <- function(params)
{
hide_params <- dplyr::filter(params, stringr::str_detect(intent,"hide"))
if (nrow(hide_params) == 0)
return ("")
hide_params %>%
dplyr::rowwise() %>%
dplyr::mutate(value = handle_shape_dimension(value, dimension, type)) %>%
dplyr::mutate(
text = paste0(name, " <- " , value)
) %>%
pull(text)
}
handle_shape_dimension <- function(value, dimension, type)
{
if (nchar(value) == 0 & (nchar(dimension) == 0 | is.na(dimension)))
return ("")
if (nchar(value) == 0)
{
# Use dimension
return (paste0("array(as.", type, "(1), c(", dimension, "))"))
}
if (!stringr::str_detect(value, "shape"))
{
# If/else in value.
if (stringr::str_detect(value, "\\?"))
{
bits <- stringr::str_match(value, "\\(([^\\?]*)\\?([^\\:]*)\\:(.*)\\)")
value <- paste0("ifelse(", bits[1,2], ",", bits[1,3], ",", bits[1,4], ")")
}
return (paste0("as.", type, "(", value, ")"))
}
obj_index <- str_extract(value, "(?<=shape\\()[^\\)]*")
bits <- stringr::str_split_fixed(obj_index, ",", 2)
dim_string <- paste0("dim(", bits[1], ")[", as.integer(bits[2])+1, "]")
res <- stringr::str_replace(value, "shape\\([^\\)]*\\)", dim_string)
return (res)
}