Writing a compiler in C#, generating C vs IL? [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have been wanting to create my own programming language and I am looking to start writing a basic compiler. I am doing this purely for learning purposes.
I will be writing the compiler in C#.
I have been trying to decide whether or not to generate IL or another high-level language. From articles and tutorials I have seen/read it seems C and MSIL (by way of reflection.emit) are the most popular.
I am wondering which approach will make my programming language faster? (assuming they were implemented optimally). Ideally I'd like the language to be able to run on both MS and Linux/OSX - I also understand that there may be better alternatives out there I am not considering

Your decision generaly depends on the design and paradigms of your language. If your language will be small and will not include complex object-oriented features, than only non "object-oriented" features of IL will be used, and the difference is about:
The availability of.NET virtual machine and BCL vs C standard library for purpose of language implementation. This includes the memory management capabilities and implementation of primitive types, such ints and strings.
The code generation: stack-based IL vs high-level C syntax. Of course, it can be easier to generate high-level constructs of another language (you should not embrace all the grammar of C, you can just use what you need), but for learning puproses it is more useful to learn how to generate low-level instructions like IL opcodes. And don't forget: it will be cool, if you split your tool into frontend and backend, as it is done in every solid compiler. Than you can just use different backends for code generation.
PROS for IL:
more solid learning process and the complete result: your compiler will not require any other tools and will be self-sufficing;
the presense of BCL and resource-management layers in CLR;
ability to bootstrap your compiler by interaction with C# code.
the unique experience with .net platform - the useful thing if you plan to raise your C# and .net skills.
PROS for C:
the ability to utilize existing backends to generate platform code
and to perform optimizations; you can compile your C output for every
platform C compiler can;
absense of the dependency from CLR: you will not need .net fw or Mono to run the produced output. Today Mono is mature thing and is running both on Mac and Linux, but it is always the choice: IL or platform code.
A lot of modern languages compile to another high-level languages (oh god, there is tons of something-to-js tools today!), and some of the languages is even DESIGNED to be compiled to another high-level language (CoffeeScript to JavaScript), but don't forget that you have another options too, for example, LLVM intermediate representation.

Related

Why is C# tightly coupled to .Net? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
In C++, Either C++ code can be compiled by a compiler or used in a C++ framework like Platinum. I mean, you have the choice to run C++ independently without frameworks. The same story goes for python and other languages. But come to C# world, I think (maybe I am wrong) you have no choice to run C# independetly by a compiler; it's coupled to dotNet framework or mono. Are there other languages that behave like C# and need its framework to run? Or only C# behave that way?
I think there are two answers to this question.
The framework
I think this is just a battle of words.
Python has an interpreter and comes with "batteries included" meaning a large standard library. That interpreter and libraries included by default is python's runtime environment.
.NET is the runtime environment for C#. It for example includes the virtual machine required to run C# programs.
Please also remember that framework is just a term and it's used at many levels. For example, ASP.NET is also a framework but it's meaning is different to .NET Framework, it's a framework on another level.
Mono is not .NET Framework
It is important to point out. mono is not .NET Framework and it's a real life proof that C# is not coupled to .NET Framework.
Why C# not completely independent of .NET?
C# was created as part of and for .NET.
I want to know that if I can run C# without .Net. I want to separate the framework concept from the language.
If you look into the C# spec, ECMA 334, you'll see that it says:
Although Microsoft’s implementation of C# relies on CLI for library and run-time support, other implementations of C# need not, provided they support an alternate way of getting at the minimum CLI features required by this C# standard (see Annex C).
CLI (Common Language Infrastructure) is based on a subset of the .NET Framework, as the standard says, so C#, the language, indeed does not rely on .NET. It only relies on the things listed in "Annex C", which you can see for yourself.
However, Microsoft's C# compiler, a specific implementation of the C# language does rely on .NET.
Also note that C# with only the things listed in Annex C is not going to be very powerful. I can't even find the System.Console class there. I wouldn't think that you will be able to write any practical program with only the things in Annex C, so at a practical level, yes C# does rely on .NET very much.

What is the most efficient method in converting AutoLISP legacy code to C#? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I am engaged in a project that works mainly in AutoCAD to design and manufacture prefabricated building components such as roofing trusses. One of our goals is to redesign a program that was written in LISP that functions in designing roofing trusses. We are to rewrite the LISP code in C# and incrementally implement it into the current libraries that they have set up.
My problem is that I have been tasked with building a rudimentary LISP to C# converter. After some research (as Google results quickly show that such a thing does not readily exist on hand), I have come to the question of which way of converting this legacy code would be more efficient. Would it be better to take chunks of the LISP code to analyze and rewrite in C#, or should I continue on with developing a rudimentary converter for the AutoLISP code?
You should take chunks of the LISP code and rewrite in C#.
Even if it was less effort to write a general purpose LISP interpreter in C# than to rewrite the LISP in c# (which is highly improbable), the LISP is probably running AutoCAD commands like you would type in the AutoCAD command line instead doing things the ObjectARX way. So you would also need to convert the commands to use the ObjectARX API.
C# is a compiled object-oriented programming language whereas AutoLISP is an interpreted expression-oriented language. Therefore there is never going to be a really straightforward way of converting one to the other without a monumental effort.
Its worth noting that AutoLISP has flexibility to be modified quickly without needing to be recompiled. The benefit to using native in-process C# is that it's extremely fast versus a similar LISP approach. I've found there's a nice middle ground for maintaining the flexibility of LISP with the speed and power of C# which leverages the LispFunction command flag and ResultBuffer type in the .NET native API.

