Пример #1
0
LValue *l_func_str_add(LValue *args, LClosure *closure) {
  LValue *v1 = l_list_get(args, 0);
  LValue *v2 = l_list_get(args, 1);
  LValue *value = l_value_new(L_STR_TYPE, closure);
  value->core.str = make_stringbuf("");
  concat_stringbuf(value->core.str, v1->core.str->str);
  concat_stringbuf(value->core.str, v2->core.str->str);
  return value;
}
Пример #2
0
LValue *l_eval_string_node(LNode *node, LClosure *closure) {
  LValue *value = l_value_new(L_STR_TYPE, closure);
  if(strchr(node->val, '\\')) {
    value->core.str = make_stringbuf("");
    int i, len = strlen(node->val);
    char c[] = " ";
    for(i=0; i<len; i++) {
      if(node->val[i] == '\\' && i < len-1) {
        i++;
        switch(node->val[i]) {
          case 'a' : c[0] = '\a'; break;
          case 'b' : c[0] = '\b'; break;
          case 'f' : c[0] = '\f'; break;
          case 'n' : c[0] = '\n'; break;
          case 'r' : c[0] = '\r'; break;
          case 't' : c[0] = '\t'; break;
          case 'v' : c[0] = '\v'; break;
          case '\'': c[0] = '\''; break;
          case '"' : c[0] = '"' ; break;
          case '\\': c[0] = '\\'; break;
          case '?' : c[0] = '?' ; break;
        }
      } else {
        c[0] = node->val[i];
      }
      concat_stringbuf(value->core.str, c);
    }
  } else {
    value->core.str = make_stringbuf(node->val);
  }
  return value;
}
Пример #3
0
LValue *l_func_str(LValue *args, LClosure *closure) {
  LValue *value = l_value_new(L_STR_TYPE, closure);
  value->core.str = make_stringbuf("");
  char *s;
  int i;
  for(i=0; i<args->core.list->length; i++) {
    s = l_str(l_list_get(args, i));
    concat_stringbuf(value->core.str, s);
  }
  return value;
}
Пример #4
0
// returns a c string representation for the given LValue
// (be sure to free the string when you're done)
char *l_str(LValue *value) {
  char *str;
  stringbuf *str2;
  switch(value->type) {
    case L_NUM_TYPE:
      str = mpz_get_str(NULL, 10, value->core.num);
      break;
    case L_STR_TYPE:
      str = GC_MALLOC(sizeof(char) * (value->core.str->length + 1));
      strcpy(str, value->core.str->str);
      break;
    case L_LIST_TYPE:
      str2 = make_stringbuf("[");
      char *s;
      int i, len = value->core.list->length;
      for(i=0; i<len; i++) {
        s = l_str(l_list_get(value, i));
        concat_stringbuf(str2, s);
        if(i<len-1) buffer_concat(str2, " ");
      }
      buffer_concat(str2, "]");
      str = GC_MALLOC(sizeof(char) * (str2->length + 1));
      strcpy(str, str2->str);
      destroy_buffer(str2);
      break;
    case L_TRUE_TYPE:
      str = GC_MALLOC(sizeof(char) * 5);
      strcpy(str, "true");
      break;
    case L_FALSE_TYPE:
      str = GC_MALLOC(sizeof(char) * 6);
      strcpy(str, "false");
      break;
    case L_NIL_TYPE:
      str = GC_MALLOC(sizeof(char) * 4);
      strcpy(str, "nil");
      break;
    default:
      str = GC_MALLOC(sizeof(char) * 1);
      strcpy(str, "");
  }
  return str;
}