From davis at space.mit.edu Mon Oct 16 02:36:21 2006 From: davis at space.mit.edu (John E. Davis) Date: Tue Jan 30 08:52:12 2007 Subject: [slang-users] comparison operator grammar change Message-ID: <200610160636.k9G6aLPV005074@aluche.mit.edu> Hi, For slang-2.1, I am thinking about making a change to the part of the grammar that deals with the four comparison operators <, <=, >, >=. In particular, I would like for boolean expressions such as a < b <= c to mean ((a < b) and (b <= c)). Similarly, a < b <= c < d would mean (a < b) and (b <= c) and (c < d). This change would not only add clarity to code, but would also result in some performance gain. For example, compare if ((r0 <= hypot(x,y)) and (hypot(x,y) < r1)) do_something (); to if (r0 <= hypot(x,y) < r1) do_something (); Unfortunately, such a change would break backward-compatibility. Currently, a Are there any plans to add support for objects to SLang? A while ago I was building a library for text forms in jed and I implemented it in an OO fashion (structures with members and methods). The thing that I am missing the most is a way to add new members to an existing structure. This way I could add some polymorphism. I found a way to do it (with an empty extra field in the base "class") but I don't consider this to be a solution: typedef struct { a, b, % members dothis, dothat % methods extra % derived types can add members } Base_Type; private define impl_do_this(self) { print(self.a); ... } private define impl_do_that(self) { print(self.b); ... } % constructor for Base_Type define New_Base_Type() { variable obj = @Base_Type; obj.a = 0; obj.b = 0; obj.dothis = &impl_do_this; obj.dothat = &impl_do_that; obj.extra = NULL; return obj; } typedef struct { c, d, % extra members for derived type op1, op2 % methods } Struct_with_extra_members; % constructor for Derived_Type define New_Derived_Type() { variable obj = @Base_Type; obj.extra = @Struct_with_extra_members; obj.extra.c = 0; ... return obj; } Here I can implement 1 level of derived objects. This was enough (and not too hard to manage) for the project. If I wanted to inherit from a derived object I would get obj.extra.extra. A mess. Some ideas ========== The typedef struct construct could be extended like this typedef struct { c, d, op1, op2 } Derived_Type (Base_Type) which would produce a structure like struct { a, b, dothis, dothat, c, d, op1, op2 } Another not so elegant solution is something like: define New_Derived_Type() { variable obj = @Base_Type; struct_add_member(obj, "c"); struct_add_member(obj, "d"); ... return obj; } But maybe the best syntax would be to add a keyword like "class" or "object": class Derived_Type (Base_Type) { c, d, ... }; For the classes to be real classes, there should be support for virtual functions. At the moment it is possible to override an existing function from the base class: private define impl_special_do_this(self) { print("Derived " + self.a); ... } define New_Derived_Type() { variable obj = @Base_Type; % override a method from the base class obj.dothis = &impl_special_do_this; } You can still call the original method from the base class: private define impl_special_do_this(self) { print("Derived implementation calling Base implementation"); impl_do_this(self); } You have to know which is the base class and what function you reimplemented in the derived class. You also have to have access to the function impl_do_this. To make the code more OO I made all impl_* functions private to force the programmer to access them only by using object methods. Now, when I define the derived object in anoter file I no longer have access to impl_do_this. What I could do is: define New_Derived_Type() { variable obj = @Base_Type; ... struct_add_member(obj, "base_do_this"); ... % override a method from the base class obj.base_do_this = obj.dothis; obj.dothis = &impl_special_do_this; } private define impl_special_do_this(self) { print("Derived implementation calling Base implementation"); self.base_do_this(); } This could be implemented in SLang library internally. Methods could somehow be declared virtual. Each instance of an object would have a poiner to its static type-descriptor (Base_Type). (could typeof() be used?). Type-descriptors would have a pointer (__base) to their base type-descriptors. define New_Base_Type() { variable obj = @Base_Type; % Base_Type.__base = NULL struct_virtual(obj.dothis, &impl_do_this); % converted internally to: typeof(obj).dothis = &impl_do_this } % Base_Type.dothis points to impl_do_this define New_Derived_Type() { variable obj = @Derived_Type; % Derived_Type.__base points to Base_Type % Derived_Type.dothis points to impl_do_this struct_override(obj.dothis, &impl_special_do_this); % converted internally to: % typeof(obj).dothis = &impl_special_do_this } % Derived_Type.dothis points to impl_special_do_this A call like obj.dothis() would be converted internally to typeof(obj).dothis(obj); In the above case it would call impl_special_do_this(obj). A call to a base classes method could be made with a type cast: Base_Type(obj).dothis(); This call could perform a check if obj is derived from Base_Type (using the __base field) and then call Base_Type.dothis(obj); Constructors (and destructors) could be added to classes. They could be declared for example using a standard name (like with python): create(); % or __init__ destroy(); % or __del__ How could it all look in SLang: class Base { a = 0, b = 0, create = &impl_base_create, virtual dothis = &impl_do_this, dothat = &impl_do_that }; class Derived (Base) { c = 0, d = 0, create = &impl_derived_create, override dothis = &impl_special_do_this, }; x = new Base(); % also calls x.create() y = new Derived(); x.dothis(); y.dothis(); Base(y).dothis(); Marko M. From davis at space.mit.edu Mon Oct 16 06:42:45 2006 From: davis at space.mit.edu (John E. Davis) Date: Tue Jan 30 08:52:12 2007 Subject: [slang-users] SLang with objects ? In-Reply-To: <45335490.8020301@email.si> References: <45335490.8020301@email.si> Message-ID: <200610161042.k9GAgjCt008296@aluche.mit.edu> Marko Mahnic wrote: >The thing that I am missing the most is a way to add new members >to an existing structure. This way I could add some polymorphism. >I found a way to do it (with an empty extra field in the base "class") >but I don't consider this to be a solution: I use code such as this: require ("structfuns"); private define default_print(obj) { ...} private define default_rotate(obj) { ...} private define default_translate(obj) { ...} private variable Base_Type = struct { print, rotate, translate }; Base_Type.print = &default_print; Base_Type.print = &default_rotate; Base_Type.print = &default_translate; define derived_type (fields) { variable type = @Base_Type; return struct_combine (m, fields); } private define foo_method (foo) { if (foo.x == 3) do_something(); something_else (); } private define foo_print (foo) { vmessage ("foo: x = %g", x); } define new_foo (x) { variable s = derived_type (["foo", "x"]); s.method = &foo_method; s.x = x; return s; } foo = new_foo (3.1); foo.rotate (); foo.method (); foo.print (); --John From joerg at alea.gnuu.de Mon Oct 23 05:56:07 2006 From: joerg at alea.gnuu.de (=?UTF-8?Q?J=C3=B6rg?= Sommer) Date: Tue Jan 30 08:52:12 2007 Subject: [slang-users] comparison operator grammar change References: <200610160636.k9G6aLPV005074@aluche.mit.edu> Message-ID: Hello John, "John E. Davis" wrote: > For slang-2.1, I am thinking about making a change to the part of the > grammar that deals with the four comparison operators <, <=, >, >=. > In particular, I would like for boolean expressions such as > > a < b <= c > > to mean > > ((a < b) and (b <= c)). A really interesting idea. I don't know other programming language that supports such a syntax, although it is a common notation in mathematics. > Unfortunately, such a change would break backward-compatibility. > Currently, a My feeling is that usage of such constructs is extremely rare ACK. > Any thoughts on this matter? I think it would be a nice feature. Bye, J?rg. -- Ein Optimist ist in der Regel ein Zeitgenosse, der ungenuegend informiert ist. (John B. Priestley) From Ajay.Kumar at omantel.om Tue Oct 31 01:09:15 2006 From: Ajay.Kumar at omantel.om (Ajay Kumar) Date: Tue Jan 30 08:52:12 2007 Subject: [slang-users] Reg bitwise & Message-ID: <5898189355437B4290D885FA68B6C8A779F548@OMEXCHANGE.corp.omantel.om> Hi, I am a new bee in slang. I would like to know the functionality of bit wise and (&) operator in slang. Int1 = (25 & 1) Int2 = (25 & 2) Int3 = (25 & 4) Int4 = (26 & 1) Int5 = (26 & 2) Int6 = (26 & 4) What is expected return values in the IntX specified above? How does slang proceeds with calculating the values. Thanks in advance, Bill. From davis at space.mit.edu Tue Oct 31 23:29:08 2006 From: davis at space.mit.edu (John E. Davis) Date: Tue Jan 30 08:52:12 2007 Subject: [slang-users] Reg bitwise & In-Reply-To: <5898189355437B4290D885FA68B6C8A779F548@OMEXCHANGE.corp.omantel.om> References: <5898189355437B4290D885FA68B6C8A779F548@OMEXCHANGE.corp.omantel.om> Message-ID: <200611010429.kA14T8hJ022723@aluche.mit.edu> Ajay Kumar wrote: >Int1 = (25 & 1) >Int2 = (25 & 2) >Int3 = (25 & 4) > >Int4 = (26 & 1) >Int5 = (26 & 2) >Int6 = (26 & 4) > >What is expected return values in the IntX specified above? How does >slang proceeds with calculating the values. The binary representations of the above numbers are given in the following table: 1 00001 2 00010 4 00100 25 11001 26 11010 So: Int1 = 25&1 = 00001 = 1 Int2 = 25&2 = 00000 = 0 Int3 = 25&4 = 00000 = 0 Int4 = 26&1 = 00000 = 0 Int5 = 26&2 = 00010 = 1 int6 = 26&4 = 00000 = 0 If you are getting different values, then there is a problem. --John