commit bb37ffe1e43c9fa3c8475807c801f9e228f9100a
parent 4ed32812fd0a83d1bd86fb41f5c4531a6c477148
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Wed, 3 Jul 2013 16:14:39 +0200
Add function operator
This operator marks when a function body is generated.
Diffstat:
5 files changed, 28 insertions(+), 20 deletions(-)
diff --git a/decl.c b/decl.c
@@ -146,10 +146,7 @@ listdcl(register struct ctype *tp)
"type defaults to 'int' in declaration of '%s'",
yytext);
} else if (curfun->type == FTN && yytoken == '{') {
- struct node *np = compound();
- prtree(np);
- putchar('\n');
- freesyms();
+ function(cursym);
return;
}
} while (accept(','));
diff --git a/flow.c b/flow.c
@@ -230,6 +230,22 @@ _default(void)
}
static struct node *
+compound(void)
+{
+ register struct node *np = nodecomp();
+
+ expect('{');
+ new_ctx();
+ while (decl())
+ /* nothing */;
+ while (!accept('}'))
+ addstmt(np, stmt());
+ del_ctx();
+
+ return np;
+}
+
+static struct node *
stmt(void)
{
register struct node *np;
@@ -254,18 +270,13 @@ stmt(void)
return np;
}
-struct node *
-compound(void)
+void
+function(struct symbol *sym)
{
- register struct node *np = nodecomp();
-
- expect('{');
- new_ctx();
- while (decl())
- /* nothing */;
- while (!accept('}'))
- addstmt(np, stmt());
- del_ctx();
+ register struct node *np;
- return np;
+ np = node2(OFTN, nodesym(sym), compound());
+ prtree(np);
+ putchar('\n');
+ freesyms();
}
diff --git a/syntax.h b/syntax.h
@@ -12,16 +12,16 @@ enum opcode {
OA_MOD, OA_ADD, OA_SUB, OA_SHL, OA_SHR, OA_AND,
OA_XOR, OA_OR, OSYM, OCOMP, OSWITCH, OIF, OFOR,
OFEXP, ODO, OWHILE, OLABEL, OGOTO, OBREAK, OCONT,
- ORETURN, OCASE, ODEFAULT
+ ORETURN, OCASE, ODEFAULT, OFTN
};
struct node;
struct symbol;
-extern struct node *compound(void);
extern struct node *expr(void);
extern unsigned char decl(void);
extern void type_name(void);
+extern void function(struct symbol *sym);
extern struct node *node3(unsigned char op,
struct node *l, struct node *i, struct node *r);
diff --git a/tree.c b/tree.c
@@ -189,7 +189,8 @@ prtree_helper(register struct node *np)
[OCONT] = {1, "cont"},
[ORETURN] = {1, "return"},
[OCASE] = {1, "case"},
- [ODEFAULT] = {1, "default"}
+ [ODEFAULT] = {1, "default"},
+ [OFTN] = {2, "function"}
};
if (!np) {
fputs(" nil", stdout);
diff --git a/types.c b/types.c
@@ -74,7 +74,6 @@ decl_type(struct ctype *tp)
{
while (stackp != stack)
tp = mktype(tp, *--stackp);
- ptype(tp);
return tp;
}