troubleshooting Question

C# Code Tree for Custom Language

Avatar of Misha
MishaFlag for Russian Federation asked on
Programming.NET ProgrammingC#
6 Comments1 Solution57 ViewsLast Modified:
Hello, Experts!
I need to create code tree for custom language like pascal.
Now I try to add code statements in tree.
I have some code in this language, for examplle:
let a = 2
if ( a > 2)
   while ( a > 2)
      let a = 2
   end while
   let a = 2
end if

Open in new window

I have classes for each statements,
class BaseStatement
{
   internal List<BaseStatement> statements;
}

sealed class IfStatement : BaseStatement{}
sealed class LetStatement : BaseStatement{}
sealed class WhileStatement : BaseStatement{}
...
etc

Open in new window

Also I have Tree Class
sealed class Tree
{
  BaseStatement root;
}

Open in new window

1) I need the best and beautifull solution to select node for adding current stament. I read code by rows.
If I get "IF" in my parser, root should be IfStatement and next statements should be added to it.
If I get "END IF", I need make  step up to above node. May be it should be something like switcher..

2) LetStatement  has no descendants .Whether it should inherit class BaseStatement??

My result should be like this

root
  LetStatement
  IfStatement
     WhileStatement
        LetStatement
     LetStatement
ASKER CERTIFIED SOLUTION
dpearson

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros