473,669 Members | 2,466 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2142
<fa************ *@free.fr> wrote in message
news:11******** **************@ u72g2000cwu.goo glegroups.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 "implemente d" << 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 "implemente d" << 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.goo glegroups.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.OverallM ax_ >> Health.Overall_ >> Health.HeadMax_ >>
Health.Head_ >> Health.TorsoMax _ >> Health.Torso_ >>
Health.LeftArmM ax_ >> Health.LeftArm_ >> Health.RightArm Max_ >>
Health.RightArm _ >>
Health.LeftLegM ax_ >> Health.LeftLeg_ >> Health.RightLeg Max_ >>
Health.RightLeg _ >>
Health.LeftWing Max_ >> Health.LeftWing _ >> Health.RightWin gMax_ >>
Health.RightWin g_;

// 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.OverallM ax_ << " " << Health.Overall_ << " " <<
Health.HeadMax_ << " " << Health.Head_ << " " <<
Health.TorsoMax _ << " " << Health.Torso_ << " " <<
Health.LeftArmM ax_ << " " << Health.LeftArm_ << " " <<
Health.RightArm Max_ << " " << Health.RightArm _ << " " <<
Health.LeftLegM ax_ << " " << Health.LeftLeg_ << " " <<
Health.RightLeg Max_ << " " << Health.RightLeg _ << " " <<
Health.LeftWing Max_ << " " << Health.LeftWing _ << " " <<
Health.RightWin gMax_ << " " << Health.RightWin g_;
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
5675
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 seen commercial programs print output to the screen as well as to a log file. depending on the user and other situations, i might want to turn off one of the outputs or maybe even both outputs. so, i want a single line with operator << function...
7
4079
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 code? I am attaching a "C" code snippet that does the same thing. The problem is that I don't know how to realize this using "cin" Thanks, Lee
16
2786
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? As shown below line #21, I should remove the unwanted characters in input stream there at that time. Do I miss some other possible errors in i/o which will happen to occur sometimes in other places? And welcome your kind comments on following the...
5
2317
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
3109
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 his input buffer and trys to assign an iterator to a char pointer. .... class winzoostreambuffer : private LoggerConsole, public sts::basic_streambuf<char, std::char_traits<char>> ....
0
1349
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 implementation dependent, since the actual internal format of a float varies between machines. The documentation should specify the format of the float in the file, either by reference to some known format (e.g. IEEE), or by explicitely specifying...
6
2608
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 inside template class definition (commented out at //1) then it compiles and runs fine, but if I declare and define the function separately (at //2). Is the following syntax supported by g++?
77
3331
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 the size of input. I am not able to come up with anyting all all and doing: char* arr_of_pointers; seems like a completely wrong idea as this is a static array. I want to take the input and then decide how much memory I need and then malloc...
27
3131
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 that's causing the problem. Here's how I wrote the program originally: #include <stdio.h> #include <string.h>
0
8465
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8383
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8895
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8658
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7407
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6210
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4386
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.