Support variable initialization.
Global variables can only be initialized to integer constants.
Local variables can be initialized to arbitrary expressions.
diff --git a/libacc/acc.cpp b/libacc/acc.cpp
index 8b3765a..0cb51ff 100644
--- a/libacc/acc.cpp
+++ b/libacc/acc.cpp
@@ -2524,14 +2524,22 @@
while (acceptType(base)) {
while (tok != ';' && tok != EOF) {
Type t = acceptPointerDeclaration(t);
+ int variableAddress = 0;
if (checkSymbol()) {
addLocalSymbol();
if (tok) {
loc = loc + 4;
- *(int *) tok = -loc;
+ variableAddress = -loc;
+ ((VariableInfo*) tok)->pAddress = (void*) variableAddress;
}
}
next();
+ if (tok == '=') {
+ /* assignment */
+ next();
+ expr();
+ pGen->storeR0(variableAddress);
+ }
if (tok == ',')
next();
}
@@ -2543,7 +2551,11 @@
bool result = isSymbol();
if (!result) {
String temp;
- if (tok >= 0 && tok < 256) {
+ if (tok == EOF ) {
+ temp.printf("EOF");
+ } else if (tok == TOK_NUM) {
+ temp.printf("numeric constant");
+ } else if (tok >= 0 && tok < 256) {
temp.printf("char \'%c\'", tok);
} else if (tok >= TOK_KEYWORD && tok < TOK_UNSUPPORTED_KEYWORD) {
temp.printf("keyword \"%s\"", mTokenString.getUnwrapped());
@@ -2579,12 +2591,23 @@
mTokenString.getUnwrapped());
}
next();
- if (tok == ',' || tok == ';') {
+ if (tok == ',' || tok == ';' || tok == '=') {
// it's a variable declaration
for(;;) {
if (name) {
name->pAddress = (int*) allocGlobalSpace(4);
}
+ if (tok == '=') {
+ next();
+ if (tok == TOK_NUM) {
+ if (name) {
+ * (int*) name->pAddress = tokc;
+ }
+ next();
+ } else {
+ error("Expected an integer constant");
+ }
+ }
if (tok != ',') {
break;
}