Added parsing of volatile qualifier

This doesn't affect code execution, but is exposed through TypeParse().
This commit is contained in:
Russell Joyce 2020-06-09 12:30:47 +01:00
parent 409f520b61
commit 45d85111f0
No known key found for this signature in database
GPG key ID: 3D46BD9018AF7B72
9 changed files with 31 additions and 20 deletions

View file

@ -327,7 +327,7 @@ void PlatformLibraryInit()
/* define an example structure */ /* define an example structure */
Tokens = LexAnalyse(IntrinsicName, StructDefinition, strlen(StructDefinition), NULL); Tokens = LexAnalyse(IntrinsicName, StructDefinition, strlen(StructDefinition), NULL);
LexInitParser(&Parser, StructDefinition, Tokens, IntrinsicName, true, false); LexInitParser(&Parser, StructDefinition, Tokens, IntrinsicName, true, false);
TypeParse(&Parser, &ParsedType, &Identifier, &IsStatic); TypeParse(&Parser, &ParsedType, &Identifier, &IsStatic, &IsVolatile);
HeapFree(Tokens); HeapFree(Tokens);
} }
``` ```

View file

@ -46,7 +46,7 @@ void LibraryAdd(Picoc *pc, struct LibraryFunction *FuncList)
strlen((char*)FuncList[Count].Prototype), NULL); strlen((char*)FuncList[Count].Prototype), NULL);
LexInitParser(&Parser, pc, FuncList[Count].Prototype, Tokens, LexInitParser(&Parser, pc, FuncList[Count].Prototype, Tokens,
IntrinsicName, true, false); IntrinsicName, true, false);
TypeParse(&Parser, &ReturnType, &Identifier, NULL); TypeParse(&Parser, &ReturnType, &Identifier, NULL, NULL);
NewValue = ParseFunctionDefinition(&Parser, ReturnType, Identifier); NewValue = ParseFunctionDefinition(&Parser, ReturnType, Identifier);
NewValue->Val->FuncDef.Intrinsic = FuncList[Count].Func; NewValue->Val->FuncDef.Intrinsic = FuncList[Count].Func;
HeapFreeMem(pc, Tokens); HeapFreeMem(pc, Tokens);

View file

@ -1469,7 +1469,7 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result)
char *CastIdentifier; char *CastIdentifier;
struct Value *CastTypeValue; struct Value *CastTypeValue;
TypeParse(Parser, &CastType, &CastIdentifier, NULL); TypeParse(Parser, &CastType, &CastIdentifier, NULL, NULL);
if (LexGetToken(Parser, &LexValue, true) != TokenCloseBracket) if (LexGetToken(Parser, &LexValue, true) != TokenCloseBracket)
ProgramFail(Parser, "brackets not closed"); ProgramFail(Parser, "brackets not closed");
@ -1668,7 +1668,7 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result)
PrefixState = false; PrefixState = false;
ParserCopy(Parser, &PreState); ParserCopy(Parser, &PreState);
TypeParse(Parser, &Typ, &Identifier, NULL); TypeParse(Parser, &Typ, &Identifier, NULL, NULL);
TypeValue = VariableAllocValueFromType(Parser->pc, Parser, TypeValue = VariableAllocValueFromType(Parser->pc, Parser,
&Parser->pc->TypeType, false, NULL, false); &Parser->pc->TypeType, false, NULL, false);
TypeValue->Val->Typ = Typ; TypeValue->Val->Typ = Typ;

View file

