www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Porting RE2C to D

reply Carlos <carlos2003nov yahoo.ca> writes:
I am porting RE2C to D.
Things are going well, but there are
some C++ constructs for which i needs some help.

What do i do with (const and =0 ):

# virtual void emit(std::ostream&, bool&) = 0;
# virtual bool isRule() const;

I translated them to:

# virtual void emit( File of, bit flag );
# virtual bool isRule( );

The body of the function will be inserted
in the class.

By the way. In D, the function bodies must
be defined inside the class ? Right ?
or may i define them outside the class, but in
the same module ?

Also, what to do with the overloaded stream << operator
for the State class:

# friend std::ostream& operator<<(std::ostream&, const State&);
# friend std::ostream& operator<<(std::ostream&, const State*);

This my first real work with D.
I find the language fun. It ask for less typing....

Thanks for any advice.
Mar 09 2005
next sibling parent reply "Ben Hinkle" <bhinkle mathworks.com> writes:
"Carlos" <carlos2003nov yahoo.ca> wrote in message 
news:d0notu$72k$1 digitaldaemon.com...
I am porting RE2C to D.
 Things are going well, but there are
 some C++ constructs for which i needs some help.

 What do i do with (const and =0 ):

 # virtual void emit(std::ostream&, bool&) = 0;
 # virtual bool isRule() const;

 I translated them to:

 # virtual void emit( File of, bit flag );
 # virtual bool isRule( );

I don't think 'virtual' is allowed before member function definitions. Try abstract void emit( File of, bit flag ); bool isRule( ) {...}
 The body of the function will be inserted
 in the class.

 By the way. In D, the function bodies must
 be defined inside the class ? Right ?

yes
 or may i define them outside the class, but in
 the same module ?

no
 Also, what to do with the overloaded stream << operator
 for the State class:

 # friend std::ostream& operator<<(std::ostream&, const State&);
 # friend std::ostream& operator<<(std::ostream&, const State*);

override the function char[] toString() See "writef" and "doFormat" in http://www.digitalmars.com/d/std_format.html, http://www.digitalmars.com/d/phobos.html#stdio and http://www.digitalmars.com/d/std_stream.html or one of the various libraries that do io (eg, Mango: http://www.dsource.org/projects/mango/ and probably others but I can't think of any right now)
 This my first real work with D.
 I find the language fun. It ask for less typing....

 Thanks for any advice.

good luck! -Ben
Mar 09 2005
parent "Ben Hinkle" <bhinkle mathworks.com> writes:
"Ben Hinkle" <bhinkle mathworks.com> wrote in message 
news:d0npu1$8gb$1 digitaldaemon.com...
 "Carlos" <carlos2003nov yahoo.ca> wrote in message 
 news:d0notu$72k$1 digitaldaemon.com...
I am porting RE2C to D.
 Things are going well, but there are
 some C++ constructs for which i needs some help.

 What do i do with (const and =0 ):

 # virtual void emit(std::ostream&, bool&) = 0;
 # virtual bool isRule() const;

 I translated them to:

 # virtual void emit( File of, bit flag );
 # virtual bool isRule( );

I don't think 'virtual' is allowed before member function definitions. Try abstract void emit( File of, bit flag ); bool isRule( ) {...}

I should add you probably want Stream or OutputStream instead of File or something like that.
Mar 09 2005
prev sibling next sibling parent reply pragma <pragma_member pathlink.com> writes:
In article <d0notu$72k$1 digitaldaemon.com>, Carlos says...
I am porting RE2C to D.
Things are going well, but there are
some C++ constructs for which i needs some help.

What do i do with (const and =0 ):

# virtual void emit(std::ostream&, bool&) = 0;
# virtual bool isRule() const;

I translated them to:

# virtual void emit( File of, bit flag );
# virtual bool isRule( );

Might I reccomend this: # abstract void emit(OutputStream ostream, bool flag); // same as pure virtual. # final bool isRule(); // 'final' does the job of 'const' in this context .. just don't forget to "import std.stream" to get the OutputStream class.
By the way. In D, the function bodies must
be defined inside the class ? Right ?
or may i define them outside the class, but in
the same module ?

Declare them inside the class, as though you were inlining the whole thing. It feels wierd at first, but you'll begin to notice the convienence of not having separate header files for everything.
Also, what to do with the overloaded stream << operator
for the State class:

# friend std::ostream& operator<<(std::ostream&, const State&);
# friend std::ostream& operator<<(std::ostream&, const State*);

