If conditional with no comparison infinitely executing - actionscript-3

In the code
while(true)
{
this.rv1 = pdata.ReadString();
this.rv2 = pdata.ReadLong();
if(false)
{
continue;
}
break;
}
Why does if(false) infinitely execute, while other languages such as python's if False: Do.Something() never execute?
I didn't write the code in question, but I can't proceed without being able to read it - and I'm no AS3 pro!

Related

How to implement custom Tcl event loop?

I saw a few other posts, but they were all in TCL and I'm looking to do this in my embedded interpreter using C++. The problem I'm having is that I need my own event loop so that I can read off the network, some where's where I can check a socket. I saw Tcl_SetMainLoop() but not sure how it works.
My app is structured like the following.
int Tcl_AppInit(Tcl_Interp *interp)
{
if (Tcl_Init(interp) == TCL_ERROR)
return TCL_ERROR;
if (MyTcl_Init(interp) == TCL_ERROR)
return TCL_ERROR;
return TCL_OK;
}
int main(int argc, char* argv[])
{
//...
Tcl_Main(argc, argv, Tcl_AppInit);
/* Replaced Tcl_Main for this, didn't work.
Tcl_SetMainLoop([]() {
Tcl_DoOneEvent(0);
});
*/
}
I do not use Tk. Any thoughts on how to set a custom event loop?
The classic example of how to an event loop in Tcl is the vwait command. The core of that is this code:
done = 0;
foundEvent = 1;
while (!done && foundEvent) {
foundEvent = Tcl_DoOneEvent(TCL_ALL_EVENTS);
if (Tcl_Canceled(interp, TCL_LEAVE_ERR_MSG) == TCL_ERROR) {
break;
}
if (Tcl_LimitExceeded(interp)) {
Tcl_ResetResult(interp);
Tcl_SetObjResult(interp, Tcl_NewStringObj("limit exceeded", -1));
break;
}
}
The done variable is set by a callback when the end-loop-triggering event happens (a write on a variable), and the clauses with cancellation and limit management can probably be omitted in your own code. The cut-down bare-bones version is:
done = 0;
foundEvent = 1;
while (!done && foundEvent) {
foundEvent = Tcl_DoOneEvent(TCL_ALL_EVENTS);
}
Yes, it delegates most of its work to Tcl_DoOneEvent (which is part of the notifier layer). If you want to plug your fancy socket into that, the easiest way is to write your own event handler and install it with Tcl_CreateFileHandler or Tcl_CreateChannelHandler; probably the former, assuming you're not on Windows (as it relies on the POSIX concept of a file descriptor) and on Windows you need to do the work to make a channel type (because the underlying notifier system works a little differently on that platform under the hood). Once you do that, you can use the standard event loop; your custom handler (in C or C++) will be called at the right time. (The ClientData arguments are really just an arbitrary pointer that will be passed uninterpreted through Tcl to your callback; you'll probably cast that back to a pointer to the real object type as the first thing you do in the callback; that's what everyone else does.)
It's possible to install your own low-level event handling engine with Tcl_SetNotifier — call it very early if you want to do that — but it is usually a bad idea. In particular, you don't need a custom event loop to just handle a custom socket type. A better use for a custom event loop is integrating Tk with some other GUI toolkit, but that's a far more complex use case!

Actionscript 3 equivalent of KeyboardEvent.Repeat

I'm very much a beginner when it comes to programming; I'm doing a flash game for my 2D Game class on Adobe Animator, and I had a doubt about the code that my instructor couldn't answer himself. Long story short, I want the character speed to increase based on how long the key has been pressed, so I came up with this:
spdX = spdX + aclMov*(tempo) - aclFrc*(tempo);
Where the variable tempo would increase as long as the key is being held down, and I would check if it is with KeyboardEvent.repeat, as in:
if(heldDown){while(heldDown){tempo += 1}}
else{tempo = 0}
spdX = spdX + aclMov*(tempo) - aclFrc*(tempo);
However when I try to do that, the output responds with "Property repeat not found on flash.events.KeyboardEvent and there is no default value". I assume that this is because KeyboardEvent.repeat is not defined in the medium I'm using. Is there anyway I can reproduce the same effect of KeyboardEvent.repeat, perhaps by creating a function that mimics what it would have done?
Thanks in advance.
(Edit 1)
I begin apologizing for my shortness of clarification, as well as my ignorance in the topic as I am barely a beginner when it comes to as3, and am not properly presented yet to many of the terms I've read.
So, thanks to the meaningful contributions of comments, I already have been given a glimpse of what kind of workaround I would need to do to substitute KeyboardEvent.repeat. There are other parts of the code of relevance to the problem, as well:
stage.addEventListener(KeyboardEvent.KEY_DOWN,pressKey)
function pressKey (Event){
(...)
if(Event.keyCode == (Keyboard.A)) {left = true;}
(...)
}
stage.addEventListener(KeyboardEvent.KEY_UP,releaseKey)
function releaseKey (Event){
(...)
if(Event.keyCode == (Keyboard.A)) {left = false;}
(...)
}
This is how the code was intended to go. It was suggested that I use the getTimer() method to record the moment the event KEY_DOWN happens, stopping when the KEY_UP comes into effect. Problem is, how can I increment the code to make it differentiate between those two events, and more specifically, how can I adapt the ENTER_FRAME event so that differentiating between them still works with it? Here's the most relevant parts of it, by the way:
addEventListener(Event.ENTER_FRAME,walk);
function walk(Event) {
if(left) {
(...)
char.x -= spdX;
(...)
}
I assume that the code worked up till now because, as the state of "left" constantly switched between "true" and "false", the if conditional was met repeatedly, leading the character to move. However, if I try to make it so that the conditional depends on "left" staying "true" for a certain time, the code becomes incompatible with itself.
In short, it brings the question of how to adapt the "KEY_[]" event listeners and the "walk" function to work, in using the getTimer() method, to work together.
Again, thanks in advance.
I believe that would be somehow self-explanatory .
stage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onUp);
addEventListener(Event.ENTER_FRAME, onFrame);
var pressedKeyA:int;
// Never name variables and arguments with the same name
// as existing classes, however convenient that might seem.
function onDown(e:KeyboardEvent):void
{
if (e.keyCode == Keyboard.A)
{
// Let's record the moment it was pressed.
pressedKeyA = getTimer();
}
}
function onUp(e:KeyboardEvent):void
{
if (e.keyCode == Keyboard.A)
{
// Mark the key as not pressed.
pressedKeyA = 0;
}
}
function onFrame(e:Event):void
{
// It is true if it is not 0.
if (pressedKeyA)
{
// That's how long it has been pressed. In milliseconds.
var pressedFor:int = getTimer() - pressedKeyA;
// Change the position with regard to it.
// For example 1 base + 1 per second.
char.x -= 1 + pressedFor / 1000;
}
}

