Specifying late-bound variables

Jul 29, 2010 at 2:11 PM
Edited Jul 29, 2010 at 2:16 PM
I am stumped. I am creating a simple language that is effectively a calculator with named registers. I have the lexical analyzer and parser working, but I don't know what kind of Expression to emit when I encounter a local variable reference. For example, I can correctly parse and execute "2 + 3 * 5", but I cannot work out how to parse "x".

In my code, I have functions that look like this for numeric constants:
        public Expression NumberExpression(double value)
        {
            Expression result;
            result = Expression.Constant(value, typeof(double));
            return result;
        }
and this for a binary expression:
       public Expression BinaryMathExpression(ExpressionType type, Expression v1, Expression v2)
        {
            Expression result;
            DynamicMetaObjectBinder binder = languageContext.CreateBinaryOperationBinder(type);
            result = DynamicExpression.Dynamic(binder, typeof(object), v1, v2);
            return result;
        }
and I postulate that I need this for a member reference:
        public Expression MemberExpression(Expression subject, string membername)
        {
            Expression result;
            DynamicMetaObjectBinder binder = languageContext.CreateGetMemberBinder(membername, false);
            result = DynamicExpression.Dynamic(binder, typeof(object), subject);
            return result;
        }
but what do I emit for a simple late-bound local variable reference? All I want to do is tell the executor (not the compiler) that it should call out to my code so I can do a lookup in a ScriptScope at runtime. I have tried this:
        public Expression VariableExpression(string ident)
        {
            Expression result;
            result = Expression.Parameter(typeof(object), ident);
            return result;
        }
but the compiler wants to resolve the variable reference at compile time. Is there some kind of dynamic-ness that I can wrap around the parameter expression?
Coordinator
Jul 29, 2010 at 4:21 PM

I don't have time right now to give a more specific answer. I'm sorry, but you might look at the sympl.doc and sympl code on the dlr.codeplex.com site for the discussion of how it makes global variables work. The doc is at http://dlr.codeplex.com/wikipage?title=Docs%20and%20specs&referringTitle=Home&ProjectName=dlr

Bill

From: asthomas [mailto:notifications@codeplex.com]
Sent: Thursday, July 29, 2010 6:12 AM
To: Bill Chiles
Subject: Specifying late-bound variables [dlr:221843]

From: asthomas

I am stumped. I am creating a simple language that is effectively a calculator with named registers. I have the lexical analyzer and parser working, but I don't know what kind of Expression to emit when I encounter a local variable reference. For example, I can correctly parse and execute "2 + 3 * 5", but I cannot work out how to parse "x". In my code, I have functions that look like this for numeric constants: public Expression NumberExpression(double value) { Expression result; result = Expression.Constant(value, typeof(double)); return result; } and this for a binary expression: public Expression BinaryMathExpression(ExpressionType type, Expression v1, Expression v2) { Expression result; DynamicMetaObjectBinder binder = languageContext.CreateBinaryOperationBinder(type); result = DynamicExpression.Dynamic(binder, typeof(object), v1, v2); return result; } and I postulate that I need this for a member reference: public Expression MemberExpression(Expression subject, string membername) { Expression result; DynamicMetaObjectBinder binder = languageContext.CreateGetMemberBinder(membername, false); result = DynamicExpression.Dynamic(binder, typeof(object), subject); return result; } but what do I emit for a simple late-bound local variable reference? All I want to do is tell the executor (not the compiler) that it should call out to my code so I can do a lookup in a ScriptScope at runtime. I have tried this: public Expression VariableExpression(string ident) { Expression result; result = Expression.Parameter(typeof(object), ident); return result; } but the compiler wants to resolve the variable reference at compile time. Is there some kind of dynamic-ness that I can wrap around the parameter expression?

Read the full discussion online.

To add a post to this discussion, reply to this email (dlr@discussions.codeplex.com)

To start a new discussion for this project, email dlr@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Jul 29, 2010 at 4:46 PM
Edited Jul 29, 2010 at 4:47 PM
Believe me, I've been over the Sympl code again and again. I can see that they have a Property expression that appears to be acting as a placeholder for the global scope.

Right now I'm following the Sympl code approximately. I generate a GetMember with a placeholder target for variable look-ups. That's pretty unsatisfying, honestly, as I would have expected to be doing variable lookups through ScriptScopes. But, if that's the way it has to be done, fair enough. What's a ScriptScope for, then?

In general, the Sympl code is far from Simple. Is there a Sympler example around for DLR 1.0? I have found examples from 2008 that are for a defunct version of DLR, but nothing recent.