473,387 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Deriving input stream ?

Hi !

I've translated Andru Luvisi small LISP interpreter sl3.c
http://www.sonoma.edu/users/l/luvisi/sl3.c

in C++ in order to understand how it works and to experiment with small
LISP.
http://fabrice.marchant.free.fr/LISP/slf/060521/

(For now, my C++ code isn't the result of an object analysis, just a
translation from C, eliminating "switches" or "if ladders" for
example.)

As a LISP, the main loop is a "read, eval, print".

Please, could you give me hints about the way to define a ">>" operator
to replace the "readObj" function of LISP objects ?

// Main loop, in slf.c++
while( notExitFlag )
cout << *readObj( )->eval( top_env ) << '\n';

Thanks for advance.

Fabrice

May 21 '06 #1
4 2125
<fa*************@free.fr> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
: Please, could you give me hints about the way to define a ">>" operator
: to replace the "readObj" function of LISP objects ?
:
: // Main loop, in slf.c++
: while( notExitFlag )
: cout << *readObj( )->eval( top_env ) << '\n';

Since you're asking only about how to implement >>, I guess that you
have "implemented" << using a conversion to string ?

The proper way to define streaming operators is:
ostream& operator << ( ostream& s, MyClass const& obj ) { ... }
istream& operator >> ( istream& s, MyClass& obj ) { ... }
They are non-member functions. [ NB: both have to "return s;" ]

But you can declare them within the class, as friends, if
these functions need access to private members:
friend ostream& operator << ( ostream& s, MyClass const& obj );
friend istream& operator >> ( istream& s, MyClass& obj );
Amicalement --Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
May 22 '06 #2
Thanks for your explanations, Ivan.

---------- About output -----------
Since you're asking only about how to implement >>, I guess that you
have "implemented" << using a conversion to string ?
No, I did this way :

class obj {

public:
....
virtual ostream&
print( ostream& os ) const { return os; }
....
};

ostream& operator<<( ostream& os, const obj& ob ) {

return ob.print( os );
}

These are printable classes that derives from base "obj" :

class nilObj: public obj {
public:
....
ostream&
print( ostream& os ) const { return os << "()"; }
....
};

class symbol: public obj {
public:
....
print( ostream& os ) const { return os << name(); }

string
name( void ) const { return _M_name; }

private:
string _M_name;
};

And so on, for other derived objects.

You speak about string conversion, please how could I use this here ?

---------- About input -----------
The proper way to define streaming operators is:
ostream& operator << ( ostream& s, MyClass const& obj ) { ... }
istream& operator >> ( istream& s, MyClass& obj ) { ... }
They are non-member functions. [ NB: both have to "return s;" ] But you can declare them within the class, as friends, if
these functions need access to private members:
friend ostream& operator << ( ostream& s, MyClass const& obj );
friend istream& operator >> ( istream& s, MyClass& obj );


I know these but do not see how to implement things in the aim to be
used like this :

obj i;
cin >> i; // <- How to prepare the job for this ?
cout << *i.eval( top_env ) << '\n';

I wanted to be able to write the read of different kind of objects (
nil, symbol, cons, proc ) this simple way.

Regards

Fabrice

May 23 '06 #3

<fa*************@free.fr> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
The proper way to define streaming operators is:
ostream& operator << ( ostream& s, MyClass const& obj ) { ... }
istream& operator >> ( istream& s, MyClass& obj ) { ... }
They are non-member functions. [ NB: both have to "return s;" ]

But you can declare them within the class, as friends, if
these functions need access to private members:
friend ostream& operator << ( ostream& s, MyClass const& obj );
friend istream& operator >> ( istream& s, MyClass& obj );


I know these but do not see how to implement things in the aim to be
used like this :

obj i;
cin >> i; // <- How to prepare the job for this ?
cout << *i.eval( top_env ) << '\n';

I wanted to be able to write the read of different kind of objects (
nil, symbol, cons, proc ) this simple way.


You just input them. I do this quite a bit in my program, I'll try to show
a rather simple example.

This one is not simple but I'll cut out a lot of stuff just so you get the
idea. So it may not compile as is.

