Exemplo n.º 1
0
bool string_equals(caValue* left, caValue* right)
{
    if (!is_string(right))
        return false;

    // Shortcut, check if objects are the same.
    if (left->value_data.ptr == right->value_data.ptr)
        return true;

    if (left->value_data.ptr == NULL)
        return false;

    StringData* leftData = (StringData*) left->value_data.ptr;
    StringData* rightData = (StringData*) right->value_data.ptr;

    for (int i=0;; i++) {
        if (leftData->str[i] != rightData->str[i])
            return false;
        if (leftData->str[i] == 0)
            break;
    }

    // Strings are equal. Sneakily have both values reference the same data.
    // Prefer to preserve the one that has more references.
    if (leftData->refCount >= rightData->refCount)
        string_copy(NULL, right, left);
    else
        string_copy(NULL, left, right);


    return true;
}
Exemplo n.º 2
0
/* Parse and emit initializer code for target variable in statements such as
 * int b[] = {0, 1, 2, 3}. Generate a series of assignment operations on
 * references to target variable.
 */
static struct block *initializer(struct block *block, struct var target)
{
    assert(target.kind == DIRECT);

    /* Do not care about cv-qualifiers here. */
    target.type = unwrapped(target.type);

    if (peek().token == '{') {
        block = object_initializer(block, target);
    } else {
        block = assignment_expression(block);
        if (!target.symbol->depth && block->expr.kind != IMMEDIATE) {
            error("Initializer must be computable at load time.");
            exit(1);
        }
        if (target.kind == DIRECT && !target.type->size) {
            assert(!target.offset);
            assert(is_string(block->expr));
            assert(is_array(block->expr.type));

            /* Complete type based on string literal. Evaluation does not have
             * the required context to do this logic. */
            ((struct symbol *) target.symbol)->type.size =
                block->expr.type->size;
            target.type = block->expr.type;
        }
        eval_assign(block, target, block->expr);
    }

    return block;
}
Exemplo n.º 3
0
caValue* selector_advance(caValue* value, caValue* selectorElement, caValue* error)
{
    if (is_int(selectorElement)) {
        int selectorIndex = as_int(selectorElement);

        if (!is_list(value)) {
            set_error_string(error, "Value is not indexable: ");
            string_append_quoted(error, value);
            return NULL;
        }

        if (selectorIndex >= list_length(value)) {
            set_error_string(error, "Index ");
            string_append(error, selectorIndex);
            string_append(error, " is out of range");
            return NULL;
        }

        return get_index(value, selectorIndex);
    }
    else if (is_string(selectorElement)) {
        caValue* field = get_field(value, as_cstring(selectorElement));
        if (field == NULL) {
            set_error_string(error, "Field not found: ");
            string_append(error, selectorElement);
            return NULL;
        }
        return field;
    } else {
        set_error_string(error, "Unrecognized selector element: ");
        string_append_quoted(error, selectorElement);
        return NULL;
    }
}
Exemplo n.º 4
0
Arquivo: eval.c Projeto: kbob/schetoo
static inline bool is_self_evaluating(obj_t expr)
{
    return is_boolean(expr)   ||
	   is_fixnum(expr)    ||
	   is_character(expr) ||
	   is_string(expr)    ;
}
Exemplo n.º 5
0
void pwn::postfix_writer::do_print_node(pwn::print_node * const node, int lvl) {
  CHECK_TYPES(_compiler, _symtab, node);
  node->argument()->accept(this, lvl); // determine the value to print
  if (is_int(node->argument()->type())) {
    _pf.CALL("printi");
    _pf.TRASH(4); // delete the printed value
    std::string *f_name = new std::string("printi");
    _declared_functions.push_back(f_name);
    _called_functions.push_back(f_name);
  }
  else if (is_string(node->argument()->type())) {
    _pf.CALL("prints");
    _pf.TRASH(4); // delete the printed value's address
    std::string *f_name = new std::string("prints");
    _declared_functions.push_back(f_name);
    _called_functions.push_back(f_name);
  } else if (is_double(node->argument()->type())) {
    _pf.CALL("printd");
    _pf.TRASH(8); // delete double printed value
    std::string *f_name = new std::string("printd");
    _declared_functions.push_back(f_name);
    _called_functions.push_back(f_name);
  } else {
    std::cerr << "ERROR: Trying to print invalid type." << std::endl;
    exit(1);
  }
  if (node->new_line()) {
    _pf.CALL("println"); // print a newline
    std::string *f_name = new std::string("println");
    _declared_functions.push_back(f_name);
    _called_functions.push_back(f_name);
  }
}
Exemplo n.º 6
0
void block_update_state_type(Block* block)
{
    if (!block_state_type_is_out_of_date(block))
        return;

    // Recreate the state type
    Type* type = create_compound_type();

    // TODO: give this new type a nice name

    for (int i=0; i < block->length(); i++) {
        Term* term = block->get(i);
        if (term == NULL)
            continue;

        if (term->function != FUNCS.unpack_state || FUNCS.unpack_state == NULL)
            continue;

        Term* identifyingTerm = term->input(1);

        caValue* fieldName = get_unique_name(identifyingTerm);
        ca_assert(is_string(fieldName));
        ca_assert(!string_eq(fieldName, ""));

        compound_type_append_field(type, declared_type(term), as_cstring(fieldName));
    }

    block->stateType = type;
    block_remove_property(block, sym_DirtyStateType);

    // Might need to update any existing pack_state calls.
    block_update_pack_state_calls(block);
}
Exemplo n.º 7
0
// Check if a string is numeric
int is_numeric(char *s) {
    if (is_string(s) == 0 && isdigit(*s) != 0) {
        return 0;
    } else {
        return -1;
    }
}
Exemplo n.º 8
0
fint stringTableEntry::length() {
  if (is_string()) return 1;
  if (!get_link()) return 0;
  fint count = 0;
  for (stringTableLink* l = get_link(); l; l = l->next) count ++;
  return count;
}
Exemplo n.º 9
0
void clean_string_flag(char* clean_string_line) {
    if (is_string(clean_string_line)) {
        *(clean_string_line+strlen(clean_string_line)-1)='\0';
        memcpy(clean_string_line,clean_string_line+1,strlen(clean_string_line)-1);
        *(clean_string_line+strlen(clean_string_line)-1)='\0';
    }
}
Exemplo n.º 10
0
	void Printer::print(LispObjRef obj) {
		if (is_nil(obj))
			output_ << "NIL";
		else if (is_fixnum(obj))
			output_ << get_ctype<FixnumType>(obj); // (CFixnum)(boost::get<FixnumType>(*obj));
		else if (is_floatnum(obj))
			output_ <<  get_ctype<FloatnumType>(obj); //(CFloatnum)(boost::get<FloatnumType>(*obj));
		else if (is_string(obj))
			output_ << "\"" << get_ctype<StringType>(obj) << "\""; // ""(CString)(boost::get<StringType>(*obj)) << "\""; 
		else if (is_symbol(obj))
			output_ << get_ctype<SymbolType>(obj).name; // static_cast<LispSymbol>(boost::get<SymbolType>(*obj)).first;
		else if (is_cons(obj)) {
			output_ << "(";
			print_cons(obj);
			output_ << ")";			
		} else if (is_char(obj)) {
			CChar c = get_ctype<CharType>(obj);
			if (isprint(c)) {
				output_ << c;				
			} else {
				output_ << "#" << std::hex << (int) c << std::dec;
			}
		}
		else
			output_ << "#UNPRINTABLE#";
	}
