I have to create a program that reads another "non-C++" program from a text file and execute it. Right now, I think my partner has developed a program that reads the text file character by character, line by line. We also created some definitions, but I don't know if it worked quite well.
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
usingnamespace std;
struct code
{
string line;
string var;
string value;
};
class codelist
{
private:
struct code list[10];
public:
codelist();
void display();
};
codelist::codelist() //constructor reads in the information from the file.
{
ifstream ins;
ins.open("read.txt");
stringstream iss;
int count = 0;
while(ins.good()) //Gets the information from the input file.
{
getline(ins, list[count].line, ';');
iss << list[count].line;
while(iss.good())
{
getline(iss, list[count].var, '=');
getline(iss, list[count].value);
}
count++;
iss.clear();
}
}
void codelist::display()
{
for (int i=0; i<10; i++)
{
cout << list[i].var;
cout << list[i].value;
}
}
int main()
{
codelist code;
code.display();
system("pause");
return 0;
}
This is what I have in the text file:
a=10 - 2;
b = 20 - 5 * a / 4;
print b a;
a=b;
print a:
The question I have for you experts, how can I make it so the a and b are defined as variables and everything to the right side of the equal sign are stored into their variables.
For example, if my file reading is correct, how do I store
10 - 2
into
a
*I know I have a thread created already, but this is a different question now.
After you read in the variables, you would need variables in your program of the appropriate data types in which the values read in (and stored in your 'code' struct) are assigned.
It looks like you have 'list' with the attributes you need, now just iterate through that list (as you do in your display method) and assign the values (data values) to variables you create in your program.
There is no way to magically create variables with names given to them at runtime - you can only assign values to variables.
If you really wanted to 'create' a new program, you would just write verbatum what you read in out to a text file with a .c/.cpp extension, but be sure to put in correct context information (includes/main function etc) so that it will run. I'm not sure this is what you want to do though.
you are parsing in the program you have in the file.
what you should achieve is instead of recognizing "10 - 2" as a whole string, use a vector of string and store "10", "-", and "2" as members. iterate through the vector and check out any arithmetic signs, in this case, "-". use a switch clause to catch all allowed arithmetic functions and use each string to identify the skip logic. in a simpler word, let the switch on the input value decide how should deal with 10 and 2 here.
You can use built-in functions from ctype.h to convert integers. but for float numbers, you have to recognize "." sign. also, for recognition of calculation sequence, watch out for parenthesize. they are hard to catch as you have to catch them as a pair. the hint is create a recognizer, so to speak, that starts from the head and the tail of the string. chop them into sub-strings.
do the correct arithmetic procedure on the data members according to the arithmetic sign you recognized and store the result in the corresponding value slot.
essentially, what you are doing is a prototype for a simple compiler.
For the math stuff, you could consider using a infix-postfix conversion algorithm. It uses two stacks (an operator and operand stack) to store the parts of the mathematical expression, then popping the stack in the right order, you get an expression you can evaluate. It sounds complicated, but there are numerous examples out there. A quick google search gives: http://www.dreamincode.net/forums/topic/37428-converting-and-evaluating-infix-postfix-and-prefix-expressions-in-c/ - Haven't read over this myself, but I have done something like it in the past.
It might be a little full blown, but it's interesting to read about. It is how calculators (used) to work internally, and maybe still do?