Login
gpcr/examples/case_dispatch.cr
Login

[edit]

examples/case_dispatch.cr @ trunk

../src/gpcr

require '../src/gpcr'
scale = 0.75
fontscale = 1.500
box wid 1.783 ht 0.340 with .nw at (0.035, -0.035) fill 0xCBD5E1 color 0xCBD5E1
box wid 1.783 ht 0.340 with .nw at (0.000, 0.000) fill 0x7C83E6 color 0x7C83E6
box wid 0.300 ht 0.065 rad 0.018 at (0.370, -0.022) fill 0xffffff color 0xffffff
line from (0.140, 0.000) to (0.140, -0.340) color white
line from (1.643, 0.000) to (1.643, -0.340) color white
text "require '../src/gpcr'" color white at (0.891, -0.170)

crystal source

require "../src/gpcr"

json block

{
  "kind": "require",
  "name": "../src/gpcr",
  "doc": null,
  "children": []
}

../src/gpcr/std

require '../src/gpcr/std'
scale = 0.75
fontscale = 1.500
box wid 2.068 ht 0.340 with .nw at (0.035, -0.455) fill 0xCBD5E1 color 0xCBD5E1
box wid 2.068 ht 0.340 with .nw at (0.000, -0.420) fill 0x7C83E6 color 0x7C83E6
box wid 0.300 ht 0.065 rad 0.018 at (0.370, -0.443) fill 0xffffff color 0xffffff
line from (0.140, -0.420) to (0.140, -0.760) color white
line from (1.928, -0.420) to (1.928, -0.760) color white
text "require '../src/gpcr/std'" color white at (1.034, -0.590)

crystal source

require "../src/gpcr/std"

json block

{
  "kind": "require",
  "name": "../src/gpcr/std",
  "doc": null,
  "children": []
}

dispatch_graph

Case rendering demonstration: a graph whose body contains a match/when dispatch. The @[GPCR::Graph] annotation causes the renderer to emit the full body stack, which exercises emit_case. In R3, render-node uses the same pattern over node kind strings.

