r/ProgrammingLanguages • • 9h ago

Wiret update: my text-first visual language now reads in the order it runs

Thumbnail lemonator32.github.io
5 Upvotes

I made some changes to my programming languages. There's also a browser version of the editor that you can try out.

The biggest change in this round is how the text is ordered. Until now a function body had three parts: node statements sorted by id, a flow { } block listing exec wires, and a layout { } block of positions and notes. I picked that format because it merges well: each node is one stable chunk and each wire is one line. But to know what a function does, you had to jump between the flow block and the nodes. Here's the loops lesson in the old syntax:

func Main() {
  local total: i32

  flow {
    count.each -> say
    count.done -> report
    entry -> count
    say -> keep
  }

  node count = Macro std.flow.ForRange(
    .first <- 1,
    .last <- 5,
  )
  node entry = Entry()
  node keep = SetLocal total(
    .value <- Add::<i32>(
      .a <- GetLocal total(),
      .b <- count.index,
    ),
  )
  node report = Call host.Log( ... )
  node say = Call host.Log( ... )

  layout {
    count at (200, 0)
    entry at (0, 0)
    keep at (1160, 0)
    ...
  }
}

Now the body starts at Entry and follows the exec wires. Each exec statement runs after the one before it in its block. A node with several exec outputs (Branch, Switch, Sequence, Try, loop macros) writes each output as a nested block where it runs. Here's the same program but with the new syntax:

func Main() {
  local total: i32

  exec Entry{pos=(0, 0)}()
  exec Macro std.flow.ForRange{pos=(200, 0)}(
    .first <- 1
    .last <- 5
  ).each|index| -> {
    exec Call host.Log{pos=(820, 0)}(
      .message <- Call std.string.Concat{pos=(541, 240)}(
        .a <- "Counting: "
        .b <- Convert#<i32, string>{pos=(302, 260)}(
          .value <- index
        )
      )
    )
    @note("Adds index to the running total each time round.")
    exec SetLocal total{pos=(1160, 0)}(
      .value <- Add#<i32>{pos=(1021, 155)}(
        .a <- GetLocal total{pos=(844, 115)}()
        .b <- index
      )
    )
  }.done -> {
    exec Call host.Log{pos=(1200, 268)}( ... )
  }
}

It's still a graph, with the same model, the same compiled output and the same bytecode format. But it now reads top to bottom like an imperative program: .each|index| -> { ... } is basically a for-loop, and .then -> { } .else -> { } is basically an if/else. Inline node read backwards from the inputs, data wires form a tree, so a pure node whose output feeds exactly one input is written inside that input. Only shared outputs break the tree. The last exec output can be written as .pin with no block, a consept I call flattening, and the statements after it in the enclosing block continue from it. An if with no else then reads naturally, and so does the .then that runs after a Try. An output's block binds the node's data outputs by name between bars, .each|index, break| -> { ... }. You write just pin by itself, but if a node is declared, you can also write the pin as name.pin. A node is anonymous unless it's used from more than one place. If it is, it gets declared at the top as node name = ..., placed once with exec name, and entered from anywhere else with goto name. A loop is a goto back to an earlier node, and a merge is a goto forward:

node say_hi = Call host.Log(.message <- "Hi!")

exec Entry()
exec say_hi
exec SetLocal count(.value <- Add#<i32>(.a <- GetLocal count(), .b <- 1))
exec Branch(.cond <- Ge#<i32>(.a <- GetLocal count(), .b <- 10)).then -> {
  exec Return()
}.else
goto say_hi

Yes, a visual language ended up with goto. Arbitrary exec wiring is goto, and the text is just honest about it. In practice the structured blocks cover most graphs.

Unreachable code is kept, not dropped. In a graph editor you often have nodes that aren't wired up yet. These go after a ! at the end of the body: !exec ... for an unwired node, !{ ... } for a chain that's wired together but not reached from Entry, and !Kind(...) for a pure node nothing reads. The editor draws them greyed out. This is different from @disabled, which marks a node that is on the path and gets skipped.

Anonymous nodes no longer have ids in the text. Tools refer to them by kind plus execution position, e.g. Main.1.fault.0 is the first statement in the .fault block of statement 1. The debugger, diagnostics and the edit/merge tooling all use these.

The layout block is gone. Positions and display properties go in an attribute block on the node, Kind{pos=(x, y), label="..."}(...), and notes are @note(...) on the line above it. Attribute blocks are excluded from the semantic hash, so moving a node never changes what the program means. A node without one gets auto-placed, so you can write a valid graph with no layout at all.


r/ProgrammingLanguages • • 23h ago

Discussion Compact notational/numeral-system-like representations for polynomials, series, simple programs and iterating terms?

5 Upvotes

Good evening!

Recently I've stumbled onto the notation [a; b, c, d, e...] for continued fractions "a + 1/(b + 1/(c + 1/...)..)". I have a gut feeling this can be a very useful representation and maybe some clever algebra may be defined over it, turning it effectively into an alternative "numeral-like" system.

I tried searching for such similar compact representations for other finitely and infinitely iterating terms/series in mathematics as well as normal forms for them and rewriting algorithms but I've didn't manage to find anything.

There are series that, if you abstract away the concrete operations and base sets/algebras, seem to have a very similar structure throughout analysis; they almost feel like a very definite class of programs. That would feel amenable for a more abstract characterization in category theory but I've never seen that.

From my intuition most of these series seem to rely a bit on fold, maybe streams, initial algebras and other structures commonly studied in functional programming, but if any of you have other ideas to suggest, I would appreciate :))

I wish everyone a great weekend!


r/ProgrammingLanguages • • 50m ago

Help Help with resources for learning

2 Upvotes

Hello everyone. I have learning about programming languages and compilers for over a year now and I have struggled finding useful resources for learning advanced topics.
I find things like type theory hard to follow (it’s not really the math, just that most resources don’t take the time to explain the implications of the theory in actual language design).
I have taken a look at crafting interpreters and the dragon book and have found them good resources but I wanted to know if there is any good central resource to deepen my understanding.
Thanks for taking the time!


r/ProgrammingLanguages • • 15h ago

Help How are conditional and unconditional jumps generatet.

2 Upvotes

I'm writing a simple Interpreted Language with custom bytecode. I have implemented a major part of bytecode generation, jet my problem is to understand how to generate bytecode for conditional and unconditional jumps.

What i have understood is that jump opcode must be derived based on condition operator (==, <, >, etc.) and binding operator (&& and ||).

My questions are:

How codegenerators select right jump opcode?

How codegenerators patch jump offset?

Are there other ways to implement conditions?