Skip subtree in Listener ANTLR4

Is there any Way to skip the parsing of specific block while using Listener in ANTLR4 using enter or exit method.
I have read link here but unable to make it work.
Thank You!
By the time you're using the Listener pattern with your own Listener class, the input is already correctly lexed and parsed. Therefore, the answer to your question is no. When you're using the listener you're typically just walking the tree post-parse.
Does that mean all is lost though? Of course not. All you have to do is simply not code the Enter or Exit events for those constructs you want to "ignore." It's that easy.
As to if-else statements, I've always implemented them using the visitor pattern like this:
As to how to program an if statement, I'll give you a peek at they way I implement them:
public override MuValue VisitIfstmt(LISBASICParser.IfstmtContext context)
{
LISBASICParser.Condition_blockContext[] conditions = context.condition_block();
bool evaluatedBlock = false;
foreach (LISBASICParser.Condition_blockContext condition in conditions)
{
MuValue evaluated = Visit(condition.expr());
if (evaluated.AsBoolean())
{
evaluatedBlock = true;
Visit(condition.stmt_block());
break;
}
}
if (!evaluatedBlock && context.stmt_block() != null)
{
Visit(context.stmt_block());
}
return MuValue.Void;
}
Granted, this probably doesn't make much sense out of context, but rest assured it works. To see this in its full context, please visit Bart Kiers for an excellent example of grammar and implementation .

What's the difference between looping through an array and using array.every()

What's the difference between looping through an array or using array.every() to assign a callback to each array element?
The array.every() function uses a test callback function on each array element, but stops once that test function returns false. If you loop through the array, it will go through every element regardless. In other words, the array.every() function is more properly used to test if every element of an array fits a certain criteria. See the documentation of array.every() for more details.
Suppose you want to check if every element of your array is of type IFoo, and depending on that perform a certain operation.
There are at least 3 ways to do this.
1. Iteration
var allFoo:Boolean = true;
for (var i:int = 0; i < array.length; i++) {
if (!(array[i] is IFoo)) {
allFoo = false;
break;
}
}
if (allFoo) {
// perform operation
} else {
// do something else
}
2. Enumeration
var allFoo:Boolean = true;
for each (var e:* in array) {
if (!(e is IFoo)) {
allFoo = false;
break;
}
}
if (allFoo) {
// perform operation
} else {
// do something else
}
3. Array.every()
function isElementFoo(item:*, index:int, array:Array):Boolean
{
return (item is IFoo);
}
if (array.every(isElementFoo)) {
// perform operation
} else {
// do something else
}
I expect the second one to be the fastest, whereas the third one is the most elegant due to the absence of any temporary variables. Ultimately which one you choose depends on the nature of your program as much as your own personal style and philosophy.
It is basically a convenience function that abstracts common uses of for or while loops on arrays. To make it a little quicker to code and depending on you preferences a little clearer to read.
You would get a little bigger overhead using array.every since it does a function call for every element butt this is a none issue in 99.99...% of the time especially on the flash platform.

slowing down a loop in a recursive function

I have a difficult problem with a recursive function. Essentially I need to 'slow down' a for loop within a function that repeatedly calls itself(the function);
Is this possible, or do I need to somehow extract the recursive nature of the function?
function callRecursiveFuncAgain(ob:Object):void{
//do recursive stuff;
for (var i:int = 0; i < 4; i++) {
_nextObj=foo
callRecursiveFuncAgain(_nextObj);
}
}
Try setTimeout
function callRecursiveFuncAgain(ob:Object):void{
// do recursive stuff
var i = 0;
function callNext()
{
if(i++ < 4)
{
_nextObj=foo;
callRecursiveFuncAgain(_nextObj);
setTimeout(callNext, 1000);
}
}
callNext();
}
You should use some function which will wait for some time or another function which will use much CPU and therefore will slow down your recursive function. Another way would be using debugger and breakpoints.
Are you serious? If you have a slow computer your CPU will have more load then a fast CPU which will NEVER work in a situation that needs a good solution. It's not even close to a crappy solution.
Try to use the setTimeOut that's in the flash.utils package http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/utils/package.html.
example use: setTimeout(delayedFunction, delay, arguments);
Note that the delay is expressed in milliseconds.
Check the 'clearTimeOut()' function (flash.utils) to clear your setTimeOut when you're done with it.