View previous topic :: View next topic |
Author |
Message |
mega
Joined: 25 Jun 2009 Posts: 13 Location: Italy
|
Posted: Tue Sep 08, 2009 11:02 am Post subject: The Martian |
|
|
I wrote The Martian. It is a program to include new libraries in very easy way.
Example:
File prova.d:
Code: |
#INCLUDE stringarray.d;
import std.stdio;
void main()
{
auto c=new StringArray();
c.add("ciao");
writefln(c.get(0));
}
|
the keyword #INCLUDE is necessary to include a library written in D.
The file stringarray.d:
Code: |
import std.string;
class StringArray
{
private int y;
private char[][]arr;
this()
{
}
this(char[][]array)
{
arr=array;
}
int opApply(int delegate(ref char[]) dg)
{
int result = 0;
for (int i = 0; i < arr.length; i++)
{
result = dg(arr[i]);
if (result)break;
}
return result;
}
int opApplyReverse(int delegate(ref char[]) dg)
{
int result = 0;
for (int i = (arr.length-1); i>=0; i--)
{
result = dg(arr[i]);
if (result)break;
}
return result;
}
void add(char[] str,int pos=-1)
{
if((pos<0)||(pos>=arr.length))
{
arr.length=++y;
arr[(y-1)]=str;
}
else
{
char[][]arr2;
int j;
for(int i=0;i<arr.length;i++)
{
if(i!=pos)
{
arr2.length=++j;
arr2[(j-1)]=arr[i];
}
else
{
arr2.length=++j;
arr2[(j-1)]=str;
arr2.length=++j;
arr2[(j-1)]=arr[i];
}
}
arr=arr2;
}
}
int find(char[]str)
{
int index=-1;
for(int i=0;i<arr.length;i++)
{
if (arr[i]==str)index=i;
if(index>-1)break;
}
return index;
}
bool remove(int index)
{
if ((index<0)||(index>=arr.length))return false;
char[][]arr2;
int j;
for (int i=0;i<arr.length;i++)
{
if(i!=index)
{
arr2.length=++j;
arr2[(j-1)]=arr[i];
}
}
arr=arr2;
return true;
}
bool remove(char[]str)
{
int index=this.find(str);
return remove(index);
}
bool replace(char[]str,int index)
{
if ((index<0)||(index>=arr.length))return false;
arr[index]=str;
return true;
}
char[]get(int index)
{
return arr[index];
}
int search(char str[])
{
int index=-1;
for(int i=0;i<arr.length;i++)
{
if (std.string.find(arr[i],str)==0)index=i;
if(index>-1)break;
}
return index;
}
int isearch(char str[])
{
int index=-1;
for(int i=0;i<arr.length;i++)
{
if (std.string.find(arr[i],str)==0)index=i;
}
return index;
}
char[][] sub(int a, int b)
{
char[][] z;
if ((a<0)||(a>=arr.length))return z;
if ((b<0)||(b>arr.length)||(b<a)) return z;
return arr[a..b];
}
char[]nextWord(int index,char[]str)
{
if ((index<0)||(index>=arr.length))return "";
char[]c=arr[index];
return strip(c[str.length..(c.length-1)]);
}
}
|
Now type in your prompt: martian prova.d.
Obviously make sure that 'dmd.exe' and 'martian.exe' are on your path and can compile D programs!
The martian source file is here: www.superquiz.altervista.org/softwares/martian.d.
The martian exe file is here: www.superquiz.altervista.org/softwares/martian.zip |
|
Back to top |
|
|
revcompgeek
Joined: 24 Nov 2007 Posts: 27
|
Posted: Thu Oct 15, 2009 6:32 pm Post subject: |
|
|
I don't understand why you wouldn't just put an "import stringarray;" at the beginning of the file. #INCLUDE seems to do the same thing. _________________ Mac OS 10.5.8 PowerPC |
|
Back to top |
|
|
csauls
Joined: 27 Mar 2004 Posts: 278
|
Posted: Fri Oct 16, 2009 4:01 pm Post subject: |
|
|
I think the idea is that, given a D module "foo" with "#INCLUDE bar" at the top, the command line "martian foo ..." invokes "dmd foo bar ..." for you...
But, that just makes it a build tool with its own special syntax, which I'm uneasy about. (I'm biased, of course, in favor of dmd's verbose output.) _________________ Chris Nicholson-Sauls |
|
Back to top |
|
|
|