Thread: Strings to be Interpreted as Code

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    3

    Strings to be Interpreted as Code

    Hi All,


    I'm building an integrator, and the way my program's set up now, it recieves the expression from the command line. I was wondering if there was a way to have the expression from the command line be interpreted as actually code ie if x**2 was typed in, to have a value variable that I could set equal to the input expression.

    Here's my code:

    Code:
    #include <math.h>
    #include <stdio.h>
    
    
    double fofex( int argc, char *argv[], double var_v );
    
    main(int argc, char *argv[]){
    	
    	double ans;
    	ans = fofex(argc, argv, 2);
    	printf("Value of %s at 2 is: %f",argv[1], ans);
    	
    
    /*Feed function pointer to expression and variable value*/
    
    	double fofex(int argc, char *argv[], double var_v){
    		
    /*Sets variable value to input value*/
    		
    		double x = var_v;
    
    /*Sets value to string of expression, which must contain 'x'...*/
    		
    		printf("Inside the function expression is %s\n", argv[1]);
    		double value = argv[1];
    		return (value);
    	}
    
    	
    }

    And of course I get an error because of incompatible types between value ad argv[1]. Is there a way to do what I'm trying to do? Thanks in advance.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sure, parse the strings. Read each argument and try to figure out what they've typed and what you should be doing with it.

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

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    Hi Quazah,

    Thanks for the input, but I'm not sure I follow. How would parsing the strings help. An expression inputted would be something like x**2, and that would be stored as a string in argv[1]. How could I code set value equal to the expression inside argv[1], or x**2?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You have to write functions to call when what you read fits whatever expression you need. If ** is supposed to be "to the power of", then write a function that you call that passes whatever, and the "power of" that follows the "**". If you want to interpret division, then make a function to handle division.


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

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    Ahh, so there isn't a way to just have a string interpreted as code ie have the compiler believe x**2 means take variable x and raise it to power 2. I thought that maybe you could do something similar to define statements when you can have essentially constants or expressions for constants directly substituted. But those happen before compiling so I guess this case is different.

  6. #6
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    You can't compile at run time (unless you write out a source file, compile it then run it.. .but I don't think that's what you're looking for).

    You could do that in python though.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Perspective View Post
    You can't compile at run time (unless you write out a source file, compile it then run it.. .but I don't think that's what you're looking for).

    You could do that in python though.
    Yes, most languages that "interpret" the code, such as Python, Perl, PHP, LisP, some forms of Basic are able to do this sort of thing.

    C and C++ [and C#, Objective C, Pascal, Modula-2, Cobol, Fortran, and many others] that have a compile-stage, and a run stage that doesn't requite the compiler to even be present on the target computer, are not able to do this directly.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    [QUOTE=matsp;854795][Languages] that have a compile-stage, and a run stage that doesn't requite the compiler to even be present on the target computer, are not able to do this directly.
    [quote]

    For your examples (and in the general case), this is true. However, there are counterexamples: consider a Lisp compiler that supports the eval function and hence could easily build executables which are able to interpret strings as code (although one might argue that these executables are Lisp interpreters themselves).

    From a less nitpicking point of view, I completely agree with your statement.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, since eval is a pretty basic function in LisP, most lisp COMPILERS will bring in a LisP interpreter (or even a compiler) that will allow eval to execute within the compiled code. I doubt you could get any relatively fancy LisP program to run otherwise. So I agree that this is an exception. Likewise, Python code can be compiled, but you still have a eval() function in that too - and you still have a python interpreter with the "Python Bytecode" interpreter.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  2. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  3. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  4. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM