From b10b7cd3e899a25ebb1b91f7cf0fa7ae30721447 Mon Sep 17 00:00:00 2001 From: cuz Date: Sun, 9 May 2004 20:24:51 +0000 Subject: [PATCH] Fixed a problem with {} enclosed token lists and implemented them for .blank and .tcount. git-svn-id: svn://svn.cc65.org/cc65/trunk@3014 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/ca65/expr.c | 87 +++++++++++++++++++++------------------------- src/ca65/macro.c | 15 ++------ src/ca65/nexttok.c | 11 ++---- src/ca65/toklist.c | 17 +++++++++ src/ca65/toklist.h | 14 +++++--- 5 files changed, 70 insertions(+), 74 deletions(-) diff --git a/src/ca65/expr.c b/src/ca65/expr.c index e3a1f6c0a..b3e2b3617 100644 --- a/src/ca65/expr.c +++ b/src/ca65/expr.c @@ -323,31 +323,35 @@ static ExprNode* FuncBankByte (void) static ExprNode* FuncBlank (void) /* Handle the .BLANK builtin function */ { - int Result = 1; - - /* Assume no tokens if the closing brace follows (this is not correct in - * all cases, since the token may be the closing brace, but this will - * give a syntax error anyway and may not be handled by .BLANK. + /* We have a list of tokens that ends with the closing paren. Skip + * the tokens, and count them. Allow optionally curly braces. */ - if (Tok != TOK_RPAREN) { - /* Skip any tokens */ - int Braces = 0; - while (!TokIsSep (Tok)) { - if (Tok == TOK_LPAREN) { - ++Braces; - } else if (Tok == TOK_RPAREN) { - if (Braces == 0) { - /* Done */ - break; - } else { - --Braces; - } - } - NextTok (); + enum Token Term = GetTokListTerm (TOK_RPAREN); + unsigned Count = 0; + while (Tok != Term) { + + /* Check for end of line or end of input. Since the calling function + * will check for the closing paren, we don't need to print an error + * here, just bail out. + */ + if (TokIsSep (Tok)) { + break; } - Result = 0; + + /* One more token */ + ++Count; + + /* Skip the token */ + NextTok (); } - return GenLiteralExpr (Result); + + /* If the list was enclosed in curly braces, skip the closing brace */ + if (Term == TOK_RCURLY && Tok == TOK_RCURLY) { + NextTok (); + } + + /* Return true if the list was empty */ + return GenLiteralExpr (Count == 0); } @@ -418,7 +422,6 @@ static ExprNode* DoMatch (enum TC EqualityLevel) /* Handle the .MATCH and .XMATCH builtin functions */ { int Result; - enum Token Term; TokNode* Root = 0; TokNode* Last = 0; TokNode* Node; @@ -427,12 +430,7 @@ static ExprNode* DoMatch (enum TC EqualityLevel) * single linked list of tokens including attributes. The list is * either enclosed in curly braces, or terminated by a comma. */ - if (Tok == TOK_LCURLY) { - NextTok (); - Term = TOK_RCURLY; - } else { - Term = TOK_COMMA; - } + enum Token Term = GetTokListTerm (TOK_COMMA); while (Tok != Term) { /* We may not end-of-line of end-of-file here */ @@ -465,15 +463,10 @@ static ExprNode* DoMatch (enum TC EqualityLevel) } /* Read the second list which is optionally enclosed in curly braces and - * terminated by the right parenthesis. Compare each token against the + * terminated by the right parenthesis. Compare each token against the * one in the first list. */ - if (Tok == TOK_LCURLY) { - NextTok (); - Term = TOK_RCURLY; - } else { - Term = TOK_RPAREN; - } + Term = GetTokListTerm (TOK_RPAREN); Result = 1; Node = Root; while (Tok != Term) { @@ -716,11 +709,11 @@ static ExprNode* FuncTCount (void) /* Handle the .TCOUNT function */ { /* We have a list of tokens that ends with the closing paren. Skip - * the tokens, handling nested braces and count them. + * the tokens, and count them. Allow optionally curly braces. */ - int Count = 0; - unsigned Parens = 0; - while (Parens != 0 || Tok != TOK_RPAREN) { + enum Token Term = GetTokListTerm (TOK_RPAREN); + int Count = 0; + while (Tok != Term) { /* Check for end of line or end of input. Since the calling function * will check for the closing paren, we don't need to print an error @@ -733,17 +726,15 @@ static ExprNode* FuncTCount (void) /* One more token */ ++Count; - /* Keep track of the nesting level */ - switch (Tok) { - case TOK_LPAREN: ++Parens; break; - case TOK_RPAREN: --Parens; break; - default: break; - } - /* Skip the token */ NextTok (); } + /* If the list was enclosed in curly braces, skip the closing brace */ + if (Term == TOK_RCURLY && Tok == TOK_RCURLY) { + NextTok (); + } + /* Return the number of tokens */ return GenLiteralExpr (Count); } @@ -760,7 +751,7 @@ static ExprNode* FuncXMatch (void) static ExprNode* Function (ExprNode* (*F) (void)) /* Handle builtin functions */ -{ +{ ExprNode* E; /* Skip the keyword */ diff --git a/src/ca65/macro.c b/src/ca65/macro.c index aea87253c..06d619582 100644 --- a/src/ca65/macro.c +++ b/src/ca65/macro.c @@ -677,12 +677,7 @@ static void StartExpClassic (Macro* M) } /* The macro may optionally be enclosed in curly braces */ - if (Tok == TOK_LCURLY) { - NextTok (); - Term = TOK_RCURLY; - } else { - Term = TOK_COMMA; - } + Term = GetTokListTerm (TOK_COMMA); /* Read tokens for one parameter, accept empty params */ Last = 0; @@ -760,16 +755,10 @@ static void StartExpDefine (Macro* M) /* Read the actual parameters */ while (Count--) { - enum Token Term; TokNode* Last; /* The macro may optionally be enclosed in curly braces */ - if (Tok == TOK_LCURLY) { - NextTok (); - Term = TOK_RCURLY; - } else { - Term = TOK_COMMA; - } + enum Token Term = GetTokListTerm (TOK_COMMA); /* Check if there is really a parameter */ if (TokIsSep (Tok) || Tok == Term) { diff --git a/src/ca65/nexttok.c b/src/ca65/nexttok.c index 4c89bc1a7..b455d32fd 100644 --- a/src/ca65/nexttok.c +++ b/src/ca65/nexttok.c @@ -71,22 +71,15 @@ static TokList* CollectTokens (unsigned Start, unsigned Count) * return this token list. */ { - enum Token Term; - unsigned Current; /* Create the token list */ TokList* List = NewTokList (); /* Determine if the list is enclosed in curly braces. */ - if (Tok == TOK_LCURLY) { - NextTok (); - Term = TOK_RCURLY; - } else { - Term = TOK_LCURLY; - } + enum Token Term = GetTokListTerm (TOK_RPAREN); /* Read the token list */ - Current = 0; + unsigned Current = 0; while (Tok != Term) { /* Check for end of line or end of input */ diff --git a/src/ca65/toklist.c b/src/ca65/toklist.c index ff1cfa267..a6830613a 100644 --- a/src/ca65/toklist.c +++ b/src/ca65/toklist.c @@ -42,6 +42,7 @@ /* ca65 */ #include "error.h" #include "istack.h" +#include "nexttok.h" #include "scanner.h" #include "toklist.h" @@ -174,6 +175,22 @@ void FreeTokList (TokList* List) +enum Token GetTokListTerm (enum Token Term) +/* Determine if the following token list is enclosed in curly braces. This is + * the case if the next token is the opening brace. If so, skip it and return + * a closing brace, otherwise return Term. + */ +{ + if (Tok == TOK_LCURLY) { + NextTok (); + return TOK_RCURLY; + } else { + return Term; + } +} + + + void AddCurTok (TokList* List) /* Add the current token to the token list */ { diff --git a/src/ca65/toklist.h b/src/ca65/toklist.h index c195d54fb..642469521 100644 --- a/src/ca65/toklist.h +++ b/src/ca65/toklist.h @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 2000 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (C) 2000-2004 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 */ @@ -109,6 +109,12 @@ TokList* NewTokList (void); void FreeTokList (TokList* T); /* Delete the token list including all token nodes */ +enum Token GetTokListTerm (enum Token Term); +/* Determine if the following token list is enclosed in curly braces. This is + * the case if the next token is the opening brace. If so, skip it and return + * a closing brace, otherwise return Term. + */ + void AddCurTok (TokList* T); /* Add the current token to the token list */