Added enums
git-svn-id: svn://svn.cc65.org/cc65/trunk@2665 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
135
src/ca65/enum.c
Normal file
135
src/ca65/enum.c
Normal file
@@ -0,0 +1,135 @@
|
|||||||
|
/*****************************************************************************/
|
||||||
|
/* */
|
||||||
|
/* enum.c */
|
||||||
|
/* */
|
||||||
|
/* .ENUM command */
|
||||||
|
/* */
|
||||||
|
/* */
|
||||||
|
/* */
|
||||||
|
/* (C) 2003 Ullrich von Bassewitz */
|
||||||
|
/* R<>merstra<72>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
57
src/ca65/enum.h
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
/*****************************************************************************/
|
||||||
|
/* */
|
||||||
|
/* enum.h */
|
||||||
|
/* */
|
||||||
|
/* .ENUM command */
|
||||||
|
/* */
|
||||||
|
/* */
|
||||||
|
/* */
|
||||||
|
/* (C) 2003 Ullrich von Bassewitz */
|
||||||
|
/* R<>merstra<72>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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -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)
|
ExprNode* Expression (void)
|
||||||
/* Evaluate an expression, build the expression tree on the heap and return
|
/* Evaluate an expression, build the expression tree on the heap and return
|
||||||
* a pointer to the root of the tree.
|
* 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)
|
ExprNode* GenLiteralExpr (long Val)
|
||||||
/* Return an expression tree that encodes the given literal value */
|
/* Return an expression tree that encodes the given literal value */
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -44,7 +44,7 @@
|
|||||||
|
|
||||||
|
|
||||||
/*****************************************************************************/
|
/*****************************************************************************/
|
||||||
/* Code */
|
/* Code */
|
||||||
/*****************************************************************************/
|
/*****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
@@ -63,7 +63,10 @@ long ConstExpression (void);
|
|||||||
void FreeExpr (ExprNode* Root);
|
void FreeExpr (ExprNode* Root);
|
||||||
/* Free the expression tree, Root is pointing to. */
|
/* 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 */
|
/* Return an expression tree that encodes the given literal value */
|
||||||
|
|
||||||
ExprNode* GenSymExpr (struct SymEntry* Sym);
|
ExprNode* GenSymExpr (struct SymEntry* Sym);
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ OBJS = anonname.o \
|
|||||||
condasm.o \
|
condasm.o \
|
||||||
dbginfo.o \
|
dbginfo.o \
|
||||||
ea.o \
|
ea.o \
|
||||||
|
enum.o \
|
||||||
error.o \
|
error.o \
|
||||||
expr.o \
|
expr.o \
|
||||||
feature.o \
|
feature.o \
|
||||||
|
|||||||
@@ -64,6 +64,7 @@ OBJS = anonname.obj \
|
|||||||
condasm.obj \
|
condasm.obj \
|
||||||
dbginfo.obj \
|
dbginfo.obj \
|
||||||
ea.obj \
|
ea.obj \
|
||||||
|
enum.obj \
|
||||||
error.obj \
|
error.obj \
|
||||||
expr.obj \
|
expr.obj \
|
||||||
feature.obj \
|
feature.obj \
|
||||||
|
|||||||
@@ -53,6 +53,7 @@
|
|||||||
#include "asserts.h"
|
#include "asserts.h"
|
||||||
#include "condasm.h"
|
#include "condasm.h"
|
||||||
#include "dbginfo.h"
|
#include "dbginfo.h"
|
||||||
|
#include "enum.h"
|
||||||
#include "error.h"
|
#include "error.h"
|
||||||
#include "expr.h"
|
#include "expr.h"
|
||||||
#include "feature.h"
|
#include "feature.h"
|
||||||
@@ -1639,6 +1640,7 @@ static CtrlDesc CtrlCmdTab [] = {
|
|||||||
{ ccKeepToken, DoConditionals }, /* .ELSE */
|
{ ccKeepToken, DoConditionals }, /* .ELSE */
|
||||||
{ ccKeepToken, DoConditionals }, /* .ELSEIF */
|
{ ccKeepToken, DoConditionals }, /* .ELSEIF */
|
||||||
{ ccKeepToken, DoEnd },
|
{ ccKeepToken, DoEnd },
|
||||||
|
{ ccNone, DoUnexpected }, /* .ENDENUM */
|
||||||
{ ccKeepToken, DoConditionals }, /* .ENDIF */
|
{ ccKeepToken, DoConditionals }, /* .ENDIF */
|
||||||
{ ccNone, DoUnexpected }, /* .ENDMACRO */
|
{ ccNone, DoUnexpected }, /* .ENDMACRO */
|
||||||
{ ccNone, DoEndProc },
|
{ ccNone, DoEndProc },
|
||||||
@@ -1646,6 +1648,7 @@ static CtrlDesc CtrlCmdTab [] = {
|
|||||||
{ ccNone, DoEndScope },
|
{ ccNone, DoEndScope },
|
||||||
{ ccNone, DoUnexpected }, /* .ENDSTRUCT */
|
{ ccNone, DoUnexpected }, /* .ENDSTRUCT */
|
||||||
{ ccNone, DoUnexpected }, /* .ENDUNION */
|
{ ccNone, DoUnexpected }, /* .ENDUNION */
|
||||||
|
{ ccNone, DoEnum },
|
||||||
{ ccNone, DoError },
|
{ ccNone, DoError },
|
||||||
{ ccNone, DoExitMacro },
|
{ ccNone, DoExitMacro },
|
||||||
{ ccNone, DoExport },
|
{ ccNone, DoExport },
|
||||||
|
|||||||
@@ -154,6 +154,7 @@ struct DotKeyword {
|
|||||||
{ ".ELSE", TOK_ELSE },
|
{ ".ELSE", TOK_ELSE },
|
||||||
{ ".ELSEIF", TOK_ELSEIF },
|
{ ".ELSEIF", TOK_ELSEIF },
|
||||||
{ ".END", TOK_END },
|
{ ".END", TOK_END },
|
||||||
|
{ ".ENDENUM", TOK_ENDENUM },
|
||||||
{ ".ENDIF", TOK_ENDIF },
|
{ ".ENDIF", TOK_ENDIF },
|
||||||
{ ".ENDMAC", TOK_ENDMACRO },
|
{ ".ENDMAC", TOK_ENDMACRO },
|
||||||
{ ".ENDMACRO", TOK_ENDMACRO },
|
{ ".ENDMACRO", TOK_ENDMACRO },
|
||||||
@@ -163,6 +164,7 @@ struct DotKeyword {
|
|||||||
{ ".ENDSCOPE", TOK_ENDSCOPE },
|
{ ".ENDSCOPE", TOK_ENDSCOPE },
|
||||||
{ ".ENDSTRUCT", TOK_ENDSTRUCT },
|
{ ".ENDSTRUCT", TOK_ENDSTRUCT },
|
||||||
{ ".ENDUNION", TOK_ENDUNION },
|
{ ".ENDUNION", TOK_ENDUNION },
|
||||||
|
{ ".ENUM", TOK_ENUM },
|
||||||
{ ".ERROR", TOK_ERROR },
|
{ ".ERROR", TOK_ERROR },
|
||||||
{ ".EXITMAC", TOK_EXITMACRO },
|
{ ".EXITMAC", TOK_EXITMACRO },
|
||||||
{ ".EXITMACRO", TOK_EXITMACRO },
|
{ ".EXITMACRO", TOK_EXITMACRO },
|
||||||
|
|||||||
@@ -144,7 +144,8 @@ enum Token {
|
|||||||
TOK_DWORD,
|
TOK_DWORD,
|
||||||
TOK_ELSE,
|
TOK_ELSE,
|
||||||
TOK_ELSEIF,
|
TOK_ELSEIF,
|
||||||
TOK_END,
|
TOK_END,
|
||||||
|
TOK_ENDENUM,
|
||||||
TOK_ENDIF,
|
TOK_ENDIF,
|
||||||
TOK_ENDMACRO,
|
TOK_ENDMACRO,
|
||||||
TOK_ENDPROC,
|
TOK_ENDPROC,
|
||||||
@@ -152,6 +153,7 @@ enum Token {
|
|||||||
TOK_ENDSCOPE,
|
TOK_ENDSCOPE,
|
||||||
TOK_ENDSTRUCT,
|
TOK_ENDSTRUCT,
|
||||||
TOK_ENDUNION,
|
TOK_ENDUNION,
|
||||||
|
TOK_ENUM,
|
||||||
TOK_ERROR,
|
TOK_ERROR,
|
||||||
TOK_EXITMACRO,
|
TOK_EXITMACRO,
|
||||||
TOK_EXPORT,
|
TOK_EXPORT,
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
/* */
|
/* */
|
||||||
/* struct.c */
|
/* struct.c */
|
||||||
/* */
|
/* */
|
||||||
/* .STRUCT command */
|
/* .STRUCT/.UNION commands */
|
||||||
/* */
|
/* */
|
||||||
/* */
|
/* */
|
||||||
/* */
|
/* */
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
/* */
|
/* */
|
||||||
/* struct.h */
|
/* struct.h */
|
||||||
/* */
|
/* */
|
||||||
/* .STRUCT command */
|
/* .STRUCT/.UNION commands */
|
||||||
/* */
|
/* */
|
||||||
/* */
|
/* */
|
||||||
/* */
|
/* */
|
||||||
|
|||||||
@@ -63,8 +63,8 @@
|
|||||||
#define ST_GLOBAL 0x00 /* Root level */
|
#define ST_GLOBAL 0x00 /* Root level */
|
||||||
#define ST_PROC 0x01 /* .PROC */
|
#define ST_PROC 0x01 /* .PROC */
|
||||||
#define ST_SCOPE 0x02 /* .SCOPE */
|
#define ST_SCOPE 0x02 /* .SCOPE */
|
||||||
#define ST_STRUCT 0x03 /* .STRUCT */
|
#define ST_STRUCT 0x03 /* .STRUCT/.UNION */
|
||||||
#define ST_UNION 0x04 /* .UNION */
|
#define ST_ENUM 0x04 /* .ENUM */
|
||||||
#define ST_UNDEF 0xFF
|
#define ST_UNDEF 0xFF
|
||||||
|
|
||||||
/* A symbol table */
|
/* A symbol table */
|
||||||
|
|||||||
Reference in New Issue
Block a user