Posts: 2020
Threads: 133
Joined: July 26, 2017
Reputation:
5
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
January 27, 2022 at 8:36 am
(January 25, 2022 at 8:12 pm)Jehanne Wrote: (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.
C is not good at doing high-level stuff. C++ is good at both high-level and low-level stuff.
Posts: 7259
Threads: 506
Joined: December 12, 2015
Reputation:
22
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
January 27, 2022 at 2:16 pm
(January 27, 2022 at 8:36 am)FlatAssembler Wrote: (January 25, 2022 at 8:12 pm)Jehanne Wrote: Why anyone would write a complier in C++ is beyond me; in my opinion, C would be a much better choice.
C is not good at doing high-level stuff. C++ is good at both high-level and low-level stuff.
Isn't that the whole point of a compiler, to translate computer code into machine code? I would ditch the C++ and just use C, modeling your new language on an existing one. C#, after all, "borrowed" heavily from Java.
Posts: 1659
Threads: 5
Joined: September 26, 2018
Reputation:
12
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
January 27, 2022 at 5:29 pm
(January 27, 2022 at 2:16 pm)Jehanne Wrote: Isn't that the whole point of a compiler, to translate computer code into machine code? I would ditch the C++ and just use C, modeling your new language on an existing one. C#, after all, "borrowed" heavily from Java.
Anyone writing complex code in C is crazy or a masochist. C++ at least has the standard template library (even if it is a pain to learn).
Using C++ allows Flat to potentially compile his compiler into any platform, including webassembly. Yes, that can be done with other languages, but it isn't easy with garbage-collected languages.
Posts: 29619
Threads: 116
Joined: February 22, 2011
Reputation:
159
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
January 27, 2022 at 5:58 pm
(This post was last modified: January 27, 2022 at 5:58 pm by Angrboda.)
(January 27, 2022 at 5:29 pm)HappySkeptic Wrote: (January 27, 2022 at 2:16 pm)Jehanne Wrote: Isn't that the whole point of a compiler, to translate computer code into machine code? I would ditch the C++ and just use C, modeling your new language on an existing one. C#, after all, "borrowed" heavily from Java.
Anyone writing complex code in C is crazy or a masochist. C++ at least has the standard template library (even if it is a pain to learn).
Using C++ allows Flat to potentially compile his compiler into any platform, including webassembly. Yes, that can be done with other languages, but it isn't easy with garbage-collected languages.
It's a matter of trade-offs. Hand-optimizing C is a lot more effective than trying to optimize object-based code. Time is money with compliers.
Posts: 1659
Threads: 5
Joined: September 26, 2018
Reputation:
12
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
January 27, 2022 at 7:07 pm
(January 27, 2022 at 5:58 pm)Angrboda Wrote: It's a matter of trade-offs. Hand-optimizing C is a lot more effective than trying to optimize object-based code. Time is money with compliers.
I doubt Flat is going to be selling his compiler any time soon, and he is porting from another language.
C++ can be just as fast as C, if you are careful, and if you aren't careful, you probably don't know how to write C-code that won't blow up or leak.
Posts: 30974
Threads: 204
Joined: July 19, 2011
Reputation:
141
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
January 27, 2022 at 9:24 pm
(January 27, 2022 at 5:29 pm)HappySkeptic Wrote: (January 27, 2022 at 2:16 pm)Jehanne Wrote: Isn't that the whole point of a compiler, to translate computer code into machine code? I would ditch the C++ and just use C, modeling your new language on an existing one. C#, after all, "borrowed" heavily from Java.
Anyone writing complex code in C is crazy or a masochist. C++ at least has the standard template library (even if it is a pain to learn).
Using C++ allows Flat to potentially compile his compiler into any platform, including webassembly. Yes, that can be done with other languages, but it isn't easy with garbage-collected languages.
I've written hundreds of thousands of lines of complex code in C.
Am I crazy, or did I just not have better tools?
Posts: 2020
Threads: 133
Joined: July 26, 2017
Reputation:
5
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
January 29, 2022 at 1:26 am
(This post was last modified: January 29, 2022 at 1:51 am by FlatAssembler.)
(January 27, 2022 at 2:16 pm)Jehanne Wrote: (January 27, 2022 at 8:36 am)FlatAssembler Wrote: C is not good at doing high-level stuff. C++ is good at both high-level and low-level stuff.
Isn't that the whole point of a compiler, to translate computer code into machine code?
No, that is what the assembler is doing. An assembler is translating computer code (assembly code) into machine code. A compiler is translating computer code into assembly code. A compiler does not have to deal with machine code at all, that is the assembler's job. By the way, I have also written an assembler, as a part of PicoBlaze Simulator in JavaScript.
(January 27, 2022 at 5:29 pm)HappySkeptic Wrote: (January 27, 2022 at 2:16 pm)Jehanne Wrote: Isn't that the whole point of a compiler, to translate computer code into machine code? I would ditch the C++ and just use C, modeling your new language on an existing one. C#, after all, "borrowed" heavily from Java.
Anyone writing complex code in C is crazy or a masochist. C++ at least has the standard template library (even if it is a pain to learn).
Using C++ allows Flat to potentially compile his compiler into any platform, including webassembly. Yes, that can be done with other languages, but it isn't easy with garbage-collected languages.
Why would C++ Standard Template Library be pain to learn? I used it on programming competitions back when I was 13 years old.
Yeah, my compiler can basically run anywhere there is a C++11 compiler, including FreeDOS. But it cannot really be used on FreeDOS because, well, the assembler it is targetting, `wat2wasm` from WebAssembly Binary Toolkit, cannot be run on FreeDOS. Or at least I have not managed to run it there. It would definitely need to be cross-compiled somehow since CMAKE, the build system WebAssembly Binary Toolkit is using, does not run on FreeDOS either.
(January 27, 2022 at 5:58 pm)Angrboda Wrote: (January 27, 2022 at 5:29 pm)HappySkeptic Wrote: Anyone writing complex code in C is crazy or a masochist. C++ at least has the standard template library (even if it is a pain to learn).
Using C++ allows Flat to potentially compile his compiler into any platform, including webassembly. Yes, that can be done with other languages, but it isn't easy with garbage-collected languages.
It's a matter of trade-offs. Hand-optimizing C is a lot more effective than trying to optimize object-based code. Time is money with compliers.
With modern computers, development time is far more expensive than the time the program takes to run.
Posts: 7259
Threads: 506
Joined: December 12, 2015
Reputation:
22
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
January 29, 2022 at 8:52 am
(January 29, 2022 at 1:26 am)FlatAssembler Wrote: (January 27, 2022 at 2:16 pm)Jehanne Wrote: Isn't that the whole point of a compiler, to translate computer code into machine code?
No, that is what the assembler is doing. An assembler is translating computer code (assembly code) into machine code. A compiler is translating computer code into assembly code. A compiler does not have to deal with machine code at all, that is the assembler's job.
My understanding is that some compilers, after the syntax analysis and parsing trees, proceed directly to the binary code while others generate assembly code that the assembler then uses to generate the machine code.
Posts: 30974
Threads: 204
Joined: July 19, 2011
Reputation:
141
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
January 29, 2022 at 9:15 am
(January 29, 2022 at 8:52 am)Jehanne Wrote: (January 29, 2022 at 1:26 am)FlatAssembler Wrote: No, that is what the assembler is doing. An assembler is translating computer code (assembly code) into machine code. A compiler is translating computer code into assembly code. A compiler does not have to deal with machine code at all, that is the assembler's job.
My understanding is that some compilers, after the syntax analysis and parsing trees, proceed directly to the binary code while others generate assembly code that the assembler then uses to generate the machine code.
Then, conceptually, they still have an assembler, it's an integral part of the compliler.
Posts: 2020
Threads: 133
Joined: July 26, 2017
Reputation:
5
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
January 30, 2022 at 4:19 am
(January 29, 2022 at 8:52 am)Jehanne Wrote: (January 29, 2022 at 1:26 am)FlatAssembler Wrote: No, that is what the assembler is doing. An assembler is translating computer code (assembly code) into machine code. A compiler is translating computer code into assembly code. A compiler does not have to deal with machine code at all, that is the assembler's job.
My understanding is that some compilers, after the syntax analysis and parsing trees, proceed directly to the binary code while others generate assembly code that the assembler then uses to generate the machine code.
Yes, but such compilers are significantly harder to program and to debug.
|