0

I'm writing something that will examine a function and rewrite that function in another language so basically if inside my function F1, i have this line of code var x=a.b(1) how do i break up the function body into symbols or "tokens"?

I've searched around and thought that stuff in System.Reflection.MethodInfo.GetMethodBody would do the trick however that class doesn't seem to be able to have the capabilities to do what i want..

what other solutions do we have?

Edit: Is there anyway we can get the "method body" of a method using reflection? (like as a string or something)

Edit 2: basically what I'm trying to do is to write a program in c#/vb and when i hit F5 a serializer function will (use reflection and) take the entire program (all the classes in that program) and serialize it into a single javascript file. of course javascript doesn't have the .net library so basically the C#/VB program will limit its use of classes to the .js library (which is a library written in c#/vb emulating the framework of javascript objects).

The advantage is that i have type safety while coding my javascript classes and many other benefits like using overloading and having classes/etc. since javascript doesn't have classes/overloading features natively, it rely on hacks to get it done. so basically the serializer function will write the javascript based on the C#/VB program input for me (along with all the hacks and possible optimizations).

I'm trying to code this serializer function

Pacerier
  • 86,231
  • 106
  • 366
  • 634
  • Are you talking about using a parser generator targeting C# ? – Albert Perrien Apr 22 '11 at 00:41
  • @Albert Perrien heys I'm not sure what's a parser generator but basically what I have is code written in Vb/C# (the input) and the output is the same code but different syntax (for another language) – Pacerier Apr 22 '11 at 00:44
  • Hmm... An automated code translator? like [this](http://www.carlosag.net/Tools/CodeTranslator/)? – Albert Perrien Apr 22 '11 at 00:46
  • Basically you're talking about writing a compiler... look into Bison/Flex or Yacc/Lex – photoionized Apr 22 '11 at 00:46
  • @Sasquiha i've looked at codedom but i don't think it does the trick – Pacerier Apr 22 '11 at 00:55
  • @Albert Perrien yes what i'm trying to do is something like an automated code translator (but its not exactly the same). anyway is system.reflection capable of returning the actual "body" of methods? – Pacerier Apr 22 '11 at 00:55
  • @AlbertPerrien basically what I'm trying to do is to write a program in c#/vb and when i hit F5 it will take the entire program (all the classes in that program) and serialize it into javascript. of course js doesn't have the .net library so basically the C#/VB program will limit its use of classes to the .js library (which is a library written in c#/vb emulating the framework of js). The advantage is that i have type safety while coding my js classes and many other benefits like using overloading and having classes/etc (the c#/vb classes/overloading features will be translated into js hacks) – Pacerier Apr 22 '11 at 01:07
  • `Type.GetMembers` Returns member info from a C# class. But you will end up with problems of syntax and structure as well. – Albert Perrien Apr 22 '11 at 01:08
  • @Albert Perrien Type.GetMembers doesn't do the trick because i need the actual structure of the function body.. – Pacerier Apr 22 '11 at 01:11
  • Structure of the function body? As in it's source? Or parameters? – Albert Perrien Apr 22 '11 at 01:16
  • Reflection allows you to modify program flow at runtime. I'm not sure if that's what you're after. – Albert Perrien Apr 22 '11 at 01:19
  • @Albert Perrien as in the exact lines or tree representation of the code within each function body – Pacerier Apr 22 '11 at 01:45

4 Answers4

3

It sounds like you want a parse tree, which Reflection won't give you. Have a look at NRefactory, which is a VB and C# parser.

Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
  • @Mark Cidade i've just downloaded it. it didn't come with mono.cecil.dll so i downloaded that too, but when i merged it together the versions seem to be off (i had error Mono.Collections missing class) – Pacerier Apr 22 '11 at 02:26
  • I haven't used NRefactory recently so I'm not sure which version of mono is required by the latest release. You can post another question about the error. – Mark Cidade Apr 22 '11 at 02:47
3

If you want to do this, the best way would be to parse the C#/VB code with a parser/lexer, such as the Gardens Point Parser Generator, flex/bison or ANTLR. then at the token level, reassemble it with proper javascript grammar. There are a few out there for C# and Java.

Albert Perrien
  • 1,153
  • 12
  • 27
2

See this answer on analyzing and transforming source code and this one on translating between programming languages.

These assume that you use conventional compiler methods for breaking your text into tokens ("lexing") and grouping related tokens into program structures ("parsing"). If you analysis is anything other than trivial, you'll need all the machinery, or it won't be reliable.

Reflection can only give you what the language designers decided to give you. They invariably don't give you detail inside functions.

Community
  • 1
  • 1
Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
1

If you want to go from IL to other language it may be easier than parsing source language first. If you want to go this route consider reading on Microsoft's "Volta" project (IL->JavaScript), while project is no longer available there are still old blogs discussing issues around it.

Note that reflection alone is not enough - reflection gives you byte array for the body of any particular method (MethodInfo.GetMethodBody.GetILAsByteArray - http://msdn.microsoft.com/en-us/library/system.reflection.methodbody.aspx) and you have to read it. There are several publically available "IL reader" libraries.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179