Answer the question
In order to leave comments, you need to log in
How to replace eval and how to correctly transform a human-readable description into executable code?
I'm a little not strong in the mat part of problems of this kind, perhaps such problems have a certain name and ready-made methods for solving, so I decided to ask the community. Couldn't google the right query.
Situation:
1) There is a code written in the OOP paradigm with a bunch of classes, objects, methods, etc. etc.
2) You need to create a human-readable descriptive language with your own syntax (or use one of the existing ones, for example xml) so that the executable code from paragraph 1 automatically creates class instances, runs methods, etc. etc. based on the description file fed to him.
In other words: the structure and syntax of a descriptive language must somehow be incorporated into the executable code, i.e. create something like a parser, only after parsing the received data, it will not display them on the screen and write them to the database, which is usually required from the parser, but execute instructions based on what description the received data contains.
Of course, actions with received data and executable logic can be covered by if, else, etc., but in fact, there are not enough built-in programming language tools, because the descriptive part is variable, which means that the amount of logic in potentially created objects is also variable. For example:
There is a class "Kolobok". It has a method (some kind of internal logic) that must make a decision when a subject (s) appears on the horizon: a fox, a wolf, a hare, a bear, as well as their combinations, the number of which is finite. When creating the Kolobok object, we must give it a kind of character (using a descriptive language), on which further actions with other subjects of the game and their combinations will depend. The task is to make sure that each kolobok has a unique character, but at the same time, each of them has the same methods (walk, look, talk, go around, run away). Variation examples:
- The descriptive part of one kolobok may look briefly like this: when a fox appears on the horizon, run away, when a hare appears, talk. His brother (another object) has a similar character, but a little crazy - when meeting any hero other than a hare or a fox, a random method is launched.
- At the second kolobok, on the contrary, when a fox appears on the horizon - talk to her (but in case of an attack - run away), when a hare appears - bypass, if the hare moves in his direction - go on a run.
- The third kolobok has a different character - to speak only when all the other characters are nearby, in other cases - to run away.
And this internal logic for each kolobok must be unique and depend on the parameters-instructions (character) passed when creating the "Kolobok" object.
For such tasks, it seems like they use eval , as far as I know, strings with conditions can be passed to this method. But eval is dangerous, although it contains exactly the essence of what I'm asking about: when creating objects, each of them will have its own, and if necessary, a unique logic (character). Those. you need to create a kind of safe alternative to eval, so to speak.
From the description of eval MDN:
if(eval(string)){
//do something
}
string . A string represented by a JavaScript expression, statement, or sequence of statements. The expression can contain variables and properties of existing objects.
Answer the question
In order to leave comments, you need to log in
You need to make a parser, a lexer, and possibly a compiler for your Wishlist (in the jap that you did not specify)
It is approximately told that you will have to follow the path of
writing https://www.youtube.com/watch?v=byKYLHx5jHo
I advise
If your task didn’t immediately show keywords in your head or Google didn’t give you an answer, then you won’t write anything sane
when creating objects, each of them will have its own, and if required - a unique logic (character)if you slightly reduce the requirements, then you can describe the behavior and push the behavior into the settings
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question