@ -174,7 +174,8 @@ enum LexToken {
/* 0x5c */ TokenEOF, /* 0x5c */ TokenEOF,
TokenEndOfLine, TokenEndOfLine,
TokenEndOfFunction, TokenEndOfFunction,
TokenBackSlash TokenBackSlash,
TokenVolatileType
}; };
/* used in dynamic memory allocation */ /* used in dynamic memory allocation */
@ -571,11 +572,11 @@ extern int TypeSizeValue(struct Value *Val, int Compact);
extern int TypeStackSizeValue(struct Value *Val); extern int TypeStackSizeValue(struct Value *Val);
extern int TypeLastAccessibleOffset(Picoc *pc, struct Value *Val); extern int TypeLastAccessibleOffset(Picoc *pc, struct Value *Val);
extern int TypeParseFront(struct ParseState *Parser, struct ValueType **Typ, extern int TypeParseFront(struct ParseState *Parser, struct ValueType **Typ,
int *IsStatic); int *IsStatic, int *IsVolatile);
extern void TypeParseIdentPart(struct ParseState *Parser, extern void TypeParseIdentPart(struct ParseState *Parser,
struct ValueType *BasicTyp, struct ValueType **Typ, char **Identifier); struct ValueType *BasicTyp, struct ValueType **Typ, char **Identifier);
extern void TypeParse(struct ParseState *Parser, struct ValueType **Typ, extern void TypeParse(struct ParseState *Parser, struct ValueType **Typ,
char **Identifier, int *IsStatic); char **Identifier, int *IsStatic, int *IsVolatile);
extern struct ValueType *TypeGetMatching(Picoc *pc, struct ParseState *Parser, extern struct ValueType *TypeGetMatching(Picoc *pc, struct ParseState *Parser,
struct ValueType *ParentType, enum BaseType Base, int ArraySize, const char *Identifier, int AllowDuplicates); struct ValueType *ParentType, enum BaseType Base, int ArraySize, const char *Identifier, int AllowDuplicates);
extern struct ValueType *TypeCreateOpaqueStruct(Picoc *pc, struct ParseState *Parser, extern struct ValueType *TypeCreateOpaqueStruct(Picoc *pc, struct ParseState *Parser,

5
lex.c
View file

@ -93,7 +93,8 @@ static struct ReservedWord ReservedWords[] = {
{"union", TokenUnionType}, {"union", TokenUnionType},
{"unsigned", TokenUnsignedType}, {"unsigned", TokenUnsignedType},
{"void", TokenVoidType}, {"void", TokenVoidType},
{"while", TokenWhile} {"while", TokenWhile},
{"volatile", TokenVolatileType}
}; };
@ -848,7 +849,7 @@ enum LexToken LexGetRawToken(struct ParseState *Parser, struct Value **Value,
#ifdef DEBUG_LEXER #ifdef DEBUG_LEXER
printf("Got token=%02x inc=%d pos=%d\n", Token, IncPos, Parser->CharacterPos); printf("Got token=%02x inc=%d pos=%d\n", Token, IncPos, Parser->CharacterPos);
#endif #endif
assert(Token >= TokenNone && Token <= TokenEndOfFunction); assert(Token >= TokenNone && Token <= TokenVolatileType);
return Token; return Token;
} }

View file

