© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
139 replies
rick_o_max

❔ System.Linq.Expressions custom expression

Hi guys!

I'm parsing a custom language and building an Expression Tree based on it, but I came into an issue:

My language can use Labels, but since I parse the file line by line, I can't reference a label that hasn't been parsed yet when creating a
Goto
Goto
expression.

So parsing something like that would give issues:
IFEQUALS var1, 10;
  GOTO label;
END;
label:
  PRINT "HELLO WORLD";
IFEQUALS var1, 10;
  GOTO label;
END;
label:
  PRINT "HELLO WORLD";

So, my idea was to inherit the
Expression
Expression
class to create a kind of
LazyGotoExpression
LazyGotoExpression
, where the
LabelTarget
LabelTarget
would be evaluated by the time the expression is compiled, but I have no clue how to do that.

Could you guys give me a hand?

Current parsing code:
  private Expression ParseExpression(List<string> tokens, Func<List<string>> parseNextStatement)
        {
            switch (tokens[0])
            {
                case "LABEL":
                    {
                        var label = tokens[1];
                        var labelTarget = Expression.Label(label);
                        _labels.Add(label, labelTarget);
                        return Expression.Label(labelTarget);
                    }
                case "GOTO":
                    {
                        //I MIGHT NOT HAVE THE LABEL REFERENCE HERE, SO I NEED A WAY TO LAZY EVALUATE IT
                        var label = tokens[1];
                        var expression = Expression.Empty();
                        _goto.Add(expression, label);
                        return expression;
                    }
  private Expression ParseExpression(List<string> tokens, Func<List<string>> parseNextStatement)
        {
            switch (tokens[0])
            {
                case "LABEL":
                    {
                        var label = tokens[1];
                        var labelTarget = Expression.Label(label);
                        _labels.Add(label, labelTarget);
                        return Expression.Label(labelTarget);
                    }
                case "GOTO":
                    {
                        //I MIGHT NOT HAVE THE LABEL REFERENCE HERE, SO I NEED A WAY TO LAZY EVALUATE IT
                        var label = tokens[1];
                        var expression = Expression.Empty();
                        _goto.Add(expression, label);
                        return expression;
                    }


If there is a way to make a custom
Expression
Expression
return another
Expression
Expression
instance, that would be perfect.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

✅ System.InvalidOperationException: The LINQ expression
C#CC# / help
2y ago
Build ref return with System.Linq.Expressions
C#CC# / help
4y ago
Await Linq Expression
C#CC# / help
4y ago
Why is this Linq.Expressions.Expression is evaluating to NULL?
C#CC# / help
2y ago