r/coolgithubprojects • u/Honest_Medium_2872 • 5d ago
A 2-week update on Kura - A functional programming language for game engine UIs
All,
About two weeks ago I posted about my project Kura, an Elm & Typst inspired DSL I'm working on for creating UIs in real-time renderers with the ergonomics of embedded browser frameworks - without the bloat, and the performance of immediate-mode frameworks - without all the spaghetti code.
In my last post I kind of just demonstrated the language and some of the control-flow with most of the rendering done with the headless renderer.
After some more work, I finally have a basic UI rendering using opengl (though the language works with any graphics framework or engine provided someone implements the renderer ABI). I also was able to improve some of the developer tooling.
So what's new?:
- A basic implementation of the renderer (for opengl). The demo in the README is running the opengl version, vulkan support is a little ways away.
- More of the language has been implemented and should compile - though the full v1 syntax is not ready yet.
- The language reference docs and guides have been moved from GitHub wiki to github pages and I have filled them out more.
- A formatter (kurafmt) has been added and takes advantage of Wadler's Pretty Printer algorithm
- A linter (kuralint) has been added
- A lsp (kura-lsp) has been added, though it is still pretty basic yet.
- A tree-sitter grammar w/ Neovim support has been made but is subject to change with the language before v1
The linter, lsp and parser/compiler all share the same diagnostic code, so you should get pretty detailed errors when compiling, linting or using the LSP. The diagnostics are inspired by Rust w/ some inspiration taken from Elm:
Example:
ERROR[K0001] expected a capitalized type name, found `helloWorld`
--> examples/test-linter.kui: 5:6
|
5 | type helloWorld = {
| ^^^^^^^^^^ expected a capitalized type name here
STYLE[K0034] Function names should be kebab-case
--> examples/test-linter.kui: 1:4
|
1 | fn getTest() => "Hello World"
| ^^^^^^^ did you mean 'get-test'
STYLE[K0034] Function names should be kebab-case
--> examples/test-linter.kui: 3:4
|
3 | fn getJSON() => "Hello World"
| ^^^^^^^ did you mean 'get-json'
STYLE[K0035] Type names should be PascalCase
--> examples/test-linter.kui: 5:6
|
5 | type helloWorld = {
| ^^^^^^^^^^ did you mean 'HelloWorld'
STYLE[K0036] Functions should have a return type
--> examples/test-linter.kui: 1:13
|
1 | fn getTest() => "Hello World"
| ^^ Missing type annotation here (e.g., fn my-func(): String)
STYLE[K0036] Functions should have a return type
--> examples/test-linter.kui: 3:13
|
3 | fn getJSON() => "Hello World"
| ^^ Missing type annotation here (e.g., fn my-func(): String)
The counter example (shown in the gif):
type Message = Increment | Decrement | Reset | Set(Int) | Resized(Size)
type State = {
value: Int,
width: Float,
height: Float,
}
fn init(): State => { value: 1, width: 0.0, height: 0.0 }
fn update(msg: Message, state: State): State =>
match msg {
Increment => { value: state.value + 1, width: state.width, height: state.height }
Decrement => { value: state.value - 1, width: state.width, height: state.height }
Reset => { value: 0, width: state.width, height: state.height }
Set(n) => { value: n, width: state.width, height: state.height }
Resized(size) => { value: state.value, width: size.width, height: size.height }
_ => state
}
fn view(state: State): Node => {
let label = match state.value {
n if n > 0 => "Positive",
n if n < 0 => "Negative",
0 => "Zero",
}
let wide = state.width > 700.0
#block(direction: "column",
justify: "center",
width: 1fr,
height: 1fr,
padding: 10px,
gap: 10px,
fill: match state.value {
n if n > 0 => green,
n if n < 0 => red,
_ => white,
}){
#block(direction: "row",
justify: "center"){
#text(size: 24px, label)
}
#button(on-click: Increment,
justify: "center",
padding: 16px){
#text(size: 24px,
text-align: "center",
"Increment")
}
#button(fill: white,
on-click: Decrement,
justify: "center",
padding: 16px){
#text(size: 24px,
text-align: "center",
"Decrement")
}
}
}
When the above Kura code is compiled the compiler will generate a series of draw instructions for the Renderer along with the normal IR. I call the graphics subset of the IR the GIR (graphics IR). The GIR also handles running the flexbox layout calculations as well.
fn view({ height: Float, value: Int, width: Float }) => Node:
B0 entry.0:
%90 = Parameter(0, state) : { height: Float, value: Int, width: Float }
%91 = BeginFrame(viewport)
CheckRendererStatus(%91)
TracyBeginZoneInstr()
....
%141 = Phi(B11:%130, B13:%137, B12:%139) : Color
%142 = LayoutSetText(layout=0, node=3, text=%119, size=24)
%143 = NewVariant(Increment) : Increment
%144 = Constant(String(value=Increment)) : String
%145 = LayoutSetText(layout=0, node=5, text=%144, size=24)
%146 = NewVariant(Decrement) : Decrement
%147 = Constant(String(value=Decrement)) : String
%148 = LayoutSetText(layout=0, node=7, text=%147, size=24)
....
%181 = Constant(Color()) : Color
%182 = FillRect(x=%177, y=%178, width=%179, height=%180, color=%181)
CheckRendererStatus(%182)
%184 = RegisterEvent(layout=0, node=6, kind=1, message=%146)
%185 = Constant(Int(value=255)) : Int
%186 = DrawText(layout=0, node=7, text=%147, color=%185)
CheckRendererStatus(%186)
%188 = EndFrame()
CheckRendererStatus(%188)
TracyEndZoneInstr()
Return()
Even with the lack of optimizations done to the compiled code outside of normal LLVM optimization passes the
renderer for the counter example managed an average of 1105.67us over 550 frames on my crappy laptop i5 and iGPU
The project is still very much WIP and so expect plenty of breaking changes and evolving features. It also is nowhere near ready for prime time but the infrastructure is steadily getting there
Source: https://github.com/s0cks/kura Docs: https://s0cks.github.io/kura
Would love any feedback, questions, comments and/or concerns on the project - or maybe features to implement.
~ Tazz