From 8975b5a5b9ecf2423fbf414162904be9328cfdee Mon Sep 17 00:00:00 2001 From: Alan Cox Date: Fri, 24 Jun 2016 16:15:55 +0100 Subject: [PATCH] scc: tea break quickie Tweak the parser logic so you can put a leading type on the function - not that we record or check it yet. Also allow static functions. The parsing was trivial so might as well do it --- Applications/SmallC/function.c | 17 +++++++++++++---- Applications/SmallC/main.c | 8 +++++--- Applications/SmallC/prototype.h | 3 ++- Applications/SmallC/sym.c | 14 ++++++++++---- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/Applications/SmallC/function.c b/Applications/SmallC/function.c index 001f7d33..a790d609 100644 --- a/Applications/SmallC/function.c +++ b/Applications/SmallC/function.c @@ -16,7 +16,7 @@ int argtop; */ void newfunc(void) { char n[NAMESIZE]; - int idx, type; + int type; fexitlab = getlabel(); if (!symname(n)) { @@ -24,6 +24,17 @@ void newfunc(void) { do_kill(); return; } + if (!match("(")) + error("missing open paren"); + newfunc_typed(PUBLIC, n, CINT); +} + +void newfunc_typed(int storage, char *n, int type) +{ + int idx; + /* TODO: external storage */ + if (storage == EXTERN) + error("not yet supported"); if ((idx = find_global(n)) > -1) { if (symbol_table[idx].identity != FUNCTION) multidef(n); @@ -32,9 +43,7 @@ void newfunc(void) { else symbol_table[idx].offset = FUNCTION; } else - add_global(n, FUNCTION, CINT, FUNCTION, PUBLIC); - if (!match("(")) - error("missing open paren"); + add_global(n, FUNCTION, CINT, FUNCTION, storage); output_string(n); output_label_terminator(); newline(); diff --git a/Applications/SmallC/main.c b/Applications/SmallC/main.c index a26cc7ac..d9edbbf5 100644 --- a/Applications/SmallC/main.c +++ b/Applications/SmallC/main.c @@ -177,6 +177,7 @@ int do_declarations(int stclass, TAG_SYMBOL *mtag, int is_struct) { int otag; // tag of struct object being declared int sflag; // TRUE for struct definition, zero for union char sname[NAMESIZE]; + int ns = 0; blanks(); if ((sflag=amatch("struct", 6)) || amatch("union", 5)) { @@ -188,13 +189,14 @@ int do_declarations(int stclass, TAG_SYMBOL *mtag, int is_struct) { } declare_global(STRUCT, stclass, mtag, otag, is_struct); } else if ((type = get_type()) != 0) { - declare_global(type, stclass, mtag, 0, is_struct); + ns = declare_global(type, stclass, mtag, 0, is_struct); } else if (stclass == PUBLIC) { return (0); } else { - declare_global(CINT, stclass, mtag, 0, is_struct); + ns = declare_global(CINT, stclass, mtag, 0, is_struct); } - need_semicolon(); + if (!ns) + need_semicolon(); return (1); } diff --git a/Applications/SmallC/prototype.h b/Applications/SmallC/prototype.h index 615049f6..36d42d63 100644 --- a/Applications/SmallC/prototype.h +++ b/Applications/SmallC/prototype.h @@ -89,6 +89,7 @@ extern int hier9(LVALUE *lval); extern int hier10(LVALUE *lval); extern int hier11(LVALUE *lval); extern void newfunc(void); +extern void newfunc_typed(int storage, char *n, int type); extern void getarg(int t); extern int doAnsiArguments(void); extern void doLocalAnsiArgument(int type); @@ -200,7 +201,7 @@ extern int find_tag(char *sname); extern SYMBOL *find_member(TAG_SYMBOL *tag, char *sname); extern void add_member(char *sname, char identity, char type, int offset, int storage_class); extern int define_struct(char *sname, int storage, int is_struct); -extern void declare_global(int type, int storage, TAG_SYMBOL *mtag, int otag, int is_struct); +extern int declare_global(int type, int storage, TAG_SYMBOL *mtag, int otag, int is_struct); extern int initials(char *symbol_name, int type, int identity, int dim, int otag); extern void struct_init(TAG_SYMBOL *tag, char *symbol_name); extern int init(char *symbol_name, int type, int identity, int *dim, TAG_SYMBOL *tag); diff --git a/Applications/SmallC/sym.c b/Applications/SmallC/sym.c index c1ddf9ce..769b04eb 100644 --- a/Applications/SmallC/sym.c +++ b/Applications/SmallC/sym.c @@ -13,16 +13,16 @@ * @param mtag tag of struct whose members are being declared, or zero * @param otag tag of struct object being declared. only matters if mtag is non-zero * @param is_struct struct or union or no meaning - * @return + * @return 1 if a function was parsed */ -void declare_global(int type, int storage, TAG_SYMBOL *mtag, int otag, int is_struct) { +int declare_global(int type, int storage, TAG_SYMBOL *mtag, int otag, int is_struct) { int dim, identity; char sname[NAMESIZE]; FOREVER { FOREVER { if (endst ()) - return; + return 0; dim = 1; if (match ("*")) { identity = POINTER; @@ -33,6 +33,12 @@ void declare_global(int type, int storage, TAG_SYMBOL *mtag, int otag, int is_st illname (); if (find_global (sname) > -1) multidef (sname); + if (match ("(")) { + /* FIXME: We need to deal with pointer types properly here */ + newfunc_typed(storage, sname, type); + /* Can't int foo(x){blah),a=4; */ + return 1; + } if (match ("[")) { dim = needsub (); //if (dim || storage == EXTERN) { @@ -70,7 +76,7 @@ void declare_global(int type, int storage, TAG_SYMBOL *mtag, int otag, int is_st } } if (!match (",")) - return; + return 0; } } -- 2.34.1