Sign in to follow this  

Command Based Scripting Language

This topic is 4727 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Does anybody know of any tutorials for COMMAND BASED SCRIPTING LANGUAGES.... I know Game Scripting Mastery has one in it however i lost the cd for it and well.... The book just shows the scope of the source code not the full source :S..... so if anybody knows of any simple implementations or tutorials i'd very VERY much appreciate it.... After i implement this im going to do a command based language with a compiler then a full scripting language that has a c syntax. But i want to start simple then work my way up, doing it the right way. Btw... Im a C/C++ programmer so i'd appreciate it if there was no VB tutorials and all. and i know about tghe scripting tutorial on this site and flipcode EDIT: I wanna do this for experience, NOT to use instead of a existing one

Share this post


Link to post
Share on other sites
You can probably contact the publisher for a replacement of the cd for a small charge. I'd try it at least.

Also, the language that the scripting language is implemented in shouldn't matter if you're only trying to get a feel as to how it's done. If anything it's better because you can't directly copy anything and must exercise your mind a little in converting the various aspects.

Share this post


Link to post
Share on other sites
Ill do that however i dont know if your familiar with a command based language its a language that really doesnt have a lexer or anything it just opens up a file and parses it and comares it to hardcoded commands example:


print "hello"
print_line
get_key



like that

that way it just compares it to something like this


if(currline == CMD_PRINT)
{
char param = get_params(currline);
print("%s", param);
}


however im not familiar with file io and parsing and such so thats where it gets me

[Edited by - Machlana on December 31, 2004 2:53:25 PM]

Share this post


Link to post
Share on other sites
Is the example that you've given me as complex as your engine gets? If so, it's relatively straightforward.

Read a line in from file.
Parse instruction (assuming that instuction is always first, which seems to be the case).

Here, parsing is a simple as reading until you encounter whitespace or the end of the string. Then simply compare this segment of the string to expected commands. Handle it if it passes, otherwise throw an exception (or whatever you want to handle errors).

If the command has arguments, and you haven't reached the end of the string, parse the next "word", or, in the event of a quotation, read until you encounter end of string (error) or the next quotation and you have the string substring.

I'm not sure, specifically, what else you'd need pointed out. If you can't think it through, then give me a solid example of what you want to do and perhaps I or another can get you moving.

Share this post


Link to post
Share on other sites
Right, lemme explain a little more

a function like
LoadScript()
That will load the script

a function like
RunScript()
That runs the script (like my above example)

a function like
GetCommand()
That gets the next command

a function like
GetParam()
That gets the paramater after the command

I know what i need but my problem is i have NEVER programmed file I/O in my life so im completley confused in it

Share this post


Link to post
Share on other sites
Then do a quick google or msdn search for it. All you need is something to read a line of text.

Here's a good one.

So, learn how to read a line of text into a string. After that it's largely common sense and thinking it out. Grab a line. Break off the first chunk (probably "someFunc("), see if there are parameters to apply, process it.

Share this post


Link to post
Share on other sites
So would i do something like this?


for( int CurrLineIndex = 0;
CurrLineIndex < ScriptSize;
++CurrLineIndex )
{
// Allocate space for the line and a null terminator
strScript[ CurrLineIndex ] = ( char * ) malloc ( MAX_SOURCE_LINE_SIZE + 1 );

// Load the line
fgets( strScript[ CurrLineIndex ], MAX_SOURCE_LINE_SIZE, ScriptFile );
}



Im still so confused what to do after that is there any examples?

Share this post


Link to post
Share on other sites
Try it out. Does it read in lines?

To test it, have it echo the lines out. Or run it through a debugger.

Anyway, next, read characters until you run into a space, end of string or a '('. You then have the first function.

Check the next character, if it's not whitespace or a ')', then you have a parameter. Read it until you encounter a space, a comma, end of string or ')'.

Is it starting to come into focus?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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=259
var2=2555.255
strNPCTALK=Hey, buzz off n00b. # a comment
#another comment

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
What you're after is really just basic file I/O stuff. It'll be covered in any introductory C++ text, or you might try asking in General Programming or For Beginners. I'd recommend using an ifstream, strings (not char[50] or similar), and the getline function.

Share this post


Link to post
Share on other sites
alright someone posted this

// Step 1: Open the file and store a handle to the file in pFile
//
FILE* pFile = fopen("somefile.txt", "r"); // <<< The "r" specifies that you want read only access
if ( !pFile )
{
// Failed to open
return;
}


// Step 2: Read the file until we are at the end of the file
//
while ( !feof(pFile) )
{
char szStringBuf[256];
fgets( szStringBuf, 256, pFile );

// szStringBuf now stores the current line that you are parsing
}


// Step 3: Make sure to close the file once you have finished
//
fclose(pFile);







If i do this


// Step 1: Open the file and store a handle to the file in pFile
//
FILE* pFile = fopen("somefile.txt", "r"); // <<< The "r" specifies that you want read only access
if ( !pFile )
{
// Failed to open
return;
}


// Step 2: Read the file until we are at the end of the file
//
while ( !feof(pFile) )
{
char szStringBuf[256];
fgets( szStringBuf, 256, pFile );

// ADDED
if (szStringBuf == CMD_PRINT)
{
printf("CMD_PRINT successfully called!");
}
else if( szStringBuf == CMD_WAIT )
{
printf("CMD_WAIT successfully called!");
}
else
{
printf("COMMAND NOT VALID!");
}
// END ADDED CODE

// szStringBuf now stores the current line that you are parsing
}


// Step 3: Make sure to close the file once you have finished
//
fclose(pFile);



would that work?
if so how would i get params?


Share this post


Link to post
Share on other sites

This topic is 4727 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this