Skip to content

GramScript

Owen Lynch edited this page Nov 30, 2021 · 10 revisions

Introduction

GramScript is the extension language of Semagrams, and takes care of the details of rendering, editing, and layouting a Semagram.

GramScript should really be thought of as a collection of DSLs with a common parser and syntax. There is a pure core of GramScript that is shared across all of the DSLs that encapsulates logic and math. Then there will be specialized constructs in GramScript for interfacing with different parts of Semagrams.

For instance, acset schemas will be written in GramScript.

There will be a JSX-like syntax for constructing SVGs that are automatically reactive with respect to an laid-out ACSet, and provide handles for manipulating the attributes of that ACSet.

Keyboard shortcuts and mouse actions will be able to be bound to snippets of GramScript that edit the current ACSet.

Finally, acsets themselves will have reactive layouting controlled by gramscript.

Example

schema Graph
  V
  E(src::V, tgt::V)
end

sprite Box(radius: Float)(center: Vector2{Float})
  svg 
    draghandle circle(cx=center[0], cy=center[1], r=radius) 
      dragchange => do
        center <- center + dragchange
      end
    end
  end
end

sprite Wire()(p1: Vector2{Float}, p2: Vector2{Float})
  svg
    line(x1=p1[0], x2=p2[0], y1=p1[1], y2=p2[1])
  end
end

semagram BasicGraph(Graph)
  V <- Box(10)(center <- @free)
  E <- Wire()(p1 <- src.center, p2 <- tgt.center)
end

editor BasicEditor(BasicGraph)
  binding v "Add Vertex"
    p <- curMousePos
    addPart V (center = p)
  end

  binding e "Add Edge"
    v1 <- selection 1
    v2 <- selection 2
    addPart E (src = v1, tgt = v2)
  end

  binding x "Delete Entity"
    e <- selection 1
    remPart e
  end
end

Alternatives

It will have to be seen whether it makes more sense to have a lot of the functionality that I'm thinking about putting in GramScript in Scala instead. I'm now thinking that using external DSLs may hinder more than help, as creating a full-fledged programming language is hard.

Then all but the most casual user would have to compile Scala code when creating new editors, but this isn't the end of the world. The main problem with this is that ACSets are not native to Scala, and even worse, may require some amount of dynamic typing in order to work. However, given that I don't really want to write a custom type system anyways, this is also not the end of the world.

The main thing that I've learned from the whole GramScript exercise is how to think generically about all of this.

Clone this wiki locally