C# to C++ Translation [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am coming towards the end of a project that I created in C#. Unfortunately, the target hardware only comes with compilers for C/C++. My dad is an embedded programmer so he will be making the necessary code to integrate with the hardware, but in the meantime I need to find a way to translate the language. Free translators are a very high preference as I am extremely tight on funds at the moment.
While I am not fluent in C++, with a dirty translation I should be able to figure out most of what is required to make it run.
Edit:
The target platform is mbed Microcontroller
Don't. This will not work.
C# has a garbage collector. C and C++ don't. You will have to rethink how you allocate objects and release them in C++.
Chances are, since you already have completed the project, rewriting it in C/C++ will be quite easy. You already understand the problem and have expressed it once before.
There is no 1 to 1 mapping from c# to c++. The programming model and platforms are very different at the lower levels. Take memory management for example.
Your best chance is either to rewrite your application or try to get .NET Compact Framework or .NET Micro Framework to run in the hardware.
Edit:
Note that at least the .NET Micro Framework has a porting kit if your hardware is not supported.
Since design is half the battle in application development, your C# prototype should serve you well, but you are unlikely to find a suitable automatic translation tool. If you have not made heavy use of the .NET class library, especially those parts that relate to the underlying OS API, C# should be easily manually translated to C++. The code body syntax and semantics are very similar; it is the enclosing structural elements such as class definitions that are more different.
The required effort depends on the size of the application code, but much of that is mechanistic. The biggest difference being that you need to be more careful with memory management in C++ since there is no automatic garbage collection.
Learn C or C++. There are no alternatives.
Both languages are radically different from C# and .NET, and automatic conversion is not possible. (and if it were, it certainly wouldn't allow you to "figure out most of what is required to make it run". It would be completely unrecognizable code, that'd be impossible to read or extend.)
In order to write a working C or C++ program, a C or C++ programmer needs to write the code. The good news is that it doesn't have to be that difficult. You don't have to learn every corner of the C++ language. But you do need to learn the basics.
If you're looking for the quick and dirty way to get off the ground, learning C might be a better option, because the language is so much smaller and simpler. But C++ is doable too. Just don't think you can get away with reading a 15-minute online tutorial.
There are no translators.
The .NET Micro Framework has been ported to a Phycore LPC3180 (NXP) platform that is not to dissimilar to your board so it can be done but you still need to port the .NET framwork to your platform.
It is unlikely that you will be able to use Mono AOT unless you are going to port Meamo to to your mbed board.
Any porting would require you to be able to program C code.
The best and fastest way forward would be for You to learn C++.
The differences between C++ and C# are not to big once you get going with C++ and understand the differences. You also going to have to use the mbed library for your hardware control and communications instead of what is provided by C#.
The C# code was a good prototype to debug your program design but it is not going to help you on the target. Now that you understand the problem it should not be to hard.
I'm not sure how you got this far without realising that the target platform couldn't run .NET, but it might be worth seeing if Mono's Ahead-of-Time compiler is able to output to your target platform.
At least then you wouldn't be throwing out (months of?) code.
Since you wrote the application in a garbarge-collected language, the fastest way to port this to an mbed platform should be to port the application to a garbage-collected language which runs on mbed.
I haven't tried it, but eLua is supposed to have a preliminary port which runs on the mbed platform, and Lua is fairly simple to learn.
http://mbed.org/users/jsnyder/notebook/elua-preliminary-port/
If you can get your dad to bring up eLua on the mbed platform, I suspect you could do the conversion fairly easily compared to trying to convert your application to C++.
Port the code manually, and as you do, you will learn C++. :D

