What it's really doing is giving me a blueprint for the state machine I don't want to write from scratch, and giving me a flowchart to keep track of it to boot.Īt around the time I realized that a state machine* and a regular expression were pretty much interchangable and convertible from one form to the other mathematically, I decided I really liked regular expressions. In this case, I'm going to fool it into thinking it's generating pretty graphs and C# code for matching regular expressions. I'm not above threatening my computer, but I find it's more effective to resort to trickery to get it to do what I want. They are sort of "asynchronish" in that they are effectively non-blocking, but more importantly for us, they can handle their work a small bit of data at a time, which means we can for example, read a character from the disk, parse it, and then move to the next value, and repeat.Īs I said, I prefer to cajole my computer into doing my work for me whenever possible, because it's a better programmer than I am, in terms of the number of bugs it tends to avoid. They are also useful for something very similar - iteratively parsing, and that's what we'll be doing. Coroutines are useful for cooperative multithreading because they don't typically block for long periods. A coroutine is called multiple times, each time performing a small fraction of its work before returning control to the caller. A traditional function is called once, performs an entire unit of work and then returns. It performs one piece of its total function each time its called. If you do, well aren't you fancy! You can skip this mess, unless you're interested, but with your shiny new compiler you have less hackish options.Ī coroutine basically is a method that is broken up iteratively. C++20 is relatively new, so you may not either. Since I was targeting C++11 for reasons, I don't have those. They have these in C++20 but spoiler alert, the compiler builds state machines in your code to make them work. In this case, we're going to use a state machine to make a sort of coroutine - a restartable method. The generated code is ugly, but at least there aren't bugs until I touch it. Even then however, I can use the computer to help me write the code, which I can then modify. The trouble with them is they're murder to maintain so I prefer to make the computer deal with that where possible, though that's not always possible. If I can solve a problem with one, I get giddy. It's better than staring at a switch wrapped around a bunch of if statements. You'll want to install Graphviz probably, at least if you want a pretty flow chart to go along with the state machine.I've provided a link to it above as well. It's dusty code, but it's perfect for this. Speaking of the code, you'll need the code from this article since that's what I'm using to generate the C# state machine we'll be porting.I use MonoDevelop on Linux, but the code in question - or maybe I should say the questionable code - will run on Windows and work from Visual Studio. You'll need a computer that isn't presently on fire.But how do you do both at the same time? I needed to solve that problem, and now I'll show you how I did it. However, there's no reason you shouldn't be able to preserve that entire number as a string of characters, especially if you can stream it in chunks. For example, if you had pi to one million digits, there's no way you're holding that in value space at least with standard floating point. My goal was to present a best effort numeric approximation of a JSON number in value space, while also preserving it in lexical space for the purpose of round tripping. I was not, but this could be adapted to one, because the principles in here are similar in terms of parsing. You might suspect I was writing an arbitrary precision library. How can I parse a number of indefinite length, that may be too large to hold in memory at once? atoi() and strtod() fall down here. This article was inspired by a recent coding adventure of mine wherein I set out to solve a problem. And now for something completely different.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |