[cexpr] Add string and voidptr types

The string type is useful for passing around strings (the only thing
that they can do, currently), particularly as arguments to functions.
The voidptr type is (currently) never generated by the core cexpr
system, but is useful for storing pointers via cexpr (probably a bit of
a hack, but it seems to work well in my current use).
This commit is contained in:
Bill Currie 2023-06-18 17:20:38 +09:00
parent d8239bf9e2
commit 8e25fb13d1
3 changed files with 29 additions and 0 deletions

View file

@ -151,6 +151,9 @@ extern exprtype_t cexpr_exprval;
extern exprtype_t cexpr_field;
extern exprtype_t cexpr_function;
extern exprtype_t cexpr_plitem;
extern exprtype_t cexpr_string;
// generic pointer holder, never generated directly by cexpr
extern exprtype_t cexpr_voidptr;
extern binop_t cexpr_array_binops[];
extern binop_t cexpr_struct_binops[];

View file

@ -76,6 +76,7 @@ static exprval_t *parse_uint (const char *str, exprctx_t *context);
static exprval_t *parse_size_t (const char *str, exprctx_t *context);
static exprval_t *parse_float (const char *str, exprctx_t *context);
static exprval_t *parse_double (const char *str, exprctx_t *context);
static exprval_t *parse_string (const char *str, exprctx_t *context);
static exprsym_t *parse_name (const char *str, exprctx_t *context);
static exprval_t *parse_variable (const char *str, exprctx_t *context);
@ -174,6 +175,8 @@ STRING \"(\\.|[^"\\])*\"
}
{STRING} {
yylval->value = parse_string (yytext, context);
return VALUE;
}
@ return '@';
@ -292,6 +295,19 @@ static exprval_t *parse_double (const char *str, exprctx_t *context)
return val;
}
static exprval_t *parse_string (const char *str, exprctx_t *context)
{
exprval_t *val = cexpr_value (&cexpr_string, context);
// str includes the quotes, which add 2 to the length of the string, but
// only one byte is needed for the terminating nul
size_t size = strlen (str) - 1;
char *dup = cmemalloc (context->memsuper, size);
strncpy (dup, str + 1, size - 1);
dup[size - 1] = 0;
*(char **) val->value = dup;
return val;
}
static exprsym_t *
parse_name (const char *name, exprctx_t *context)
{

View file

@ -774,6 +774,16 @@ exprtype_t cexpr_plitem = {
.unops = 0,
};
exprtype_t cexpr_string = {
.name = "string",
.size = sizeof (char *),
};
exprtype_t cexpr_voidptr = {
.name = "voidptr",
.size = sizeof (void *),
};
VISIBLE binop_t *
cexpr_find_cast (exprtype_t *dst_type, exprtype_t *src_type)
{