Skip to content

Commit

Permalink
R: capture the loop counter of for control structure
Browse files Browse the repository at this point in the history
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
  • Loading branch information
masatake committed May 8, 2020
1 parent ee67009 commit 82036e8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Units/parser-r.r/r-loop-counters.d/args.ctags
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--kinds-R=+vz
--sort=no
6 changes: 6 additions & 0 deletions Units/parser-r.r/r-loop-counters.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
`%*%` input.r /^`%*%` <- function (v, p) {$/;" f
v input.r /^`%*%` <- function (v, p) {$/;" z function:`%*%`
p input.r /^`%*%` <- function (v, p) {$/;" z function:`%*%`
r input.r /^ r <- 1$/;" v function:`%*%`
i input.r /^ for (i in 1:p) {$/;" v function:`%*%`
j input.r /^for (j in 1:3) {$/;" g
20 changes: 20 additions & 0 deletions Units/parser-r.r/r-loop-counters.d/input.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# defining an operator
`%*%` <- function (v, p) {
r <- 1
if (p == 1) {
r <- v
} else {
for (i in 1:p) {
r <- r * v
}
}
r
}

print (c(2 %*% 10, 2%*%2, 2%*%3)[1:2])

# j introduced as a global var.
for (j in 1:3) {
print (j)
}
print (j)
22 changes: 22 additions & 0 deletions parsers/r.c
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,26 @@ static bool preParseExternalEntitiy (tokenInfo *const token, tokenInfo *const fu
return r;
}

static bool preParseLoopCounter(tokenInfo *const token, int parent)
{
bool r = true;
TRACE_ENTER();

tokenReadNoNewline (token);
if (tokenIsType (token, SYMBOL))
makeSimpleRTag (token, parent,
(parent == CORK_NIL) ? K_GLOBALVAR: K_FUNCVAR , NULL);

if (tokenIsEOF (token)
|| tokenIsTypeVal (token, ')'))
r = false;

TRACE_LEAVE_TEXT(r
? "unread tokens and request parsing again to the upper context"
: "parse all arguments");
return r;
}


/* If funcall is non-NULL, this pair represents the argument list for the function
* call for FUNCALL. */
Expand All @@ -867,6 +887,8 @@ static void parsePair (tokenInfo *const token, int parent, tokenInfo *const func
if (tokenIsKeyword (funcall, LIBRARY) ||
tokenIsKeyword (funcall, SOURCE))
done = !preParseExternalEntitiy (token, funcall);
else if (tokenIsKeyword (funcall, FOR))
done = !preParseLoopCounter (token, parent);
}

if (done)
Expand Down

0 comments on commit 82036e8

Please sign in to comment.