-
Notifications
You must be signed in to change notification settings - Fork 1
/
oldparser.R
161 lines (137 loc) · 4.84 KB
/
oldparser.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
#
# Henry Samuelson
#
# This is the main parser. This is used to read through the file and call the respective libraries
#
script <- readLines("mainScript.txt")
# From loading from a file have to conver to table matrix
script <- as.matrix(script)
# Remove all comments
updatedScript <- 0
counter <- 1
for(i in 1:length(script)){
if(isTRUE(strsplit(script[i], "")[[1]][1] != "#")){
updatedScript[counter] <- script[i]
counter = counter + 1
}
}
script <- updatedScript
# takes the processed script as input
findBounds <- function(script, startingVal = 1){
lowerBound <- 0
upperBound <- 0
moduleName <- 0
for(i in startingVal:length(script[,1])){
if(script[i,] == "**R" || script[i,] == "**python" || script[i,] == "**js"){
lowerBound <- i
moduleName <- script[i-1,]
for(j in i:length(script[,1])){
if(script[j,] == "**/"){
upperBound <- j
return(c(lowerBound, upperBound, moduleName))
}
}
}
}
}
processBounds <- function(){
counter <- 1
bounds <- c("lower", "upper", "moduleName")
for( i in 1:length(script[,1])){
bounds <- rbind(bounds, findBounds(script, startingVal = counter))
counter <- findBounds(script, startingVal = counter)[2]
if(counter == length(script[,1])){
break
}
}
bounds <- bounds[-1,]
return(bounds)
}
#Send Processing calls
allbounds <- processBounds()
titleRows <- as.integer(allbounds[,1])
for(i in 1:length(titleRows)){
w <- script[(as.integer(allbounds[i,1])+1):(as.integer(allbounds[i,2])- 1),]
if(script[titleRows[i],] == "**R"){
# Write to file and run
file.create("runner.R")
writeLines(as.character(w), con = "runner.R", sep = "\n", useBytes = FALSE) # auto indents lines
result <- shell(paste(paste0(Sys.getenv("R_HOME"), "/bin/Rscript.exe", collapse =""), "runner.R"), intern = T)
file.remove("runner.R")
print(result)
}
if(script[titleRows[i],] == "**python"){
# Write to file and run
file.create("runner.py")
writeLines(as.character(w), con = "runner.py", sep = "\n", useBytes = FALSE)
result <- shell("python runner.py", intern = T)
file.remove("runner.py")
print(result)
}
if(script[titleRows[i],] == "**js"){
file.create("runner.js")
writeLines(as.character(w), con = "runner.js", sep = "\n", useBytes = FALSE)
result <- shell(paste("node runner.js"), intern = T)
file.remove("runner.js")
}
}
#This processes Pipelines
for(i in 1:length(script)){
if(length(strsplit(script[i], " ")[[1]]) == 3){ #If this is true process the module
if(strsplit(script[i], " ")[[1]][1] == "**<<"){
pipeLine <- strsplit(script[i], " ")[[1]]
#pipeLine[2] #js mod name
#pipeLine[3] #R mode name
#First pipe
firstPipe <- allbounds[allbounds[,3] %in% pipeLine[2],]
firstLower <- as.integer(firstPipe[1])
firstUpper <- as.integer(firstPipe[2])
firstRange <- script[((firstLower+1):(firstUpper-1)),]
print(firstRange)
#Second Pipe
secondPipe <- allbounds[allbounds[,3] %in% pipeLine[3],]
secondLower <- as.integer(secondPipe[1])
secondUpper <- as.integer(secondPipe[2])
secondRange <- script[((secondLower+1):(secondUpper-1)),]
print(secondRange)
#js goes into r first into second
# So find the first value first
#processedData <- 0
if(script[firstLower] == "**js"){
file.create("pipeRun.js")
writeLines(as.character(firstRange), con = "pipeRun.js", sep = "\n", useBytes = FALSE)
processedData <<- shell("node pipeRun.js")
file.remove("pipeRun.js")
}
if(script[firstLower] == "**R"){
}
if(script[firstLower] == "**python"){
file.create("pipeRun.py")
writeLines(as.character(firstRange), con = "pipeRun.py", sep = "\n", useBytes = FALSE)
processedData <<- shell("python pipeRun.py", intern = T)
file.remove("pipeRun.py")
print("this is the python processed data")
print(processedData)
}
# Find second lanaguge and then run it
if(script[secondLower] == "**js"){
}
if(script[secondLower] == "**R"){
file.create("pipeRun.R")
writeLines(as.character(secondRange), con = "pipeRun.R", sep = "\n", useBytes = FALSE)
outputData <<- shell(paste(paste0(Sys.getenv("R_HOME"), "/bin/Rscript.exe", collapse =""), "pipeRun.R" , processedData), intern = T)
file.remove("pipeRun.R")
outputData <<- strsplit(outputData, "")[[1]][5:length(strsplit(outputData,"")[[1]])] # removes a [1]__ from the front of the output
print("THis is the R processed data")
print(outputData)
}
if(script[secondLower] == "**python"){
}
}
}
}
master <- function(){
for(i in 1:length(script)){
headTag <- strsplit(script[i], " ")[[1]]
}
}