From 68d63b089d9be2e69defac2b9f6002af459bda3a Mon Sep 17 00:00:00 2001 From: acqn Date: Mon, 3 Aug 2020 01:34:02 +0800 Subject: [PATCH] Reduced error flood raised by misplaced variable declarations. --- src/cc65/declare.c | 2 +- src/cc65/declare.h | 3 +++ src/cc65/expr.c | 31 +++++++++++++++++++++++++++++-- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/cc65/declare.c b/src/cc65/declare.c index 7b868380d..f6aa9c430 100644 --- a/src/cc65/declare.c +++ b/src/cc65/declare.c @@ -263,7 +263,7 @@ static void OptionalSigned (void) -static void InitDeclSpec (DeclSpec* D) +void InitDeclSpec (DeclSpec* D) /* Initialize the DeclSpec struct for use */ { D->StorageClass = 0; diff --git a/src/cc65/declare.h b/src/cc65/declare.h index 117ac14a6..615f16a4a 100644 --- a/src/cc65/declare.h +++ b/src/cc65/declare.h @@ -93,6 +93,9 @@ typedef enum { +void InitDeclSpec (DeclSpec* D); +/* Initialize the DeclSpec struct for use */ + Type* ParseType (Type* Type); /* Parse a complete type specification */ diff --git a/src/cc65/expr.c b/src/cc65/expr.c index 65238af61..7b9fd0b9d 100644 --- a/src/cc65/expr.c +++ b/src/cc65/expr.c @@ -920,8 +920,35 @@ static void Primary (ExprDesc* E) /* Illegal primary. Be sure to skip the token to avoid endless ** error loops. */ - Error ("Expression expected"); - NextToken (); + { + /* Let's see if this is a C99-style declaration */ + DeclSpec Spec; + InitDeclSpec (&Spec); + ParseDeclSpec (&Spec, -1, T_QUAL_NONE); + + if (Spec.Type->C != T_END) { + + Error ("Mixed declarations and code are not supported in cc65"); + while (CurTok.Tok != TOK_SEMI) { + Declaration Decl; + + /* Parse one declaration */ + ParseDecl (&Spec, &Decl, DM_ACCEPT_IDENT); + if (CurTok.Tok == TOK_ASSIGN) { + NextToken (); + ParseInit (Decl.Type); + } + if (CurTok.Tok == TOK_COMMA) { + NextToken (); + } else { + break; + } + } + } else { + Error ("Expression expected"); + NextToken (); + } + } ED_MakeConstAbsInt (E, 1); break; }