Posts: 2020
Threads: 133
Joined: July 26, 2017
Reputation:
5
Complicated AST manipulation looks ugly in C++. How to refactor it?
January 25, 2022 at 2:43 am
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?
Posts: 35280
Threads: 204
Joined: August 13, 2012
Reputation:
146
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
January 25, 2022 at 2:52 am
(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???"
Posts: 30974
Threads: 204
Joined: July 19, 2011
Reputation:
141
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
January 25, 2022 at 2:56 am
If you're worried about refactoring ten lines of code, you might want to reexamine your priorities
.-- someone that refactors code and shit
Posts: 6610
Threads: 73
Joined: May 31, 2014
Reputation:
56
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
January 25, 2022 at 3:12 am
(This post was last modified: January 25, 2022 at 3:38 am by GrandizerII.)
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);
}
Posts: 46122
Threads: 538
Joined: July 24, 2013
Reputation:
109
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
January 25, 2022 at 6:49 am
Boru
‘I can’t be having with this.’ - Esmeralda Weatherwax
Posts: 29649
Threads: 116
Joined: February 22, 2011
Reputation:
159
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
January 25, 2022 at 9:43 am
Have you tried turning it off then on again?
Posts: 7259
Threads: 506
Joined: December 12, 2015
Reputation:
22
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
January 25, 2022 at 7:03 pm
My hourly rate is only $35/hr without benefits; no guarantees, all sales are final.
Posts: 2020
Threads: 133
Joined: July 26, 2017
Reputation:
5
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
January 25, 2022 at 7:34 pm
(This post was last modified: January 25, 2022 at 7:35 pm by FlatAssembler.)
(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.
Posts: 16461
Threads: 127
Joined: July 10, 2013
Reputation:
65
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
January 25, 2022 at 8:05 pm
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.
Posts: 7259
Threads: 506
Joined: December 12, 2015
Reputation:
22
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
January 25, 2022 at 8:12 pm
(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.
|