0

I want to know, is this possible to use C# language syntax in own platform? I know that C# is ECMA standartized language. So how can it be implemented?

I know there are examples such as Mono & Unity3D who implemented C#.

So for example : One common class library (own, written in C) & C# as a programming language.

The problem is that I never did that before, so I am interested what should I read & where to start. Any other articles about implementing syntax will be good.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
geCoder
  • 217
  • 1
  • 4
  • 7
  • 5
    Writing your own parser, compiler and framework is no walk in the park and just asking this leads me to think this is a little beyond you, you're best off with existing solutions. However there are some compiler and parser generators out there to get you started like YACC and Coco/R. – Lloyd Dec 22 '10 at 12:24
  • 1
    Make your 'platform' execute IL, not C#. Now you don't care about the compiler and any will qualify. – Hans Passant Dec 22 '10 at 14:24

3 Answers3

3

If you want to write a compiler for C#, the place to start is the Dragon Book, alongside a copy of the C# 4 spec. It's an awful lot of work though, and not for the faint of heart; you generally need years of experience to write a compiler for something as complicated as C#.

I recommend starting with a smaller language, maybe a trivial language like brainfuck, or looking at existing toy compilers.

thecoop
  • 45,220
  • 19
  • 132
  • 189
  • +1 - expression parsers, which can be extended step-by-step to support variables, functions, different data types, IL generation etc., are also a good way to start playing with lexers, parsers, abstract syntax trees, semantic binding and everything else. – Lucero Dec 22 '10 at 12:32
0

One obvious option is to retarget the Mono AOT. This is how MonoTouch works, for example.

The hard route is to implement your own C# parser, compiler and a large part of its standard library.

SK-logic
  • 9,605
  • 1
  • 23
  • 35
0

imho there are a very few cases when you need to write your on parser / compiler etc.

I would do it in a different way.

First option would be to run .Net from your C application. You can for instance use Mono for that. Here is a description: http://www.mono-project.com/Embedding_Mono

The second option would be to use your c class library directly from C# applications. Check http://www.pinvoke.net/ for example on how to declare them.

jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • The exact thing I want to do is to use C# language to write native C application with my library. So somekind of "converting" C# to C at compile time. Is this possible? – geCoder Dec 22 '10 at 13:44
  • That would take longer than writing the actual application in C. C# is a object oriented language and converting it to C would be very hard and not something I would spend time on even if I got a million dollars. You can write a managed C# application which uses your C library though. Why can't you do that? – jgauffin Dec 22 '10 at 13:47
  • I just looked at pinvoke & .NET interop abilities, looks pretty cool, never used that before. Looks like most convenient way to use my C lib. Thank you. – geCoder Dec 22 '10 at 13:50