Exemplo n.º 11
0
bool MCVariableValue::coerce_to_string(MCExecPoint& ep)
{
	assert(!is_string());

	if (is_undefined())
	{
		assign_empty();
		return true;
	}

	if (is_number())
	{
		uint32_t t_length;
		t_length = MCU_r8tos(strnum . buffer . data, strnum . buffer . size, strnum . nvalue, ep . getnffw(), ep . getnftrailing(), ep . getnfforce());

		strnum . svalue . string = strnum . buffer . data;
		strnum . svalue . length = t_length;

		set_type(VF_BOTH);
		return true;
	}

	assign_empty();
	return true;
}
Exemplo n.º 12
0
Exec_stat MCVariableValue::fetch(MCExecPoint& ep, bool p_copy)
{
	switch(get_type())
	{
	case VF_UNDEFINED:
		ep . setboth("", 0.0);
	break;

	case VF_STRING:
		assert(is_string());
		ep . setsvalue(get_string());
		if (p_copy)
			ep . grabsvalue();
	break;

	case VF_NUMBER:
		ep . setnvalue(get_real());
	break;

	case VF_BOTH:
		ep . setboth(get_string(), get_real());
		if (p_copy)
			ep . grabsvalue();
	break;

	case VF_ARRAY:
		if (!p_copy)
			ep . setarray(this, False);
		else
			ep . setarray(new MCVariableValue(*this), True);
	break;
	}

	return ES_NORMAL;
}
Exemplo n.º 13
0
void test_equals_function(caValue* a, std::string b,
        const char* aText, const char* bText,
        int line, const char* file)
{
    return test_equals_function(is_string(a) ? as_string(a) : to_string(a),
            b, aText, bText, line, file);
}
Exemplo n.º 14
0
Type* get_field_specializeType(Term* caller)
{
    Type* head = caller->input(0)->type;

    for (int nameIndex=1; nameIndex < caller->numInputs(); nameIndex++) {

        // Abort if input type is not correct
        if (!is_string(term_value(caller->input(1))))
            return TYPES.any;

        if (!is_list_based_type(head))
            return TYPES.any;

        Value* name = term_value(caller->input(1));

        int fieldIndex = list_find_field_index_by_name(head, name);

        if (fieldIndex == -1)
            return TYPES.any;

        head = as_type(get_index(list_get_type_list_from_type(head),fieldIndex));
    }

    return head;
}
Exemplo n.º 15
0
int parse_line (char *line, int line_length)
{
	int i, ret, buffer_index;
	char buffer[32];	
	
	memset(buffer, 0, 32);
	buffer_index = ret = 0;
	
	printf("%s", line);
	
	for (i = 0; i < line_length; i++) {		
		if (isspace(line[i]) && !buffer_index)
			continue;
		else if (line[i] == '#') {
			ret = DIRECTIVE_FOUND;
			break;
		} else if (isspace(line[i])) {
			if (is_string(buffer)) 			
				return parse_string(line, i, line_length, 1);
			else if (is_variable(buffer[0]) && find_assign(line, i, line_length)) 
				return parse_string(line, i-1, line_length, 0);				
			else {
				memset(buffer, 0, 32);
				buffer_index = 0;
			}
		} else {
			buffer[buffer_index++] = line[i];
			if (check_overflow(buffer_index, 32))
				return OVERFLOW_DETECTED;
		}
	}
	fwrite(line, strlen(line), sizeof(char), tmp);
	
	return ret;
}
Exemplo n.º 16
0
pointer scm_load_ext(scheme *sc, pointer args)
{
  pointer first_arg;
  pointer retval;
  char filename[MAXPATHLEN], init_fn[MAXPATHLEN+6];
  char *name;
  HMODULE dll_handle;
  void (*module_init)(scheme *sc);

  if ((args != sc->NIL) && is_string((first_arg = pair_car(args)))) {
    name = string_value(first_arg);
    make_filename(name, filename);
    make_init_fn(name, init_fn);
    dll_handle = dl_attach(filename);
    if (dll_handle == 0) {
      retval = sc -> F;
    }
    else {
      module_init = (void(*)(scheme *))dl_proc(dll_handle, init_fn);
      if (module_init != 0) {
        (*module_init)(sc);
        retval = sc -> T;
      }
      else {
        retval = sc->F;
      }
    }
  }
  else {
    retval = sc -> F;
  }

  return(retval);
}
Exemplo n.º 17
0
term_t bif_read_file_info0_1(term_t Filename, process_t *ctx)
{
	apr_status_t rs;
	apr_finfo_t fi;
	const char *file_name;
	apr_pool_t *tmp;
	apr_uint32_t wanted;

	if (!is_string(Filename))
		return A_BADARG;

	apr_pool_create(&tmp, 0);
	file_name = ltoz(Filename, tmp);

	wanted = APR_FINFO_MTIME | APR_FINFO_CTIME | APR_FINFO_ATIME |
		 APR_FINFO_NAME | APR_FINFO_SIZE | APR_FINFO_TYPE |
		 APR_FINFO_USER | APR_FINFO_GROUP | APR_FINFO_PROT;

	rs = apr_stat(&fi, file_name, wanted, tmp);
	if (rs == 0 || APR_STATUS_IS_INCOMPLETE(rs))
	{
		term_t finfo = make_file_info(&fi, proc_gc_pool(ctx));
		result(finfo);
	}
	else
		result(make_tuple2(A_ERROR, decipher_status(rs), proc_gc_pool(ctx)));

	apr_pool_destroy(tmp);
	return AI_OK;
}
Exemplo n.º 18
0
	std::string NetGameEventValue::get_string() const
	{
		if (is_string())
			return value_string;
		else
			throw Exception("NetGameEventValue is not a string");
	}
