Thread: Reflection in C?

  1. #1
    Registered User Normandy's Avatar
    Join Date
    Jan 2012
    Location
    my hard drive
    Posts
    5

    Reflection in C?

    So, I am a fairly new C programmer and I was wondering...Is there any C equivalent of C#'s System.Reflection.Emit? This C# library emits Microsoft Intermediate Language code (to create an EXE or DLL). At the moment, I am using it to write my own programming language (to emit IL code based on the user's text input), but if I ever wanted my programming language to become mult-platform, I would need to write it in a multi-platform language (C, not C#). Some info on how do do this in C would be wonderful, as would some general pointers on Mac and Linux programming.

    Just in case you're interested, here is an example of a script in my programming language:

    Code:
    <<.main
        @message= "hello world!"
        @says = " says "
        @name = ""
        --> name
        <- name
        <- says
        <-- message
    
    end.>>
    the console output for this would, obviously be for instance "Bob says hello world", Bob being the user's input (-->).


    thank you very much,
    Normandy


    If at first you don't succeed, call it version 1.0

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I don't think there is the possibility for true reflection in C (presuming I understand what that means, lol), but if you describe exactly what you are trying to do, there might well be a way to implement something functionally equivalent.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User Normandy's Avatar
    Join Date
    Jan 2012
    Location
    my hard drive
    Posts
    5
    what am I trying to do? create an executable file using C based on user input. Python does something like this (I'm not entirely sure how; I downloaded the C source code of Python 2.6 and understood less than half of it). a replacement would be to, I guess, convert user input into C code and then execute that code (like Python's exec() function).

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do you honestly believe they would put something as big and complicated as reflection into C? Then you are barking up the wrong tree, sorry.
    There is no language support for reflection in either C or C++ (but there is some attempt to circumvent that in C++; though how good that solution is, I don't know). I dunno if Java support it, either.

    The thing is that you are trying to implement a non-type safe language in a type safe language. That's a little backwards and is going to be a pain in the ass.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you just use something like Lua?


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User Normandy's Avatar
    Join Date
    Jan 2012
    Location
    my hard drive
    Posts
    5
    Quote Originally Posted by Elysia View Post
    Do you honestly believe they would put something as big and complicated as reflection into C?
    lol no, not really. But it doesn't hurt to ask.

    Quote Originally Posted by quzah View Post
    Why don't you just use something like Lua?
    I'll look into that...

  7. #7
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Are you looking for something like this?
    GNU lightning - Wikipedia, the free encyclopedia

    To be clear, it sounds like you're looking for just-in-time compilation, not reflection.

  8. #8
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    I would argue that reflection is quite possible in C, albeit, crudely. Use function pointers, and if need be, a lookup list, and viola, reflection.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ needs reflection support
    By VirtualAce in forum General Discussions
    Replies: 5
    Last Post: 10-17-2011, 08:32 AM
  2. CAMP - new open-source reflection library for C++
    By Laurent Gomila in forum Projects and Job Recruitment
    Replies: 23
    Last Post: 11-21-2010, 10:50 PM
  3. iterate through types in a library (reflection)
    By bling in forum C++ Programming
    Replies: 1
    Last Post: 10-29-2008, 05:15 PM
  4. Two basic questions about reflection
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 06-01-2008, 08:52 PM
  5. Reflection?
    By Neighbor Moby in forum C++ Programming
    Replies: 4
    Last Post: 05-09-2004, 10:04 AM