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: Expression Workspace

Greetings,

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

I've been musing over a concept that I call an 'Expression Workspace', that is, an expression that has a series of statements associated to it, which delineate extra statements to help prepare the expression for whatever receives it, be it a variable, method argument, or what have you. To help illustrate this concept, I've prepared a piece of code in two chunks, first in its original form, used in a unit-testing style program to debug the framework, and second in its final form to illustrate the kind of format that would result.

In its original state, the code looks fairly straightforward, but it lacks a level of elegance and without knowledge of the foundation, might be a little difficult to follow:
Code:
[font=courier new][color=#0000FF]var[/color] [color=#008080]project[/color] [color=#808000]=[/color] [color=#008080]IntermediateGateway[/color][color=#808080].[/color][color=#008080]CreateAssembly[/color][color=#808080]([/color][color=#008000]"TestAssembly"[/color][color=#808080])[/color][color=#808080];[/color]

 

[color=#0000FF]var[/color] [color=#008080]dNameSpace[/color] [color=#808000]=[/color] [color=#008080]project[/color][color=#808080].[/color][color=#008080]DefaultNamespace[/color] [color=#808000]=[/color] [color=#008080]project[/color][color=#808080].[/color][color=#008080]Namespaces[/color][color=#808080].[/color][color=#008080]Add[/color][color=#808080]([/color][color=#008000]"AllenCopeland.Abstraction.Slf.Examples.TestAssembly"[/color][color=#808080])[/color][color=#808080];[/color]

 

[color=#0000FF]var[/color] [color=#008080]programClass[/color] [color=#808000]=[/color] [color=#008080]dNameSpace[/color][color=#808080].[/color][color=#008080]Classes[/color][color=#808080].[/color][color=#008080]Add[/color][color=#808080]([/color][color=#008000]"Program"[/color][color=#808080])[/color][color=#808080];[/color]

[color=#008080]programClass[/color][color=#808080].[/color][color=#008080]AccessLevel[/color] [color=#808000]=[/color] [color=#008080]AccessLevelModifiers[/color][color=#808080].[/color][color=#008080]Internal[/color][color=#808080];[/color]

[color=#008080]programClass[/color][color=#808080].[/color][color=#008080]SpecialModifier[/color] [color=#808000]=[/color] [color=#008080]SpecialClassModifier[/color][color=#808080].[/color][color=#008080]ExtensionTarget[/color][color=#808080];[/color]

[color=#0000FF]var[/color] [color=#008080]mainMethod[/color] [color=#808000]=[/color] [color=#008080]programClass[/color][color=#808080].[/color][color=#008080]Methods[/color][color=#808080].[/color][color=#008080]Add[/color][color=#808080]([/color][color=#008000]"Main"[/color][color=#808080])[/color][color=#808080];[/color]

 

[color=#008080]mainMethod[/color][color=#808080].[/color][color=#008080]AccessLevel[/color] [color=#808000]=[/color] [color=#008080]AccessLevelModifiers[/color][color=#808080].[/color][color=#008080]Internal[/color][color=#808080];[/color]

[color=#008080]mainMethod[/color][color=#808080].[/color][color=#008080]Call[/color][color=#808080]([/color][color=#808080]([/color][color=#008080]Symbol[/color][color=#808080])[/color][color=#008000]"Console"[/color][color=#808080],[/color] [color=#008000]"WriteLine"[/color][color=#808080],[/color] [color=#008000]"It is now {0}."[/color][color=#808080].[/color][color=#008080]ToPrimitive[/color][color=#808080]([/color][color=#808080])[/color][color=#808080],[/color] [color=#008000]"DateTime"[/color][color=#808080].[/color][color=#008080]Fuse[/color][color=#808080]([/color][color=#008000]"Now"[/color][color=#808080])[/color][color=#808080])[/color][color=#808080];[/color]

[color=#0000FF]var[/color] [color=#008080]testGeneric[/color] [color=#808000]=[/color] [color=#008080]dNameSpace[/color][color=#808080].[/color][color=#008080]Classes[/color][color=#808080].[/color][color=#008080]Add[/color][color=#808080]([/color][color=#008000]"GenericType"[/color][color=#808080])[/color][color=#808080];[/color]

[color=#0000FF]var[/color] [color=#008080]testGenericParam[/color] [color=#808000]=[/color] [color=#008080]testGeneric[/color][color=#808080].[/color][color=#008080]TypeParameters[/color][color=#808080].[/color][color=#008080]Add[/color][color=#808080]([/color][color=#008000]"TTypeParam"[/color][color=#808080])[/color][color=#808080];[/color]

[color=#008080]testGenericParam[/color][color=#808080].[/color][color=#008080]SpecialConstraint[/color] [color=#808000]=[/color] [color=#008080]GenericTypeParameterSpecialConstraint[/color][color=#808080].[/color][color=#008080]Struct[/color][color=#808080];[/color]

[color=#0000FF]var[/color] [color=#008080]testNestGeneric[/color] [color=#808000]=[/color] [color=#008080]testGeneric[/color][color=#808080].[/color][color=#008080]Classes[/color][color=#808080].[/color][color=#008080]Add[/color][color=#808080]([/color][color=#008000]"TestNestGenericType"[/color][color=#808080])[/color][color=#808080];[/color]

[color=#0000FF]var[/color] [color=#008080]testNestGenericParam[/color] [color=#808000]=[/color] [color=#008080]testNestGeneric[/color][color=#808080].[/color][color=#008080]TypeParameters[/color][color=#808080].[/color][color=#008080]Add[/color][color=#808080]([/color][color=#008000]"TestGenericParameter"[/color][color=#808080])[/color][color=#808080];[/color]

[color=#0000FF]var[/color] [color=#008080]testNestGenericMethod[/color] [color=#808000]=[/color] [color=#008080]testNestGeneric[/color][color=#808080].[/color][color=#008080]Methods[/color][color=#808080].[/color][color=#008080]Add[/color][color=#808080]([/color][color=#008000]"TestNestGenericMethod"[/color][color=#808080],[/color] [color=#0000FF]new[/color] [color=#008080]TypedNameSeries[/color][color=#808080]([/color][color=#808080])[/color] [color=#808080]{[/color] [color=#808080]{[/color] [color=#008000]"p1"[/color][color=#808080],[/color] [color=#008080]testNestGenericParam[/color] [color=#808080]}[/color][color=#808080],[/color] [color=#808080]{[/color] [color=#008000]"p2"[/color][color=#808080],[/color] [color=#008080]testGenericParam[/color] [color=#808080]}[/color][color=#808080],[/color] [color=#808080]{[/color] [color=#008000]"p3"[/color][color=#808080],[/color] [color=#008000]"TTestNestGenericMethodTypeParam"[/color] [color=#808080]}[/color] [color=#808080]}[/color][color=#808080],[/color] [color=#0000FF]new[/color] [color=#008080]GenericParameterData[/color][color=#808080]([/color][color=#008000]"TTestNestGenericMethodTypeParam"[/color][color=#808080])[/color][color=#808080])[/color][color=#808080];[/color][/font]

Kind of flat looking, if you ask me. Next, here's the same code with the concept applied, it amusingly also somewhat illustrates how the model builds upon itself:

Code:
[font=courier new][color=#008080]IntermediateGateway[/color][color=#808080].[/color][color=#008080]CreateAssembly[/color][color=#808080]([/color][color=#008000]"TestAssembly"[/color][color=#808080])[/color]

[color=#808080]{[/color]

    [color=#008080]DefaultNamespace[/color] [color=#808000]=[/color] [color=#008080]Namespaces[/color][color=#808080].[/color][color=#008080]Add[/color][color=#808080]([/color][color=#008000]"AllenCopeland.Abstraction.Slf.Examples.TestAssembly"[/color][color=#808080])[/color]

    [color=#808080]{[/color]

        [color=#008080]Classes[/color][color=#808080].[/color][color=#008080]Add[/color][color=#808080]([/color][color=#008000]"Program"[/color][color=#808080])[/color]

        [color=#808080]{[/color]

            [color=#008080]AccessLevel[/color] [color=#808000]=[/color] [color=#008080]AccessLevelModifiers[/color][color=#808080].[/color][color=#008080]Internal[/color][color=#808080];[/color]

            [color=#008080]SpecialModifier[/color] [color=#808000]=[/color] [color=#008080]SpecialModifier[/color][color=#808080].[/color][color=#008080]ExtensionTarget[/color][color=#808080];[/color]

            [color=#008080]Methods[/color][color=#808080].[/color][color=#008080]Add[/color][color=#808080]([/color][color=#008000]"Main"[/color][color=#808080])[/color]

            [color=#808080]{[/color]

                [color=#008080]AccessLevel[/color] [color=#808000]=[/color] [color=#008080]AccessLevelModifiers[/color][color=#808080].[/color][color=#008080]Internal[/color]

                [color=#008080]Call[/color][color=#808080]([/color][color=#808080]([/color][color=#008080]Symbol[/color][color=#808080])[/color][color=#008000]"Console"[/color][color=#808080],[/color] [color=#008000]"WriteLine"[/color][color=#808080],[/color] [color=#008000]"It is now {0}."[/color][color=#808080].[/color][color=#008080]ToPrimitive[/color][color=#808080]([/color][color=#808080])[/color][color=#808080],[/color] [color=#008000]"DateTime"[/color][color=#808080].[/color][color=#008080]Fuse[/color][color=#808080]([/color][color=#008000]"Now"[/color][color=#808080])[/color][color=#808080])[/color][color=#808080];[/color]

            [color=#808080]}[/color][color=#808080];[/color]

        [color=#808080]}[/color][color=#808080];[/color]

        [color=#008080]Classes[/color][color=#808080].[/color][color=#008080]Add[/color][color=#808080]([/color][color=#008000]"GenericType"[/color][color=#808080])[/color]

        [color=#808080]{[/color]

            [color=#0000FF]var[/color] [color=#008080]testGenericParam[/color] [color=#808000]=[/color] [color=#008080]TypeParameters[/color][color=#808080].[/color][color=#008080]Add[/color][color=#808080]([/color][color=#008000]"TTypeParam"[/color][color=#808080])[/color] [color=#808080]{[/color] [color=#008080]SpecialConstraint[/color] [color=#808000]=[/color] [color=#008080]GenericTypeParameterSpecialConstraint[/color][color=#808080].[/color][color=#008080]Struct[/color][color=#808080];[/color] [color=#808080]}[/color][color=#808080];[/color]

            [color=#008080]Classes[/color][color=#808080].[/color][color=#008080]Add[/color][color=#808080]([/color][color=#008000]"TestNestGenericType"[/color][color=#808080])[/color]

            [color=#808080]{[/color]

                [color=#0000FF]var[/color] [color=#008080]testNestGenericParam[/color] [color=#808000]=[/color] [color=#008080]TypeParameters[/color][color=#808080].[/color][color=#008080]Add[/color][color=#808080]([/color][color=#008000]"TestGenericParameter"[/color][color=#808080])[/color][color=#808080];[/color]

                [color=#008080]Methods[/color][color=#808080].[/color][color=#008080]Add[/color][color=#808080]([/color][color=#008000]"TestNestGenericMethod"[/color][color=#808080],[/color] [color=#0000FF]new[/color] [color=#008080]TypedNameSeries[/color][color=#808080]([/color][color=#808080])[/color] [color=#808080]{[/color] [color=#808080]{[/color] [color=#008000]"p1"[/color][color=#808080],[/color] [color=#008080]testNestGenericParam[/color] [color=#808080]}[/color][color=#808080],[/color] [color=#808080]{[/color] [color=#008000]"p2"[/color][color=#808080],[/color] [color=#008080]testGenericParam[/color] [color=#808080]}[/color][color=#808080],[/color] [color=#808080]{[/color] [color=#008000]"p3"[/color][color=#808080],[/color] [color=#008000]"TTestNestGenericMethodTypeParam"[/color] [color=#808080]}[/color] [color=#808080]}[/color][color=#808080],[/color] [color=#0000FF]new[/color] [color=#008080]GenericParameterData[/color][color=#808080]([/color][color=#008000]"TTestNestGenericMethodTypeParam"[/color][color=#808080])[/color] [color=#808080])[/color][color=#808080];[/color]

            [color=#808080]}[/color][color=#808080];[/color]

        [color=#808080]}[/color][color=#808080];[/color]

    [color=#808080]}[/color][color=#808080];[/color]

[color=#808080]}[/color][color=#808080];[/color][/font]

In the new version, the scope within the curly braces is that of the parent scope as well as the scope of the expression the scope is applied to. So if you placed an expression workspace around a string, within the workspace you could call forth the members of the string, though since strings are immutable, that would be rather silly.

This concept is an extension of object initializers introduced in C# 3.0, except that it allows method calls and the like to be easily accessible since they, too, can lend to altering an object's state. Before attempting to implement it, I'd like some insight as to whether it's useful.
 

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