match  join('com', 'mand') when  hat say  hat when  command ,  boolean say  command or boolean when  reporter say  reporter else say  unknown
scale = 0.75
fontscale = 1.500
box wid 2.720 ht 0.340 rad 0.035 with .nw at (0.035, -0.875) fill 0xCBD5E1 color 0xCBD5E1
box wid 2.720 ht 0.340 rad 0.035 with .nw at (0.000, -0.840) fill 0xFFAB19 color 0xFFAB19
box wid 0.300 ht 0.065 rad 0.018 at (0.370, -0.863) fill 0xffffff color 0xffffff
box wid 0.300 ht 0.065 rad 0.018 at (0.370, -1.213) fill 0xFFAB19 color 0xFFAB19
text "match " color white at (0.415, -1.010)
box wid 1.770 ht 0.205 rad 0.102 with .nw at (0.741, -0.907) fill 0xFFFFFF color 0xFFFFFF
text "join('com', 'mand')" color 0x263238 at (1.626, -1.010)
box wid 2.720 ht 1.210 rad 0.035 with .nw at (0.035, -1.295) fill 0xCBD5E1 color 0xCBD5E1
box wid 2.720 ht 1.210 rad 0.035 with .nw at (0.000, -1.260) fill 0xFFAB19 color 0xFFAB19
box wid 2.340 ht 0.540 rad 0.035 with .nw at (0.320, -1.720) fill 0xFFFDE7 color 0xFFFDE7
box wid 0.300 ht 0.065 rad 0.018 at (0.370, -2.502) fill 0xFFAB19 color 0xFFAB19
text "when " color white at (1.029, -1.490)
box wid 0.496 ht 0.205 rad 0.102 with .nw at (1.371, -1.388) fill 0xFFFFFF color 0xFFFFFF
text "hat" color 0x263238 at (1.619, -1.490)
box wid 1.266 ht 0.340 rad 0.035 with .nw at (0.455, -1.815) fill 0xCBD5E1 color 0xCBD5E1
box wid 1.266 ht 0.340 rad 0.035 with .nw at (0.420, -1.780) fill 0x4C97FF color 0x4C97FF
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -1.803) fill 0xFFFDE7 color 0xFFFDE7
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -2.152) fill 0x4C97FF color 0x4C97FF
text "say " color white at (0.722, -1.950)
box wid 0.496 ht 0.205 rad 0.102 with .nw at (0.990, -1.848) fill 0xFFFFFF color 0xFFFFFF
text "hat" color 0x263238 at (1.238, -1.950)
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -2.152) fill 0x4C97FF color 0x4C97FF
box wid 2.720 ht 1.210 rad 0.035 with .nw at (0.035, -2.585) fill 0xCBD5E1 color 0xCBD5E1
box wid 2.720 ht 1.210 rad 0.035 with .nw at (0.000, -2.550) fill 0xFFAB19 color 0xFFAB19
box wid 2.340 ht 0.540 rad 0.035 with .nw at (0.320, -3.010) fill 0xFFFDE7 color 0xFFFDE7
box wid 0.300 ht 0.065 rad 0.018 at (0.370, -3.792) fill 0xFFAB19 color 0xFFAB19
text "when " color white at (0.192, -2.780)
box wid 1.027 ht 0.205 rad 0.102 with .nw at (0.534, -2.677) fill 0xFFFFFF color 0xFFFFFF
text "command" color 0x263238 at (1.047, -2.780)
text ", " color white at (1.670, -2.780)
box wid 0.880 ht 0.205 rad 0.102 with .nw at (1.824, -2.677) fill 0xFFFFFF color 0xFFFFFF
text "boolean" color 0x263238 at (2.264, -2.780)
box wid 2.682 ht 0.340 rad 0.035 with .nw at (0.455, -3.105) fill 0xCBD5E1 color 0xCBD5E1
box wid 2.682 ht 0.340 rad 0.035 with .nw at (0.420, -3.070) fill 0x4C97FF color 0x4C97FF
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -3.092) fill 0xFFFDE7 color 0xFFFDE7
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -3.442) fill 0x4C97FF color 0x4C97FF
text "say " color white at (0.722, -3.240)
box wid 1.913 ht 0.205 rad 0.102 with .nw at (0.990, -3.137) fill 0xFFFFFF color 0xFFFFFF
text "command or boolean" color 0x263238 at (1.946, -3.240)
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -3.442) fill 0x4C97FF color 0x4C97FF
box wid 2.720 ht 1.210 rad 0.035 with .nw at (0.035, -3.875) fill 0xCBD5E1 color 0xCBD5E1
box wid 2.720 ht 1.210 rad 0.035 with .nw at (0.000, -3.840) fill 0xFFAB19 color 0xFFAB19
box wid 2.340 ht 0.540 rad 0.035 with .nw at (0.320, -4.300) fill 0xFFFDE7 color 0xFFFDE7
box wid 0.300 ht 0.065 rad 0.018 at (0.370, -5.082) fill 0xFFAB19 color 0xFFAB19
text "when " color white at (0.843, -4.070)
box wid 0.870 ht 0.205 rad 0.102 with .nw at (1.184, -3.968) fill 0xFFFFFF color 0xFFFFFF
text "reporter" color 0x263238 at (1.619, -4.070)
box wid 1.639 ht 0.340 rad 0.035 with .nw at (0.455, -4.395) fill 0xCBD5E1 color 0xCBD5E1
box wid 1.639 ht 0.340 rad 0.035 with .nw at (0.420, -4.360) fill 0x4C97FF color 0x4C97FF
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -4.382) fill 0xFFFDE7 color 0xFFFDE7
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -4.732) fill 0x4C97FF color 0x4C97FF
text "say " color white at (0.722, -4.530)
box wid 0.870 ht 0.205 rad 0.102 with .nw at (0.990, -4.427) fill 0xFFFFFF color 0xFFFFFF
text "reporter" color 0x263238 at (1.424, -4.530)
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -4.732) fill 0x4C97FF color 0x4C97FF
box wid 2.720 ht 1.210 rad 0.035 with .nw at (0.035, -5.165) fill 0xCBD5E1 color 0xCBD5E1
box wid 2.720 ht 1.210 rad 0.035 with .nw at (0.000, -5.130) fill 0xFFAB19 color 0xFFAB19
box wid 2.340 ht 0.540 rad 0.035 with .nw at (0.320, -5.590) fill 0xFFFDE7 color 0xFFFDE7
text "else" color white at (1.360, -5.360)
box wid 1.728 ht 0.340 rad 0.035 with .nw at (0.455, -5.685) fill 0xCBD5E1 color 0xCBD5E1
box wid 1.728 ht 0.340 rad 0.035 with .nw at (0.420, -5.650) fill 0x4C97FF color 0x4C97FF
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -5.672) fill 0xFFFDE7 color 0xFFFDE7
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -6.022) fill 0x4C97FF color 0x4C97FF
text "say " color white at (0.722, -5.820)
box wid 0.958 ht 0.205 rad 0.102 with .nw at (0.990, -5.717) fill 0xFFFFFF color 0xFFFFFF
text "unknown" color 0x263238 at (1.469, -5.820)
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -6.022) fill 0x4C97FF color 0x4C97FF

