scc: tea break quickie
authorAlan Cox <alan@linux.intel.com>
Fri, 24 Jun 2016 15:15:55 +0000 (16:15 +0100)
committerAlan Cox <alan@linux.intel.com>
Fri, 24 Jun 2016 15:15:55 +0000 (16:15 +0100)
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
Applications/SmallC/main.c
Applications/SmallC/prototype.h
Applications/SmallC/sym.c

index 001f7d3..a790d60 100644 (file)
@@ -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();
index a26cc7a..d9edbbf 100644 (file)
@@ -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);
 }
 
index 615049f..36d42d6 100644 (file)
@@ -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);
index c1ddf9c..769b04e 100644 (file)
  * @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;
     }
 }