I've already tried to develop languages using C and C++, but how can I create a interpreted language using C#? Thanks.
PS: I want to build it to run in Windows Mobile devices.
Well... what have you tried in C and C++?
Developing a language isn't exactly childs play. Do you understand lexical analysis? Do you understand different types of parsers? Where an LR parser might be more appropriate than an LALR parser? Or vice-versa? Do you understand context free grammars? Regular expressions?
That doesn't even begin to cover code generation, optimization, etc... (which may not all apply for an interpreted language, but you're still going to want to know a thing or two about them before you dive in).
You seem to be familiar with compiler construction, so I'll just point to the tools:
MPLEX and MPPG are respectively scanner and parser generators that generate a lot of what you need to build a compiler or interpreter using the C# language.
It seems that more documentation can be found in the .NET SDK, but I don't have it at hand so I'll just leave a pointer to MSDN.
If you want an interpreteded language developing it for C# is no different than developing it in C or C++.
If you want to compile to a (.Net) compiled language then .Net offers lots of possibilities through System.Reflection.Emit.
Related
I've been given the job of updating some old code written in Pascal, which was targeted to run on Windows.
My job is to rewrite the code to run on ios and Android. I've been given the source code, however the code is not well documented, difficult to understand (since I have never programmed in Pascal before) and it contains some very difficult maths.
I'll be programming in C#, however I'm not sure if I should try and decipher the given source code or start from scratch?
In theory you could use the latest version of Delphi XE (object pascal) to compile to Native iOS and Android and not have to rewrite the Pascal at all.
And also Free Pascal (http://www.freepascal.org) supports iOS and Android to some degree.
How practical it is depends on the level of investment in, and quality of the old code.
C# doesn't seem to be a good choice though, since it is not a native language on either platform. At least The main Pascal vendor (Embarcadero) supports these targets directly, contrary to MS.
I can appreciate your problem, but that is basically an impossible question to answer with the information given. Basically, you have the choice of finding a well supported Pascal compiler for iOS & Android (Unix OSes supporting gnu languages could use this http://www.gnu-pascal.de/gpc/h-index.html, perhaps a port or something similar is available for iOS and Android) and learning Pascal ... or rewriting the code in C#/Mono. You could also consider taking parts of the pascal code and creating libraries out of it and calling them from C#. Perhaps a tools is available to convert Pascal to C# (and does it work).
Things to consider are:
Timeline, how much time do you have to do this
List item
How easily can you learn a new language (do you just know c# or a
dozen other languages)
Is it easier to treat this as a completely new project, by converting
the Pascal to a specification. This will give you greater freedom in structuring your C# without being "polluted" by the original design decisions (which may not longer be valid) of the existing code.
You might want to read through this thread: http://social.msdn.microsoft.com/Forums/vstudio/en-US/60a25852-8653-407a-97ac-438ee80d99a4/ms-pascal-to-cnet?forum=csharpgeneral
I'm using CodeDom compiler to dynamically compile user defined scripts. We're working with C# scripts as standard, but I was wondering if there was a way how to support all CLI languages. To do that I'd have to detect used CLI language that was used in this particular source code.
Is there some elegant way how to detect only CLI languages from the source code?
Thanks
There are only 3 languages for which the framework provides a CodeDomProvider: C#, JScript and VB. Therefore, if the framework provides any direct method to parse "any language", it can only support these 3 languages. I don't think it does.
You may want to try to parse your code with all three implementations of CodeDomProvider, and keep the first that succeeds. It will take a dozen of lines of code.
I think it might be your best try.
Documentation for CodeDomProvider: http://msdn.microsoft.com/en-us/library/ds075xdx.aspx
Documentation for CodeDomProvider.Parse: http://msdn.microsoft.com/en-us/library/system.codedom.compiler.codedomprovider.parse%28v=vs.110%29.aspx
I've been jumping from C# to Java an awful lot and the "differences" between the two are a bit of an annoyance.
Would it be possible to write a set of extentions/plugins that would merge the two languages syntaxes.
I'm talking about adding either IDE support or using language constructs that would for example:
treat these two lines equivalently:
System.out.println("Blah");
Console.out.writeline("Blah");
Automatically notice that when you type in string you mean String
Recognise common API calls and translate them in the background.
The end goal being to be able to write a java/C# program and to pick at compile time which VM/Runtime you are targeting.
If you could do this would it be a good idea?
If not why not?
The two languages are so similar it's painful in some aspects but in other aspects they are really different.
I've seen Code that will translate a C# project into Java and I'm assuming there is probably the reverse, what I am proposing is a middle ground, so we can all just "get along".
No, absolutely not. Certainly not in the languages themselves (as implied by the title) and preferably (IMO) not in the IDEs (as requested in the body).
They are different languages. The idioms and conventions are subtly different. I don't want to be thinking in Java when I'm writing C# or vice versa. I believe developers should be actively encouraged to separate their thinking. It's not too hard to switch between the two, but that switch should be present, IMO.
While I totally agree with Jon Skeet, if you must have this why not create your own library of Java API so you can create System.out namespace which has a method call printLn which calls Console.Writeline()?
That gets you close to what you want.
Just because Java and C# share some similar syntax you need to see past this and think in terms of Java Platform and .NET Platform. The two are distinctly different, so my answer is definitely not.
There actually already is a Java language for the .NET framework, developed by microsoft: J#
This way you get the java-syntax but you are still developing with the .NET framework.
But i am not recommending anyone to use it.
I knew Java before i knew C# so i tried out J# because i thought it would be an easier transition. At first I liked it but after I tried C# I'm never going back. First of all, nobody uses J# so it's kinda hard to find examples and tutorials. Second, C# has (IMO) much more convenient syntax, specially for events, properties, lambda, anonymus methods and alot of other things, it's also being updated every now and then with even more syntax sugar which i don't think J# is.
Maybe if you often write Java and sometimes have to write a .net app it might be a good option.
I think no. I also switch from java to c#. But if the syntax is identical was is to stop someone from trying to compile c# in a Java compiler, or vice-versa.
Visual Studio actually ships with a Java to C# converter, which tries to do some of the things you mention. Unfortunately it fails miserably (1) for anything beyond the simple hello world application.
Despite being very similar on the surface, there are many significant differences between Java and C#, so you would achieve very little by doing what you suggest imo.
(1) To be fair, it actually does a fairly good job if you consider the limitations given for such a task, but in practice the resulting code is of limited use and you have to do a lot of clean up after the conversion.
Firstly what you are describing is not a difference in language syntax but a differences in class libraries. Both languages are relatively simple in terms of keywords and features but understanding or knowing the libraries and how they operate requires considerable learning.
The mistakes you are describing are things that the developer should not be making to begin with - the IDE should not be guessing. There are going to be many cases where you can't easily / trivially translate between java or dotnet. In the end a skilled developer learns and knows when and which class libraries to use.
Actually in the beginning there was no dotnet - microsoft was behind java. They however proceeded to change java in ways not compatible with the java plstform standard. To paraphrase sun sued microsoft and won I'm court. Following that ms proceeded to create dotnet and particularly c# which became microsofts VM platform. Of course along the way a whole stack of things got changed. Microsoft introduced many things which broke Javas run anywhere etc. They have done the same thing with dotnet which have cause problems for the mono team to be able to faithfully reimplemwnt everything for other non windows platforms.
• String vs string.
• lowercase method names (java) v uppercase method names(dotnet).
• Giving java keywords new names - "package".
In the end dotnet was microsoft response so they can control the platform and do their own thing instead of following a standar
I'm looking for turn-key ANTLR grammar for C# that generates a usable Abstract Syntax Tree (AST) and is either back-end language agnostic or targets C#, C, C++ or D.
It doesn't need to support error reporting.
P.S. I'm not willing to do hardly any fix-up as the alternative is not very hard.
This may be waaaay too late, but you can get a C# 4 grammar.
Here's a C# grammar link, as well as an overview of C# and ANTLR. There are others for the other languages you mentioned here.
The DMS Software Reengineering Toolkit provides a full, validated grammar for C# 1.2, 2.0 and 3.0 with generics and LINQ expressions.
It automatically builds ASTs, allows you programmatic access to the ASTs for analysis or tranformation, or you can apply source-to-source transformations that also directly manipulate the tree. The resulting AST can be prettyprinted back to source code, even retaining indentation and comments.
DMS also has mature front ends for other languages such as Java, PHP5, JavaScript, COBOL, C and C++.
EDIT: 1/31/2010: The DMS C# parser has been extended to handle full C# 4.0.
You can find C# 6 ANTLR grammar at official grammars repository.
I'm porting a Java library to C#. I'm using Visual Studio 2008, so I don't have the discontinued Microsoft Java Language Conversion Assistant program (JLCA).
My approach is to create a new solution with a similar project structure to the Java library, and to then copy the java code into a c# file and convert it to valid c# line-by-line. Considering that I find Java easy to read, the subtle differences in the two languages have surprised me.
Some things are easy to port (namespaces, inheritance etc.) but some things have been unexpectedly different, such as visibility of private members in nested classes, overriding virtual methods and the behaviour of built-in types. I don't fully understand these things and I'm sure there are lots of other differences I haven't seen yet.
I've got a long way to go on this project. What rules-of-thumb I can apply during this conversion to manage the language differences correctly?
Your doing it in the only sane way you can...the biggest help will be this document from Dare Obasanjo that lists the differences between the two languages:
http://www.25hoursaday.com/CsharpVsJava.html
BTW, change all getter and setter methods into properties...No need to have the C# library function just the same as the java library unless you are going for perfect interface compatibility.
Couple other options worth noting:
J# is Microsoft's Java language
implementation on .NET. You can
access Java libraries (up to version
1.4*, anyways).
*actually Java 1.1.4 for java.io/lang,
and 1.2 for java.util + keep in mind that J# end of
life is ~ 2015-2017 for J# 2.0 redist
Mono's IKVM also runs Java on
the CLR, with access to other .NET
programs.
Microsoft Visual Studio 2005 comes
with a "Java language conversion
assistant" that converts Java
programs to C# programs
automatically for you.
One more quick-and-dirty idea: you could use IKVM to convert the Java jar to a .NET assembly, then use Reflector--combined with the FileDisassembler Add-in--to disassemble it into a Visual C# project.
(By the way, I haven't actually used IKVM--anyone care to vouch that this process would work?)
If you have a small amount of code then a line by line conversion is probably the most efficient.
If you have a large amount of code I would consider:
Looking for a product that does the conversation for you.
Writing a script (Ruby or Perl might be a good candidate) to do the conversion for you - at least the monotonous stuff! It could be a simple search/replace for keyword differences and renaming of files. Gives you more time/fingers to concentrate on the harder stuff.
I'm not sure if it is really the best way to convert the code line by line especially if the obstacles become overwhelming. Of course the Java code gives you a guideline and the basic structure but I think at the end the most important thing is that the library does provide the same functionality like it does in Java.