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 21, 2024, 12:56 pm

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Complicated AST manipulation looks ugly in C++. How to refactor it?
#11
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
(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.
Reply
#12
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
(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.
Reply
#13
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
(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.
Reply
#14
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
(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.
[Image: extraordinarywoo-sig.jpg]
Reply
#15
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
(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.
Reply
#16
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
(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?
Reply
#17
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
(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.
Reply
#18
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
(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.
Reply
#19
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
(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.
Reply
#20
RE: Complicated AST manipulation looks ugly in C++. How to refactor it?
(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.
Reply





Users browsing this thread: 1 Guest(s)