Thread: Interpreting code from file in C

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    26

    Post Interpreting code from file in C

    Hello.

    Say that a file contains the following text:
    Code:
    struct Data { int x; };
    
    void DoSomething(int x) { x = 42; }
    
    int main()
    {
      struct Data data;
      data.x = 0;
      DoSomething(data.x);
    }
    If I want to make this code "work as a script", how could I do that? Is there any relatively simple way to do this, or do I need to learn to write a compiler or something first?

  2. #2
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Feel free to write a compiler, but the most people use an allready finished compiler (like gcc).
    Your code is in C language and this must be compiled that it can run.
    There is no interpreter out in the world that interpet C language at runtime (so far as i know).

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by WoodSTokk View Post
    There is no interpreter out in the world that interpet C language at runtime (so far as i know).
    A simple google search for "c interpreter" will turn up plenty. All interpreters have different trade-offs (both in comparison with each other when embedding in a program, and in comparison with C compiler products) so I won't offer recommendations.


    As an aside, the implied expectation in the code sample is that "DoSomething(data.x)" will change the value of data.x. That is not so in any standard version of C. Because int arguments are passed to functions by value - and any changes the function might make to the argument are NOT visible to the caller. It is probably a good idea to work to understand fundamentals of C (and there are few things more basic and fundamental to C than this) before worrying about the use of interpreters versus compilers.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Sep 2014
    Posts
    26
    I know how C "works". I just made an example, and I meant for it to be interpreted not as C code, but as code for another imaginary language.
    Code:
    void DoSomething(int* x) { (*x) = 42; }
    If you want to see it as C, there it is.

    What I want to know is if there is a relatively simple way to WRITE an interpreter. Are there any good tutorials, should I just look at the source code of something like picoc or Lua, or do I need to learn assembly to make this work?

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by MeNeedsHelp View Post
    I know how C "works". I just made an example, and I meant for it to be interpreted not as C code, but as code for another imaginary language.
    You asked your question in a C forum, using a sample of code which had considerable resemblance to C, and made no reference to "another imaginary language". Being unspecific in a forum, as you were, is an effective approach to get advice you don't want. It's not like there are social cues or other means for people to get information you haven't provided.

    Quote Originally Posted by MeNeedsHelp View Post
    What I want to know is if there is a relatively simple way to WRITE an interpreter. Are there any good tutorials, should I just look at the source code of something like picoc or Lua, or do I need to learn assembly to make this work?
    That really depends on your purpose in writing an interpreter. Is the interpreter intended to be standalone (for example, like command shell, implementations of perl, etc)? Is it intended to be part of a larger program, where your "scripts" allow a user to extend behaviour of your program without having to build DLLs or other executables? Is your interpreter intended to support a standardised programming language (like C), a standardised language with extensions, a subset of a standardised language, a dialect that has syntax like C but completely different grammatical rules, or what? Is your interpreter required to provide complete library support for the chosen language (e.g. if your interpreter supports C, do you intend to support a non-trivial amount of the standard library)? Is your interpreter required to enforce security or other constraints (e.g. if the "script" attempts to format the hard drive, is your interpreter supposed to block it or to warn the user before doing that)? Does the interpreter produce output files from the "scripts" (for example, emit object files in some circumstances)? Is your interpreter required to support optimisation on the fly (for example, if your scripts have code that is executed many times, is the interpreter required to run the code faster as the code is executed more times)?


    The thing is, you're asking for a simple "cut and dry" answer, when you haven't even given information so people know what you are trying to achieve. An interpreter, like a compiler, can be as simple or as complicated as you need to make it.

    You probably don't need to learn assembly, unless your interpreter is required to handle assembly "scripts" or to output assembly. You will, however, need to know how to parse source code, enforce grammatical rules.

    There is a lot in common between writing an interpreter and a compiler. You will find a lot of information on writing compilers.
    Last edited by grumpy; 11-30-2014 at 03:26 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Sep 2014
    Posts
    26
    Sorry for being unclear. I'll try to give you a decent example.

    Say a text file contains the following code:
    Code:
    func<int> Add(int x, int y)
    { return (x + y); }
    
    structure Container
    { int i; };
    
    func<int> main()
    {
        Container c;
        c.i = Add(20, 22);
    }
    This should translate to something like the following code:
    Code:
    int Add(int x, int y)
    { return (x + y); }
    
    typedef struct
    { int i; } Container;
    
    int main()
    {
        Container c;
        c.i = Add(20, 22);
    }
    Simply converting the text file's contents into proper C code and exporting it into a new .c file is not what I want to do - I want to have the text interpreted and executed at loading time.

    Either that, or I want to compile it right away - for example, if I have an executable with my C program, if I drag a text file containing "code" for my "new language", I want it to export another executable, rather than having to manually compile the new .c file. Is there some sort of C compiler written in C which I can use in such a manner?

    Hopefully this is somewhat understandable.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There is generally no need for an interpreter to translate code into some other form of code before executing it (other than performance optimisation - such as saving the interpreted code into some form(which might be a binary representation rather than code as such) that can subsequently be loaded and interpreted faster).

    The "either that" option is more like a program that manages a build process (invokes the sequence of events that produce an executable from source code) rather than an interpreter. It is not necessary that such a program be a single program - a fair few IDEs and compiler drivers actually orchestrate the process of executing multiple programs (compiler, assembler, linker, etc) in sequence.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Sep 2014
    Posts
    26
    @grumpy
    I don't need it to be interpreted, if I can do the "either that" option, but do you have an example of such a tool? As you said, IDEs like Visual Studio, Eclipse and NetBeans do this, so there must be a way.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You're describing a build automation tool.

    If your host system is a unix variant, the most widely used build automation tool is a utility named make. There are similar utilities for under other operating systems - often installed with your compiler or IDE.

    I wouldn't advise trying to build a make variant from scratch. Better to use an existing utility.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Registered User
    Join Date
    Sep 2014
    Posts
    26
    @grumpy
    OK, but how could I do that with something like Make from within the program?

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Write the files needed by make. Source file, makefile, etc

    Execute make as a separate process from within your program. Look up functions like system() [standard C, but limited in functionality], the the fork-exec sequence [unix systems] or spawn functionality [a fair few other operating systems].
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  12. #12
    Registered User
    Join Date
    Sep 2014
    Posts
    26
    @grumpy
    Oh, I see. Thank you very much.

    However, I'm still interested to know how to make the code be interpreted, and considering Lua did it in ANSI C, I'm very curious as to how to make it work. The main problem I'm facing is dynamically adding variables, structures and functions to the program as the "script" is read.

    You said
    There is generally no need for an interpreter to translate code into some other form of code before executing it
    but I don't really know what you mean by this, or how to apply it.

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Look up parsing (analysis of a sequence of symbols - like characters or tokens (groups of characters) in your file to extract meaning from the file).

    The next step is writing some form of abstract machine .... a program that responds to a set of basic instructions and data (for the instructions to act on) to more complex effects.

    Then write a parser that scans the source file and build up some representation of a sequence of instructions to be fed to the abstract machine (for example, a linked list or an array). Feed output from the parser as input to the abstract machine, and you have a basic interpreter. Different manners of feeding parser output to the abstract machine give different types of interpreter. For example, one architecture might involve the parser scanning and analysing a whole file, and then feeding the complete set of instructions to the abstract machine (say, in the form of a linked list or a tree of linked lists) which executes the whole lot blindly. Another architecture might involve the parser scanning a smaller part of a file (say, a declaration or a statement in a C program), squirting a small set of instructions to the abstract machine, and then reading another small part of the file (repeat, continue).

    If you think about it in this way, a compiler and an interpreter are similar things.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  14. #14
    Registered User
    Join Date
    Sep 2014
    Posts
    26
    @grumpy
    Hm. I suppose I'll try learning more about that, then.

    Thank you for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Am I interpreting this correctly?
    By SCRIPT_KITTEH in forum C Programming
    Replies: 2
    Last Post: 08-03-2013, 07:00 PM
  2. Having a hard time interpreting this code
    By BLG in forum C Programming
    Replies: 13
    Last Post: 09-29-2009, 05:19 PM
  3. interpreting fcnrl F_GETFL
    By MK27 in forum C Programming
    Replies: 8
    Last Post: 10-08-2008, 09:42 AM
  4. interpreting xml
    By WebmasterMattD in forum C++ Programming
    Replies: 3
    Last Post: 09-07-2003, 08:20 AM
  5. Interpreting literal escape sequences from a file...
    By Sebastiani in forum C++ Programming
    Replies: 1
    Last Post: 07-08-2003, 02:00 PM

Tags for this Thread