Our server costs ~$56 per month to run. Please consider donating or becoming a Patron to help keep the site running. Help us gain new members by following us on Twitter and liking our page on Facebook!
Current time: November 23, 2024, 8:06 pm

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Complicated AST manipulation looks ugly in C++. How to refactor it?
#1
Complicated AST manipulation looks ugly in C++. How to refactor it?
Here is a code I have recently written in the compiler for my programming language, written in C++:
Code:
  } else if (text.size() == 2 and
             text[1] ==
                 '=') // The assignment operators "+=", "-=", "*=" and "/="...
  {
    if (children.at(0).text.back() ==
        '[') { // https://github.com/FlatAssembler/AECforWebAssembly/issues/15
               // https://discord.com/channels/530598289813536771/847014270922391563/934823770307301416
      TreeNode fakeInnerFunctionNode("Does", lineNumber, columnNumber);
      std::string subscriptName = "tmp" + std::to_string(rand());
      TreeNode declarationOfSubscript("Integer32", lineNumber, columnNumber);
      declarationOfSubscript.children.push_back(
          TreeNode(subscriptName, lineNumber, columnNumber));
      fakeInnerFunctionNode.children.push_back(declarationOfSubscript);
      TreeNode subscriptAssignment(":=", lineNumber, columnNumber);
      subscriptAssignment.children.push_back(
          TreeNode(subscriptName, lineNumber, columnNumber));
      subscriptAssignment.children.push_back(children[0].children.at(0));
      fakeInnerFunctionNode.children.push_back(subscriptAssignment);
      TreeNode convertedToSimpleAssignment(":=", lineNumber, columnNumber);
      convertedToSimpleAssignment.children.push_back(TreeNode(
          children[0].text, children[0].lineNumber, children[0].columnNumber));
      convertedToSimpleAssignment.children[0].children.push_back(
          TreeNode(subscriptName, lineNumber, columnNumber));
      convertedToSimpleAssignment.children.push_back(
          TreeNode(text.substr(0, 1), lineNumber, columnNumber));
      convertedToSimpleAssignment.children[1].children.push_back(TreeNode(
          children[0].text, children[0].lineNumber, children[0].columnNumber));
      convertedToSimpleAssignment.children[1].children[0].children.push_back(
          TreeNode(subscriptName, lineNumber, columnNumber));
      convertedToSimpleAssignment.children[1].children.push_back(
          children.at(1));
      fakeInnerFunctionNode.children.push_back(convertedToSimpleAssignment);
      CompilationContext fakeContext = context;
      fakeContext.stackSizeOfThisScope = 0;
      fakeContext.stackSizeOfThisFunction = 0;
      assembly += fakeInnerFunctionNode.compile(fakeContext) + "\n";
    } else {
      TreeNode convertedToSimpleAssignment(":=", lineNumber, columnNumber);
      convertedToSimpleAssignment.children.push_back(children[0]);
      convertedToSimpleAssignment.children.push_back(*this);
      convertedToSimpleAssignment.children[1].text =
          convertedToSimpleAssignment.children[1].text.substr(0, 1);
      assembly += convertedToSimpleAssignment.compile(context);
    }
It definitely looks incomprehensible. How would you write it better?
Reply
#2
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
(January 25, 2022 at 2:43 am)FlatAssembler Wrote: Here is a code I have recently written in the compiler for my programming language, written in C++:
Code:
  } else if (text.size() == 2 and
             text[1] ==
                 '=') // The assignment operators "+=", "-=", "*=" and "/="...
  {
    if (children.at(0).text.back() ==
        '[') { // https://github.com/FlatAssembler/AECforWebAssembly/issues/15
               // https://discord.com/channels/530598289813536771/847014270922391563/934823770307301416
      TreeNode fakeInnerFunctionNode("Does", lineNumber, columnNumber);
      std::string subscriptName = "tmp" + std::to_string(rand());
      TreeNode declarationOfSubscript("Integer32", lineNumber, columnNumber);
      declarationOfSubscript.children.push_back(
          TreeNode(subscriptName, lineNumber, columnNumber));
      fakeInnerFunctionNode.children.push_back(declarationOfSubscript);
      TreeNode subscriptAssignment(":=", lineNumber, columnNumber);
      subscriptAssignment.children.push_back(
          TreeNode(subscriptName, lineNumber, columnNumber));
      subscriptAssignment.children.push_back(children[0].children.at(0));
      fakeInnerFunctionNode.children.push_back(subscriptAssignment);
      TreeNode convertedToSimpleAssignment(":=", lineNumber, columnNumber);
      convertedToSimpleAssignment.children.push_back(TreeNode(
          children[0].text, children[0].lineNumber, children[0].columnNumber));
      convertedToSimpleAssignment.children[0].children.push_back(
          TreeNode(subscriptName, lineNumber, columnNumber));
      convertedToSimpleAssignment.children.push_back(
          TreeNode(text.substr(0, 1), lineNumber, columnNumber));
      convertedToSimpleAssignment.children[1].children.push_back(TreeNode(
          children[0].text, children[0].lineNumber, children[0].columnNumber));
      convertedToSimpleAssignment.children[1].children[0].children.push_back(
          TreeNode(subscriptName, lineNumber, columnNumber));
      convertedToSimpleAssignment.children[1].children.push_back(
          children.at(1));
      fakeInnerFunctionNode.children.push_back(convertedToSimpleAssignment);
      CompilationContext fakeContext = context;
      fakeContext.stackSizeOfThisScope = 0;
      fakeContext.stackSizeOfThisFunction = 0;
      assembly += fakeInnerFunctionNode.compile(fakeContext) + "\n";
    } else {
      TreeNode convertedToSimpleAssignment(":=", lineNumber, columnNumber);
      convertedToSimpleAssignment.children.push_back(children[0]);
      convertedToSimpleAssignment.children.push_back(*this);
      convertedToSimpleAssignment.children[1].text =
          convertedToSimpleAssignment.children[1].text.substr(0, 1);
      assembly += convertedToSimpleAssignment.compile(context);
    }
It definitely looks incomprehensible. How would you write it better?

I'd add dragons.

Possibly a couple of elves and a magical sword.

Playing Cluedo with my mum while I was at Uni:

"You did WHAT?  With WHO?  WHERE???"
Reply
#3
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
If you're worried about refactoring ten lines of code, you might want to reexamine your priorities



.-- someone that refactors code and shit
Reply
#4
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
Do you actually use an IDE for your coding, Flat? Or is it all written in a standard text doc?

Nevermind, seems like it's the former.

Maybe avoid hardcoding URLs? Or if they're just part of comments, don't explicitly state the URLs and instead come up with something shorthand

Other than that, I don't see the issue. I'd rather you do what you did in that coding than try to crunch them all in into one statement.

ETA: How about this? Still some possible misalignments, but I'm done trying

Code:
} else if (text.size() == 2 and text[1] == '=') // The assignment operators "+=", "-=", "*=" and "/="...
  {
    if (children.at(0).text.back() == '[') {
      // https://github.com/FlatAssembler/AECforWebAssembly/issues/15
      // https://discord.com/channels/530598289813536771/847014270922391563/934823770307301416
      TreeNode fakeInnerFunctionNode("Does", lineNumber, columnNumber);
      std::string subscriptName = "tmp" + std::to_string(rand());
      TreeNode declarationOfSubscript("Integer32", lineNumber, columnNumber);
      declarationOfSubscript.children.push_back(TreeNode(subscriptName, lineNumber, columnNumber));
      fakeInnerFunctionNode.children.push_back(declarationOfSubscript);
      TreeNode subscriptAssignment(":=", lineNumber, columnNumber);
      subscriptAssignment.children.push_back(TreeNode(subscriptName, lineNumber, columnNumber));
      subscriptAssignment.children.push_back(children[0].children.at(0));
      fakeInnerFunctionNode.children.push_back(subscriptAssignment);
      TreeNode convertedToSimpleAssignment(":=", lineNumber, columnNumber);
      convertedToSimpleAssignment.children.push_back(TreeNode(children[0].text, children[0].lineNumber, children[0].columnNumber));
      convertedToSimpleAssignment.children[0].children.push_back(TreeNode(subscriptName, lineNumber, columnNumber));
      convertedToSimpleAssignment.children.push_back(TreeNode(text.substr(0, 1), lineNumber, columnNumber));
      convertedToSimpleAssignment.children[1].children.push_back(TreeNode(children[0].text, children[0].lineNumber, children[0].columnNumber));
      convertedToSimpleAssignment.children[1].children[0].children.push_back(TreeNode(subscriptName, lineNumber, columnNumber));
      convertedToSimpleAssignment.children[1].children.push_back(children.at(1));
      fakeInnerFunctionNode.children.push_back(convertedToSimpleAssignment);
      CompilationContext fakeContext = context;
      fakeContext.stackSizeOfThisScope = 0;
      fakeContext.stackSizeOfThisFunction = 0;
      assembly += fakeInnerFunctionNode.compile(fakeContext) + "\n";
    } else {
      TreeNode convertedToSimpleAssignment(":=", lineNumber, columnNumber);
      convertedToSimpleAssignment.children.push_back(children[0]);
      convertedToSimpleAssignment.children.push_back(*this);
      convertedToSimpleAssignment.children[1].text = convertedToSimpleAssignment.children[1].text.substr(0, 1);
      assembly += convertedToSimpleAssignment.compile(context);
    }
Reply
#5
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
[Image: giphy-downsized-large.gif]

Boru
‘I can’t be having with this.’ - Esmeralda Weatherwax
Reply
#6
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
Have you tried turning it off then on again?
[Image: extraordinarywoo-sig.jpg]
Reply
#7
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
My hourly rate is only $35/hr without benefits; no guarantees, all sales are final.
Reply
#8
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
(January 25, 2022 at 2:52 am)The Valkyrie Wrote:
(January 25, 2022 at 2:43 am)FlatAssembler Wrote: Here is a code I have recently written in the compiler for my programming language, written in C++:
Code:
  } else if (text.size() == 2 and
             text[1] ==
                 '=') // The assignment operators "+=", "-=", "*=" and "/="...
  {
    if (children.at(0).text.back() ==
        '[') { // https://github.com/FlatAssembler/AECforWebAssembly/issues/15
               // https://discord.com/channels/530598289813536771/847014270922391563/934823770307301416
      TreeNode fakeInnerFunctionNode("Does", lineNumber, columnNumber);
      std::string subscriptName = "tmp" + std::to_string(rand());
      TreeNode declarationOfSubscript("Integer32", lineNumber, columnNumber);
      declarationOfSubscript.children.push_back(
          TreeNode(subscriptName, lineNumber, columnNumber));
      fakeInnerFunctionNode.children.push_back(declarationOfSubscript);
      TreeNode subscriptAssignment(":=", lineNumber, columnNumber);
      subscriptAssignment.children.push_back(
          TreeNode(subscriptName, lineNumber, columnNumber));
      subscriptAssignment.children.push_back(children[0].children.at(0));
      fakeInnerFunctionNode.children.push_back(subscriptAssignment);
      TreeNode convertedToSimpleAssignment(":=", lineNumber, columnNumber);
      convertedToSimpleAssignment.children.push_back(TreeNode(
          children[0].text, children[0].lineNumber, children[0].columnNumber));
      convertedToSimpleAssignment.children[0].children.push_back(
          TreeNode(subscriptName, lineNumber, columnNumber));
      convertedToSimpleAssignment.children.push_back(
          TreeNode(text.substr(0, 1), lineNumber, columnNumber));
      convertedToSimpleAssignment.children[1].children.push_back(TreeNode(
          children[0].text, children[0].lineNumber, children[0].columnNumber));
      convertedToSimpleAssignment.children[1].children[0].children.push_back(
          TreeNode(subscriptName, lineNumber, columnNumber));
      convertedToSimpleAssignment.children[1].children.push_back(
          children.at(1));
      fakeInnerFunctionNode.children.push_back(convertedToSimpleAssignment);
      CompilationContext fakeContext = context;
      fakeContext.stackSizeOfThisScope = 0;
      fakeContext.stackSizeOfThisFunction = 0;
      assembly += fakeInnerFunctionNode.compile(fakeContext) + "\n";
    } else {
      TreeNode convertedToSimpleAssignment(":=", lineNumber, columnNumber);
      convertedToSimpleAssignment.children.push_back(children[0]);
      convertedToSimpleAssignment.children.push_back(*this);
      convertedToSimpleAssignment.children[1].text =
          convertedToSimpleAssignment.children[1].text.substr(0, 1);
      assembly += convertedToSimpleAssignment.compile(context);
    }
It definitely looks incomprehensible. How would you write it better?

I'd add dragons.

Possibly a couple of elves and a magical sword.
I have no idea what that means.

(January 25, 2022 at 9:43 am)Angrboda Wrote: Have you tried turning it off then on again?

How is that supposed to help? The file in which the code is stored will (hopefully) remain unchanged.
Reply
#9
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
This is not an IT forum...read the top of the page.

There are most likely numerous places where you could perhaps get some assistance or at least find someone who cares.
[Image: MmQV79M.png]  
                                      
Reply
#10
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
(January 25, 2022 at 8:05 pm)arewethereyet Wrote: This is not an IT forum...read the top of the page.

There are most likely numerous places where you could perhaps get some assistance or at least find someone who cares.

Why anyone would write a complier in C++ is beyond me; in my opinion, C would be a much better choice.
Reply





Users browsing this thread: 1 Guest(s)