A feture adjustment request and a syntax blasphemy

BCS BCS at pathlink.com
Fri Oct 27 17:49:14 PDT 2006


This is allowed:

   void foo(int[] ar){}
   ...
   static int[] i = [1,2,3];
   i.foo();

And this is allowed:

   struct I
   {
     int[] v;
     void foo(){}
   }
   ...
   I i;
   auto dg = &i.foo;

So why no this?

   void foo(int[] ar){}
   ...
   static int[] i = [1,2,3];
   auto dg = &i.foo;



And another one:

This is allowed:

   static int[] i = [1,2,3];
   void function(int[]) fp = function void(int[] j){return;};
   i.fp();

So why no this?

   static int[] i = [1,2,3];
   i.function void(int[] j){return;}();




You may be wondering where this is all going (and what that was about a 
blasphemy). Well her it is:

<code type="WTF!">

	// a base type for derivation
class Type{}
	// a templated derived type to store things in
class TypeT(T) : Type
{
	public T val;
	static TypeT!(T) opCall(T v)
	{
		auto ret = new TypeT!(T);
		ret.val = v;
		return ret;
	}
}

/*
Doc ::=
	  A:a B:b C:c	= new Fig(a,b,c)
	| D:d E:e		= new Fig(d,e)
*/
TypeT!(Fig) delegate() Parse_Doc(inout char[] from)
{
	char[] tmp;

		// copy from
	tmp = from;
	if(auto a = Parse_A(tmp)) // try to parse an "a"
	if(auto b = Parse_B(tmp)) // try to parse an "b"
	if(auto c = Parse_C(tmp)) // try to parse an "c"
	{	// if all's good
			// update what's been parsed
		from = tmp;
		return &(
				// make an array from the args
			[cast(Type delegate())a,b,c]
				// copy it (force off stack)
			.dup
			)
				// form delegate from array
				// and function literal				
			.function TypeT!(Fig)(Type delegate()[] args)
			{
					// literal calls given code
				auto ret =
					new Fig(
						// call args for values
					cast(a.typeof)args[0]().val,
					cast(b.typeof)args[1]().val,
					cast(b.typeof)args[2]().val);

					// place in object and return
				return TypeT!(ret.typeof)(ret);
			};
	}

		// restore tmp
	tmp = from;

		// blah, blah, blah
	if(auto d = Parse_D(tmp))
	if(auto e = Parse_E(tmp))
	{
		from = tmp;
		return &([cast(Type delegate())d,e].dup)
			.function TypeT!(Fig)(Type delegate()[] args)
			{
				auto ret =
					new Fig(
				cast(d.typeof)args[0]().val,
				cast(e.typeof)args[1]().val);
				return TypeT!(ret.typeof)(ret);
			};
	}

	return null;
}
/*
A ::=
	"hello":v	= v
*/
TypeT!(char[]) delegate() Parse_A(inout char[] from)
{
	char[] tmp;

	tmp=from;
		// check for a "hello"
	if(from[0.."hello".length] == "hello")
	{
			// update from
		from = from["hello".length..$];
			// return delegate returning "hello" in obj
		return &("hello".dup).function TypeT!(char[])()
			{
				auto ret =
					from[0.."hello".length];
				return TypeT!(ret.typeof)(ret);
			};
	}

	return null;
}
/// ........

</code>
<script> for(auto i = brain.Remove; true; i.Lather.Rinse){} </script>


WHAT IN ALL THAT IS HOLY WAS THAT!!!!

It is a recursive decent parser that avoids executing actions that are 
never used. This would allow for actions to have side effects that would 
otherwise have to be undone after it is discovered that the current 
parse attempt fails.

Rather than do each action as it is tried, each reduction function 
returns a delegate that has enough info to do the actions for the 
components and the code to use the result to execute the action. After 
calling the topmost Reduction function, the return value then needs to 
be called.

auto a = Parse_Doc(SomeString);
if(a is null) return;
auto a().val;

The important things are

# making delegates from function used as properties of arrays
# using function literals in the above.

I would like to see both of these things added.

Also, this syntax blasphemy shows that there is a use for som of these 
things  and was to cool to not post about



More information about the Digitalmars-d-learn mailing list