FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The Martian

 
Post new topic   Reply to topic     Forum Index -> Potential Projects
View previous topic :: View next topic  
Author Message
mega



Joined: 25 Jun 2009
Posts: 13
Location: Italy

PostPosted: Tue Sep 08, 2009 11:02 am    Post subject: The Martian Reply with quote

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
View user's profile Send private message
revcompgeek



Joined: 24 Nov 2007
Posts: 27

PostPosted: Thu Oct 15, 2009 6:32 pm    Post subject: Reply with quote

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
View user's profile Send private message
csauls



Joined: 27 Mar 2004
Posts: 278

PostPosted: Fri Oct 16, 2009 4:01 pm    Post subject: Reply with quote

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
View user's profile Send private message AIM Address Yahoo Messenger
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Potential Projects All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group