Thread: Nested functions for a "new language"

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

    Exclamation Nested functions for a "new language"

    I am currently trying to make a "new programming language" (the term used VERY loosely) based on C. To do this, I decided to use C to read strings and interpret their contents.

    Code:
    void Read()
    {
        char* file = "int() mol { return 42; }";
        
        for (unsigned short i = 0; i < length_of(file); i++)
        {
            if (get_substring(file, i, *length*) == "int()")
            { int *name*(); }
        }
    }
    Basically, if the program reads "int() funcName;", it should execute "int funcName();".

    Apparently, this can be accomplished by using a GCC extension for nested functions.

    I assume there is a much better way to do this, but I can't figure out what. Would it be better to just export the custom file into a valid .c file and compile it afterwards? If so, how can I do that without forcing the user to compile it manually?

    Best regards,
    a guy on the Internet
    Last edited by MeNeedsHelp; 09-23-2014 at 06:03 AM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I don't think nested functions will work the way you are hoping. It would still need to be recompiled if you change the code. Consider using a scripting language like Python. It has a good C/C++ integration library, and has good performance.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    26
    Is there a way to compile it within the program itself? What I want to do is to open a text file with instructions, turn it into C code and then compile it into an executable. I understand how to do the first two parts, but how can I compile it in the same process (as in not forcing the user to compile it on their own)? I assume it's possible, is it just too advanced for me to be able to do at the moment, or is there any simple way to do it?

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    So you want to write a script-to-C translator, which then compiles the resultant C code , and loads it dynamically? This is definitely possible, but will require the compiler to be installed on the user's computer. As far as I know, there is no way to embed gcc in a program of your own, short of downloading the gcc source and hacking it to suit your needs. Again, this is possible, but probably too much work. Look at Ch
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Sep 2014
    Posts
    26
    Could I do it if GCC is installed? (MinGW, in this case.) If so, how?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How complex is your "new programming language"?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by MeNeedsHelp View Post
    Could I do it if GCC is installed? (MinGW, in this case.) If so, how?
    Refer to my previous post:

    Quote Originally Posted by Elkvis View Post
    ...but will require the compiler to be installed on the user's computer.
    GCC is a compiler, or rather, a collection of compilers. There may be an actual embedded C compiler available somewhere, but my preference would be to use a scripting engine, such as Ch or cint, if you insist on using C, or Python or another scripting language. You could even embed V8, and use javascript instead, and still have the performance of native code, with no need to recompile source between runs.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  8. #8
    Registered User
    Join Date
    Sep 2014
    Posts
    26
    I am doing this for a project, just to learn. If I wanted the best, I would use something like Python.

    I mean, is it possible to compile with GCC without having the user do it themselves - for example, to "open" (or whatever) GCC in the background and compile the file without showing it to the user?

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by MeNeedsHelp View Post
    is it possible to compile with GCC without having the user do it themselves - for example, to "open" (or whatever) GCC in the background
    That is what IDEs generally do. You should look into the topics of launching processes in the background with console redirection. For example there is the popen and pclose calls in stdio.h.

  10. #10
    Registered User
    Join Date
    Sep 2014
    Posts
    26
    Thank you, I'll do that.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > if (get_substring(file, i, *length*) == "int()")
    Unless this is anything other than an absolute toy language, I would strongly suggest you first look at writing out a grammar for the language using BNF

    And then implement a proper parser based on that grammar, who's output would be a syntax tree of some kind. At this stage, you could either execute the code by walking the tree (an interpreter), or generate machine code (for a real or a virtual machine) for later execution.

    Without doing this, ad-hoc parsers like you suggest would instantly trip over "int ()" or "int( )"

    The "Dragon Books" are a good source of information as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    If you want to embed a compiler into an application, I'd suggest using LLVM instead of gcc. One of its design goals was to be useful as a software component and not just a standalone toolchain.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  13. #13
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    This sort of thing is done in assembly programming alot.
    It can be confusing to follow a program like this, but save some code.

    One way to do this with C is to use goto statements to make your program behave like a non-structured, assembly program.
    Be warned though, it will get messy and VERY hard to follow.

    Code:
    /* Example */
    
    int DoSomething(int intInput)
    {
        intInput += 1;
    
        #SUB intInput2=intInput
        int DoSomethingGreat(int intInput2)
        {
            intInput *= intInput;
    
            return intInput;
        }
    }
    
    Becomes...
    //Global variables
    int intInput, intInput2;
    ....
    
    DoSomething:
    
        intInput += 1;
        intInput2=intInput;
    
    DoSomethingGreat:
    
        intInput *= intInput;
    However, in assembly there are commands like RTS (Return from subroutine), which go at the end of any subroutine which makes this sort of programming easier.

    One way to get around this is to have some sort of jump table - Another global variable which you store your position in the code to return to:
    Code:
    ReturnSubroutine:
      switch(ReturnToken)
      {
        case 0:
          goto Label0;
    
        case 1:
          goto Label1;
    
      }
    Everything would have to be in your main function.

    I'm sure that there are better ways of doing this, but it could be a bit of fun as a learning exersise.

    I'm fairly sure that what you will learn is that the 'goto' statement makes things harder. (A good lesson to learn!)
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Marking functions as "Local" or "Global"
    By zyxwvuts in forum C Programming
    Replies: 10
    Last Post: 02-22-2014, 07:58 AM
  2. Replies: 4
    Last Post: 11-19-2011, 07:57 PM
  3. Working with Nested "Switch Multiple-Selection"
    By MAV_DevWantB in forum C Programming
    Replies: 5
    Last Post: 09-18-2009, 08:05 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread