struct Value *handle_loadlua_command(String *args, int offset) { // check if lua has been initialised if(lua_state == NULL) { // it hasn't: initialise now lua_state = lua_open(); luaL_openlibs(lua_state); // add our tf_eval() function for Lua scripts to call lua_register(lua_state, "tf_eval", tfeval_for_lua); // add tf_getvar() function lua_register(lua_state, "tf_getvar", getvar_for_lua); } // now read and execute the scripts that user asked for if(luaL_dofile(lua_state, args->data + offset) != 0) { myerror("Cannot load script: "); myerror(lua_tostring(lua_state, -1)); lua_pop(lua_state, 1); return newint(1); } return newint(0); }
struct Value *handle_purgelua_command(String *args, int offset) { if(lua_state != NULL) { lua_close(lua_state); lua_state = NULL; myinfo("All LUA scripts forgotten"); return newint(1); } myinfo("No LUA scripts loaded"); return newint(0); }
int main() { int n;cout<<"n=";cin>> n; int k;cout<<"k=";cin>> k; int*a =newint(n); for(int i =0; i < n;++i) { cout<<"a["<< i <<"]=";cin>> a[i]; } for(int i =0; i < n;++i) { if(a[i]< k) { a[i]= k; } } for(int i =0; i < n;++i) { if(i ==0) { cout<< endl; } cout<<"a["<< i <<"]=";cout<< a[i]<<" "; } return0; }
Object* add_obj(Object* o1, Object* o2) { // TODO: type safety Integer* i1 = (Integer*)o1; Integer* i2 = (Integer*)o2; return newint( i1->value + i2->value ); }
/*FUNCTION*/ LVAL c_char_code(tpLspObject pLSP, LVAL p ){ /*noverbatim CUT*/ LVAL q; if( null(p) || !characterp(p) )return NIL; q = newint(); setint(q,(int)getchr(p)); return q; }
void asgn(char *rhs, char *lhs, char *err) { /* * Variable Assignment Function; * perfroms a variable assignment * on var with names rhs & lhs, then * does the appropriate post-evaluation * operations and if errored, then gives * a description about the error in *err. * ------------------------------------------------ * WARNING: Strings *rhs and *lhs are subject to * changes, forget them after calling this function. */ long int *value = NULL; long int rvalue = 0; /* to find assignment operator */ char esymbol=0; rvalue=strlen(lhs)-1; lhs += rvalue; /* now find the assignment operator */ if(*lhs=='*'||*lhs=='/'|| *lhs=='+'||*lhs=='-'|| *lhs=='%'||*lhs=='^'|| *lhs=='~'||*lhs==':'|| *lhs=='<'||*lhs=='>'|| *lhs=='|'||*lhs=='&'|| *lhs=='!'||*lhs=='=') { esymbol = *lhs; *lhs = '\0'; } /* set lhs ptr to origin */ lhs -= rvalue; /* evaluate rhs */ eval_long(rhs,&rvalue); value = newint(lhs); /* if variable elready exists, get it. */ if(value==NULL) value = getint(lhs); /* set the value */ if(value!=NULL) mov_int(value,&rvalue,esymbol); else /* getint() hasn't worked properly */ strcpy(err,"Fatal Data Transfer Error."); printf("%s = %ld\n",lhs,*value); }
void exec(char *str) { char *lhs = (char *) allocs(strlen(str)); char *rhs = (char *) allocs(strlen(str)); char *sptr = NULL; /* here the rhs & lhs are obtained */ for(sptr=lhs;*str!='\0'&&*str!='=';str++) *sptr++ = *str; *sptr = '\0'; if(*str=='=') for(sptr=rhs,str++;*str!='\0';str++) { *sptr++ = *str; *sptr = '\0'; } else *rhs = '\0'; printf("%s",lhs); int32 *value= NULL; if(*rhs=='\0') /* rhs is empty */ { /* no assignment involved */ value = getint(lhs); if(value!=NULL) printf(" = %ld\n",*value); } else /* rhs exists */ { int32 rvalue=0; rvalue = atoi(rhs); printf(" = %ld\n",rvalue); value = newint(lhs); *value = rvalue; } free(lhs); free(rhs); }
/* * local function to read an expression */ static LVAL _readexpr(tpLspObject pLSP,FILE *f) { int ch,ch1,ch2,i; LVAL p; char *s; double dval; long lval; spaceat(ch,f); if( ch == EOF ) { return NIL; } if( ch == pLSP->cClose ) { return NIL; } if( ch == pLSP->cOpen )/* Read a cons node. */ return readcons(pLSP,f); /**** Note: XLISP allows 1E++10 as a symbol. This is dangerous. We do not change XLISP (so far), but here I exclude all symbol names starting with numeral. */ if( const_p1(ch) )/* Read a symbol. */ { for( i = 0 ; const_p(ch) ; i++ ){ if( storech(pLSP,i,ch) )return NIL; ch = getC(pLSP,f); } UNGETC(ch); /* Recognize NIL and nil symbols. */ if( !strcmp(BUFFER,"NIL") || !strcmp(BUFFER,"nil") ) return NIL; p = newsymbol(); s = StrDup( BUFFER ); if( null(p) || s == NULL )return NIL; setsymbol(p,s); return p; } if( ch == '\"' ){ ch = GETC(f); storech(pLSP,0,0); /* inititalize the buffer */ if( ch != '\"' )goto SimpleString; ch = GETC(f); if( ch != '\"' ){ UNGETC(ch); ch = '\"';/* ch should hold the first character of the string that is " now */ goto SimpleString; } ch = GETC(f); /* multi line string */ for( i = 0 ; ch != EOF ; i++ ){ if( ch == '\"' ){ ch1 = GETC(f); ch2 = GETC(f); if( ch1 == '\"' && ch2 == '\"' )break; UNGETC(ch2); UNGETC(ch1); } if( ch == '\\' ){ ch = GETC(f); s = escapers; while( *s ){ if( *s++ == ch ){ ch = *s; break; } if( *s )s++; } } if( storech(pLSP,i,ch) )return NIL; ch = GETC(f); } p = newstring(); s = StrDup( BUFFER ); if( null(p) || s == NULL )return NIL; setstring(p,s); return p; } if( ch == '\"' ){/* Read a string. */ ch = GETC(f);/* Eat the " character. */ SimpleString: for( i = 0 ; ch != '\"' && ch != EOF ; i++ ){ if( ch == '\\' ){ ch = GETC(f); s = escapers; while( *s ){ if( *s++ == ch ){ ch = *s; break; } if( *s )s++; } } if( ch == '\n' )return NIL; if( storech(pLSP,i,ch) )return NIL; ch = GETC(f); } p = newstring(); s = StrDup( BUFFER ); if( null(p) || s == NULL ) { return NIL; } setstring(p,s); return p; } if( numeral1(ch) ) { for( i = 0 ; isinset(ch,"0123456789+-eE.") ; i++ ) { if( storech(pLSP,i,ch) )return NIL; ch = getC(pLSP,f); } UNGETC(ch); cnumeric(BUFFER,&i,&dval,&lval); switch( i ) { case 0: return NIL; case 1: /* A float number is coming. */ p = newfloat(); if( null(p) ) { return NIL; } setfloat(p,dval); return p; case 2: /* An integer is coming. */ p = newint(); if( null(p) ) { return NIL; } setint(p,lval); return p; default: return NIL; } } return NIL; }
int *newintstr(char *val) { return(newint(atoi(val))); }
struct Value *handle_calllua_command(String *args, int offset) { // for the love of <enter_your_gods_name_here>, // REMEMBER TO FREE cstr_args before returning! char *cstr_args = strdup(args->data + offset); char *func, *tmp; int argpos=-1; int num_args; if(lua_state == NULL) { myerror("No script loaded"); free(cstr_args); return newint(1); } func = cstr_args; tmp = strchr(cstr_args, ' '); if(tmp != NULL) { *tmp = '\0'; argpos = tmp - func + 1; } lua_getfield(lua_state, LUA_GLOBALSINDEX, func); if(lua_isfunction(lua_state, -1) != 1) { myerror("No such function"); lua_pop(lua_state, 1); free(cstr_args); return newint(1); } if(argpos > -1) { int i, table_i=0; // first argument: the string data lua_pushstring(lua_state, func + argpos); // second arg: the attributes of that string as an array of ints lua_newtable(lua_state); if(args->charattrs) { for(i=argpos; i<args->len; i++) { lua_pushinteger(lua_state, args->charattrs[i]); lua_rawseti(lua_state, -2, table_i++); } } // third arg: attributes for the whole line lua_pushinteger(lua_state, args->attrs); num_args = 3; } else num_args = 0; if(lua_pcall(lua_state, num_args, 0, 0) != 0) { myerror(lua_tostring(lua_state, -1)); // pop the error message lua_pop(lua_state, 1); free(cstr_args); return newint(1); } free(cstr_args); return newint(0); }