1
0
mirror of https://github.com/cc65/cc65.git synced 2024-06-26 20:29:34 +00:00

Added enums

git-svn-id: svn://svn.cc65.org/cc65/trunk@2665 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz 2003-11-14 09:03:32 +00:00
parent 742b1ffd8e
commit 0e392b79bd
12 changed files with 235 additions and 31 deletions

135
src/ca65/enum.c Normal file
View File

@ -0,0 +1,135 @@
/*****************************************************************************/
/* */
/* enum.c */
/* */
/* .ENUM command */
/* */
/* */
/* */
/* (C) 2003 Ullrich von Bassewitz */
/* Römerstraße 52 */
/* D-70794 Filderstadt */
/* EMail: uz@cc65.org */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
/* warranty. In no event will the authors be held liable for any damages */
/* arising from the use of this software. */
/* */
/* Permission is granted to anyone to use this software for any purpose, */
/* including commercial applications, and to alter it and redistribute it */
/* freely, subject to the following restrictions: */
/* */
/* 1. The origin of this software must not be misrepresented; you must not */
/* claim that you wrote the original software. If you use this software */
/* in a product, an acknowledgment in the product documentation would be */
/* appreciated but is not required. */
/* 2. Altered source versions must be plainly marked as such, and must not */
/* be misrepresented as being the original software. */
/* 3. This notice may not be removed or altered from any source */
/* distribution. */
/* */
/*****************************************************************************/
/* common */
#include "addrsize.h"
/* ca65 */
#include "enum.h"
#include "error.h"
#include "expr.h"
#include "nexttok.h"
#include "scanner.h"
#include "symbol.h"
#include "symtab.h"
/*****************************************************************************/
/* Code */
/*****************************************************************************/
void DoEnum (void)
/* Handle the .ENUM command */
{
/* Start at zero */
ExprNode* NextExpr = GenLiteralExpr (0);
/* Check for a name */
int Anon = (Tok != TOK_IDENT);
if (!Anon) {
/* Enter a new scope, then skip the name */
SymEnterLevel (SVal, ST_ENUM, ADDR_SIZE_ABS);
NextTok ();
}
/* Test for end of line */
ConsumeSep ();
/* Read until end of struct */
while (Tok != TOK_ENDENUM && Tok != TOK_EOF) {
SymEntry* Sym;
ExprNode* EnumExpr;
/* The format is "identifier [ = value ]" */
if (Tok != TOK_IDENT) {
ErrorSkip ("Identifier expected");
continue;
}
/* We have an identifier, generate a symbol */
Sym = SymFind (CurrentScope, SVal, SYM_ALLOC_NEW);
/* Skip the member name */
NextTok ();
/* Check for an assignment */
if (Tok == TOK_EQ) {
/* Skip the equal sign */
NextTok ();
/* Delete the old next expression */
FreeExpr (NextExpr);
/* Read the new one */
EnumExpr = Expression ();
} else {
EnumExpr = NextExpr;
}
/* Generate the next expression from the current one */
NextExpr = GenAddExpr (CloneExpr (EnumExpr), GenLiteralExpr (1));
NextExpr = SimplifyExpr (NextExpr);
/* Assign the value to the enum member */
SymDef (Sym, EnumExpr, ADDR_SIZE_DEFAULT, SF_NONE);
/* Expect end of line */
ConsumeSep ();
}
/* If this is not an anon enum, leave its scope */
if (!Anon) {
/* Close the enum scope */
SymLeaveLevel ();
}
/* End of enum definition */
Consume (TOK_ENDENUM, "`.ENDENUM' expected");
/* Free the last (unused) enum expression */
FreeExpr (NextExpr);
}

57
src/ca65/enum.h Normal file
View File

@ -0,0 +1,57 @@
/*****************************************************************************/
/* */
/* enum.h */
/* */
/* .ENUM command */
/* */
/* */
/* */
/* (C) 2003 Ullrich von Bassewitz */
/* Römerstraße 52 */
/* D-70794 Filderstadt */
/* EMail: uz@cc65.org */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
/* warranty. In no event will the authors be held liable for any damages */
/* arising from the use of this software. */
/* */
/* Permission is granted to anyone to use this software for any purpose, */
/* including commercial applications, and to alter it and redistribute it */
/* freely, subject to the following restrictions: */
/* */
/* 1. The origin of this software must not be misrepresented; you must not */
/* claim that you wrote the original software. If you use this software */
/* in a product, an acknowledgment in the product documentation would be */
/* appreciated but is not required. */
/* 2. Altered source versions must be plainly marked as such, and must not */
/* be misrepresented as being the original software. */
/* 3. This notice may not be removed or altered from any source */
/* distribution. */
/* */
/*****************************************************************************/
#ifndef ENUM_H
#define ENUM_H
/*****************************************************************************/
/* Code */
/*****************************************************************************/
void DoEnum (void);
/* Handle the .ENUM command */
/* End of enum.h */
#endif

View File

