shithub: scc

Download patch

ref: f1182f58c64e86472355891de7a035d984032d06
parent: 76953d9121994abce24da20d6b96587badb93f39
author: Roberto E. Vargas Caballero <[email protected]>
date: Sun Aug 10 18:56:33 EDT 2014

Add increment operator in cc2

This operator is used for post increment operations.

--- a/cc2/cc2.h
+++ b/cc2/cc2.h
@@ -30,6 +30,7 @@
 
 typedef struct node {
 	char op;
+	char subop;
 	Type *type;
 	uint8_t complex;
 	uint8_t addable;
@@ -67,6 +68,7 @@
 #define OADD      '+'
 #define OSUB      '-'
 #define OASSIG    ':'
+#define OINC      ';'
 
 extern void error(unsigned nerror, ...);
 extern void genaddable(Node *list[]);
--- a/cc2/cgen.c
+++ b/cc2/cgen.c
@@ -124,8 +124,8 @@
 	}
 
 	switch (np->op) {
-	case OADD:
-	case OASSIG:
+	case OINC:
+	case OADD: case OASSIG:
 		break;
 	default:
 		abort();
@@ -190,6 +190,7 @@
 	case CONST:
 		np->addable = 20;
 		break;
+	case OINC:
 	case OASSIG: case OADD: case OSUB:
 		xaddable(lp);
 		xaddable(rp);
--- a/cc2/parser.c
+++ b/cc2/parser.c
@@ -228,6 +228,24 @@
 	push(np);
 }
 
+static void
+increment(char *token)
+{
+	Node *np = newnode();
+
+	np->right = pop();
+	np->left = pop();
+	np->type = gettype(token+2);
+	np->op = token[0];
+	switch (np->subop = token[1]) {
+	case '-': case '+':
+		push(np);
+		break;
+	default:
+		error(ESYNTAX);
+	}
+}
+
 static void (*optbl[])(char *) = {
 	['+'] = operator,
 	['-'] = operator,
@@ -234,6 +252,7 @@
 	['*'] = operator,
 	['/'] = operator,
 	[':'] = operator,
+	[';'] = increment,
 	['A'] = variable,
 	['T'] = variable,
 	['G'] = variable,