RE: PicoBlaze Simulator in JavaScript
August 18, 2022 at 9:31 am
(This post was last modified: August 18, 2022 at 9:33 am by HappySkeptic.)
(August 18, 2022 at 6:43 am)FlatAssembler Wrote: HappySkepticA map that calls different pieces of code.
You mean, like virtual methods
You don't use a switch statement to handle polymorphism. That is the one "bad" use of a switch statement that I agree with.
Virtual methods are implemented by pointers to function calls. The pointer gets set at object instantiation based on the type of object instantiated. Each object instance has a virtual function table (if it has at least one virtual method). There is one pointer in the table for each virtual method.
A map is different. A map of function pointers would allow one to find the function pointer based on a Key of some type (string or int, for instance). Internally a Map uses a a tree or a hashtable, but you don't care how its implemented.
If you haven't used a map yet, you need to learn about them. ES6 javascript has maps. Any regular Object in javascript is a map with "string" as the type of key (though it has performance issues if you constantly change its contents). However, a generic Map could use any type as the key (as long as the type is hashable and can be tested for equality).
In Flash, the name Dictionary was used instead of Map. In some ways I like that description.