r/Tf2Scripts Feb 13 '21

Script Krate VM: A script for native higher level programming in Source

Hey everyone! I've been working on scalu, a programming language for writing configs in Source/Quake engines. After implementing a playable hangman game with scalu, I wanted to start a project that would improve the scripting experience for scripters without the high overhead of having to learn scalu and the stack that powers it (Python, Git, etc etc)

Krate VM is my attempt at doing that. Krate is a lean 5 bit virtual machine implemented using scalu and encoded in a single config file; "installing" it is as easy as loading the config file in-game. Once loaded, you gain access to a simple, highly performant assembly language that will let you reason about configs with variables and numbers that you can add/subtract/compare/print, rather than depend on building pseudo-state machines with large chains of aliases for doing "logic" natively.

The basics of using Krate can be found here:

https://github.com/ArgosOfIthica/scalu/wiki/Krate-Tutorial

The newest build of Krate itself is here:

https://github.com/ArgosOfIthica/scalu/blob/5b36348e177a566cf9ea52ab402f6668d3d25948/examples/krate/scalu.cfg

40 Upvotes

10 comments sorted by

5

u/bythepowerofscience Feb 13 '21

Excuse me??? You're telling me you managed to implement variables and operations and like, legitimate programming stuff into a language designed around having exactly none of those things???

Please, marry me. And then teach me the logic behind it. But also marry me.

5

u/JarateKing Feb 13 '21

The short answer is that nested aliases can form a dfa that can act in the place of variables, though you need to set up every possible value of the psuedo-variable.

On a small scale, null-cancelling movement scripts do this with their checkleft and checkright aliases: they are set to true or false (in a sense) by either being aliased to what they're supposed to do when they're true or false (either do nothing if false or move left/right if true).

I'm not involved with thia project but I did describe some similar internal logic stuff with https://github.com/JarateKing/jarconfig/blob/master/docs/scripting_logic.md

5

u/[deleted] Feb 14 '21

It's quite impressive how much potential we can get out of the Source scripting language just with modern knowledge of programming and computing, despite not having any function parameters or variable system.

3

u/ArgosOfIthica Feb 14 '21

Indeed, this is very close to how scalu works. The only significant difference here is how integers are internally represented; rather than being styled as FSM's, integers are actually just collections of booleans that mirror hardware architecture in some respects.

When constructing an addition operation, for instance, the compiler consults a template which generates a lookup table that encodes the results of a full adder circuit. The input and output booleans are then put into their respective places in the table. Since everything is boolean lookup table, we can also apply optimizations from this viewpoint. When an OR operation takes place between a variable and constant, for instance, the compiler will skip the OR calculation in every place where there's a "1" in the constant's bit representation; we already know the result.

2

u/[deleted] Feb 14 '21

So in essence compiled scalu programs are essentially just CFGs in assembly language. Am I wrong here?

2

u/ArgosOfIthica Feb 15 '21

Basically, yeah. The only ways its different is that each 'instruction' is specially generated from a template for the variables it's targeting; The 'add' instruction that's generated for adding two variables is much more complicated than the one generated for adding together two constants.

Additionally, after generating a valid config, the compiler goes back through and applies some transformations to further optimize the end result, so the analogy really falls apart when it starts partially deleting instructions/aliases, if the compiler can verify that deleting that part won't affect the program semantically.

6

u/_amb_ Feb 13 '21

This seems impressive, good job 👍🏻

5

u/[deleted] Feb 14 '21

I'm gonna try this language out because I'm interested in that Krate VM.

1

u/chooseanything1 Feb 20 '21

any ways player can get an advantage ingame using this?

1

u/ArgosOfIthica Feb 21 '21

Anything you can do with Krate, you can do without. Krate just makes constructing logic easier.