Skip to content

Instantly share code, notes, and snippets.

@AbePralle
Last active April 21, 2020 23:08
Show Gist options
  • Select an option

  • Save AbePralle/43ca8f8a74a4ef5a5fd001ad0e62f1d2 to your computer and use it in GitHub Desktop.

Select an option

Save AbePralle/43ca8f8a74a4ef5a5fd001ad0e62f1d2 to your computer and use it in GitHub Desktop.

Revisions

  1. AbePralle revised this gist Apr 21, 2020. 1 changed file with 33 additions and 1 deletion.
    34 changes: 33 additions & 1 deletion Cmd.rogue
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,9 @@
    uses Utility/Node

    class Cmd : Node<<Cmd>>
    METHODS
    method is_literal->Logical
    return false
    endClass

    class CmdStatement : Cmd
    @@ -36,7 +39,11 @@ class CmdSubtract : CmdBinary<<"Subtract","-">>;

    class CmdInt32( value:Int32 ) : Cmd
    METHODS
    method name->String: return value->String
    method is_literal->Logical
    return true

    method name->String
    return value->String
    endClass


    @@ -86,9 +93,12 @@ local op = CmdPrint( CmdAdd(CmdInt32(3),CmdSubtract(CmdInt32(6),CmdInt32(2))) )
    @trace op
    # op:Print(Add(3,Subtract(6,2)))

    # ConstantFolder().visit( op ) # another example of possibilities

    local writer = CPPWriter()
    writer.visit( op )
    println writer
    # OUTPUT:
    # #include <cstdio>
    # using namespace std;
    #
    @@ -97,3 +107,25 @@ println writer
    # printf( "%d\n",(3+(6-2)) );
    # return 0;
    # }


    # class ConstantFolder : NodeVisitor<<Cmd>>
    # METHODS
    # method handle( cmd:CmdAdd )->Cmd
    # visit_children( cmd )
    # if (cmd.first.is_literal and cmd.last.is_literal)
    # if (cmd.first instanceOf CmdInt32 and cmd.last instanceOf CmdInt32)
    # return visit( CmdInt32( (cmd.first as CmdInt32).value + (cmd.last as CmdInt32).value ) )
    # endIf
    # endIf
    # return cmd
    #
    # method handle( cmd:CmdSubtract )->Cmd
    # visit_children( cmd )
    # if (cmd.first.is_literal and cmd.last.is_literal)
    # if (cmd.first instanceOf CmdInt32 and cmd.last instanceOf CmdInt32)
    # return visit( CmdInt32( (cmd.first as CmdInt32).value - (cmd.last as CmdInt32).value ) )
    # endIf
    # endIf
    # return cmd
    # endClass
  2. AbePralle revised this gist Apr 21, 2020. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions Cmd.rogue
    Original file line number Diff line number Diff line change
    @@ -27,10 +27,8 @@ endClass

    class CmdBinary<<$name,$symbol>> : CmdBinary
    METHODS
    method name->String: return $name

    method symbol->String
    return $symbol
    method name->String: return $name
    method symbol->String: return $symbol
    endClass

    class CmdAdd : CmdBinary<<"Add","+">>;
  3. AbePralle revised this gist Apr 21, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions Cmd.rogue
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    # More Complex Cmd node example with multimethod-style visitor
    uses Utility/Node

    class Cmd : Node<<Cmd>>
    endClass

  4. AbePralle created this gist Apr 21, 2020.
    99 changes: 99 additions & 0 deletions Cmd.rogue
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,99 @@
    # More Complex Cmd node example with multimethod-style visitor
    class Cmd : Node<<Cmd>>
    endClass

    class CmdStatement : Cmd
    METHODS
    method init( operand:Cmd )
    add( operand )
    endClass

    class CmdPrint : CmdStatement
    METHODS
    method name->String: return "Print"
    endClass

    class CmdBinary : Cmd
    METHODS
    method init( left:Cmd, right:Cmd )
    add( left )
    add( right )

    method symbol->String
    return "(undefined)"
    endClass

    class CmdBinary<<$name,$symbol>> : CmdBinary
    METHODS
    method name->String: return $name

    method symbol->String
    return $symbol
    endClass

    class CmdAdd : CmdBinary<<"Add","+">>;
    class CmdSubtract : CmdBinary<<"Subtract","-">>;

    class CmdInt32( value:Int32 ) : Cmd
    METHODS
    method name->String: return value->String
    endClass


    class CPPWriter : NodeVisitor<<Cmd>>
    PROPERTIES
    builder = StringBuilder()
    code : String

    METHODS
    method init
    builder.println @|#include <cstdio>
    |using namespace std;
    |
    |int main()
    |{
    builder.indent += 2

    method handle( node:CmdPrint )->Cmd
    builder.print @|printf( "%d\n",
    visit( node.first )
    builder.println " );"
    return node

    method handle( node:CmdBinary )->Cmd
    builder.print '('
    visit( node.first )
    builder.print node.symbol
    visit( node.last )
    builder.print ')'
    return node

    method on( node:CmdInt32 )
    builder.print node.value

    method to->String
    if (code is null)
    builder.indent -= 2
    builder.println " return 0;"
    builder.println "}"
    code = builder->String
    endIf
    return code

    endClass

    local op = CmdPrint( CmdAdd(CmdInt32(3),CmdSubtract(CmdInt32(6),CmdInt32(2))) )
    @trace op
    # op:Print(Add(3,Subtract(6,2)))

    local writer = CPPWriter()
    writer.visit( op )
    println writer
    # #include <cstdio>
    # using namespace std;
    #
    # int main()
    # {
    # printf( "%d\n",(3+(6-2)) );
    # return 0;
    # }