le * evaluateBranch( lithp_burrito *lb, le * trybranch) { le * keyword; int tryit = 0; if (!trybranch) return( NULL ); if (trybranch->branch) { keyword = evaluateBranch(lb, trybranch->branch); } else keyword = leNew( trybranch->data ); if (!keyword->data) { leWipe( keyword ); return( leNew( "NIL" )); } for ( tryit=0 ; evalTable[tryit].word ; tryit++) { if (!strcmp(evalTable[tryit].word, keyword->data)) { leWipe( keyword ); return( evalTable[tryit].callback( lb, countNodes( trybranch ), trybranch) ); } } leWipe( keyword ); return( evaluateNode( lb, trybranch )); }
le * eval_cb_car( lithp_burrito * lb, const int argc, le * branch ) { le * result = NULL; le * temp = NULL; if (!branch || argc != 2 ) return( leNew( "NIL" )); result = evaluateNode(lb, branch->list_next); if( result == NULL ) return( leNew( "NIL" ) ); if (countNodes(result) <= 1) { if (result->branch) { temp = result; result = result->branch; temp->branch = NULL; leWipe( temp ); } return( result ); } result->list_next->list_prev = NULL; leWipe( result->list_next ); result->list_next = NULL; if (result->branch) { temp = result; result = result->branch; temp->branch = NULL; leWipe( temp ); } return( result ); }
le * eval_cb_enum( lithp_burrito * lb, const int argc, le * branch ) { le * current; int count = -1; char value[16]; if (!branch || argc < 2) return( leNew( "NIL" ) ); current = branch->list_next; while ( current ) { if (current->data) { sprintf( value, "%d", ++count); lb->mainVarList = variableSetString( lb->mainVarList, current->data, value ); } current = current->list_next; } if (count == -1) return( leNew( "NIL" ) ); else return( evalCastIntToLe(count) ); }
le * eval_cb_cons( lithp_burrito * lb, const int argc, le * branch ) { le * result1 = NULL; le * result2 = NULL; if (!branch || argc != 3 ) return( leNew( "NIL" )); result1 = evaluateNode(lb, branch->list_next); if ( result1 == NULL ) return( leNew( "NIL" )); result2 = evaluateNode(lb, branch->list_next->list_next); if ( result2 == NULL ) { leWipe( result1 ); return( leNew( "NIL" )); } if ( countNodes(result1) > 1 ) { le * temp = leNew( NULL ); temp->branch = result1; result1 = temp; } result1->list_next = result2; result2->list_prev = result1; return( result1 ); }
le * eval_cb_whenunless_helper( enum whenunless which, lithp_burrito * lb, const int argc, le * branch ) { le * retval = NULL; le * trythis = NULL; if (!branch || argc < 3 ) return( leNew( "NIL" )); /* conditional */ retval = evaluateNode(lb, branch->list_next); if ( which == WU_UNLESS ) { /* unless - it wasn't true... bail */ if ( strcmp( retval->data, "NIL" )) { leWipe( retval ); return( leNew( "NIL" ) ); } } else { /* when: it wasn't false... bail */ if ( !strcmp( retval->data, "NIL" )) return( retval ); } trythis = branch->list_next->list_next; while( trythis ) { if (retval) leWipe( retval ); retval = evaluateNode(lb, trythis); trythis = trythis->list_next; } return( retval ); }
/* valid input: (DrawPen r g b m) (DrawPen (r g b m)) */ le * eval_gfx_DrawPen ( lithp_burrito * lb, const int argc, le * branch ) { int r,g,b; int ac; le *mono, *result; if( !lb || !branch || (argc != 5 && argc != 2) ) return( leNew( "NIL" )); if( argc == 5 ) { mono = eval_getint_3( lb, branch, &r, &g, &b ); result = evaluateNode( lb, mono ); } else { result = evaluateNode( lb, branch->list_next ); ac = countNodes( result ); if( ac != 4 ) return( leNew( "NIL" )); mono = eval_getint_3_noskip( lb, result, &r, &g, &b ); } lb->pen1 = eval_make_color( lb, r, g, b, result->data ); return( leNew( "T" )); }
le * eval_cb_terpri( lithp_burrito * lb, const int argc, le * branch ) { if (!branch || argc != 1 ) return( leNew( "NIL" )); printf( "\n" ); fflush( stdout ); return( leNew( "NIL" ) ); }
le * eval_gfx_DrawFillEllipse ( lithp_burrito * lb, const int argc, le * branch ) { int x,y,a,b; if( !lb || !branch || argc != 5 ) return( leNew( "NIL" )); eval_getint_4( lb, branch, &x, &y, &a, &b ); ttk_fillellipse( lb->srf, x, y, a, b, lb->pen1 ); return( leNew( "T" )); }
le * eval_gfx_DrawHGradient ( lithp_burrito * lb, const int argc, le * branch ) { int x,y,a,b; if( !lb || !branch || argc != 5 ) return( leNew( "NIL" )); eval_getint_4( lb, branch, &x, &y, &a, &b ); ttk_hgradient( lb->srf, x, y, a, b, lb->pen1, lb->pen2 ); return( leNew( "T" )); }
le * eval_gfx_DrawPixel ( lithp_burrito * lb, const int argc, le * branch ) { int x,y; if( !lb || !branch || argc != 3 ) return( leNew( "NIL" )); eval_getint_2( lb, branch, &x, &y ); ttk_pixel( lb->srf, x, y, lb->pen1 ); return( leNew( "T" )); }
le * evaluateDefun( lithp_burrito *lb, le * fcn, le * params ) { le * function; le * thisparam; le * result; int count; /* make sure both lists exist */ if (!fcn) return( leNew( "NIL" )); /* check for the correct number of parameters */ if (countNodes(fcn->branch) > countNodes(params)) return( leNew( "NIL" )); /* allocate another function definition, since we're gonna hack it */ function = leDup(fcn); /* pass 1: tag each node properly. for each parameter: (fcn) - look for it in the tree, tag those with the value */ count = 0; thisparam = fcn->branch; while (thisparam) { leTagData(function, thisparam->data, count); thisparam = thisparam->list_next; count++; } /* pass 2: replace for each parameter: (param) - evaluate the passed in value - replace it in the tree */ count = 0; thisparam = params; while (thisparam) { result = evaluateNode( lb, thisparam ); leTagReplace(function, count, result); thisparam = thisparam->list_next; leWipe(result); count++; } /* then evaluate the resulting tree */ result = evaluateBranch( lb, function->list_next ); /* free any space allocated */ leWipe( function ); /* return the evaluation */ return( result ); }
le * eval_gfx_DrawPen2 ( lithp_burrito * lb, const int argc, le * branch ) { int r,g,b; le *mono, *result; if( !lb || !branch || argc != 5 ) return( leNew( "NIL" )); mono = eval_getint_3( lb, branch, &r, &g, &b ); result = evaluateNode( lb, mono ); lb->pen1 = eval_make_color( lb, r, g, b, result->data ); return( leNew( "T" )); }
le * eval_cb_require( lithp_burrito * lb, const int argc, le * branch ) { if (!branch || argc != 2) return( leNew( "NIL" ) ); /* we should do different things based on this eventually... */ if( !strcmp( branch->list_next->data, "pz2:0.8" )) { lb->apiVersion = 0.8; return( leNew( "T" )); } return( leNew( "NIL" )); }
le * eval_gfx_DrawVectorTextCentered ( lithp_burrito * lb, const int argc, le * branch ) { int x,y,w,h; le *t, *t2; if( !lb || !branch || argc < 6 ) return( leNew( "NIL" )); t = eval_getint_4( lb, branch, &x, &y, &w, &h ); t2 = evaluateNode( lb, t ); pz_vector_string_center( lb->srf, t2->data, x, y, w, h, 1, lb->pen1 ); return( leNew( "T" )); }
le * eval_cb_atom( lithp_burrito * lb, const int argc, le * branch ) { le * result = NULL; if (!branch || argc != 2 ) return( leNew( "NIL" )); result = evaluateNode( lb, branch->list_next ); if (countNodes(result) == 1) { leWipe( result ); return( leNew( "T" ) ); } return( leNew( "NIL" ) ); }
/* this is to be called like: (RandomOf A B C D) ** although, it probably should be (RandomOf (list A B C D)) ** this should work fine for now though */ le * eval_gfx_RandomOf( lithp_burrito * lb, const int argc, le * branch ) { le * ptr; int value; int r; if (!branch) return( leNew( "NIL" ) ); value = (int)((float)(argc-1) * rand() / (RAND_MAX + 1.0)); ptr = branch->list_next; for( r=0 ; r<value ; r++ ) ptr = ptr->list_next; return( leNew( ptr->data )); }
le * eval_cb_defun( lithp_burrito * lb, const int argc, le * branch ) { if (!branch || argc < 4 ) return( leNew( "NIL" )); if ( !branch->list_next->data ) return( leNew( "NIL" )); lb->defunList = variableSet( lb->defunList, branch->list_next->data, branch->list_next->list_next ); return( leNew( branch->list_next->data )); }
le * evaluateNode( lithp_burrito * lb, le * node) { le * temp; le * value; if (node->branch) { if( node->quoted ) { value = leDup( node->branch ); } else { value = evaluateBranch( lb, node->branch ); } } else { temp = variableGet( lb->defunList, node->data ); if (temp) { value = evaluateDefun( lb, temp, node->list_next ); } else { temp = variableGet( lb->mainVarList, node->data ); if (temp) { value = leDup( temp ); } else { value = leNew( node->data ); } } } return( value ); }
le * eval_cb_multiply( lithp_burrito * lb, const int argc, le * branch ) { if (!branch || argc < 2) return( leNew( "NIL" ) ); return( evalCastIntToLe( eval_cume_helper( C_MULTIPLY, lb, 1, branch->list_next))); }
le * evalCastIntToLe( int intvalue ) { char buffer[80]; sprintf (buffer, "%d", intvalue); return( leNew(buffer) ); }
static le * evaluateBranch (le * trybranch) { le *keyword; fentry * func; if (trybranch == NULL) return NULL; if (trybranch->branch) keyword = evaluateBranch (trybranch->branch); else keyword = leNew (trybranch->data); if (keyword->data == NULL) { leWipe (keyword); return leNIL; } func = get_fentry (keyword->data); leWipe (keyword); if (func) return func->func (1, false, trybranch); return NULL; }
le * eval_cb_select( lithp_burrito * lb, const int argc, le * branch ) { le * result; if (argc < 2) return( leNew( "NIL" )); branch = branch->list_next; result = evaluateNode(lb, branch); branch = branch->list_next; while( branch ) { if( branch->branch ) { le * check = branch->branch; if (check && check->data && (!strcmp( check->data, result->data ))) { /* we're in the right place, evaluate and return */ le * computelist = check->list_next; while( computelist ) { leWipe( result ); result = evaluateNode( lb, computelist ); computelist = computelist->list_next; } return( result ); } } branch = branch->list_next; } return( result ); }
le * eval_cb_add( lithp_burrito * lb, const int argc, le * branch ) { if (!branch || argc < 2) return( leNew( "NIL" ) ); return( evalCastIntToLe( eval_cume_helper( C_ADD, lb, 0, branch->list_next) )); }
le * eval_cb_prog( lithp_burrito * lb, const int argc, le * branch, int returnit ) { le * curr; le * retval = NULL; le * tempval = NULL; int current = 0; if (!branch || argc < (returnit +1) ) return( leNew( "NIL" )); curr = branch->list_next; while (curr) { ++current; if ( tempval ) leWipe (tempval); tempval = evaluateNode( lb, curr ); if (current == returnit) retval = leDup( tempval ); curr = curr->list_next; } if (!retval) retval = tempval; return( retval ); }
void variableSetString(le **varlist, char *key, char *value) { if (key && value) { le *temp = leNew(value); variableSet(varlist, key, temp); leWipe(temp); } }
le * eval_cb_eqsign( lithp_burrito * lb, const int argc, le * branch ) { le * letemp; int value1, value2; if (!branch || argc != 3 ) return( leNew( "NIL" ) ); letemp = evaluateNode( lb, branch->list_next ); value1 = evalCastLeToInt( letemp ); leWipe( letemp ); letemp = evaluateNode( lb, branch->list_next->list_next ); value2 = evalCastLeToInt( letemp ); leWipe( letemp ); return( leNew ( (value1 == value2 )?"T":"NIL" ) ); }
le * eval_cb_equal( lithp_burrito * lb, const int argc, le * branch ) { le * list1 = NULL; le * list2 = NULL; int retval = 0; if (!branch || argc != 3 ) return( leNew( "NIL" ) ); list1 = evaluateNode( lb, branch->list_next ); list2 = evaluateNode( lb, branch->list_next->list_next ); retval = eval_cb_lists_same( list1, list2 ); leWipe( list1 ); leWipe( list2 ); return( leNew ( (retval == 1) ? "T" : "NIL" ) ); }
le * eval_gfx_RangeWrap( lithp_burrito * lb, const int argc, le * branch ) { int v, min, max; if (!branch || argc != 4) return( leNew( "NIL" ) ); (void)eval_getint_3( lb, branch, &v, &min, &max ); if( v>max ) v=min; if( v<min ) v=max; return( evalCastIntToLe( v ) ); }
le * eval_cb_eval( lithp_burrito * lb, const int argc, le * branch ) { le * temp; le * retval; if (!branch || argc != 2 ) return( leNew( "NIL" )); temp = evaluateNode(lb, branch->list_next); retval = evaluateBranch(lb, temp); leWipe( temp ); return( retval ); }
le * eval_cb_list( lithp_burrito * lb, const int argc, le * branch ) { le * currelement = NULL; le * finaltree = NULL; le * lastadded = NULL; le * result = NULL; if (!branch) return( leNew( "NIL" )); currelement = branch->list_next; while (currelement) { result = evaluateNode(lb, currelement); if ( result == NULL ) { leWipe( finaltree ); return( leNew( "NIL" )); } if( countNodes(result) > 1) { le * temp = leNew( NULL ); temp->branch = result; result = temp; } if (!finaltree) { finaltree = result; lastadded = result; } else { lastadded->list_next = result; result->list_prev = lastadded; lastadded = result; } currelement = currelement->list_next; } if (!finaltree) { return( leNew( "NIL" )); } return( finaltree ); }