Soar Kernel  9.3.2 08-06-12
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Macros | Functions
rhsfun.cpp File Reference
#include <portability.h>
#include <stdlib.h>
#include "rhsfun.h"
#include "kernel.h"
#include "print.h"
#include "mem.h"
#include "symtab.h"
#include "init_soar.h"
#include "gsysparam.h"
#include "agent.h"
#include "production.h"
#include "rhsfun_math.h"
#include "io_soar.h"
#include "recmem.h"
#include "wmem.h"
#include "gdatastructs.h"
#include "xml.h"
#include "soar_TraceNames.h"
#include <map>
#include <string>
#include <time.h>

Go to the source code of this file.

Macros

#define TIMESTAMP_BUFFER_SIZE   100

Functions

Symbolaccept_rhs_function_code (agent *thisAgent, list *, void *)
void add_rhs_function (agent *thisAgent, Symbol *name, rhs_function_routine f, int num_args_expected, Bool can_be_rhs_value, Bool can_be_stand_alone_action, void *user_data)
Symbolcapitalize_symbol_rhs_function_code (agent *thisAgent, list *args, void *)
Symbolcount_rhs_function_code (agent *thisAgent, list *args, void *)
Symbolcrlf_rhs_function_code (agent *thisAgent, list *, void *)
Symboldeep_copy_rhs_function_code (agent *thisAgent, list *args, void *)
Symboldont_learn_rhs_function_code (agent *thisAgent, list *args, void *)
Symbolforce_learn_rhs_function_code (agent *thisAgent, list *args, void *)
Symbolhalt_rhs_function_code (agent *thisAgent, list *, void *)
Symbolifeq_rhs_function_code (agent *thisAgent, list *args, void *)
void init_built_in_rhs_functions (agent *thisAgent)
rhs_functionlookup_rhs_function (agent *thisAgent, Symbol *name)
Symbolmake_constant_symbol_rhs_function_code (agent *thisAgent, list *args, void *)
void recursive_deep_copy_helper (agent *thisAgent, Symbol *id_to_process, Symbol *parent_id, std::map< Symbol *, Symbol * > &processedSymbols)
void recursive_wme_copy (agent *thisAgent, Symbol *parent_id, wme *curwme, std::map< Symbol *, Symbol * > &processedSymbols)
void remove_built_in_rhs_functions (agent *thisAgent)
void remove_rhs_function (agent *thisAgent, Symbol *name)
Symbolstrlen_rhs_function_code (agent *thisAgent, list *args, void *)
Symboltimestamp_rhs_function_code (agent *thisAgent, list *, void *)
Symboltrim_rhs_function_code (agent *thisAgent, list *args, void *)
Symbolwrite_rhs_function_code (agent *thisAgent, list *args, void *)

Macro Definition Documentation

#define TIMESTAMP_BUFFER_SIZE   100

Function Documentation

Symbol* accept_rhs_function_code ( agent thisAgent,
list ,
void *   
)

Definition at line 282 of file rhsfun.cpp.

References get_next_io_symbol_from_text_input_line(), NIL, release_io_symbol(), symbol_add_ref(), and TRUE.

Referenced by init_built_in_rhs_functions().

{
char buf[2000], *s;
Symbol *sym;
while (TRUE) {
s = fgets (buf, 2000, stdin);
// s = Soar_Read(thisAgent, buf, 2000); /* kjh(CUSP-B10) */
if (!s) {
/* s==NIL means immediate eof encountered or read error occurred */
return NIL;
}
s = buf;
if (sym) break;
}
release_io_symbol (thisAgent, sym); /* because it was obtained using get_io_... */
return sym;
}
void add_rhs_function ( agent thisAgent,
Symbol name,
rhs_function_routine  f,
int  num_args_expected,
Bool  can_be_rhs_value,
Bool  can_be_stand_alone_action,
void *  user_data 
)

Definition at line 63 of file rhsfun.cpp.