What are the advantages of F# over C# for enterprise application development? [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
What are the benefits of using C# vs F# or F# vs c#?
My team is currently using C# .NET to develop enterprise applications for our company. We have a history of Winforms dev, but are now moving over toward SilverLight.
My boss recently saw a video on F# and thought it looked pretty exciting, and he has asked me to check it out. My question is this - what advantages would a functional language (like F#) give over an OO language (like C#) in the realm of enterprise application development?
I really want to see if there are any compelling reasons to even begin contemplating a shift. Some F# and C# comparison code might be nice to see as well.
You can express a lot of concepts much more concisely in a functional language like F#.
Functions are first class objects, and can be applied directly to collections to perform transformations/filtering much more efficiently.
Immutability is encouraged in functional languages, and this makes (for instance) multi-threaded code much more reliable (you know data structures aren't changing under you).
By being able to write multi-threaded code more reliably and more easily it's much easier to take advantage of multiple processors/cores (increasingly important now Moore's law doesn;t apply so much).
Note that you can use your existing C# objects within F#. As such, you may want to write certain parts of your code in F# and other parts in C#. You should be able to mix and match according to your requirements and the suitability of each approach.
I just ordered the Manning 'Real World Functional Programming' which does a fantastic job of explaining all of the nooks and crannies of F#, functional programming, and especially how it compares to C#, and OOP
What I'm coming down to is that functional programming in general increases the 'rigor' of your programs, assuming you conform to the principles of side-effect free functions, and immutability.
Additionally, there seem to be benefits to thinking about code in a declaritive sense, rather than an imperative sense. In OOP I tend to think of the steps it takes to accomplish something, but in FP it seems like you're more concerned with how you want to apply functions to data to obtain results.
firstly you ask "functional language (like F#) give over an OO language (like C#) i" f# is both functional and OO. any thing you would write in c# can be directly translated to f#. F# is a multi paradigm language but the advantage comes from the functional part in my opinion. f# and other functional langauge are a clear winner when it comes to writing multi threaded applications due to the higher possibilities for the compiler to reason about what can safely be run in parallel.
When it comes to web development f# has a very cool tool kit that allows you to write all code (as in server side and client side) in the same language aka f#. the client side will be translated into JavaScript by the web toolkit.

CAD/CAM without C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Is it possible to do CAD/CAM software without having to use C++? My company developed their software with c/C++ but that was more than 10 years ago. Today,there is a lot of legacy code that switching would force us to get rid of but i was wondering what the actual risks are. We have a lot of mathematical algorithms for toolpath calculations, feature recognition and simulation and 3D Rendering and i was wondering if C# can handles all of that without great performance loss.
Is it a utopia to rewrite such algorithms in c# or should that language only deal with UI.
We are not talking about game development here (Halo 3 or Call Of Duty) so how much processing does CAD/CAM really need?
Can anybody enlighten me on this matter? Most of my colleagues are hardcore C++ programmers and although i program in c++ i love .NET but i am having a hard time selling .NET to them other than basic UI. Does it make sense to consider switching to .NET in such a field, or is it just not a wise idea?
Thank you
If you have a lot of legacy code that would need to be rewritten, I don't see it making business sense to switch to a different language. Even if there were gains to be had from using a different language (which is questionable), the cost of testing and debugging the new code would more than overcome them. You also have a development team that are experts in C++. There would be a big productivity drop while they came up to speed on the new language.
C# Can interop with C++ code. You can start writing new code in C# and have it call existing c++ code when needed. It wouldn't have to be just for UI. Look into C++/CLI and the C# Interop methods for information on how to use existing c++ code with new C# code.
Also, I asked a similar question here:
C# Performance For Proxy Server (vs C++)
CAD/CAM applications are fairly calculation intensive, and speed will definitely be one of the criteria for selecting a package, so I would be wary of moving to a slower language.
You need to think very carefully about the reasons for switching language. Is it because you don't like C++, or because C# will bring real benefits. It is quite likely to slow your application down. Check out the C++ C# speed comparisons.
Computer Language Benchmarks Game C++ vs C#
In my humble opinion, you'd be better off keeping all of the toolpath calculations in C++, and if you really must move any code over to another language, move it over to a scripting language which the user can easily edit, without re-compiling.
I use CAD/CAM applications every day at work, and there are a number of things in the UI which get on my nerves. They would be simple fixes if only I could get at the source.
If your company makes a CAD/CAM application which has a UI written in a scripting language which I can tweak (Lua, Python etc), I'll buy a copy.
Hugo
Have a look at pythonocc. Its provides you with a python module that wraps the OpenCASCADE CAD kernel. OpenCASCADE is the sole industry strength open source kernel I'm aware of. Nice features are STEP and IGES support and the ability to generate FEM meshes from BRep data.
Another thing you need to consider is platform independence - if there is a possibility that you/you company need to migrate your CAD software to Linux/Unix (Of course, for bussiness decision), it will be quite painful. Currently, even C++ with MFC/Win32 calls gave us many headache...
The Open Design Alliance library is cross-platform. They have recently introduced a beta of the .NET version of their library. See my answer to Open source cad drawing (dwg) library in C# for more details.
Having said that I concur with the other answers here - if it ain't broken, don't fix it, both the code and the coders. MSFT still use C++, as does the ODA - their codebase originates in C++ & is wrapped for .NET.

Categories