ref: b180e15d2a5fb72ee3aa45a5cf6a3e27a016aed0
parent: b584afc229761ee1e36f523ea33e717ea0ef2586
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Fri Mar 20 07:48:38 EDT 2015
Add initial support for return statements This is only a stab, because it doesn't do anything with the statement.
--- a/cc2/cc2.h
+++ b/cc2/cc2.h
@@ -36,6 +36,7 @@
#define ONEG '_'
#define OCPL '~'
#define OCOMMA ','
+#define ORET 'y'
#define ADDABLE 10
--- a/cc2/cgen.c
+++ b/cc2/cgen.c
@@ -359,10 +359,16 @@
np->reg = rp->reg;
}
+static void
+ret(Node *np)
+{
+}
+
static void (*opnodes[])(Node *) = {
[OADD] = add,
[OSUB] = add,
- [OASSIG] = assign
+ [OASSIG] = assign,
+ [ORET] = ret
};
static void
--- a/cc2/parser.c
+++ b/cc2/parser.c
@@ -90,7 +90,7 @@
static void cast(char *), operator(char *), assignment(char *), increment(char *),
globvar(char *), localvar(char *), paramvar(char *), label(char *),
- immediate(char *), unary(char *);
+ immediate(char *), unary(char *), oreturn(char *);
/*TODO: Remove hardcoded symbols */
@@ -137,7 +137,7 @@
['['] = operator,
['='] = operator,
['!'] = unary,
- ['y'] = NULL,
+ ['y'] = oreturn,
['j'] = NULL,
['o'] = operator,
['_'] = unary,
@@ -421,21 +421,31 @@
}
static void
-expression(char *token)
+expr(char *token)
{
Node *np;
void (*fun)(char *);
unsigned c;
- if (!curfun)
- error(ESYNTAX);
-
do {
if ((c = token[0]) > 0x7f || (fun = optbl[c]) == NULL)
error(ESYNTAX);
(*fun)(token);
} while (token = strtok(NULL, "\t"));
+}
+static void
+expression(char *token)
+{
+ Node *np;
+ void (*fun)(char *);
+ unsigned c;
+
+ if (!curfun)
+ error(ESYNTAX);
+
+ expr(token);
+
np = pop();
if (stackp != stack)
error(EEXPBAL);
@@ -442,6 +452,23 @@
if (listp == &listexp[NR_EXPRESSIONS])
error(EEXPROV);
*listp++ = np;
+}
+
+static void
+oreturn(char *token)
+{
+ Node *np = newnode();
+
+ np->op = token[0];
+
+ if (token = strtok(NULL, "\t")) {
+ expr(token);
+ np -> left = pop();
+ } else {
+ np->left = NULL;
+ }
+ np->right = NULL;
+ push(np);
}
static void