References allocate_memory(), rhs_function_struct::can_be_rhs_value, rhs_function_struct::can_be_stand_alone_action, rhs_function_struct::f, MISCELLANEOUS_MEM_USAGE, rhs_function_struct::name, rhs_function_struct::next, NIL, rhs_function_struct::num_args_expected, print(), print_with_symbols(), agent_struct::rhs_functions, and rhs_function_struct::user_data.

Referenced by init_built_in_rhs_functions(), and init_built_in_rhs_math_functions().

{
if ((!can_be_rhs_value) && (!can_be_stand_alone_action))
{
print (thisAgent, "Internal error: attempt to add_rhs_function that can't appear anywhere\n");
return;
}
for (rf=thisAgent->rhs_functions; rf!=NIL; rf=rf->next)
{
if (rf->name==name)
{
print_with_symbols (thisAgent, "Internal error: attempt to add_rhs_function that already exists: %y\n", name);
return;
}
}
rf = static_cast<rhs_function_struct *>(allocate_memory (thisAgent, sizeof(rhs_function), MISCELLANEOUS_MEM_USAGE));
/* Insertion into the list */
rf->next = thisAgent->rhs_functions;
thisAgent->rhs_functions = rf;
/* Rest of the stuff */
rf->name = name;
rf->f = f;
rf->num_args_expected = num_args_expected;
rf->can_be_rhs_value = can_be_rhs_value;
rf->can_be_stand_alone_action = can_be_stand_alone_action;
rf->user_data = user_data;
}
Symbol* capitalize_symbol_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 308 of file rhsfun.cpp.

References FALSE, cons_struct::first, make_sym_constant(), NIL, print(), print_with_symbols(), cons_struct::rest, savestring(), SYM_CONSTANT_SYMBOL_TYPE, and symbol_to_string().

Referenced by init_built_in_rhs_functions().

{
char * symbol_to_capitalize;
Symbol * sym;
if (!args) {
print (thisAgent, "Error: 'capitalize-symbol' function called with no arguments.\n");
return NIL;
}
sym = static_cast<Symbol *>(args->first);
if (sym->common.symbol_type != SYM_CONSTANT_SYMBOL_TYPE) {
print_with_symbols (thisAgent, "Error: non-symbol (%y) passed to capitalize-symbol function.\n", sym);
return NIL;
}
if (args->rest) {
print (thisAgent, "Error: 'capitalize-symbol' takes exactly 1 argument.\n");
return NIL;
}
symbol_to_capitalize = symbol_to_string(thisAgent, sym, FALSE, NIL, 0);
symbol_to_capitalize = savestring(symbol_to_capitalize);
*symbol_to_capitalize = static_cast<char>(toupper(*symbol_to_capitalize));
return make_sym_constant(thisAgent, symbol_to_capitalize);
}
Symbol* count_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 710 of file rhsfun.cpp.

References agent_struct::dyn_counters, FALSE, cons_struct::first, NIL, cons_struct::rest, and symbol_to_string().

Referenced by init_built_in_rhs_functions().

{
Symbol *arg;
char *string;
for ( ; args!=NIL; args=args->rest) {
arg = static_cast<symbol_union *>(args->first);
/* --- Note use of FALSE here--print the symbol itself, not a rereadable
version of it --- */
string = symbol_to_string (thisAgent, arg, FALSE, NIL, 0);
(*thisAgent->dyn_counters)[ string ]++;
}
return NIL;
}
Symbol* crlf_rhs_function_code ( agent thisAgent,
list ,
void *   
)

Definition at line 190 of file rhsfun.cpp.

References make_sym_constant().

Referenced by init_built_in_rhs_functions().

{
return make_sym_constant (thisAgent, "\n");
}
Symbol* deep_copy_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 681 of file rhsfun.cpp.

References cons_struct::first, make_new_identifier(), make_sym_constant(), and recursive_deep_copy_helper().

Referenced by init_built_in_rhs_functions().

