Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

O.I.L. - Concept: Variadic Type-cast

Greetings,

Over the past few days I've been working on my compiler foundation, the objectified intermediate language, kid sister project to OILexer.

A while back I had an idea that I call a variadic type-cast; that is, a type-cast that determines the type to cast an object to, based upon whether it is one of the types within the cast. This concept is most useful when the target framework doesn't have a visitor model to use, or implementing the visitor target would bloat the code because the focus of your code is much smaller than that of the visitor model.

Here's how you'd currently do it, without a variadic type-cast:
Code:
[font=courier new][color=#0000FF]if[/color] [color=#808080]([/color][color=#008080]item[/color] [color=#808000]is[/color] [color=Magenta]ILiteralCharReferenceProductionRuleItem[/color][color=#808080])[/color]

    [color=#008080]DisplaySyntax[/color][color=#808080]([/color][color=#808080]([/color][color=Magenta]ILiteralCharReferenceProductionRuleItem[/color][color=#808080])[/color][color=#008080]item[/color][color=#808080],[/color] [color=#0000FF]ref[/color] [color=#008080]startingLine[/color][color=#808080],[/color] [color=#008080]depth[/color][color=#808080])[/color][color=#808080];[/color]

[color=#0000FF]else[/color] [color=#0000FF]if[/color] [color=#808080]([/color][color=#008080]item[/color] [color=#808000]is[/color] [color=Magenta]ILiteralStringReferenceProductionRuleItem[/color][color=#808080])[/color]

    [color=#008080]DisplaySyntax[/color][color=#808080]([/color][color=#808080]([/color][color=Magenta]ILiteralStringReferenceProductionRuleItem[/color][color=#808080])[/color][color=#008080]item[/color][color=#808080],[/color] [color=#0000FF]ref[/color] [color=#008080]startingLine[/color][color=#808080],[/color] [color=#008080]depth[/color][color=#808080])[/color][color=#808080];[/color]

[color=#0000FF]else[/color] [color=#0000FF]if[/color] [color=#808080]([/color][color=#008080]item[/color] [color=#808000]is[/color] [color=Magenta]IRuleReferenceProductionRuleItem[/color][color=#808080])[/color]

    [color=#008080]DisplaySyntax[/color][color=#808080]([/color][color=#808080]([/color][color=Magenta]IRuleReferenceProductionRuleItem[/color][color=#808080])[/color][color=#008080]item[/color][color=#808080],[/color] [color=#0000FF]ref[/color] [color=#008080]startingLine[/color][color=#808080],[/color] [color=#008080]depth[/color][color=#808080])[/color][color=#808080];[/color]

[color=#0000FF]else[/color] [color=#0000FF]if[/color] [color=#808080]([/color][color=#008080]item[/color] [color=#808000]is[/color] [color=Magenta]ITokenReferenceProductionRuleItem[/color][color=#808080])[/color]

    [color=#008080]DisplaySyntax[/color][color=#808080]([/color][color=#808080]([/color][color=Magenta]ITokenReferenceProductionRuleItem[/color][color=#808080])[/color][color=#008080]item[/color][color=#808080],[/color] [color=#0000FF]ref[/color] [color=#008080]startingLine[/color][color=#808080],[/color] [color=#008080]depth[/color][color=#808080])[/color][color=#808080];[/color]

[color=#0000FF]else[/color] [color=#0000FF]if[/color] [color=#808080]([/color][color=#008080]item[/color] [color=#808000]is[/color] [color=Purple][b]InlinedTokenReferenceTokenItem[/b][/color][color=#808080])[/color]

    [color=#008080]DisplaySyntax[/color][color=#808080]([/color][color=#808080]([/color][color=Purple][b]InlinedTokenReferenceTokenItem[/b][/color][color=#808080])[/color][color=#808080]([/color][color=#0000FF]object[/color][color=#808080])[/color][color=#008080]item[/color][color=#808080],[/color] [color=#0000FF]ref[/color] [color=#008080]startingLine[/color][color=#808080],[/color] [color=#008080]depth[/color][color=#808080])[/color][color=#808080];[/color]

[color=#0000FF]else[/color] [color=#0000FF]if[/color] [color=#808080]([/color][color=#008080]item[/color] [color=#808000]is[/color] [color=Magenta]ILiteralCharTokenItem[/color][color=#808080])[/color]

    [color=#008080]DisplaySyntax[/color][color=#808080]([/color][color=#808080]([/color][color=Magenta]ILiteralCharTokenItem[/color][color=#808080])[/color][color=#008080]item[/color][color=#808080],[/color] [color=#0000FF]ref[/color] [color=#008080]startingLine[/color][color=#808080],[/color] [color=#008080]depth[/color][color=#808080])[/color][color=#808080];[/color]

[color=#0000FF]else[/color] [color=#0000FF]if[/color] [color=#808080]([/color][color=#008080]item[/color] [color=#808000]is[/color] [color=Magenta]ILiteralStringTokenItem[/color][color=#808080])[/color]

    [color=#008080]DisplaySyntax[/color][color=#808080]([/color][color=#808080]([/color][color=Magenta]ILiteralStringTokenItem[/color][color=#808080])[/color][color=#008080]item[/color][color=#808080],[/color] [color=#0000FF]ref[/color] [color=#008080]startingLine[/color][color=#808080],[/color] [color=#008080]depth[/color][color=#808080])[/color][color=#808080];[/color]

[color=#0000FF]else[/color] [color=#0000FF]if[/color] [color=#808080]([/color][color=#008080]item[/color] [color=#808000]is[/color] [color=Magenta]ICharRangeTokenItem[/color][color=#808080])[/color]

    [color=#008080]DisplaySyntax[/color][color=#808080]([/color][color=#808080]([/color][color=Magenta]ICharRangeTokenItem[/color][color=#808080])[/color][color=#008080]item[/color][color=#808080],[/color] [color=#0000FF]ref[/color] [color=#008080]startingLine[/color][color=#808080],[/color] [color=#008080]depth[/color][color=#808080])[/color][color=#808080];[/color]

[color=#0000FF]else[/color] [color=#0000FF]if[/color] [color=#808080]([/color][color=#008080]item[/color] [color=#808000]is[/color] [color=Magenta]ICommandTokenItem[/color][color=#808080])[/color]

    [color=#008080]DisplaySyntax[/color][color=#808080]([/color][color=#808080]([/color][color=Magenta]ICommandTokenItem[/color][color=#808080])[/color][color=#008080]item[/color][color=#808080],[/color] [color=#0000FF]ref[/color] [color=#008080]startingLine[/color][color=#808080],[/color] [color=#008080]depth[/color][color=#808080])[/color][color=#808080];[/color][/font]

Lengthy and annoying to type. A variadic type-cast would simplify it, though it would admittedly take some getting used to:
Code:
[font=courier new][color=#008080]DisplaySyntax[/color][color=#808080]([/color]

    [color=#808080]([/color]

        [color=Magenta]ILiteralCharReferenceProductionRuleItem[/color][color=#808080],[/color]

        [color=Magenta]ILiteralStringReferenceProductionRuleItem[/color][color=#808080],[/color]

        [color=Magenta]IRuleReferenceProductionRuleItem[/color][color=#808080],[/color]

        [color=Magenta]ITokenReferenceProductionRuleItem[/color][color=#808080],[/color]

        [color=Purple][b]InlinedTokenReferenceTokenItem[/b][/color] [color=#808000]|[/color] [color=#0000FF]object[/color][color=#808080],[/color]

        [color=Magenta]ILiteralCharTokenItem[/color][color=#808080],[/color]

        [color=Magenta]ILiteralStringTokenItem[/color][color=#808080],[/color]

        [color=Magenta]ICharRangeTokenItem[/color][color=#808080],[/color]

        [color=Magenta]ICommandTokenItem[/color]

    [color=#808080])[/color][color=#008080]item[/color][color=#808080],[/color] [color=#0000FF]ref[/color] [color=#008080]startingLine[/color][color=#808080],[/color] [color=#008080]depth[/color][color=#808080])[/color][color=#808080];[/color][/font]

In one instance you'll notice an '| object' this is to indicate that to make the cast work, it must be first cast to object, a rare instance that's common in casting a type-parameter to a specific type that's not an interface.

So the question again is, is this useful, or useless?
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top