Casting improvement (#3)

* d

* Removed placeholder

* Parser

- Added a new default parameter to `parseTypedDeclaration()` which is `onlyType=false`. If one sets this to `true` then we will parse the type till the identifier (what would be it) but stopping on the identifier and returning a bogus `TypedEntity` with a fake name string but with the type string intact
- `parseCast()` now uses `parseTypedDeclaration(onlyType=true)` - this should help us when we want to cast for arrays or pointers (where all that logic is in `parseTypedDeclaration()`).

Test cases

- Added a complex `cast(<expr>)` with a pointer - this should work in upstream `pointers` branch
- File name is `simple_cast_complex_type.t`
This commit is contained in:
Tristan B. Velloza Kildaire 2023-04-14 13:38:44 +02:00 committed by GitHub
parent a346f60c2e
commit 814f595cd5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 5 deletions

View File

@ -1035,10 +1035,22 @@ public final class Parser
expect(SymbolType.LBRACE, getCurrentToken());
nextToken();
/* Expect a type */
expect(SymbolType.IDENT_TYPE, getCurrentToken());
string toType = getCurrentToken().getToken();
nextToken();
/**
* Expect a type
*
* The way we do this is to re-use the logic
* that `parseTypedDeclaration()` uses but we
* ask it to not parse further than the last token
* constituting the type (i.e. before starting to
* parse the identifier token).
*
* It then will return a bogus `TypedEntity` with
* a verfiable bogus name `BOGUS_NAME_STOP_SHORT_OF_IDENTIFIER_TYPE_FETCH` (TODO: Make sure we use this)
* which means we can call `getType()` and extract
* the type string
*/
TypedEntity bogusEntity = parseTypedDeclaration(false, false, false, true);
string toType = bogusEntity.getType();
/* Expect a `)` closing brace */
expect(SymbolType.RBRACE, getCurrentToken());
@ -1432,7 +1444,7 @@ public final class Parser
return retExpression[0];
}
private TypedEntity parseTypedDeclaration(bool wantsBody = true, bool allowVarDec = true, bool allowFuncDef = true)
private TypedEntity parseTypedDeclaration(bool wantsBody = true, bool allowVarDec = true, bool allowFuncDef = true, bool onlyType = false)
{
gprintln("parseTypedDeclaration(): Enter", DebugType.WARNING);
@ -1457,6 +1469,15 @@ public final class Parser
type=type~"*";
nextToken();
}
/* If were requested to only find a type, then stop here and return it */
if(onlyType)
{
/* Create a bogus TypedEntity for the sole purpose of returning the type */
generated = new TypedEntity("BOGUS_NAME_STOP_SHORT_OF_IDENTIFIER_TYPE_FETCH", type);
return generated;
}
/* Expect an identifier (CAN NOT be dotted) */
expect(SymbolType.IDENT_TYPE, getCurrentToken());

View File

@ -0,0 +1,9 @@
module simple_cast_complex_type;
int myInt;
void function(int x)
{
byte bruh;
byte myByte = (cast(byte)int*)+bruh;
}