Title: How would I do this?
Description: My head hurts
Kylevision - August 18, 2003 11:22 PM (GMT)
This may seem stupid and long buit here goes... How would I make a program that got its source from another file? like if text.txt had a hello world program in it, our program.exe would run it. Does anyone know how to do that?
Shadow of the Moon - August 18, 2003 11:29 PM (GMT)
You mean like an interperter? The only way I can think of would be to have a list of every possiblity and check that on each line.
Too bad C++ has no execute_string(); -_-
Kylevision - August 18, 2003 11:38 PM (GMT)
well, what I wanted to do was get the example of this and make it sp that lets say runner.exe would run test.txt and in test.txt was this:
| CODE |
main() [start] draw_text("Hello World!"); [end]
|
and it would make it be:
| CODE |
#include <iostream.h> void main() { cout<<"Hello World"<<endl; }
|
I was thinking of using #define for this, like in my pseudo code example (I don't have it with me)
Shadow of the Moon - August 18, 2003 11:46 PM (GMT)
You would most likely have to read a line from the file, filter it into different reconizable parts, and then test to see if it is a valid command, then act according to what it says.
Macros wouldn't really help, as you would have to handle the data as a string, though it would make it more readable
Kylevision - August 18, 2003 11:50 PM (GMT)
| QUOTE (Antitrust @ Aug 18 2003, 11:46 PM) |
You would most likely have to read a line from the file, filter it into different reconizable parts, and then test to see if it is a valid command, then act according to what it says.
Macros wouldn't really help, as you would have to handle the data as a string, though it would make it more readable |
thats what I dont know how to do, I get the concept, I will keep trying though.
Dragon - August 19, 2003 12:11 AM (GMT)
Yeah, you would have to be able to read parts of that file and then interpret the data and act accordingly.
Kylevision - August 19, 2003 12:15 AM (GMT)
| QUOTE (Dragon @ Aug 19 2003, 12:11 AM) |
| Yeah, you would have to be able to read parts of that file and then interpret the data and act accordingly. |
thats what I dont know how to do, I get the concept, I will keep trying though.
TheHawgMaster - August 19, 2003 12:34 AM (GMT)
I will attempt to explain the basics of making an interpreter. For starters you will probably want to have a language similar to assembler. Here is an example of what some code might look like:
| CODE |
var i; This would create a variable set i 0; i = 0 :jump; This is a point for your program to jump to if= i 0; if(i = 0) jmp jump; Jump to the jump point set i 100; Else i = 100
|
I'll post more later but I have to go now.
Kylevision - August 19, 2003 01:11 AM (GMT)
Ok, I look forward to it :)
TheHawgMaster - August 19, 2003 05:09 PM (GMT)
The reason you would want a language like this to start with is because it would be easy to interpret into byte codes. Byte codes are numbers that repesent a command word. Here is an exampe of how to create byte codes:
#define var 0
#define set 1
#define ifq 2
#define jmp 3
In order to write the script into the file, you will probably want to use a standard text editing program like Notepad. You will have to convert the strings that you read from the file into interers. There are standard functions for this but I can't remember the names right now.
Make it so that all the variables and numbers are replaced with their index in an array. Look back at the little program. In the first line your program needs to take the variable I and store it in an array of probably floats and an array of strings. Then in the rest of the code whenever the interpreter runs into a string named I It will look through all the strings for I and write it's index in the array. You should do the same thing with numbers. It sounds kind of strange but you should create a float variable and string variable for 0 also in that program.
Here is an example of what the file format of your program might look like:
Number of variables
All the variable values
All the program code