/* * Enumerate new symbol */ void _lex::_enum_symbol(void) { trace( LEX_TRACE_ENABLE, TRACE_ENTRY, "lex::enum_symbol" ); _typ = LEX_TOKEN_SYMBOL; _txt += get_char(); if(!IS_SYMBOL(_txt)) throw std::runtime_error( _format_exception( LEX_EXC_UKNOWN_SYM, _txt, get_line() )); if(!next_char()) throw std::runtime_error( _format_exception( LEX_EXC_UNEXP_EOF, get_line() )); trace( LEX_TRACE_ENABLE, TRACE_EXIT, "lex::enum_symbol" ); }
int eval(int addr){ int res; if(atomp(addr)){ if(IS_NUMBER(addr)) return(addr); if(IS_SYMBOL(addr)){ res = findsym(GET_NAME(addr)); switch(GET_TAG(res)){ case NUM: return(makenum(GET_NUMBER(res))); case SYM: return(GET_BIND(res)); case LIS: return(GET_BIND(res)); } } } else{ if(HAS_NAME(car(addr),"quote")) return(cadr(addr)); if(subrp(car(addr))) return(apply(symname(car(addr)),evlis(cdr(addr)))); if(fsubrp(car(addr))) return(apply(symname(car(addr)),cdr(addr))); if(lambdap(car(addr))) return(apply(symname(car(addr)),evlis(cdr(addr)))); } return(NIL); }
static int symbol_to_string(vm_thread_t *thread, obj_t *sym) { SAFE_ASSERT(IS_SYMBOL(*sym)); char *str = SYMBOL(*sym); RETURN_OBJ(_string(&thread->heap.allocator, str, 0)); }
int Parser::findPrevSymbol(const int& pos, const string& expr) { for (int i = pos - 1; i > 0; i--) if (IS_SYMBOL(i)) return i; return -1; }
int Parser::findNextSymbol(const int& pos, const string& expr) { for (int i = pos + 1; i < expr.size(); i++) if (IS_SYMBOL(i)) return i; return expr.size(); }
static void find_and_change (char* in, int len) { // just to avoid underflows.. len can't be < then len(padding). if (!in || len < 1) { return; } char *end; RFindCTX ctx = {0}; end = in + len; // type = TYPE_NONE; for (ctx.linebegin = in; in < end; ++in) { if (*in == '\n' || !*in) { if (ctx.type == TYPE_SYM && ctx.linecount < 1) { ctx.linecount++; ctx.linebegin = in + 1; continue; } if (ctx.type != TYPE_NONE && ctx.right && ctx.left && ctx.rightlen > 0 && ctx.leftlen > 0) { char* copy = NULL; if (ctx.leftlen > ctx.rightlen) { copy = (char*) malloc (ctx.leftlen); if (copy) { memcpy (copy, ctx.left, ctx.leftlen); memcpy (ctx.left, ctx.right, ctx.rightlen); memmove (ctx.comment - ctx.leftlen + ctx.rightlen, ctx.comment, ctx.right - ctx.comment); memcpy (ctx.right - ctx.leftlen + ctx.rightlen, copy, ctx.leftlen); } } else if (ctx.leftlen < ctx.rightlen) { if (ctx.linecount < 1) { copy = (char*) malloc (ctx.rightlen); if (copy) { memcpy (copy, ctx.right, ctx.rightlen); memcpy (ctx.right + ctx.rightlen - ctx.leftlen, ctx.left, ctx.leftlen); memmove (ctx.comment + ctx.rightlen - ctx.leftlen, ctx.comment, ctx.right - ctx.comment); memcpy (ctx.left + ctx.rightlen - ctx.leftlen, copy, ctx.rightlen); } } else { // copy = (char*) malloc (ctx.linebegin - ctx.left); // if (copy) { // memcpy (copy, ctx.left, ctx.linebegin - ctx.left); memset (ctx.right - ctx.leftpos, ' ', ctx.leftpos); *(ctx.right - ctx.leftpos - 1) = '\n'; // memcpy (ctx.comment + 3, copy, ctx.linebegin - ctx.left); memset (ctx.left, ' ', ctx.leftlen); memset (ctx.linebegin - ctx.leftlen, ' ', ctx.leftlen); // } } } else if (ctx.leftlen == ctx.rightlen) { copy = (char*) malloc (ctx.leftlen); if (copy) { memcpy (copy, ctx.right, ctx.leftlen); memcpy (ctx.right, ctx.left, ctx.leftlen); memcpy (ctx.left, copy, ctx.leftlen); } } free (copy); } memset (&ctx, 0, sizeof (ctx)); ctx.linebegin = in + 1; } else if (!ctx.comment && *in == ';' && in[1] == ' ') { ctx.comment = in - 1; ctx.comment[1] = '/'; ctx.comment[2] = '/'; while (!IS_WHITESPACE (*(ctx.comment - ctx.commentcolor))) { ctx.commentcolor++; } ctx.commentcolor--; } else if (!ctx.comment && ctx.type == TYPE_NONE) { if (IS_STRING (in, ctx)) { ctx.type = TYPE_STR; ctx.left = in; while (!IS_WHITESPACE (*(ctx.left - ctx.leftcolor))) { ctx.leftcolor++; } ctx.leftcolor--; ctx.leftpos = ctx.left - ctx.linebegin; } else if (IS_SYMBOL (in, ctx)) { ctx.type = TYPE_SYM; ctx.left = in; while (!IS_WHITESPACE (*(ctx.left - ctx.leftcolor))) { ctx.leftcolor++; } ctx.leftcolor--; ctx.leftpos = ctx.left - ctx.linebegin; } } else if (ctx.type == TYPE_STR) { if (!ctx.leftlen && ctx.left && IS_WHITESPACE (*in)) { ctx.leftlen = in - ctx.left; } else if (ctx.comment && *in == '"' && in[-1] != '\\') { if (!ctx.right) { ctx.right = in; while (!IS_WHITESPACE (*(ctx.right - ctx.rightcolor))) { ctx.rightcolor++; } ctx.rightcolor--; } else { ctx.rightlen = in - ctx.right + 1; } } } else if (ctx.type == TYPE_SYM) { if (!ctx.leftlen && ctx.left && IS_WHITESPACE (*in)) { ctx.leftlen = in - ctx.left + 3; } else if (ctx.comment && *in == '(' && IS_ALPHA (in[-1]) && !ctx.right) { // ok so i've found a function written in this way: // type = [const|void|int|float|double|short|long] // type fcn_name (type arg1, type arg2, ...) // right now 'in' points at '(', but the function name is before, so i'll go back // till a space is found // 'int print(const char*, ...)' // ^ ctx.right = in - 1; while (IS_ALPHA (*ctx.right) || *ctx.right == '_' || *ctx.right == '*') { ctx.right--; } // 'int print(const char*, ...)' // ^ // right now 'in' points at ' ' before 'p' , but there can be a return value // like 'int' in 'int print(const char*, ...)'. // so to find for example 'int' we have to go back till a space is found. // if a non alpha is found, then we can cut from the function name if (*ctx.right == ' ') { ctx.right--; while (IS_ALPHA (*ctx.right) || *ctx.right == '_' || *ctx.right == '*') { ctx.right--; } // moving forward since it points now to non alpha. ctx.right++; } while (!IS_WHITESPACE (*(ctx.right - ctx.rightcolor))) { ctx.rightcolor++; } ctx.rightcolor--; } else if (ctx.comment && *in == ')' && in[1] != '\'') { ctx.rightlen = in - ctx.right + 1; } } } }
int symbolp(int addr){ if(IS_SYMBOL(addr)) return(1); else return(0); }
int atomp(int addr){ if((IS_NUMBER(addr)) || (IS_SYMBOL(addr))) return(1); else return(0); }
int symbolp(int x){ if(IS_SYMBOL(x)) return(1); else return(0); }