{
/* Getting the argument symbol */
Symbol* baseid = static_cast<Symbol *>(args->first);
if ( baseid->common.symbol_type != 1 ) {
return make_sym_constant(thisAgent,"*symbol not id*");
}
/* Making the new root identifier symbol */
Symbol* retval = make_new_identifier(thisAgent, 'D', 1);
/* Now processing the wme's associated with the passed in symbol */
std::map<Symbol*,Symbol*> processedSymbols;
baseid,
retval,
processedSymbols);
return retval;;
}
Symbol* dont_learn_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 491 of file rhsfun.cpp.

References agent_struct::chunk_free_problem_spaces, cons_struct::first, symbol_union::id, IDENTIFIER_SYMBOL_TYPE, identifier_struct::isa_goal, member_of_list(), NIL, print(), print_with_symbols(), push(), and cons_struct::rest.

Referenced by init_built_in_rhs_functions().

{
Symbol *state;
if (!args) {
print (thisAgent, "Error: 'dont-learn' function called with no arg.\n");
return NIL;
}
state = static_cast<Symbol *>(args->first);
if (state->common.symbol_type != IDENTIFIER_SYMBOL_TYPE) {
print_with_symbols (thisAgent, "Error: non-identifier (%y) passed to dont-learn function.\n", state);
return NIL;
} else if (! state->id.isa_goal) {
print_with_symbols(thisAgent, "Error: identifier passed to dont-learn is not a state: %y.\n",state);
}
if (args->rest) {
print (thisAgent, "Error: 'dont-learn' takes exactly 1 argument.\n");
return NIL;
}
if (! member_of_list (state, thisAgent->chunk_free_problem_spaces)) {
push(thisAgent, state, thisAgent->chunk_free_problem_spaces);
/* print_with_symbols("State %y added to chunk_free_list.\n",state); */
}
return NIL;
}
Symbol* force_learn_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 527 of file rhsfun.cpp.

References agent_struct::chunky_problem_spaces, cons_struct::first, symbol_union::id, IDENTIFIER_SYMBOL_TYPE, identifier_struct::isa_goal, member_of_list(), NIL, print(), print_with_symbols(), push(), and cons_struct::rest.

Referenced by init_built_in_rhs_functions().

{
Symbol *state;
if (!args) {
print (thisAgent, "Error: 'force-learn' function called with no arg.\n");
return NIL;
}
state = static_cast<Symbol *>(args->first);
if (state->common.symbol_type != IDENTIFIER_SYMBOL_TYPE) {
print_with_symbols (thisAgent, "Error: non-identifier (%y) passed to force-learn function.\n", state);
return NIL;
} else if (! state->id.isa_goal) {
print_with_symbols(thisAgent, "Error: identifier passed to force-learn is not a state: %y.\n",state);
}
if (args->rest) {
print (thisAgent, "Error: 'force-learn' takes exactly 1 argument.\n");
return NIL;
}
if (! member_of_list (state, thisAgent->chunky_problem_spaces)) {
push(thisAgent, state, thisAgent->chunky_problem_spaces);
/* print_with_symbols("State %y added to chunky_list.\n",state); */
}
return NIL;
}
Symbol* halt_rhs_function_code ( agent thisAgent,
list ,
void *   
)
Symbol* ifeq_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 402 of file rhsfun.cpp.

References cons_struct::first, NIL, print(), cons_struct::rest, and symbol_add_ref().

Referenced by init_built_in_rhs_functions().

{
Symbol *arg1, *arg2;
cons *c;
if (!args) {
print (thisAgent, "Error: 'ifeq' function called with no arguments\n");
return NIL;
}
/* --- two or more arguments --- */
arg1 = static_cast<symbol_union *>(args->first);
c=args->rest;
arg2 = static_cast<symbol_union *>(c->first);
c=c->rest;
if (arg1 == arg2)
{
symbol_add_ref(static_cast<Symbol *>(c->first));
return static_cast<symbol_union *>(c->first);
}
else if (c->rest)
{
symbol_add_ref(static_cast<Symbol *>(c->rest->first));
return static_cast<symbol_union *>(c->rest->first);
}
else return NIL;
}
void init_built_in_rhs_functions ( agent thisAgent)

