Skip to content

Commit

Permalink
support _Bool.
Browse files Browse the repository at this point in the history
  • Loading branch information
activesys committed Dec 20, 2013
1 parent 513de80 commit 5f6ada2
Show file tree
Hide file tree
Showing 23 changed files with 779 additions and 69 deletions.
4 changes: 4 additions & 0 deletions README.Win32
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ is libcstl unit testing execute file. You will need cmockery.lib
and google\*.h, the header files of cmockery libaray, in vc8/vc9
directory.

NOTE
-----------------------------------------
LIBCSTL WILL BE CANCEL MSVS SUPPORT IN FURTER VERSION,
BECAUSE THE MSVS DOES NOT SUPPORT C99 FULLY.
7 changes: 5 additions & 2 deletions cstl/cstl_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ extern "C" {
#include <ctype.h>
#include <float.h>
#include <time.h>
#ifndef _MSC_VER
# include <stdbool.h>
#endif

/** constant declaration and macro section **/
#ifdef _CSTL_UNIT_TESTING
Expand Down Expand Up @@ -73,13 +76,13 @@ extern "C" {
#define CSTL_MINOR_VERSION 3
#define CSTL_REVISION_VERSION 0

#ifdef _MSC_VER
/**
* for bool_t type
*/
#define FALSE 0 /* declaration false bool type */
#define false 0
#define TRUE 1 /* declaration true bool type */
#define true 1
#endif

/** data type declaration and struct, union, enum section **/
typedef unsigned char _byte_t;
Expand Down
9 changes: 9 additions & 0 deletions cstl/cstl_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,9 @@ extern void fun_less_equal_hash_multimap(const void* cpv_first, const void* cpv_
* @remarks The input and output must not be NULL, otherwise the behavior is undefined.
*/
extern void fun_logical_and_cstl_bool(const void* cpv_first, const void* cpv_second, void* pv_output);
#ifndef _MSC_VER
extern void fun_logical_and_bool(const void* cpv_first, const void* cpv_second, void* pv_output);
#endif

/**
* Logical or.
Expand All @@ -398,6 +401,9 @@ extern void fun_logical_and_cstl_bool(const void* cpv_first, const void* cpv_sec
* @remarks The input and output must not be NULL, otherwise the behavior is undefined.
*/
extern void fun_logical_or_cstl_bool(const void* cpv_first, const void* cpv_second, void* pv_output);
#ifndef _MSC_VER
extern void fun_logical_or_bool(const void* cpv_first, const void* cpv_second, void* pv_output);
#endif

/**
* Logical not.
Expand All @@ -407,6 +413,9 @@ extern void fun_logical_or_cstl_bool(const void* cpv_first, const void* cpv_seco
* @remarks The input and output must not be NULL, otherwise the behavior is undefined.
*/
extern void fun_logical_not_cstl_bool(const void* cpv_input, void* pv_output);
#ifndef _MSC_VER
extern void fun_logical_not_bool(const void* cpv_input, void* pv_output);
#endif

/**
* Random number.
Expand Down
1 change: 1 addition & 0 deletions cstl/cstl_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ extern "C" {
#define _POINTER_TYPE "void*"

#define _CSTL_BOOL_TYPE "bool_t"
#define _BOOL_TYPE "_Bool"
/* cstl type */
#define _VECTOR_TYPE "vector_t"
#define _LIST_TYPE "list_t"
Expand Down
17 changes: 9 additions & 8 deletions doc/project/libcstl.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ space : ' ' | '\t' | '\v' | '\f' | '\r' | '\n'

keyword:
void
char, int, short, long, float, double, signed, unsigned, bool_t,
char, int, short, long, float, double, signed, unsigned, bool_t, _Bool
struct, enum, union,
vector_t, list_t, slist_t, deque_t, stack_t, queue_t, priority_queue_t,
set_t, map_t, multiset_t, multimap_t,
Expand Down Expand Up @@ -65,7 +65,7 @@ C_BUILTIN -> char | signed char |
long double |
char* |
void* |
bool_t
bool_t | _Bool
USER_DEFINE -> USER_DEFINE_TYPE identifier | identifier
USER_DEFINE_TYPE -> struct | enum | union
CSTL_BUILTIN -> SEQUENCE | RELATION | string_t | ITERATOR
Expand All @@ -81,7 +81,7 @@ RELATION -> RELATION_NAME < TYPE_DESCRIPT , TYPE_DESCRIPT >
RELATION_NAME -> map_t | multimap_t | hash_map_t | hash_multimap_t | pair_t
========================================================================
C_BUILTIN -> SIMPLE_BUILTIN | SIGNED_BUILTIN | UNSIGNED_BUILTIN | POINTER_BUILTIN
SIMPLE_BUILTIN -> char | short | short int | int | long | long int | bool_t
SIMPLE_BUILTIN -> char | short | short int | int | long | long int | bool_t | _Bool
float | double | long double
SIGNED_BUILTIN -> signed char |
signed short | signed short int |
Expand All @@ -97,7 +97,7 @@ POINTER_BUILTIN -> char * | void *
TYPE_DESCRIPT -> C_BUILTIN | USER_DEFINE | CSTL_BUILTIN

C_BUILTIN -> SIMPLE_BUILTIN | SIGNED_BUILTIN | UNSIGNED_BUILTIN | POINTER_BUILTIN
SIMPLE_BUILTIN -> char POINTER_SUFFIX | short COMMON_SUFFIX | int | long SIMPLE_LONG_SUFFIX | float | double | bool_t
SIMPLE_BUILTIN -> char POINTER_SUFFIX | short COMMON_SUFFIX | int | long SIMPLE_LONG_SUFFIX | float | double | bool_t | _Bool
SIGNED_BUILTIN -> signed COMPLEX_SUFFIX
UNSIGNED_BUILTIN -> unsigned COMPLEX_SUFFIX
POINTER_BUILTIN -> void *
Expand All @@ -121,7 +121,7 @@ SEQUENCE_NAME -> vector_t | list_t | slist_t | deque_t | queue_t | stack_t | pri
RELATION -> RELATION_NAME < TYPE_DESCRIPT , TYPE_DESCRIPT >
RELATION_NAME -> map_t | multimap_t | hash_map_t | hash_multimap_t | pair_t
---------------------------------------------------------------------------
first(TYPE_DESCRIPT) = {char, short, int, long, float, double, signed, unsigned, void, bool_t,
first(TYPE_DESCRIPT) = {char, short, int, long, float, double, signed, unsigned, void, bool_t, _Bool,
struct, enum, union, identifier,
vector_t, list_t, slist_t, deque_t, queue_t, stack_t, priority_queue_t,
set_t, map_t, multiset_t, multimap_t, hash_set_t, hash_map_t, hash_multiset_t,
Expand All @@ -132,8 +132,8 @@ first(TYPE_DESCRIPT) = {char, short, int, long, float, double, signed, unsigned,
string_iterator_t,
input_iterator_t, output_iterator_t, forward_iterator_t, bidirectional_iterator_t,
random_access_iterator_t}
first(C_BUILTIN) = {char, short, int, long, float, double, signed, unsigned, void, bool_t}
first(SIMPLE_BUILTIN) = {char, short, int, long, float, double, bool_t}
first(C_BUILTIN) = {char, short, int, long, float, double, signed, unsigned, void, bool_t, _Bool}
first(SIMPLE_BUILTIN) = {char, short, int, long, float, double, bool_t, _Bool}
first(SIGNED_BUILTIN) = {signed}
first(UNSIGNED_BUILTIN) = {unsigned}
first(POINTER_BUILTIN) = {void}
Expand Down Expand Up @@ -203,7 +203,7 @@ select(TYPE_DESCRIPT -> CSTL_BUILTIN) = {vector_t, list_t, slist_t, deque_t, que
input_iterator_t, output_iterator_t, forward_iterator_t, bidirectional_iterator_t,
random_access_iterator_t}

select(C_BUILTIN -> SIMPLE_BUILTIN) = {char, short, int, long, float, double, bool_t}
select(C_BUILTIN -> SIMPLE_BUILTIN) = {char, short, int, long, float, double, bool_t, _Bool}
select(C_BUILTIN -> SIGNED_BUILTIN) = {signed}
select(C_BUILTIN -> UNSIGNED_BUILTIN) = {unsigned}
select(C_BUILTIN -> POINTER_BUILTIN) = {void}
Expand All @@ -215,6 +215,7 @@ select(SIMPLE_BUILTIN -> long SIMPLE_LONG_SUFFIX) = {long}
select(SIMPLE_BUILTIN -> float) = {float}
select(SIMPLE_BUILTIN -> double) = {double}
select(SIMPLE_BUILTIN -> bool_t) = {bool_t}
select(SIMPLE_BUILTIN -> _Bool) = {_Bool}

selcet(SIGNED_BUILTIN -> signed COMPLEX_SUFFIX) = {signed}
select(UNSIGNED_BUILTIN -> unsigned COMPLEX_SUFFIX) = {unsigned}
Expand Down
27 changes: 27 additions & 0 deletions src/cstl_function.c
Original file line number Diff line number Diff line change
Expand Up @@ -1837,6 +1837,15 @@ void fun_logical_and_cstl_bool(const void* cpv_first, const void* cpv_second, vo
*(bool_t*)pv_output = *(bool_t*)cpv_first && *(bool_t*)cpv_second;
}

#ifndef _MSC_VER
void fun_logical_and_bool(const void* cpv_first, const void* cpv_second, void* pv_output)
{
assert(cpv_first != NULL && cpv_second != NULL && pv_output != NULL);

*(bool_t*)pv_output = *(_Bool*)cpv_first && *(_Bool*)cpv_second;
}
#endif

/**
* Logical or.
*/
Expand All @@ -1847,6 +1856,15 @@ void fun_logical_or_cstl_bool(const void* cpv_first, const void* cpv_second, voi
*(bool_t*)pv_output = *(bool_t*)cpv_first || *(bool_t*)cpv_second;
}

#ifndef _MSC_VER
void fun_logical_or_bool(const void* cpv_first, const void* cpv_second, void* pv_output)
{
assert(cpv_first != NULL && cpv_second != NULL && pv_output != NULL);

*(bool_t*)pv_output = *(_Bool*)cpv_first || *(_Bool*)cpv_second;
}
#endif

/**
* Logical not.
*/
Expand All @@ -1857,6 +1875,15 @@ void fun_logical_not_cstl_bool(const void* cpv_input, void* pv_output)
*(bool_t*)pv_output = !*(bool_t*)cpv_input;
}

#ifndef _MSC_VER
void fun_logical_not_bool(const void* cpv_input, void* pv_output)
{
assert(cpv_input != NULL && pv_output != NULL);

*(bool_t*)pv_output = !*(_Bool*)cpv_input;
}
#endif

/**
* Random number.
*/
Expand Down
14 changes: 14 additions & 0 deletions src/cstl_function_private.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ ufun_t _fun_get_unary(iterator_t it_iter, fun_type_t ftype_type)
if (ftype_type == _LOGICAL_NOT_FUN) {
return fun_logical_not_cstl_bool;
}
#ifndef _MSC_VER
} else if (strncmp(s_typename, _BOOL_TYPE, _TYPE_NAME_SIZE) == 0) { /* _Bool */
if (ftype_type == _LOGICAL_NOT_FUN) {
return fun_logical_not_bool;
}
#endif
}

return fun_default_unary;
Expand Down Expand Up @@ -359,6 +365,14 @@ bfun_t _fun_get_binary(iterator_t it_iter, fun_type_t ftype_type)
case _LOGICAL_OR_FUN: return fun_logical_or_cstl_bool; break;
default: break;
}
#ifndef _MSC_VER
} else if (strncmp(s_typename, _BOOL_TYPE, _TYPE_NAME_SIZE) == 0) { /* _Bool */
switch (ftype_type) {
case _LOGICAL_AND_FUN: return fun_logical_and_bool; break;
case _LOGICAL_OR_FUN: return fun_logical_or_bool; break;
default: break;
}
#endif
} else if (strncmp(s_typename, _VECTOR_TYPE, _TYPE_NAME_SIZE) == 0) { /* vector_t */
switch (ftype_type) {
case _EQUAL_FUN: return fun_equal_vector; break;
Expand Down
6 changes: 6 additions & 0 deletions src/cstl_types.c
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,12 @@ void _type_get_varg_value(_typeinfo_t* pt_typeinfo, va_list val_elemlist, void*
assert(b_result);
memset(pv_output, 0x00, pt_typeinfo->_pt_type->_t_typesize);
}
#ifndef _MSC_VER
} else if (strncmp(pt_typeinfo->_pt_type->_s_typename, _BOOL_TYPE, _TYPE_NAME_SIZE) == 0) {
/* _Bool */
assert(pt_typeinfo->_pt_type->_t_typesize == sizeof(_Bool));
*(_Bool*)pv_output = va_arg(val_elemlist, int);
#endif
} else {
/* invalid c builtin style */
assert(false);
Expand Down
8 changes: 7 additions & 1 deletion src/cstl_types_aux.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* The implementation of cstl types auxiliary functions.
* Copyright (C) 2008 - 2012 Wangbo
* Copyright (C) 2008 - 2014 Wangbo
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -248,6 +248,12 @@ void _type_register_c_builtin(void)
_TYPE_REGISTER_TYPE(void*, _POINTER_TYPE, pointer, _TYPE_C_BUILTIN);
_TYPE_REGISTER_TYPE_NODE(void*, _POINTER_TYPE);

#ifndef _MSC_VER
/* register _Bool */
_TYPE_REGISTER_TYPE(_Bool, _BOOL_TYPE, bool, _TYPE_C_BUILTIN);
_TYPE_REGISTER_TYPE_NODE(_Bool, _BOOL_TYPE);
#endif

_TYPE_REGISTER_END();
}

Expand Down
28 changes: 28 additions & 0 deletions src/cstl_types_builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,34 @@ void _type_destroy_iterator(const void* cpv_input, void* pv_output)
_type_destroy_default(cpv_input, pv_output);
}

#ifndef _MSC_VER
/* _Bool */
void _type_init_bool(const void* cpv_input, void* pv_output)
{
assert(cpv_input != NULL && pv_output != NULL);
*(_Bool*)cpv_input = false;
*(bool_t*)pv_output = true;
}

void _type_copy_bool(const void* cpv_first, const void* cpv_second, void* pv_output)
{
assert(cpv_first != NULL && cpv_second != NULL && pv_output != NULL);
*(_Bool*)cpv_first = *(_Bool*)cpv_second;
*(bool_t*)pv_output = true;
}

void _type_less_bool(const void* cpv_first, const void* cpv_second, void* pv_output)
{
assert(cpv_first != NULL && cpv_second != NULL && pv_output != NULL);
*(bool_t*)pv_output = *(_Bool*)cpv_first < *(_Bool*)cpv_second ? true : false;
}

void _type_destroy_bool(const void* cpv_input, void* pv_output)
{
_type_destroy_default(cpv_input, pv_output);
}
#endif

/** local function implementation section **/

/** eof **/
Expand Down
8 changes: 8 additions & 0 deletions src/cstl_types_builtin.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ extern void _type_copy_iterator(const void* cpv_first, const void* cpv_second, v
extern void _type_less_iterator(const void* cpv_first, const void* cpv_second, void* pv_output);
extern void _type_destroy_iterator(const void* cpv_input, void* pv_output);

#ifndef _MSC_VER
/* _Bool */
extern void _type_init_bool(const void* cpv_input, void* pv_output);
extern void _type_copy_bool(const void* cpv_first, const void* cpv_second, void* pv_output);
extern void _type_less_bool(const void* cpv_first, const void* cpv_second, void* pv_output);
extern void _type_destroy_bool(const void* cpv_input, void* pv_output);
#endif

#ifdef __cplusplus
}
#endif
Expand Down
12 changes: 11 additions & 1 deletion src/cstl_types_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#define _TOKEN_TEXT_UNSIGNED "unsigned"
#define _TOKEN_TEXT_VOID "void"
#define _TOKEN_TEXT_CSTL_BOOL "bool_t"
#define _TOKEN_TEXT_BOOL "_Bool"
#define _TOKEN_TEXT_STRUCT "struct"
#define _TOKEN_TEXT_ENUM "enum"
#define _TOKEN_TEXT_UNION "union"
Expand Down Expand Up @@ -127,6 +128,7 @@ static keytable_t _sgt_table[] = {
{_TOKEN_KEY_UNSIGNED, _TOKEN_TEXT_UNSIGNED},
{_TOKEN_KEY_VOID, _TOKEN_TEXT_VOID},
{_TOKEN_KEY_CSTL_BOOL, _TOKEN_TEXT_CSTL_BOOL},
{_TOKEN_KEY_BOOL, _TOKEN_TEXT_BOOL},
{_TOKEN_KEY_STRUCT, _TOKEN_TEXT_STRUCT},
{_TOKEN_KEY_ENUM, _TOKEN_TEXT_ENUM},
{_TOKEN_KEY_UNION, _TOKEN_TEXT_UNION},
Expand Down Expand Up @@ -214,6 +216,7 @@ _typestyle_t _type_get_style(const char* s_typename, char* s_formalname)
case _TOKEN_KEY_UNSIGNED:
case _TOKEN_KEY_VOID:
case _TOKEN_KEY_CSTL_BOOL:
case _TOKEN_KEY_BOOL:
t_style = _type_parse_c_builtin(s_formalname) ? _TYPE_C_BUILTIN : _TYPE_INVALID;
break;
/* TYPE_DESCRIPT -> USER_DEFINE */
Expand Down Expand Up @@ -397,6 +400,7 @@ bool_t _type_parse_c_builtin(char* s_formalname)
case _TOKEN_KEY_FLOAT:
case _TOKEN_KEY_DOUBLE:
case _TOKEN_KEY_CSTL_BOOL:
case _TOKEN_KEY_BOOL:
return _type_parse_simple_builtin(s_formalname);
break;
/* C_BUILTIN -> SIGNED_BUILTIN */
Expand Down Expand Up @@ -510,7 +514,7 @@ bool_t _type_parse_simple_builtin(char* s_formalname)
assert(s_formalname != NULL);
/*
* SIMPLE_BUILTIN -> char POINTER_SUFFIX | short COMMON_SUFFIX | int |
* long SIMPLE_LONG_SUFFIX | float | double | bool_t
* long SIMPLE_LONG_SUFFIX | float | double | bool_t | _Bool
*/
switch (_gt_typeanalysis._t_token) {
/* SIMPLE_BUILTIN -> char POINTER_SUFFIX */
Expand Down Expand Up @@ -551,6 +555,11 @@ bool_t _type_parse_simple_builtin(char* s_formalname)
_TOKEN_MATCH(_TOKEN_TEXT_CSTL_BOOL, s_formalname);
return true;
break;
/* SIMPLE_BUILTIN -> _Bool */
case _TOKEN_KEY_BOOL:
_TOKEN_MATCH(_TOKEN_TEXT_BOOL, s_formalname);
return true;
break;
default:
return false;
break;
Expand Down Expand Up @@ -1025,6 +1034,7 @@ bool_t _type_parse_type_descript(char* s_formalname)
case _TOKEN_KEY_UNSIGNED:
case _TOKEN_KEY_VOID:
case _TOKEN_KEY_CSTL_BOOL:
case _TOKEN_KEY_BOOL:
return _type_parse_c_builtin(s_formalname);
break;
/* TYPE_DESCRIPT -> USER_DEFINE */
Expand Down
2 changes: 1 addition & 1 deletion src/cstl_types_parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ typedef enum _tagtypetoken
/* c builtin */
_TOKEN_KEY_CHAR, _TOKEN_KEY_SHORT, _TOKEN_KEY_INT, _TOKEN_KEY_LONG, _TOKEN_KEY_FLOAT,
_TOKEN_KEY_DOUBLE, _TOKEN_KEY_SIGNED, _TOKEN_KEY_UNSIGNED, _TOKEN_KEY_VOID,
_TOKEN_KEY_CSTL_BOOL,
_TOKEN_KEY_CSTL_BOOL, _TOKEN_KEY_BOOL,
/* user define */
_TOKEN_KEY_STRUCT, _TOKEN_KEY_ENUM, _TOKEN_KEY_UNION, _TOKEN_IDENTIFIER,
/* cstl container */
Expand Down
Loading

0 comments on commit 5f6ada2

Please sign in to comment.