WheretIB / nullc

Fast C-like programming language with advanced features
MIT License
163 stars 13 forks source link

Hi,how can I blind a value in nullc? #12

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I had tried such way:

int tv=123;
nullcBuild("auto testv=1;");
nullcSetGlobal("testv",&tv);//sucess

char* source = "auto ti=testv;"
nullres good = nullcBuild(source);

but there is an error:
ERROR: unknown identifier 'testv'

Original issue reported on code.google.com by happygre...@gmail.com on 31 May 2012 at 12:00

GoogleCodeExporter commented 9 years ago
You are making two main mistakes here:
1) nullcBuild function cleans the nullc compiler, linker and executer state, so 
with every call, you receive a clean environment to run nullc code.
nullc main use case is to build your code once, call nullcRun once to execute 
global code and then call nullc functions using nullcRunFunction calls.

2) nullcSetGlobal will set internal nullc variable value to the value of your 
C++ variable, but it will not save the link between them. To do that, you 
should make a reference to an int in nullc code and set its value to a pointer 
to your variable.

Here is an example of code that "binds" a variable to nullc:
nullcInit("Modules/");

int value = 2;
int *valuePtr = &value;

nullres good = nullcBuild("int ref value; int get(){ return *value; } void 
set(int x){ *value = x; }");

// Run global code
nullcRun();

// Set global reference to C++ variable
nullcSetGlobal("value", &valuePtr);

printf("value equals %d\n", value);

// Get the variable value as seen from nullc
if(nullcRunFunction("get"))
    printf("*value in nullc equals %d\n", nullcGetResultInt());

// Change the variable value from nullc
if(nullcRunFunction("set", 5))
    printf("value is now %d\n", value);

nullcTerminate();

Original comment by Where...@gmail.com on 26 Jun 2012 at 9:22