Definition at line 731 of file rhsfun.cpp.

References accept_rhs_function_code(), add_rhs_function(), capitalize_symbol_rhs_function_code(), count_rhs_function_code(), crlf_rhs_function_code(), deep_copy_rhs_function_code(), dont_learn_rhs_function_code(), FALSE, force_learn_rhs_function_code(), halt_rhs_function_code(), ifeq_rhs_function_code(), init_built_in_rhs_math_functions(), make_constant_symbol_rhs_function_code(), make_sym_constant(), strlen_rhs_function_code(), timestamp_rhs_function_code(), trim_rhs_function_code(), TRUE, and write_rhs_function_code().

Referenced by init_soar_agent().

{
add_rhs_function (thisAgent, make_sym_constant (thisAgent, "write"), write_rhs_function_code,
-1, FALSE, TRUE, 0);
add_rhs_function (thisAgent, make_sym_constant (thisAgent, "crlf"), crlf_rhs_function_code,
0, TRUE, FALSE, 0);
add_rhs_function (thisAgent, make_sym_constant (thisAgent, "halt"), halt_rhs_function_code,
0, FALSE, TRUE, 0);
/*
Replaced with a gSKI rhs function
add_rhs_function (thisAgent, make_sym_constant (thisAgent, "interrupt"),
interrupt_rhs_function_code,
0, FALSE, TRUE, 0);
*/
add_rhs_function (thisAgent, make_sym_constant (thisAgent, "make-constant-symbol"),
-1, TRUE, FALSE, 0);
add_rhs_function (thisAgent, make_sym_constant (thisAgent, "timestamp"),
0, TRUE, FALSE, 0);
add_rhs_function (thisAgent, make_sym_constant (thisAgent, "accept"), accept_rhs_function_code,
0, TRUE, FALSE, 0);
add_rhs_function (thisAgent, make_sym_constant (thisAgent, "trim"),
1,
0);
add_rhs_function (thisAgent, make_sym_constant (thisAgent, "capitalize-symbol"),
1,
0);
/* AGR 520 begin */
add_rhs_function (thisAgent, make_sym_constant (thisAgent, "ifeq"), ifeq_rhs_function_code,
4, TRUE, FALSE, 0);
add_rhs_function (thisAgent, make_sym_constant (thisAgent, "strlen"), strlen_rhs_function_code,
1, TRUE, FALSE, 0);
/* AGR 520 end */
add_rhs_function (thisAgent, make_sym_constant (thisAgent, "dont-learn"),
1, FALSE, TRUE, 0);
add_rhs_function (thisAgent, make_sym_constant (thisAgent, "force-learn"),
1, FALSE, TRUE, 0);
add_rhs_function (thisAgent, make_sym_constant (thisAgent, "deep-copy"),
1,TRUE,FALSE,0);
add_rhs_function (thisAgent, make_sym_constant (thisAgent, "count"),
-1,FALSE,TRUE,0);
}
rhs_function* lookup_rhs_function ( agent thisAgent,
Symbol name 
)

Definition at line 102 of file rhsfun.cpp.

References rhs_function_struct::name, rhs_function_struct::next, NIL, and agent_struct::rhs_functions.

Referenced by parse_function_call_after_lparen(), and reteload_rhs_value().

{
for (rf=thisAgent->rhs_functions; rf!=NIL; rf=rf->next)
{
if (rf->name==name)
return rf;
}
return NIL;
}
Symbol* make_constant_symbol_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 218 of file rhsfun.cpp.

References FALSE, find_sym_constant(), cons_struct::first, generate_new_sym_constant(), make_sym_constant(), agent_struct::mcs_counter, NIL, cons_struct::rest, and symbol_to_string().

