From d7461a7342e706bece657172799ea8db9a104237 Mon Sep 17 00:00:00 2001 From: Jack Palevich Date: Fri, 12 Jun 2009 14:26:58 -0700 Subject: [PATCH] Support variable initialization. Global variables can only be initialized to integer constants. Local variables can be initialized to arbitrary expressions. --- libacc/acc.cpp | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/libacc/acc.cpp b/libacc/acc.cpp index 8b3765a3d..0cb51ffba 100644 --- a/libacc/acc.cpp +++ b/libacc/acc.cpp @@ -2524,14 +2524,22 @@ class Compiler : public ErrorSink { 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 @@ class Compiler : public ErrorSink { 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 @@ class Compiler : public ErrorSink { 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; }