From 62969cb0ee620e0fb5020c1d8b0ba2092ed7c387 Mon Sep 17 00:00:00 2001 From: Russell Joyce Date: Thu, 11 Jun 2020 11:30:51 +0100 Subject: [PATCH] Added handling of 'const' type qualifier keyword There is currently no enforcement of a variable being constant, and the actual type is the same as if 'const' wasn't present. --- c-tests/types.c | 5 ++++- interpreter.h | 3 ++- lex.c | 5 +++-- parse.c | 1 + stats.c | 3 ++- stats.h | 2 +- type.c | 4 ++-- 7 files changed, 15 insertions(+), 8 deletions(-) diff --git a/c-tests/types.c b/c-tests/types.c index 93f7463..353a95f 100644 --- a/c-tests/types.c +++ b/c-tests/types.c @@ -9,7 +9,10 @@ unsigned long f; unsigned long int g; volatile int i; -int volatile j; +int volatile static j; + +const int x; +long const y; int main(void) { return 0; diff --git a/interpreter.h b/interpreter.h index 7b58d14..cfee88a 100644 --- a/interpreter.h +++ b/interpreter.h @@ -177,7 +177,8 @@ enum LexToken { TokenBackSlash, TokenVolatileType, TokenHashPragma, - TokenUnderscorePragma + TokenUnderscorePragma, + TokenConstType }; /* used in dynamic memory allocation */ diff --git a/lex.c b/lex.c index 489daba..f27c314 100644 --- a/lex.c +++ b/lex.c @@ -96,7 +96,8 @@ static struct ReservedWord ReservedWords[] = { {"while", TokenWhile}, {"volatile", TokenVolatileType}, {"#pragma", TokenHashPragma}, - {"_Pragma", TokenUnderscorePragma} + {"_Pragma", TokenUnderscorePragma}, + {"const", TokenConstType} }; @@ -851,7 +852,7 @@ enum LexToken LexGetRawToken(struct ParseState *Parser, struct Value **Value, #ifdef DEBUG_LEXER printf("Got token=%02x inc=%d pos=%d\n", Token, IncPos, Parser->CharacterPos); #endif - assert(Token >= TokenNone && Token <= TokenUnderscorePragma); + assert(Token >= TokenNone && Token <= TokenConstType); return Token; } diff --git a/parse.c b/parse.c index 2dbbd9b..591ce22 100644 --- a/parse.c +++ b/parse.c @@ -748,6 +748,7 @@ enum ParseResult ParseStatement(struct ParseState *Parser, case TokenRegisterType: case TokenExternType: case TokenVolatileType: + case TokenConstType: *Parser = PreState; CheckTrailingSemicolon = ParseDeclaration(Parser, Token); break; diff --git a/stats.c b/stats.c index 405d547..88da697 100644 --- a/stats.c +++ b/stats.c @@ -114,7 +114,8 @@ struct LexTokenStat LexTokenStats[NO_TOKENS] = { {"TokenBackSlash", {0, 0, 0, 0, 0, 0, 0}}, {"TokenVolatileType", {0, 0, 0, 0, 0, 0, 0}}, {"TokenHashPragma", {0, 0, 0, 0, 0, 0, 0}}, - {"TokenUnderscorePragma", {0, 0, 0, 0, 0, 0, 0}} + {"TokenUnderscorePragma", {0, 0, 0, 0, 0, 0, 0}}, + {"TokenConstType", {0, 0, 0, 0, 0, 0, 0}} }; diff --git a/stats.h b/stats.h index 0ca78e9..ffceeb4 100644 --- a/stats.h +++ b/stats.h @@ -8,7 +8,7 @@ #include "interpreter.h" #define NO_RUN_MODES 7 -#define NO_TOKENS 100 +#define NO_TOKENS 101 extern const char *RunModeNames[NO_RUN_MODES]; diff --git a/type.c b/type.c index 57ec289..d9cc0d3 100644 --- a/type.c +++ b/type.c @@ -411,7 +411,7 @@ int TypeParseFront(struct ParseState *Parser, struct ValueType **Typ, /* handle any leading type qualifiers/storage classes */ while (Token == TokenStaticType || Token == TokenAutoType || Token == TokenRegisterType || Token == TokenExternType || - Token == TokenVolatileType) { + Token == TokenVolatileType || Token == TokenConstType) { if (Token == TokenStaticType) StaticQualifier = true; else if (Token == TokenVolatileType) @@ -424,7 +424,7 @@ int TypeParseFront(struct ParseState *Parser, struct ValueType **Typ, enum LexToken FollowToken = LexGetToken(Parser, &LexerValue, false); while (FollowToken == TokenStaticType || FollowToken == TokenAutoType || FollowToken == TokenRegisterType || FollowToken == TokenExternType || - FollowToken == TokenVolatileType) { + FollowToken == TokenVolatileType || FollowToken == TokenConstType) { if (FollowToken == TokenStaticType) StaticQualifier = true; else if (FollowToken == TokenVolatileType)