crystal source

@[GPCR::Graph(x: 10, y: 30)]
def dispatch_graph
  case join("com", "mand")
  when "hat"
    say "hat"
  when "command", "boolean"
    say "command or boolean"
  when "reporter"
    say "reporter"
  else
    say "unknown"
  end
end

json block

{
  "kind": "graph",
  "name": "dispatch_graph",
  "doc": "Case rendering demonstration: a graph whose body contains a match/when dispatch.\nThe @[GPCR::Graph] annotation causes the renderer to emit the full body stack,\nwhich exercises emit_case. In R3, render-node uses the same pattern over\nnode kind strings.",
  "children": [
    {
      "kind": "case",
      "subject": "join(\"com\", \"mand\")",
      "op_index": 0,
      "whens": [
        {
          "kind": "when",
          "cond": "when [hat]",
          "body": [
            {
              "kind": "call",
              "name": "say",
              "receiver": null,
              "op_index": 0,
              "args": [
                "hat"
              ],
              "label": "say [hat]",
              "shape": "command",
              "category": "Output",
              "children": []
            }
          ]
        },
        {
          "kind": "when",
          "cond": "when [command], [boolean]",
          "body": [
            {
              "kind": "call",
              "name": "say",
              "receiver": null,
              "op_index": 0,
              "args": [
                "command or boolean"
              ],
              "label": "say [command or boolean]",
              "shape": "command",
              "category": "Output",
              "children": []
            }
          ]
        },
        {
          "kind": "when",
          "cond": "when [reporter]",
          "body": [
            {
              "kind": "call",
              "name": "say",
              "receiver": null,
              "op_index": 0,
              "args": [
                "reporter"
              ],
              "label": "say [reporter]",
              "shape": "command",
              "category": "Output",
              "children": []
            }
          ]
        }
      ],
      "else": [
        {
          "kind": "call",
          "name": "say",
          "receiver": null,
          "op_index": 0,
          "args": [
            "unknown"
          ],
          "label": "say [unknown]",
          "shape": "command",
          "category": "Output",
          "children": []
        }
      ]
    }
  ]
}

dispatch_range

Second graph exercises a non-literal when condition (a RangeLiteral). Non-literal conditions render as unbracketed source text — the bracket visual grammar implies a single substitutable slot value, which a range is not.