@ -121,7 +121,7 @@ struct Value *ParseFunctionDefinition(struct ParseState *Parser,
break; break;
} else { } else {
/* add a parameter */ /* add a parameter */
TypeParse(&ParamParser, &ParamType, &ParamIdentifier, NULL); TypeParse(&ParamParser, &ParamType, &ParamIdentifier, NULL, NULL);
if (ParamType->Base == TypeVoid) { if (ParamType->Base == TypeVoid) {
/* this isn't a real parameter at all - delete it */ /* this isn't a real parameter at all - delete it */
//ParamCount--; //ParamCount--;
@ -338,6 +338,7 @@ void ParseDeclarationAssignment(struct ParseState *Parser,
int ParseDeclaration(struct ParseState *Parser, enum LexToken Token) int ParseDeclaration(struct ParseState *Parser, enum LexToken Token)
{ {
int IsStatic = false; int IsStatic = false;
int IsVolatile = false;
int FirstVisit = false; int FirstVisit = false;
char *Identifier; char *Identifier;
struct ValueType *BasicType; struct ValueType *BasicType;
@ -345,7 +346,7 @@ int ParseDeclaration(struct ParseState *Parser, enum LexToken Token)
struct Value *NewVariable = NULL; struct Value *NewVariable = NULL;
Picoc *pc = Parser->pc; Picoc *pc = Parser->pc;
TypeParseFront(Parser, &BasicType, &IsStatic); TypeParseFront(Parser, &BasicType, &IsStatic, &IsVolatile);
do { do {
TypeParseIdentPart(Parser, BasicType, &Typ, &Identifier); TypeParseIdentPart(Parser, BasicType, &Typ, &Identifier);
if ((Token != TokenVoidType && Token != TokenStructType && if ((Token != TokenVoidType && Token != TokenStructType &&
@ -576,7 +577,7 @@ void ParseTypedef(struct ParseState *Parser)
struct ValueType **TypPtr; struct ValueType **TypPtr;
struct Value InitValue; struct Value InitValue;
TypeParse(Parser, &Typ, &TypeName, NULL); TypeParse(Parser, &Typ, &TypeName, NULL, NULL);
if (Parser->Mode == RunModeRun) { if (Parser->Mode == RunModeRun) {
TypPtr = &Typ; TypPtr = &Typ;
@ -737,6 +738,7 @@ enum ParseResult ParseStatement(struct ParseState *Parser,
case TokenAutoType: case TokenAutoType:
case TokenRegisterType: case TokenRegisterType:
case TokenExternType: case TokenExternType:
case TokenVolatileType:
*Parser = PreState; *Parser = PreState;
CheckTrailingSemicolon = ParseDeclaration(Parser, Token); CheckTrailingSemicolon = ParseDeclaration(Parser, Token);
break; break;

View file

@ -111,7 +111,8 @@ struct LexTokenStat LexTokenStats[NO_TOKENS] = {
{"TokenEOF", {0, 0, 0, 0, 0, 0, 0}}, {"TokenEOF", {0, 0, 0, 0, 0, 0, 0}},
{"TokenEndOfLine", {0, 0, 0, 0, 0, 0, 0}}, {"TokenEndOfLine", {0, 0, 0, 0, 0, 0, 0}},
{"TokenEndOfFunction", {0, 0, 0, 0, 0, 0, 0}}, {"TokenEndOfFunction", {0, 0, 0, 0, 0, 0, 0}},
{"TokenBackSlash", {0, 0, 0, 0, 0, 0, 0}} {"TokenBackSlash", {0, 0, 0, 0, 0, 0, 0}},
{"TokenVolatileType", {0, 0, 0, 0, 0, 0, 0}}
}; };

View file

@ -8,7 +8,7 @@
#include "interpreter.h" #include "interpreter.h"
#define NO_RUN_MODES 7 #define NO_RUN_MODES 7
#define NO_TOKENS 97 #define NO_TOKENS 98
extern const char *RunModeNames[NO_RUN_MODES]; extern const char *RunModeNames[NO_RUN_MODES];

18
type.c
View file

@ -265,7 +265,7 @@ void TypeParseStruct(struct ParseState *Parser, struct ValueType **Typ,
STRUCT_TABLE_SIZE, true); STRUCT_TABLE_SIZE, true);
do { do {
TypeParse(Parser, &MemberType, &MemberIdentifier, NULL); TypeParse(Parser, &MemberType, &MemberIdentifier, NULL, NULL);
if (MemberType == NULL || MemberIdentifier == NULL) if (MemberType == NULL || MemberIdentifier == NULL)
ProgramFail(Parser, "invalid type in struct"); ProgramFail(Parser, "invalid type in struct");
@ -393,10 +393,11 @@ void TypeParseEnum(struct ParseState *Parser, struct ValueType **Typ)
/* parse a type - just the basic type */ /* parse a type - just the basic type */
int TypeParseFront(struct ParseState *Parser, struct ValueType **Typ, int TypeParseFront(struct ParseState *Parser, struct ValueType **Typ,
int *IsStatic) int *IsStatic, int *IsVolatile)
{ {
int Unsigned = false; int Unsigned = false;
int StaticQualifier = false; int StaticQualifier = false;
int VolatileQualifier = false;
enum LexToken Token; enum LexToken Token;
struct ParseState Before; struct ParseState Before;
struct Value *LexerValue; struct Value *LexerValue;
@ -408,15 +409,20 @@ int TypeParseFront(struct ParseState *Parser, struct ValueType **Typ,
ParserCopy(&Before, Parser); ParserCopy(&Before, Parser);
Token = LexGetToken(Parser, &LexerValue, true); Token = LexGetToken(Parser, &LexerValue, true);
while (Token == TokenStaticType || Token == TokenAutoType || while (Token == TokenStaticType || Token == TokenAutoType ||
Token == TokenRegisterType || Token == TokenExternType) { Token == TokenRegisterType || Token == TokenExternType ||
Token == TokenVolatileType) {
if (Token == TokenStaticType) if (Token == TokenStaticType)
StaticQualifier = true; StaticQualifier = true;
else if (Token == TokenVolatileType)
VolatileQualifier = true;
Token = LexGetToken(Parser, &LexerValue, true); Token = LexGetToken(Parser, &LexerValue, true);
} }
if (IsStatic != NULL) if (IsStatic != NULL)
*IsStatic = StaticQualifier; *IsStatic = StaticQualifier;
if (IsVolatile != NULL)
*IsVolatile = VolatileQualifier;
/* handle signed/unsigned with no trailing type */ /* handle signed/unsigned with no trailing type */
if (Token == TokenSignedType || Token == TokenUnsignedType) { if (Token == TokenSignedType || Token == TokenUnsignedType) {
@ -541,7 +547,7 @@ void TypeParseIdentPart(struct ParseState *Parser, struct ValueType *BasicTyp,
if (*Typ != NULL) if (*Typ != NULL)
ProgramFail(Parser, "bad type declaration"); ProgramFail(Parser, "bad type declaration");
TypeParse(Parser, Typ, Identifier, NULL); TypeParse(Parser, Typ, Identifier, NULL, NULL);
if (LexGetToken(Parser, NULL, true) != TokenCloseBracket) if (LexGetToken(Parser, NULL, true) != TokenCloseBracket)
ProgramFail(Parser, "')' expected"); ProgramFail(Parser, "')' expected");
break; break;
@ -577,11 +583,11 @@ void TypeParseIdentPart(struct ParseState *Parser, struct ValueType *BasicTyp,
/* parse a type - a complete declaration including identifier */ /* parse a type - a complete declaration including identifier */
void TypeParse(struct ParseState *Parser, struct ValueType **Typ, void TypeParse(struct ParseState *Parser, struct ValueType **Typ,
char **Identifier, int *IsStatic) char **Identifier, int *IsStatic, int *IsVolatile)
{ {
struct ValueType *BasicType; struct ValueType *BasicType;
TypeParseFront(Parser, &BasicType, IsStatic); TypeParseFront(Parser, &BasicType, IsStatic, IsVolatile);
TypeParseIdentPart(Parser, BasicType, Typ, Identifier); TypeParseIdentPart(Parser, BasicType, Typ, Identifier);
} }