class CHealth
{
public:
// Snipped - Not needed for example
private:
// Lot of other stuff snipped here
int OverallMax_; int Overall_;
int HeadMax_;
int Head_;
int TorsoMax_;
int Torso_;
int LeftArmMax_;
int LeftArm_;
int RightArmMax_;
int RightArm_;
int LeftLegMax_;
int LeftLeg_;
int RightLegMax_;
int RightLeg_;
int LeftWingMax_;
int LeftWing_;
int RightWingMax_;
int RightWing_;
};

std::istream& operator>>( std::istream& is, CHealth& Health)
{
is >> Health.OverallMax_ >> Health.Overall_ >> Health.HeadMax_ >>
Health.Head_ >> Health.TorsoMax_ >> Health.Torso_ >>
Health.LeftArmMax_ >> Health.LeftArm_ >> Health.RightArmMax_ >>
Health.RightArm_ >>
Health.LeftLegMax_ >> Health.LeftLeg_ >> Health.RightLegMax_ >>
Health.RightLeg_ >>
Health.LeftWingMax_ >> Health.LeftWing_ >> Health.RightWingMax_ >>
Health.RightWing_;

// Snipped checking of some values and setting values in class that were
also snipped,
// such as Dead_, LeftArmDisabled_, etc...

return is;
}

std::ostream& operator<<( std::ostream& os, CHealth& Health)
{
os << Health.OverallMax_ << " " << Health.Overall_ << " " <<
Health.HeadMax_ << " " << Health.Head_ << " " <<
Health.TorsoMax_ << " " << Health.Torso_ << " " <<
Health.LeftArmMax_ << " " << Health.LeftArm_ << " " <<
Health.RightArmMax_ << " " << Health.RightArm_ << " " <<
Health.LeftLegMax_ << " " << Health.LeftLeg_ << " " <<
Health.RightLegMax_ << " " << Health.RightLeg_ << " " <<
Health.LeftWingMax_ << " " << Health.LeftWing_ << " " <<
Health.RightWingMax_ << " " << Health.RightWing_;
return os;

}
May 24 '06 #4
Hi Jim !

Thanks for your answer.

Apologizes for the delay.
I understand what you do for the input of your numerous fields
structure but I can't imagine how to apply this in my case where the
input objects - all derived from the "obj" base type - can be "cons" (
lists ) or symbols or nil...

http://fabrice.marchant.free.fr/LISP...0521/input.c++

Regards fabrice

May 27 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Julian | last post by:
I would like to have output from my program to be written to cout as well as a file. (actually, i want several other output options but this should explain my problem in the simplest way). I have...
7
by: Generic Usenet Account | last post by:
I am trying to set up a delimiter character string for the input stream such that the delimiter string is "skipped over" in the input stream. Can someone suggest how to do this with some sample...
16
by: lovecreatesbeauty | last post by:
/* When should we worry about the unwanted chars in input stream? Can we predicate this kind of behavior and prevent it before debugging and testing? What's the guideline for dealing with it? ...
5
by: Kavya | last post by:
I saw these two ways for validating input First Way -------------- #include <iostream> #include <limits> using namespace std; int main() {
7
by: Christopher Pisz | last post by:
I found an article http://spec.winprog.org/streams/ as a starting point, but it seems to do alot of things that aren't very standard at all. One particular problem is, that he is using a vector as...
0
by: James Kanze | last post by:
On 11 avr, 17:44, "mc" <mc_r...@yahoo.comwrote: OK. If the actual format is well documented, that's half the battle won already. Note, however, that reading a float as an int is still very...
6
by: Dan Smithers | last post by:
I want to write my own class derived from the ostream class. I have been getting errors with my templates: First, I get an error writing a nested template. If I leave the function definition...
77
by: arnuld | last post by:
1st I think of creating an array of pointers of size 100 as this is the maximum input I intend to take. I can create a fixed size array but in the end I want my array to expand at run-time to fit...
27
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I have a fully-portable C program (or at least I think I do). It works fine on Windows, but malfunctions on Linux. I suspect that there's something I don't know about the standard input stream...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.