@ -1523,30 +1523,6 @@ static void StudyExpr (ExprNode* Expr, ExprDesc* D, int Sign)
static ExprNode* SimplifyExpr (ExprNode* Expr)
/* Try to simplify the given expression tree */
{
if (Expr && Expr->Op != EXPR_LITERAL) {
/* Create an expression description and initialize it */
ExprDesc D;
InitExprDesc (&D);
/* Study the expression */
StudyExpr (Expr, &D, 1);
/* Now check if we can generate a literal value */
if (ExprDescIsConst (&D)) {
/* No external references */
FreeExpr (Expr);
Expr = GenLiteralExpr (D.Val);
}
}
return Expr;
}
ExprNode* Expression (void)
/* Evaluate an expression, build the expression tree on the heap and return
* a pointer to the root of the tree.
@ -1610,6 +1586,30 @@ void FreeExpr (ExprNode* Root)
ExprNode* SimplifyExpr (ExprNode* Expr)
/* Try to simplify the given expression tree */
{
if (Expr && Expr->Op != EXPR_LITERAL) {
/* Create an expression description and initialize it */
ExprDesc D;
InitExprDesc (&D);
/* Study the expression */
StudyExpr (Expr, &D, 1);
/* Now check if we can generate a literal value */
if (ExprDescIsConst (&D)) {
/* No external references */
FreeExpr (Expr);
Expr = GenLiteralExpr (D.Val);
}
}
return Expr;
}
ExprNode* GenLiteralExpr (long Val)
/* Return an expression tree that encodes the given literal value */
{

View File

@ -44,7 +44,7 @@
/*****************************************************************************/
/* Code */
/* Code */
/*****************************************************************************/
@ -63,7 +63,10 @@ long ConstExpression (void);
void FreeExpr (ExprNode* Root);
/* Free the expression tree, Root is pointing to. */
ExprNode* GenLiteralExpr (long Val);
ExprNode* SimplifyExpr (ExprNode* Expr);
/* Try to simplify the given expression tree */
ExprNode* GenLiteralExpr (long Val);
/* Return an expression tree that encodes the given literal value */
ExprNode* GenSymExpr (struct SymEntry* Sym);

View File

@ -15,6 +15,7 @@ OBJS = anonname.o \
condasm.o \
dbginfo.o \
ea.o \
enum.o \
error.o \
expr.o \
feature.o \

View File

@ -64,6 +64,7 @@ OBJS = anonname.obj \
condasm.obj \
dbginfo.obj \
ea.obj \
enum.obj \
error.obj \
expr.obj \
feature.obj \

View File

@ -53,6 +53,7 @@
#include "asserts.h"
#include "condasm.h"
#include "dbginfo.h"
#include "enum.h"
#include "error.h"
#include "expr.h"
#include "feature.h"
@ -1639,6 +1640,7 @@ static CtrlDesc CtrlCmdTab [] = {
{ ccKeepToken, DoConditionals }, /* .ELSE */
{ ccKeepToken, DoConditionals }, /* .ELSEIF */
{ ccKeepToken, DoEnd },
{ ccNone, DoUnexpected }, /* .ENDENUM */
{ ccKeepToken, DoConditionals }, /* .ENDIF */
{ ccNone, DoUnexpected }, /* .ENDMACRO */
{ ccNone, DoEndProc },
@ -1646,6 +1648,7 @@ static CtrlDesc CtrlCmdTab [] = {
{ ccNone, DoEndScope },
{ ccNone, DoUnexpected }, /* .ENDSTRUCT */
{ ccNone, DoUnexpected }, /* .ENDUNION */
{ ccNone, DoEnum },
{ ccNone, DoError },
{ ccNone, DoExitMacro },
{ ccNone, DoExport },

View File

@ -154,6 +154,7 @@ struct DotKeyword {
{ ".ELSE", TOK_ELSE },
{ ".ELSEIF", TOK_ELSEIF },
{ ".END", TOK_END },
{ ".ENDENUM", TOK_ENDENUM },
{ ".ENDIF", TOK_ENDIF },
{ ".ENDMAC", TOK_ENDMACRO },
{ ".ENDMACRO", TOK_ENDMACRO },
@ -163,6 +164,7 @@ struct DotKeyword {
{ ".ENDSCOPE", TOK_ENDSCOPE },
{ ".ENDSTRUCT", TOK_ENDSTRUCT },
{ ".ENDUNION", TOK_ENDUNION },
{ ".ENUM", TOK_ENUM },
{ ".ERROR", TOK_ERROR },
{ ".EXITMAC", TOK_EXITMACRO },
{ ".EXITMACRO", TOK_EXITMACRO },

View File

@ -144,7 +144,8 @@ enum Token {
TOK_DWORD,
TOK_ELSE,
TOK_ELSEIF,
TOK_END,
TOK_END,
TOK_ENDENUM,
TOK_ENDIF,
TOK_ENDMACRO,
TOK_ENDPROC,
@ -152,6 +153,7 @@ enum Token {
TOK_ENDSCOPE,
TOK_ENDSTRUCT,
TOK_ENDUNION,
TOK_ENUM,
TOK_ERROR,
TOK_EXITMACRO,
TOK_EXPORT,

View File

@ -2,7 +2,7 @@
/* */
/* struct.c */
/* */
/* .STRUCT command */
/* .STRUCT/.UNION commands */
/* */
/* */
/* */

View File

@ -2,7 +2,7 @@
/* */
/* struct.h */
/* */
/* .STRUCT command */
/* .STRUCT/.UNION commands */
/* */
/* */
/* */

View File

@ -63,8 +63,8 @@
#define ST_GLOBAL 0x00 /* Root level */
#define ST_PROC 0x01 /* .PROC */
#define ST_SCOPE 0x02 /* .SCOPE */
#define ST_STRUCT 0x03 /* .STRUCT */
#define ST_UNION 0x04 /* .UNION */
#define ST_STRUCT 0x03 /* .STRUCT/.UNION */
#define ST_ENUM 0x04 /* .ENUM */
#define ST_UNDEF 0xFF
/* A symbol table */