Introduce a typedef keyword.

This commit is contained in:
David Anderson
2014-08-03 21:51:34 -07:00
parent 09edda93ad
commit ad376ff0b8
20 changed files with 172 additions and 47 deletions
+2 -1
View File
@@ -357,7 +357,7 @@ typedef struct {
/* Tokens recognized by lex()
* Some of these constants are assigned as well to the variable "lastst" (see SC1.C)
*/
enum {
enum TokenKind {
/* value of first multi-character operator */
tFIRST = 256,
/* multi-character operators */
@@ -431,6 +431,7 @@ enum {
tSWITCH,
tTAGOF,
tTHEN,
tTYPEDEF,
tVOID,
tWHILE,
/* compiler directives */
+90
View File
@@ -143,6 +143,7 @@ static void dogoto(void);
static void dolabel(void);
static void doreturn(void);
static void dofuncenum(int listmode);
static void dotypedef();
static void domethodmap(LayoutSpec spec);
static void dobreak(void);
static void docont(void);
@@ -1558,6 +1559,9 @@ static void parse(void)
case tFUNCTAG:
dofuncenum(FALSE);
break;
case tTYPEDEF:
dotypedef();
break;
case tSTRUCT:
declstruct();
break;
@@ -2996,6 +3000,13 @@ static int consume_line()
static int parse_new_typename(const token_t *tok)
{
token_t tmp;
if (!tok) {
lextok(&tmp);
tok = &tmp;
}
switch (tok->id) {
case tINT:
return 0;
@@ -4176,6 +4187,85 @@ cleanup:
}
}
/**
* function-type ::= "(" function-type-inner ")"
* | function-type-inner
* function-type-inner ::= "function" type-expr "(" new-style-args ")"
*/
static void parse_function_type(functag_t *type)
{
memset(type, 0, sizeof(*type));
int lparen = matchtoken('(');
needtoken(tFUNCTION);
type->ret_tag = parse_new_typename(NULL);
type->type = uPUBLIC;
needtoken('(');
while (!matchtoken(')')) {
declinfo_t decl;
// Initialize.
memset(&decl, 0, sizeof(decl));
decl.type.ident = iVARIABLE;
parse_new_decl(&decl, NULL, DECLFLAG_ARGUMENT);
// Eat optional symbol name.
matchtoken(tSYMBOL);
// Error once when we're past max args.
if (type->argcount == sARGS_MAX) {
error(45);
continue;
}
funcarg_t *arg = &type->args[type->argcount++];
arg->tagcount = 1;
arg->tags[0] = decl.type.tag;
arg->dimcount = decl.type.numdim;
memcpy(arg->dims, decl.type.dim, arg->dimcount * sizeof(decl.type.dim[0]));
arg->fconst = (decl.type.usage & uCONST) ? TRUE : FALSE;
if (decl.type.ident == iARRAY)
arg->ident = iREFARRAY;
else
arg->ident = decl.type.ident;
if (!matchtoken(',')) {
needtoken(')');
break;
}
}
if (lparen)
needtoken(')');
require_newline(TRUE);
errorset(sRESET, 0);
}
static void dotypedef()
{
token_ident_t ident;
if (!needsymbol(&ident))
return;
int prev_tag = pc_findtag(ident.name);
if (prev_tag != -1 && !(prev_tag & FUNCTAG))
error(94);
needtoken('=');
funcenum_t *def = funcenums_add(ident.name);
functag_t type;
parse_function_type(&type);
functags_add(def, &type);
}
/**
* dofuncenum - declare function enumerations
*/
+2 -2
View File
@@ -1961,7 +1961,7 @@ char *sc_tokens[] = {
"public",
"return",
"sizeof", "sleep", "static", "stock", "struct", "switch",
"tagof", "*then",
"tagof", "*then", "typedef",
"void",
"while",
"#assert", "#define", "#else", "#elseif", "#emit", "#endif", "#endinput",
@@ -1969,7 +1969,7 @@ char *sc_tokens[] = {
"#tryinclude", "#undef",
";", ";", "-integer value-", "-rational value-", "-identifier-",
"-label-", "-string-"
};
};
static full_token_t *advance_token_ptr()
{
+1 -1
View File
@@ -22,7 +22,7 @@ typedef struct funcarg_s
int tagcount;
int tags[sTAGS_MAX];
int dimcount;
cell dims[sDIMEN_MAX];
int dims[sDIMEN_MAX];
int ident;
int fconst;
int ommittable;