Command Based Scripting Language

Started by
25 comments, last by choffstein 19 years, 3 months ago
Since you're using C++, check out the fstream package in addition to the C f-functions.

In particular, the getline() method of the ifstream class which lets you read n characters until a specified delimiter.

Like so:
ifstream in("script.x");in.getline(buffer, MAX_CMD_LEN, ' ');


Also, the fstream classes are a bit more intuitive when it comes to input/output since they provide overloaded operators for << and >>.

Like so:
ifstream stream("script.x");int arg1, arg2;stream.getline(strCommand, MAX_CMD_LEN, ' ');stream >> arg1 >> arg2;


Of course that is just pseudo code without any error checking what so ever. If you want to be really serious you should think about writing a tokenizer...

Hope it sheds some light
________________________________pro.gram.mer - an organism that turns caffeine into code
Advertisement
well brandon it didnt work :S
and saandman could you show me what headers are used for that

I wish that there was a example for this even a example or tutorial for something to read line by line
Hey!
I found this example

#include <string.h> /* a header needed for scanf function */
#include <stdio.h> /* a header needed for FILE */

/* define the pointer for the input file globally */
FILE *input_file_pointer;

void main(void)
{
/* variable to read in the words from the file */
char word[50]; /* no word above 50 characters */

printf("\n We will split the file parse.c into words");
printf("\n each word will be output to the screen delimited by ()\n");
printf("\n all words will be on the same line\n");


/* open input file n.b. no test for the case that filled does
not exist */
input_file_pointer = fopen("parse.c", "r");

/* use while construction to use fscanf until an EOF is reached */
/* %s reads a string - a word and != means not equal */
/* do not place semicolon at the end of the line */
/* do not put & before word as it is an array */
while( fscanf( input_file_pointer, " %s", word)!=EOF)
{
printf(" (%s)", word); /* print word surrounded by brackets */
} /* end of while construction */

} /* end of main function */



what do you think of that would that work? if so how would i implement it further?
Sorry actually came out more like a configuration file parser but may be of some help, I hope. (Part of my project).

#include <fstream>
using namespace std;

is the header config you asked for.

#include "../inc/GFileTokenizer.h"GFileTokenizer::GFileTokenizer(){}GFileTokenizer::GFileTokenizer(GString &source){	open(source);}GFileTokenizer::~GFileTokenizer(){	close();}bool GFileTokenizer::open(GString &source){	close();	return file.open(source);}void GFileTokenizer::close(){	file.close();}char GFileTokenizer::peek(){	return file.peek();}char GFileTokenizer::getChar(){	return file.getChar();}void GFileTokenizer::putBack(char c){	file.putBack(c);}void GFileTokenizer::reWind(){	file.seek(0, ios_base::beg);}GString GFileTokenizer::getNextToken(GString &delimiters){	if (!file.isOpen())		return GString();	GString temp;	skipTerminators();	skipDelimiters();	while (!eof() && (delimiters.empty() ? (!isDelimiter(peek())) : 		   (delimiters.find(GString(peek())) < 0)) &&		   !isTerminator(peek()))		   temp += getChar();	skipTerminators();	skipDelimiters();	return temp;}bool GFileTokenizer::skipDelimiters(){	if (!file.isOpen())		return false;		if (!isDelimiter(peek()))		return false;	while (!eof() && isDelimiter(peek()))		getChar();	return true;}bool GFileTokenizer::skipWhiteSpace(){	if (!file.isOpen())		return false;	if (!isWhiteSpace(peek()))		return false;	while (!eof() && isWhiteSpace(peek()))		getChar();	return true;}bool GFileTokenizer::skipTerminators(){	if (!file.isOpen())		return false;	if (!isTerminator(peek()))		return false;	while (isTerminator(peek()))		skipLine();	return true;}GString GFileTokenizer::skipLine(){	if (!file.isOpen())		return GString();	GString temp;	if (skipWhiteSpace())		return GString();	while (!eof() && peek() != '\n')		temp += getChar();	skipWhiteSpace();	return temp;}/* virtual */ bool GFileTokenizer::isDelimiter(char c){	return isWhiteSpace(c);}bool GFileTokenizer::isWhiteSpace(char c){	return (c == ' ' || c == '\t' || c == '\n' || c == '\r');}/* virtual */ bool GFileTokenizer::isTerminator(char c){	return c == '\n';}bool GFileTokenizer::eof(){	return file.eof();}int GFileTokenizer::getPos(){	return file.getPos();}


Example: (GConfigFile : GConfiguration, GFileTokenizer)
// other irrelevant shit...// note that '#' is used as terminator (comment)bool GConfigFile::parseConfiguration(GString &source){	close();	if (!open(source))		return false;	while (true)	{		GString cmd = getNextToken();     // get command		if (cmd.empty())			break;		GString val = getNextToken();     // get value		if (val.empty())			break;		parsed[cmd] = val;     // put in map	}	return true;}


..... when the config file looks like:
var1=259var2=2555.255strNPCTALK=Hey,  buzz off n00b. # a comment#another comment
________________________________pro.gram.mer - an organism that turns caffeine into code
Thanks :)
I'll work on what you gave me to see if i can figure it out
I still can get it :S
Quote:Original post by Machlana
I still can get it :S
What can't you get?

To be honest, I don't want to just hand you code right away. That won't help you with how to learn.

Googling for compiler or parser design and implementation might help you.

Anyway, write out an example of exactly what you'll be working with. Then, character by character, try to see what you'll have to implement to understand which action is being performed and how to get the parameters.

If you figure this out on your own, you'll be a much better programmer for it.
Ok, so you're not having luck with the given explanations and code. You need to break this down so you can better learn the tools necessary as well as the process that it takes to turn ascii into script.

As an idea, try implementing a simple language that is made up only of commands. Something like:
print
push1 // pushes 1 onto stack
pop1 // pops a value off of stack, discarding it
add1 // adds 1 to value at top of stack
sub1 // subs 1 from value at top of stack
print // prints top of stack

Ok? Don't worry about the specifics. This is an exercise.

For fun, next add commenting (using '//' or whatever) and error checking (bad function name causes an error).

After that, change it so you pass a value to push, add and sub (determines what value to push/add/sub). Something like "push 1" or "push(1)". It's up to you.

Basically, implement simple pieces in a dummy system. It should make itself clear.
Maybe overkill, but Crenshaw's Tutorial may be of assistance to you.
Well i understand what to do with the command i can implement something like print and stuff however i dont get how to parse files read it line by line and do something for each line i just dont know file io functions at all. References dont teach me i need a book, or a tutorial or complete code to learn it its weird also i cant really use OOP to learn things i dont know that as well as straight C
U get what im sayin

I went to borders today and bought a 120.00 gift card cuz they didnt have the book i can use that gift card on amazon.com but amazon wont lemme add the book i want on compiler design to the cart they said its a problem and theyre fixing it:S

This topic is closed to new replies.

Advertisement