scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit 66948d9f2f71c593469751b3b022aea1a499c94f
parent e9f3f4b6f347e80196c224028a80d48099495168
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri, 11 Apr 2014 16:55:30 +0200

First version af assignments

This version is not full functional because types are
not checked.

Diffstat:
Mcc.h | 4+++-
Mcode.c | 13++++++++++++-
Mexpr.c | 36+++++++++++++++++++++++++++++++++++-
3 files changed, 50 insertions(+), 3 deletions(-)

diff --git a/cc.h b/cc.h @@ -221,7 +221,9 @@ enum { OCAST, OPTR, OADD, OARY, OSIZE, OMUL, OSUB, OINC, ODEC, OPINC, OPDEC, ODIV, OMOD, OSHL, OSHR, OLT, OGT, OGE, OLE, OEQ, ONE, OBAND, - OBXOR, OBOR + OBXOR, OBOR, OASSIGN, OA_MUL, OA_DIV, + OA_MOD, OA_ADD, OA_SUB, OA_SHL, OA_SHR, + OA_AND, OA_XOR, OA_OR }; extern void diff --git a/code.c b/code.c @@ -26,7 +26,18 @@ char *opcodes[] = { [ONE] = "!", [OBAND] = "&", [OBXOR] = "^", - [OBOR] = "|" + [OBOR] = "|", + [OASSIGN] = ":", + [OA_MUL] = ":*", + [OA_DIV] = ":/", + [OA_MOD] = ":%", + [OA_ADD] = ":+", + [OA_SUB] = ":-", + [OA_SHL] = ":l", + [OA_SHR] = ":r", + [OA_AND] = ":&", + [OA_XOR] = ":^", + [OA_OR] = ":|" }; Node * diff --git a/expr.c b/expr.c @@ -355,6 +355,10 @@ relational(void) default: return np; } next(); + /* + * FIX: This is incorrect because in relation + * we cannot change the order of the operands + */ np = arithmetic(op, np, shift()); } } @@ -416,13 +420,43 @@ bit_or(void) return np; } +static Node * +assign(void) +{ + register Node *np = bit_or(); + + for (;;) { + register char op; + + switch (yytoken) { + case '=': op = OASSIGN; break; + case MUL_EQ: op = OA_MUL; break; + case DIV_EQ: op = OA_DIV; break; + case MOD_EQ: op = OA_MOD; break; + case ADD_EQ: op = OA_ADD; break; + case SUB_EQ: op = OA_SUB; break; + case SHL_EQ: op = OA_SHL; break; + case SHR_EQ: op = OA_SHR; break; + case AND_EQ: op = OA_AND; break; + case XOR_EQ: op = OA_XOR; break; + case OR_EQ: op = OA_OR; break; + default: goto return_np; + } + next(); + /* TODO: cast types */ + np = bincode(op, np->type, np, assign()); + } +return_np: + return np; +} + Node * expr(void) { register Node *np; do - np = bit_or(); + np = assign(); while (yytoken == ','); return np; }