Referenced by init_built_in_rhs_functions().

{
std::stringstream buf;
char *string;
cons *c;
if (!args) {
buf << "constant";
} else {
for (c=args; c!=NIL; c=c->rest) {
string = symbol_to_string (thisAgent, static_cast<symbol_union *>(c->first), FALSE, NIL, 0);
buf << string;
}
}
if ((!args) && (!find_sym_constant (thisAgent, buf.str().c_str())))
return make_sym_constant (thisAgent, buf.str().c_str());
return generate_new_sym_constant (thisAgent, buf.str().c_str(), &thisAgent->mcs_counter);
}
void recursive_deep_copy_helper ( agent thisAgent,
Symbol id_to_process,
Symbol parent_id,
std::map< Symbol *, Symbol * > &  processedSymbols 
)

Definition at line 635 of file rhsfun.cpp.

References symbol_union::id, identifier_struct::input_wmes, wme_struct::next, slot_struct::next, recursive_wme_copy(), and identifier_struct::slots.

Referenced by deep_copy_rhs_function_code(), and recursive_wme_copy().

{
/* If this symbol has already been processed then ignore it and return */
if ( processedSymbols.find(id_to_process) != processedSymbols.end() ) {
return;
}
processedSymbols.insert(std::pair<Symbol*,Symbol*>(id_to_process,parent_id));
/* Iterating over the normal slot wmes */
for (slot* curslot = id_to_process->id.slots;
curslot != 0;
curslot = curslot->next) {
/* Iterating over the wmes in this slot */
for (wme* curwme = curslot->wmes;
curwme != 0;
curwme = curwme->next) {
recursive_wme_copy(thisAgent,
parent_id,
curwme,
processedSymbols);
}
}
/* Iterating over input wmes */
for (wme* curwme = id_to_process->id.input_wmes;
curwme != 0;
curwme = curwme->next) {
recursive_wme_copy(thisAgent,
parent_id,
curwme,
processedSymbols);
}
}
void recursive_wme_copy ( agent thisAgent,
Symbol parent_id,
wme curwme,
std::map< Symbol *, Symbol * > &  processedSymbols 
)

Definition at line 565 of file rhsfun.cpp.

References wme_struct::attr, glbDeepCopyWMEs, symbol_union::id, make_new_identifier(), make_wme(), identifier_struct::name_letter, wme_struct::next, recursive_deep_copy_helper(), symbol_add_ref(), and wme_struct::value.

Referenced by recursive_deep_copy_helper().

{
bool made_new_attr_symbol = false;
bool made_new_value_symbol = false;
Symbol* new_id = parent_id;
Symbol* new_attr = curwme->attr;
Symbol* new_value = curwme->value;
/* Handling the case where the attribute is an id symbol */
if ( curwme->attr->common.symbol_type == 1 ) {
/* Have I already made a new identifier for this identifier */
std::map<Symbol*,Symbol*>::iterator it = processedSymbols.find(curwme->attr);
if ( it != processedSymbols.end() ) {
/* Retrieve the previously created id symbol */
new_attr = it->second;
} else {
/* Make a new id symbol */
new_attr = make_new_identifier(thisAgent,
curwme->attr->id.name_letter,
1);
made_new_attr_symbol = true;
}
curwme->attr,
new_attr,
processedSymbols);
}
/* Handling the case where the value is an id symbol */
if ( curwme->value->common.symbol_type == 1 ) {
/* Have I already made a new identifier for this identifier */
std::map<Symbol*,Symbol*>::iterator it = processedSymbols.find(curwme->value);
if ( it != processedSymbols.end() ) {
/* Retrieve the previously created id symbol */
new_value = it->second;
} else {
/* Make a new id symbol */
new_value = make_new_identifier(thisAgent,
curwme->value->id.name_letter,
1);
made_new_value_symbol = true;
}
curwme->value,
new_value,
processedSymbols);
}
/* Making the new wme (Note just reusing the wme data structure, these
wme's actually get converted into preferences later).*/
wme* oldGlobalWme = glbDeepCopyWMEs;
/* TODO: We need a serious reference counting audit of the kernel But I think
this mirrors what happens in the instantiate rhs value and execute action
functions. */
symbol_add_ref(new_id);
if ( !made_new_attr_symbol ) symbol_add_ref(new_attr);
if ( !made_new_value_symbol) symbol_add_ref(new_value);
glbDeepCopyWMEs = make_wme(thisAgent, new_id, new_attr, new_value, true);
glbDeepCopyWMEs->next = oldGlobalWme;
}
void remove_built_in_rhs_functions ( agent thisAgent)

