ok...
heres a simple script interperator, (you'll have to use your imagination though!!)
the script is run from the command line, in the form script.exe [script file]
there are three code files (at the minute) because I've only coded two very smple function, "add" and "writeword", there is currently no other function and no support for variables...
(I'll add some more functions later).
everyone should feel free to add to this language, it's a first come first server affair,
so if someone wants to write a function with a stupid name, or indeed a stupid function. respect the fact that they did that, and don't go changng other peoples functions name or functions function...
It's currently written in C and is compilable on both windows Linux and Mac, (a truly portable scripting language?)...
feel free to write OS specific functions, but don't to also write the code to make it so that it'll still compile (albeit ignoring your functins) on another OS...
heres the code so far....
script.c
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include "functionslist.h"
int main(int argc, char *argv[])
{
int x;
float a,b,c; /*declare some random variables incase they are needed later*/
char inpath[50], task[50], arg[100];
FILE *input;
for (x=0; x<argc; x++)
{
if (x>1)
{
printf("you can only run one script at a time");
exit(1);
}
}
if (x==1)
{
printf("Usage is scrip.exe [scriptname]");
exit(1);
}
if (strlen(argv[1])>=50)
{
printf("script names are limited to 50 chars");
exit(1);
}
input = fopen(argv[1],"r");
if (input==NULL)
{
printf("Error 1 - could not find input file - %s\n\n\r",inpath);
exit(1);
}
/*here we decide what functions we'll be allowing, and the permissible arguments for each function
for this example the functions that I have decided to allow are
add some numbers (because it's simple)
*/
while (x=fscanf(input,"%s",&task)!=EOF) /*scan for task*/
{
if(strcmp(task,addfunction)==NULL)
{
fscanf(input,"%f,%f",&a,&b);
c = addfunc(a,b);
printf("%f\r\n",c);
}
if(strcmp(task,printwordfunction)==NULL)
{
fscanf(input,"%s",&arg); /*not all scripting functions will need a seperate function, echo to the screen is a good example of this*/
printf("%s\r\n",arg);
}
}
}
functionslist.h
Code:
/*list functions here*/
float addfunc(float, float);
/*declare functions that will be available
these are global variables, just because it
was easier and cleaner to do it like this*/
char *addfunction = "add";
char *printwordfunction = "writeword";
char *printstringfunction = "writestring"; //not included this but you get the picture right
[code]
and lastly the function specific file
addfunc.c
[code]
float addfunc(float a, float b)
{
float result;
result = a + b;
return(result);
}
I've tried to structure the programming so tat new functions can be added without bloating the software.
For some users they may have to add lines to include the addfunc.c file at the start of the functionslist.h file... the IE I used was VS.NET and having these three files added to the solution meant that the addfunc.c file was added automatically...
so like I say, if anyone is intersted, please feel free to extend this language.
---edit--
just for completion... the script I used was called hello.txt and looked like this
add 1,20
writeword hello