In a rector rule's refactor function, how to print the current node as code? - rector

Having a Node in the refactor function of a rule, how can I print the full node as php code?

Related

Appending in linked lists

Repl: https://repl.it/#Stylebender/LinkedList#index.js
So I know that the code is correct but I'm just curious how Line 21 works with respect to the append method().
If we declare that the pointer (this.tail.next) should point to newNode, shouln't this.head.next be referencing a single newNode each time so that this.head.next would ever only be === { value: X, next: null } of the latest newNode that is being appended.
I know this is clearly wrong and clearly not the case since when you run the repl and start invoking the append method multiple times, you get an increasingly larger and larger next value.
I guess my question is that why doesn't newNode in the form of { value: X, next: null } just overwrite the head property each time we call the append method since isn't this what we are telling the program to do? (Even though I know this is clearly wrong)
The head of a linked list will always point to the same node. When new nodes are added, they're added to the end become the tail, the head doesn't actually change.
In your case when the first node is added, the head only changes because it isn't pointing to null anymore, it points to the new node that was just added. However it's value isn't changing. Any consecutive nodes that are added are tacked on to the end of the list. So when a third node is added, the second node changes to point to the third node.

c++: evaluation of an "if" statement in a lambda function

Suppose I have the following snippet of code:
bool flag = true;
auto myFunction = [](int a, int b, bool flag)
{
if (flag)
{
// do something with a and b
}
}
Later in the code, I call myFunction thousands of times in a loop, for the same value of flag.
Then, I have another loop that also calls myFunction thousands of times, but for a different value of flag.
My understanding is that, being a lambda function, it is an inline function and thus will be repeated wherever it is called.
My question is: will the compiler evaluate the if statement before "copying" the inline function, and thus not have to perform that check at every single iteration?
Disclaimers:
I know that this may fall under the category of micro-optimization, but I would like an answer nonetheless.
My example is silly; I could just put the if statements outside the loops. But this is just meant to be a representative example of a much more complicated case.
My use of lambda functions is inspired from the answer to this question.
Thanks!
My question is: will the compiler evaluate the if statement before "copying" the inline function, and thus not have to perform that check at every single iteration?
The language does not require it. An optimizing compiler might be able pull that off if it knows the value of flag at compile time. However, it's hard telling without looking at the assembly code generated by the compiler.

Ocaml - match n-ary_tree with empty tree

Let's say I have a n-ary tree type
type tree = Node of (char*tree) list ref;;
and this correspond to the empty tree
let empty_tree ()= Node(ref[]);;
I'm trying to write a function that only looks if my tree is empty or not, such as
let checkIsEmpty t = match t with empty_tree-> print_string "tree is empty";;
But when I write checkIsEmpty empty_tree;; it just return a unit, it won't print "tree is empty"
I also tried that way
let checkisEmpty t = match t with z when z = empty_tree-> print_string "tree is empty";;
Sadly it still fails.
How can I look if my tree is empty? I'd like to keep the match with and the way tree is declared (Empty is not part of the type..) if possible.
Thanks!
You most likely need to flush the output to see it. You can write this:
print_string "tree is empty"; flush stdout
However, your first match doesn't work. Any identifier in a pattern introduces a new variable. So the variable empty_tree in your first implementation will always match whatever tree you pass to your function. (Or indeed it will match any value whatsoever.)
The second implementation should work a little better once you add flush stdout, and assuming there is a global value empty_tree that is an empty tree. But you should also add a case that matches when the tree is not empty; otherwise you'll get an exception for that case.
Furthermore your empty_tree is a function that returns an empty tree. It's not an empty tree itself (as your latest code is assuming).
(I would suggest you not modify your question too much after asking it. It makes it hard to write an answer that corresponds to what you asked.)

Java Grammar To AST

In java grammar I have a parser rule,
name
: Identifier ('.' Identifier)* ';'
;
How to get all the identifiers under a single AST tree node?
It seems impossible to me only with your lexer-parser.
For this, you will need the called: tree-walker.This third part of the parsing process will make you able to go through the generated AST and, with a counter, print the number of occurrences.
I let you a reference here in case you decide to implement it.
https://theantlrguy.atlassian.net/wiki/display/ANTLR3/Tree+construction
I hope this would help you!

endless loop on code analysis with FxCop Introspection

I'm trying to write a custom FxCop code analysis rule
that will warn developers from methods containing too deeply nested code blocks,
and will urge them to re-factor out the mess.
ex. I'm trying to avoid the following situation:
if(condition)
{
foreach(var item in items)
{
if(anotherCondition)
{
for(var product in item.Products)
{
// even more nested statement blocks...
}
}
}
}
I get a stackoverflow when I override the VisitBlock(Block block) method
that counts the block's depth, because apparently, there is a cyclic reference
from one of the properties of the block to
the block itself.
i.e. the following is true for some i: block.Statements[i] == block
Why does such a cyclic reference exist? How to avoid it?
Thanks!
after some more research, I've figured out I had actually TWO main problems
The VisitXXX methods are not visiting nodes in an abstract syntax tree of the source code
but actually visit nodes in the generated IL. Just compare the generated IL instructions per method
and the generated statements per method.Body.
I wonder what we could have achieved if FxCop
could provide us with a true AST visitor?
To answer my initial question, to prevent developers of writing too many nested
code blocks, we should just scan the method code by ourselves, I mean, take out the start line and the end line inside the SourceContext property of the method.Body and keep track of every
'{' and '}' we find. Increment counter for '{' and decrement counter for '}'. That should work, right?

Resources