Definition at line 789 of file rhsfun.cpp.

References find_sym_constant(), remove_built_in_rhs_math_functions(), and remove_rhs_function().

Referenced by destroy_soar_agent().

{
// DJP-FREE: These used to call make_sym_constant, but the symbols must already exist and if we call make here again we leak a reference.
remove_rhs_function (thisAgent, find_sym_constant (thisAgent, "write"));
remove_rhs_function (thisAgent, find_sym_constant (thisAgent, "crlf"));
remove_rhs_function (thisAgent, find_sym_constant (thisAgent, "halt"));
remove_rhs_function (thisAgent, find_sym_constant (thisAgent, "make-constant-symbol"));
remove_rhs_function (thisAgent, find_sym_constant (thisAgent, "timestamp"));
remove_rhs_function (thisAgent, find_sym_constant (thisAgent, "accept"));
remove_rhs_function (thisAgent, find_sym_constant (thisAgent, "trim"));
remove_rhs_function (thisAgent, find_sym_constant (thisAgent, "capitalize-symbol"));
remove_rhs_function (thisAgent, find_sym_constant (thisAgent, "ifeq"));
remove_rhs_function (thisAgent, find_sym_constant (thisAgent, "strlen"));
remove_rhs_function (thisAgent, find_sym_constant (thisAgent, "dont-learn"));
remove_rhs_function (thisAgent, find_sym_constant (thisAgent, "force-learn"));
remove_rhs_function (thisAgent, find_sym_constant (thisAgent, "deep-copy"));
remove_rhs_function (thisAgent, find_sym_constant (thisAgent, "count"));
}
void remove_rhs_function ( agent thisAgent,
Symbol name 
)

Definition at line 114 of file rhsfun.cpp.

References free_memory(), MISCELLANEOUS_MEM_USAGE, rhs_function_struct::name, rhs_function_struct::next, NIL, print_with_symbols(), agent_struct::rhs_functions, and symbol_remove_ref().

Referenced by remove_built_in_rhs_functions(), and remove_built_in_rhs_math_functions().

{ /* code from Koss 8/00 */
rhs_function *rf = NIL, *prev;
/* Find registered function by name */
for (prev = NIL, rf = thisAgent->rhs_functions;
rf != NIL;
prev = rf, rf = rf->next)
{
if (rf->name == name)
break;
}
/* If not found, there's a problem */
if (rf == NIL) {
fprintf(stderr, "Internal error: attempt to remove_rhs_function that does not exist.\n");
print_with_symbols(thisAgent, "Internal error: attempt to remove_rhs_function that does not exist: %y\n", name);
}
/* Else, remove it */
else
{
/* Head of list special */
if (prev == NIL)
thisAgent->rhs_functions = rf->next;
else
prev->next = rf->next;
}
// DJP-FREE: The name reference needs to be released now the function is gone
symbol_remove_ref(thisAgent,name);
}
Symbol* strlen_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 470 of file rhsfun.cpp.

References FALSE, cons_struct::first, make_int_constant(), NIL, and symbol_to_string().

Referenced by init_built_in_rhs_functions().