Exemplo n.º 19
0
/* calculate approximate length of a printed term.  For space alloc. */
DWORD clenpterm(prolog_term term)
{
  int i, clen;

  if (is_var(term)) return 11;
  else if (is_int(term)) return 12;
  else if (is_float(term)) return 12;
  else if (is_nil(term)) return 2;
  else if (is_string(term)) return strlen(p2c_string(term))+5;
  else if (is_list(term)) {
      clen = 1;
      clen += clenpterm(p2p_car(term)) + 1;
      while (is_list(term)) {
          clen += clenpterm(p2p_car(term)) + 1;
          term = p2p_cdr(term);
      }
      if (!is_nil(term)) {
          clen += clenpterm(term) + 1;
      }
      return clen+1;
  } else if (is_functor(term)) {
      clen = strlen(p2c_functor(term))+5;
      if (p2c_arity(term) > 0) {
          clen += clenpterm(p2p_arg(term,1)) + 1;
          for (i = 2; i <= p2c_arity(term); i++) {
              clen += clenpterm(p2p_arg(term,i)) + 1;
          }
          return clen + 1;
      } else return clen;
  } else {
      fprintf(stderr,"error, unrecognized type");
      return 0;
  }
}
Exemplo n.º 20
0
void pwn::postfix_writer::do_function_call_node(pwn::function_call_node * const node, int lvl) {
  CHECK_TYPES(_compiler, _symtab, node);

  std::string id = fix_id(node->name());
  std::shared_ptr<pwn::symbol> symbol = _symtab.find(id);
  
  // put arguments in stack (reverse order)
  int args_size = 0;
  if (node->arguments()) {
    for (int i = node->arguments()->size() - 1; i >= 0; i--) {
      node->arguments()->node(i)->accept(this, lvl);
      args_size += ((cdk::expression_node *) node->arguments()->node(i))->type()->size();
    }
  }
  
  // call function
  _pf.CALL(id);
  
  // remove arguments from stack
  _pf.TRASH(args_size);
  
  // put return value in the stack
  if (is_int(symbol->type()) || is_pointer(symbol->type()) || is_string(symbol->type())) {
    _pf.PUSH();
  } else if (is_double(symbol->type())) {
    _pf.DPUSH();
  }
  
  // add this function to called functions
  _called_functions.push_back(new std::string(id));
}
Exemplo n.º 21
0
R_API RAnalData *r_anal_data(RAnal *anal, ut64 addr, const ut8 *buf, int size) {
	ut64 dst = 0;
	int n, nsize = 0;
	int bits = anal->bits;
	int word = R_MIN (8, bits / 8);

	if (size < 4)
		return NULL;
	if (size >= word && is_invalid (buf, word))
		return r_anal_data_new (addr, R_ANAL_DATA_TYPE_INVALID,
					-1, buf, word);
	{
		int i, len = R_MIN (size, 64);
		int is_pattern = 0;
		int is_sequence = 0;
		char ch = buf[0];
		char ch2 = ch + 1;
		for (i = 1; i < len; i++) {
			if (ch2 == buf[i]) {
				ch2++;
				is_sequence++;
			} else is_sequence = 0;
			if (ch == buf[i]) {
				is_pattern++;
			}
		}
		if (is_sequence > len - 2) {
			return r_anal_data_new (addr, R_ANAL_DATA_TYPE_SEQUENCE, -1,
						buf, is_sequence);
		}
		if (is_pattern > len - 2) {
			return r_anal_data_new (addr, R_ANAL_DATA_TYPE_PATTERN, -1,
						buf, is_pattern);
		}
	}
	if (size >= word && is_null (buf, word))
		return r_anal_data_new (addr, R_ANAL_DATA_TYPE_NULL,
					-1, buf, word);
	if (is_bin (buf, size))
		return r_anal_data_new (addr, R_ANAL_DATA_TYPE_HEADER, -1,
					buf, word);
	if (size >= word) {
		dst = is_pointer (anal, buf, word);
		if (dst) return r_anal_data_new (addr,
						R_ANAL_DATA_TYPE_POINTER, dst, buf, word);
	}
	switch (is_string (buf, size, &nsize)) {
	case 1: return r_anal_data_new_string (addr, (const char *)buf,
					nsize, R_ANAL_DATA_TYPE_STRING);
	case 2: return r_anal_data_new_string (addr, (const char *)buf,
					nsize, R_ANAL_DATA_TYPE_WIDE_STRING);
	}
	if (size >= word) {
		n = is_number (buf, word);
		if (n) return r_anal_data_new (addr, R_ANAL_DATA_TYPE_NUMBER,
					n, buf, word);
	}
	return r_anal_data_new (addr, R_ANAL_DATA_TYPE_UNKNOWN, dst,
				buf, R_MIN (word, size));
}
Exemplo n.º 22
0
int test_string()
{
   string *s = new_string("hello!");
   assert(is_string(s));
   assert(strcmp(car(s), "hello!") == 0);
   return 1;
}
Exemplo n.º 23
0
Arquivo: print.cpp Projeto: shaurz/amp
void print(Value x)
{
	if (is_nil(x))
		prints("nil");
	else if (is_eof(x))
		printf("#eof");
	else if (is_fixnum(x))
		printf("%d", as_fixnum(x));
	else if (is_bool(x))
		printf("%s", as_bool(x) ? "true" : "false");
	else if (is_char(x))
		printf("'%c'", as_char(x));
	else if (is_pair(x))
		print_list(x);
	else if (is_symbol(x))
		prints(as_symbol(x)->value);
	else if (is_string(x))
		print_string(as_string(x));
	else if (is_procedure(x))
		printf("#<procedure %s>", as_procedure(x)->name->value);
	else if (is_module(x))
		printf("#<module>");
	else if (is_type(x))
		printf("#<type %s>", as_type(x)->name->value);
	else if (is_ptr(x))
		printf("#<object %p>", as_ptr(x));
	else if (is_undefined(x))
		printf("#undefined");
	else
		printf("#ufo");
}
Exemplo n.º 24
0
Arquivo: json.c Projeto: AmesianX/wine
/* ECMA-262 5.1 Edition    15.12.3 */
static HRESULT JSON_stringify(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
{
    stringify_ctx_t stringify_ctx = {ctx, NULL,0,0, NULL,0,0, {0}};
    HRESULT hres;

    TRACE("\n");

    if(argc >= 2 && is_object_instance(argv[1])) {
        FIXME("Replacer %s not yet supported\n", debugstr_jsval(argv[1]));
        return E_NOTIMPL;
    }

    if(argc >= 3) {
        jsval_t space_val;

        hres = maybe_to_primitive(ctx, argv[2], &space_val);
        if(FAILED(hres))
            return hres;

        if(is_number(space_val)) {
            double n = get_number(space_val);
            if(n >= 1) {
                int i, len;
                if(n > 10)
                    n = 10;
                len = floor(n);
                for(i=0; i < len; i++)
                    stringify_ctx.gap[i] = ' ';
                stringify_ctx.gap[len] = 0;
            }
        }else if(is_string(space_val)) {
            jsstr_t *space_str = get_string(space_val);
            size_t len = jsstr_length(space_str);
            if(len > 10)
                len = 10;
            jsstr_extract(space_str, 0, len, stringify_ctx.gap);
        }

        jsval_release(space_val);
    }

    hres = stringify(&stringify_ctx, argv[0]);
    if(SUCCEEDED(hres) && r) {
        assert(!stringify_ctx.stack_top);

        if(hres == S_OK) {
            jsstr_t *ret = jsstr_alloc_len(stringify_ctx.buf, stringify_ctx.buf_len);
            if(ret)
                *r = jsval_string(ret);
            else
                hres = E_OUTOFMEMORY;
        }else {
            *r = jsval_undefined();
        }
    }

    heap_free(stringify_ctx.buf);
    heap_free(stringify_ctx.stack);
    return hres;
}
Exemplo n.º 25
0
const char*
Term::name()
{
    if (!is_string(&nameValue))
        return "";
    return as_cstring(&nameValue);
}
Exemplo n.º 26
0
bool is_self_evaluating(object *exp) {
    return is_boolean(exp)   ||
           is_fixnum(exp)    ||
           is_character(exp) ||
           is_empty(exp)     ||
           is_string(exp);
}
Exemplo n.º 27
0
Arquivo: heap.c Projeto: MrAiki/LL1LL
/* GCのマークフェーズ */
static void gc_mark(void)
{
  int i;
  /* スタックトップと, スタックを指すポインタを取得 */
  int stack_top        = getStackTop();  
  LL1LL_Value *stack_p = getStackPointer(); 
  LL1LL_Object *pos;

  /* 全マークをリセット */
  for (pos = heap_head; pos != NULL; pos = pos->next)
    pos->marked = LL1LL_FALSE;

  /* スタック(参照できるオブジェクト)を走査 */
  for (i = 0; i < stack_top; i++) {
    if (stack_p[i].type != LL1LL_OBJECT_TYPE) { 
      /* オブジェクトでないなら次へ */
      continue;
    } else {
      /* マークを付ける */
      if (is_string(stack_p[i])) {
        stack_p[i].u.object->marked = LL1LL_TRUE;
      } else {
        /* should be ARRAY_OBJECT here */
        array_mark(stack_p[i].u.object);  /* 配列マークのルーチンへ */
      }
    }
  }

}
Exemplo n.º 28
0
R_API RAnalData *r_anal_data (RAnal *anal, ut64 addr, const ut8 *buf, int size) {
	ut64 dst = 0;
	int n, nsize = 0;
	int bits = anal->bits;
	int endi = !anal->big_endian;
	int word = R_MIN (8, bits/8);

	if (size >= word && is_invalid (buf, word))
		return r_anal_data_new (addr, R_ANAL_DATA_TYPE_INVALID,
			-1, buf, word);
	if (size >= word && is_null (buf, word))
		return r_anal_data_new (addr, R_ANAL_DATA_TYPE_NULL,
			0, buf, word);
	if (is_bin (buf, size))
		return r_anal_data_new (addr, R_ANAL_DATA_TYPE_HEADER, -1,
				buf, word);
	if (size>=word) {
		dst = is_pointer (&anal->iob, buf, endi, word);
		if (dst) return r_anal_data_new (addr,
			R_ANAL_DATA_TYPE_POINTER, dst, buf, word);
	}
	switch (is_string (buf, size, &nsize)) {
	case 1: return r_anal_data_new_string (addr, (const char *)buf,
		nsize, R_ANAL_DATA_TYPE_STRING);
	case 2: return r_anal_data_new_string (addr, (const char *)buf,
		nsize, R_ANAL_DATA_TYPE_WIDE_STRING);
	}
	if (size >= word) {
		n = is_number (buf, endi, word);
		if (n) return r_anal_data_new (addr, R_ANAL_DATA_TYPE_NUMBER,
				n, buf, word);
	}
	return r_anal_data_new (addr, R_ANAL_DATA_TYPE_UNKNOWN, dst,
		buf, R_MIN(word, size));
}
Exemplo n.º 29
0
void dll_loading_check_for_patches_on_loaded_branch(Branch* branch)
{
    for (BranchIteratorFlat it(branch); it.unfinished(); it.advance()) {
        if (it.current()->function == FUNCS.dll_patch) {
            Term* caller = it.current();

            // Find the DLL.
            String filename;
            find_dll_for_script(branch, &filename);

            if (!is_string(&filename)) {
                mark_static_error(caller, "Couldn't find DLL");
                continue;
            }

            Value error;
            patch_with_dll(as_cstring(&filename), branch, &error);

            if (!is_null(&error)) {
                std::cout << as_string(&error) << std::endl;

                mark_static_error(caller, &error);
                continue;
            }
        }
    }
}
Exemplo n.º 30
0
 void ConfUnit::set_string(const std::string& str) {
     if (is_string()) {
         _string = str;
     }
     else {
         throw std::exception();
     }
 }