D uses special names to denote operators (http://www.digitalmars.com/d/operatoroverloading.html). For the two signatures above, you'd only need one override: # pubilc OutputStream opShl(OutputStream); - EricAnderton at yahoo
Mar 09 2005
parent reply "Zz" <Zz Zz.com> writes:
look at yasm, they converted re2c from c++ to some pretty clean 'c', it may
be easier to work from there.

Zz
"pragma" <pragma_member pathlink.com> wrote in message
news:d0nqmo$9ev$1 digitaldaemon.com...
 In article <d0notu$72k$1 digitaldaemon.com>, Carlos says...
I am porting RE2C to D.
Things are going well, but there are
some C++ constructs for which i needs some help.

What do i do with (const and =0 ):

# virtual void emit(std::ostream&, bool&) = 0;
# virtual bool isRule() const;

I translated them to:

# virtual void emit( File of, bit flag );
# virtual bool isRule( );

Might I reccomend this: # abstract void emit(OutputStream ostream, bool flag); // same as pure

 # final bool isRule(); // 'final' does the job of 'const' in this context

 .. just don't forget to "import std.stream" to get the OutputStream class.

By the way. In D, the function bodies must
be defined inside the class ? Right ?
or may i define them outside the class, but in
the same module ?

Declare them inside the class, as though you were inlining the whole

 feels wierd at first, but you'll begin to notice the convienence of not

 separate header files for everything.

Also, what to do with the overloaded stream << operator
for the State class:

# friend std::ostream& operator<<(std::ostream&, const State&);
# friend std::ostream& operator<<(std::ostream&, const State*);

D uses special names to denote operators (http://www.digitalmars.com/d/operatoroverloading.html). For the two signatures above, you'd only need one override: # pubilc OutputStream opShl(OutputStream); - EricAnderton at yahoo

Mar 10 2005
parent Carlos <carlos2003nov yahoo.ca> writes:
Zz wrote:
 look at yasm, they converted re2c from c++ to some pretty clean 'c', it may
 be easier to work from there.
 

Thanks for the tip and your attention. Over the year i found 4 versions of RE2C: 1 - Sourceforge 2 - Yasm 3 - OpenWatcom has one also. + a 4th who has since vanished. I have invested some time anf effort with the C++ version so, i'l stick with it. Whatever the difficulties .
Mar 10 2005
prev sibling parent reply Carlos <carlos2003nov yahoo.ca> writes:
Thanks for the help to the 3 of you.
I downloaded Mango and had a better look
at Phobos.
I am not D strong enough to use this.

After may essais,
here what i camed up with:

class State
{	
public:
   char[] text;

   this(char[]txt){text=txt;}
   ostream put(ostream out)
   { out.writef( "State: ", text );
     return out;
   }
}

class ostream : File
{
   alias put opShl;
   this( ) { super( ); }
   ostream put(byte X) { writef(X); return this;	}
   ostream put(ubyte X){ writef(X); return this;	}
   ostream put(short X){ writef(X); return this;	}
   ... etc ..., and then:
   ostream put( State s )
     { s.put( this ); return this; }
}

Now if i declare:
   State S = new state("some text");
   ostream os;
   os = new File( "out.log", FileMode.OutNew );

I can do this:
   os << "text" << "\n";

Well, this work, but this is less than perfect.
If ostream is buried in a library, how can i add
new data structures to it without modifying it ?

Can i add something to each class (like State) i want
to be able to send to stream with the << operator ?

Or, what is the best way to do this ?

I may use Mango to do this. But Mango is big library
and i would like re2d to be standalone exe with
no dependencies

Thanks.
Mar 10 2005
parent Kris <Kris_member pathlink.com> writes:
Inline:

In article <d0qbaq$309h$1 digitaldaemon.com>, Carlos says...
Thanks for the help to the 3 of you.
I downloaded Mango and had a better look
at Phobos.
I am not D strong enough to use this.

After may essais,
here what i camed up with:

class State
{	
public:
   char[] text;

   this(char[]txt){text=txt;}
   ostream put(ostream out)
   { out.writef( "State: ", text );
     return out;
   }
}

class ostream : File
{
   alias put opShl;
   this( ) { super( ); }
   ostream put(byte X) { writef(X); return this;	}
   ostream put(ubyte X){ writef(X); return this;	}
   ostream put(short X){ writef(X); return this;	}
   ... etc ..., and then:
   ostream put( State s )
     { s.put( this ); return this; }
}

Now if i declare:
   State S = new state("some text");
   ostream os;
   os = new File( "out.log", FileMode.OutNew );

I can do this:
   os << "text" << "\n";

Well, this work, but this is less than perfect.
If ostream is buried in a library, how can i add
new data structures to it without modifying it ?

Mango uses an Interface to extent the basic I/O system ~ if your class implements the IReadable and/or IWritable interface, the mango.io subsystem will treat your class just like a native type. For example: # class MyClass : IReadable # { # short i; # real[] r; # char[] foo; # # void read (IReader input) # { # input (foo) (i) (r); # } # } # # # Reader input = new Reader (new FileConduit("myfile")); # # short i; # long l; # MyClass mc = new MyClass; # input (i) (mc) (l); Substitute the () parens for >> if you prefer the iostream syntax. The same notion is used for writing also ~ encapsulation is nicely maintained and the setup is rather trivial, as you can see. This pattern extends to serializing, sending, and re-instantiating an entire class-tree across a network (via IPickle and friends).
Can i add something to each class (like State) i want
to be able to send to stream with the << operator ?

Or, what is the best way to do this ?

I may use Mango to do this. But Mango is big library
and i would like re2d to be standalone exe with
no dependencies

Thanks.

If you want to make a standalone exe using Mango, just go ahead. The linker will exclude anything you don't use, and the resultant program should be equivalent to the size of a Phobos-based application with similar functionality (and probably a bit smaller once Walter finally gets around to removing printf() from Object.print ...). Point Build.exe at the Mango path (via -I) and compile with -nounittest. - Kris
Mar 10 2005