{
Symbol *arg;
char *string;
arg = static_cast<symbol_union *>(args->first);
/* --- Note use of FALSE here--print the symbol itself, not a rereadable
version of it --- */
string = symbol_to_string (thisAgent, arg, FALSE, NIL, 0);
return make_int_constant (thisAgent, static_cast<int64_t>(strlen(string)));
}
Symbol* timestamp_rhs_function_code ( agent thisAgent,
list ,
void *   
)

Definition at line 244 of file rhsfun.cpp.

References make_sym_constant(), and TIMESTAMP_BUFFER_SIZE.

Referenced by init_built_in_rhs_functions().

{
time_t now;
struct tm *temp;
#define TIMESTAMP_BUFFER_SIZE 100
now = time(NULL);
#ifdef THINK_C
temp = localtime ((const time_t *)&now);
#else
#ifdef __SC__
temp = localtime ((const time_t *)&now);
#else
#ifdef __ultrix
temp = localtime ((const time_t *)&now);
#else
#ifdef MACINTOSH
temp = localtime ((const time_t *) &now);
#else
temp = localtime (&now);
#endif
#endif
#endif
#endif
SNPRINTF (buf,TIMESTAMP_BUFFER_SIZE, "%d/%d/%d-%02d:%02d:%02d",
temp->tm_mon + 1, temp->tm_mday, temp->tm_year,
temp->tm_hour, temp->tm_min, temp->tm_sec);
buf[TIMESTAMP_BUFFER_SIZE - 1] = 0; /* ensure null termination */
return make_sym_constant (thisAgent, buf);
}
Symbol* trim_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 430 of file rhsfun.cpp.

References FALSE, cons_struct::first, make_sym_constant(), NIL, print(), print_with_symbols(), cons_struct::rest, savestring(), SYM_CONSTANT_SYMBOL_TYPE, and symbol_to_string().

Referenced by init_built_in_rhs_functions().

{
char *symbol_to_trim;
Symbol *sym;
if ( !args )
{
print( thisAgent, "Error: 'trim' function called with no arguments.\n" );
return NIL;
}
sym = (Symbol *) args->first;
if ( sym->common.symbol_type != SYM_CONSTANT_SYMBOL_TYPE )
{
print_with_symbols( thisAgent, "Error: non-symbol (%y) passed to 'trim' function.\n", sym );
return NIL;
}
if ( args->rest )
{
print( thisAgent, "Error: 'trim' takes exactly 1 argument.\n" );
return NIL;
}
symbol_to_trim = symbol_to_string( thisAgent, sym, FALSE, NIL, 0 );
symbol_to_trim = savestring( symbol_to_trim );
std::string str( symbol_to_trim );
size_t start_pos = str.find_first_not_of( " \t\n" );
size_t end_pos = str.find_last_not_of( " \t\n" );
if ( ( std::string::npos == start_pos ) || ( std::string::npos == end_pos ) )
str = "";
else
str = str.substr( start_pos, end_pos - start_pos + 1 );
return make_sym_constant( thisAgent, str.c_str() );
}
Symbol* write_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 163 of file rhsfun.cpp.

References add_to_growable_string(), FALSE, cons_struct::first, free_growable_string(), make_blank_growable_string(), NIL, print_string(), cons_struct::rest, symbol_to_string(), text_of_growable_string(), and xml_object().

Referenced by init_built_in_rhs_functions().

{
Symbol *arg;
char *string;
growable_string gs = make_blank_growable_string(thisAgent); // for XML generation
for ( ; args!=NIL; args=args->rest) {
arg = static_cast<symbol_union *>(args->first);
/* --- Note use of FALSE here--print the symbol itself, not a rereadable
version of it --- */
string = symbol_to_string (thisAgent, arg, FALSE, NIL, 0);
add_to_growable_string(thisAgent, &gs, string); // for XML generation
print_string (thisAgent, string);
}
xml_object( thisAgent, kTagRHS_write, kRHS_String, text_of_growable_string(gs) );
free_growable_string(thisAgent, gs);
return NIL;
}