match  3 when 1..5 say  in range else say  out of range
scale = 0.75
fontscale = 1.500
box wid 2.720 ht 0.340 rad 0.035 with .nw at (0.035, -0.875) fill 0xCBD5E1 color 0xCBD5E1
box wid 2.720 ht 0.340 rad 0.035 with .nw at (0.000, -0.840) fill 0xFFAB19 color 0xFFAB19
box wid 0.300 ht 0.065 rad 0.018 at (0.370, -0.863) fill 0xffffff color 0xffffff
box wid 0.300 ht 0.065 rad 0.018 at (0.370, -1.213) fill 0xFFAB19 color 0xFFAB19
text "match " color white at (1.126, -1.010)
box wid 0.348 ht 0.205 rad 0.102 with .nw at (1.451, -0.907) fill 0xFFFFFF color 0xFFFFFF
text "3" color 0x263238 at (1.626, -1.010)
box wid 2.720 ht 1.210 rad 0.035 with .nw at (0.035, -1.295) fill 0xCBD5E1 color 0xCBD5E1
box wid 2.720 ht 1.210 rad 0.035 with .nw at (0.000, -1.260) fill 0xFFAB19 color 0xFFAB19
box wid 2.340 ht 0.540 rad 0.035 with .nw at (0.320, -1.720) fill 0xFFFDE7 color 0xFFFDE7
box wid 0.300 ht 0.065 rad 0.018 at (0.370, -2.502) fill 0xFFAB19 color 0xFFAB19
text "when 1..5" color white at (1.360, -1.490)
box wid 1.659 ht 0.340 rad 0.035 with .nw at (0.455, -1.815) fill 0xCBD5E1 color 0xCBD5E1
box wid 1.659 ht 0.340 rad 0.035 with .nw at (0.420, -1.780) fill 0x4C97FF color 0x4C97FF
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -1.803) fill 0xFFFDE7 color 0xFFFDE7
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -2.152) fill 0x4C97FF color 0x4C97FF
text "say " color white at (0.722, -1.950)
box wid 0.890 ht 0.205 rad 0.102 with .nw at (0.990, -1.848) fill 0xFFFFFF color 0xFFFFFF
text "in range" color 0x263238 at (1.434, -1.950)
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -2.152) fill 0x4C97FF color 0x4C97FF
box wid 2.720 ht 1.210 rad 0.035 with .nw at (0.035, -2.585) fill 0xCBD5E1 color 0xCBD5E1
box wid 2.720 ht 1.210 rad 0.035 with .nw at (0.000, -2.550) fill 0xFFAB19 color 0xFFAB19
box wid 2.340 ht 0.540 rad 0.035 with .nw at (0.320, -3.010) fill 0xFFFDE7 color 0xFFFDE7
text "else" color white at (1.360, -2.780)
box wid 1.964 ht 0.340 rad 0.035 with .nw at (0.455, -3.105) fill 0xCBD5E1 color 0xCBD5E1
box wid 1.964 ht 0.340 rad 0.035 with .nw at (0.420, -3.070) fill 0x4C97FF color 0x4C97FF
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -3.092) fill 0xFFFDE7 color 0xFFFDE7
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -3.442) fill 0x4C97FF color 0x4C97FF
text "say " color white at (0.722, -3.240)
box wid 1.195 ht 0.205 rad 0.102 with .nw at (0.990, -3.137) fill 0xFFFFFF color 0xFFFFFF
text "out of range" color 0x263238 at (1.587, -3.240)
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -3.442) fill 0x4C97FF color 0x4C97FF

crystal source

@[GPCR::Graph(x: 10, y: 80)]
def dispatch_range
  case 3
  when 1..5
    say "in range"
  else
    say "out of range"
  end
end

json block

{
  "kind": "graph",
  "name": "dispatch_range",
  "doc": "Second graph exercises a non-literal when condition (a RangeLiteral).\nNon-literal conditions render as unbracketed source text — the bracket\nvisual grammar implies a single substitutable slot value, which a range\nis not.",
  "children": [
    {
      "kind": "case",
      "subject": "3",
      "op_index": 0,
      "whens": [
        {
          "kind": "when",
          "cond": "when 1..5",
          "body": [
            {
              "kind": "call",
              "name": "say",
              "receiver": null,
              "op_index": 0,
              "args": [
                "in range"
              ],
              "label": "say [in range]",
              "shape": "command",
              "category": "Output",
              "children": []
            }
          ]
        }
      ],
      "else": [
        {
          "kind": "call",
          "name": "say",
          "receiver": null,
          "op_index": 0,
          "args": [
            "out of range"
          ],
          "label": "say [out of range]",
          "shape": "command",
          "category": "Output",
          "children": []
        }
      ]
    }
  ]
}