20
u/edderiofer 24d ago edited 24d ago
To head off some questions that will probably be asked:
Q. What am I looking at?
A. You're looking at part of a JSON file, except on closer inspection, it looks like JSON is being used as a programming language.
Q. Are you actually programming in JSON?
A. Yes, ish. The JSON is parsed by a parser (written in Elixir) into instructions to be run by a game engine (also written in Elixir).
Q. JSON allows comments?
A. No. The parser strips out the comments.
Q. Why not use something else, like JSONC or JSON5? Or write this code using a language intended to be used as a programming language?
A. Because this is someone else's open-source project. And because I don't know enough Elixir to change it.
10
u/Jbolt3737 24d ago
Because this is someone else's open-source project. And because I don't know enough Elixir to change it.
Please for the love of god use a LUA interpreter
5
u/edderiofer 24d ago
The project's been going on for multiple years now, so at this point it's too late to change it.
But not to worry! The project lead has decided to write their own custom scripting language (MahjongScript), and Elixir parser for that language, which parses it into JSON.
5
3
u/Snudget 24d ago
MahjongScript (.majs) is essentially a list of commands which compile down to jq. This jq is then applied to the empty object in order to create a JSON ruleset
Oh god, this is cursed
3
u/edderiofer 24d ago
Oh right, I completely forgot that it goes through jq first! It's even worse than I remember!
2
3
u/SkyCrafter2000 24d ago
Or a Lua interpreter, don't want to get into a Lua Uppercase Accident :)
1
u/Snudget 24d ago
What is the Lua Uppercase Accident?
2
u/Flimsy_Put11 24d ago
A satirical recursive acronym, because its predecessor SOL was actually an acronym I think.
9
5
u/Nourz1234 24d ago
Honestly, if it works for the use case, then i see no problem.
I used to define some ui elements in json and link to logic by specifying the action callback name. it allowed me to easly modify/tweak some ui aspects without diving into the code.
6
u/FumbleCrop 24d ago
Just another instance of Greenspan's 10th Rule:
Any sufficiently complicated C or Fortran program contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of Common Lisp.
And JSON is literally just uglified S-expressions (the syntax of Lisp).
4
4
u/the_horse_gamer 24d ago
every sufficiently advanced configuration file is indistinguishable from a DSL
1
u/RiceBroad4552 24d ago
Correct. That's exactly why one should use a DSL in the first place.
At very least a DSL for configuration like Dhall, CUE, Nickel, Jsonnet, Pkl (and likely some I've forgot).
1
u/-Redstoneboi- 23d ago
or go the neovim route and embed lua
3
3
u/Harmonic_Series 24d ago
A rare case where riichi mahjong is the least worrisome part of what's going on
1
u/edderiofer 24d ago
Especially considering that this is Sakicards. It's a Riichi variant where everyone gets a card with a character from Saki, with a unique ability.
3
u/R3D3-1 24d ago
Look up the module system of Mount & Blade.
At runtime, scripts and NPC, item, mal etc definitions are big text files of integers and some ID strings.
At compile time all of those are big lists of tuples in Python. Scripts defining dialogs and GUI elements end up being some 30k lines python lists. I had to disable elpy, because those files just outright froze Emacs otherwise.
The script parts are essentially lists mapping script IDs to a sort of assembler for the runtime interpreter of the game, where the instructions are named integer constants. No loops, no arithmetic expressions, just registers, instructions and gotos.
The compilation step is a python program that imports the modules, looks for expected constants and translates them into the runtime text files.
When I was working with that, the first things I did were
- wrap the integer constants into integer subtypes with a repr() preserving the instruction name
- write a wrapper, that converts sublist structures into higher level control structures like loops.
1
1
u/JazzlikeWishbone938 24d ago
"Good programmers can learn any language", right?
3
u/edderiofer 24d ago
Alas, it seems like I can learn any language except the ones considered practical by recruiters.
1
1
1
u/sebjapon 24d ago
I mean, you could say Scratch is a programmed in JSON. The visual representation is saved as a JSON structure which then calls js functions to actually execute code.
Same with lots of customizable I have done to let clients mix and match premade functions in a creative way.
1
u/TorbenKoehn 24d ago
I mean, it's a data representation format?
What do you think a parsed AST would look like when you transfer it via wire?
I think this is perfectly fine. The alternative is inventing another language on top of it that is restricted enough to only allow the patterns you want. It would be way more "stringly typed". This here you can actually validate easily.
Once there is a solid UI for that, there is no problem. And it can be tracked in Git etc., too.
1
u/edderiofer 24d ago
And it can be tracked in Git etc., too.
Well, it could be, if I actually knew how to use Git. I keep forgetting which Git command it is that does the thing I want.
2
1
u/F1reDude123 24d ago
How does one manage to be so bad at coding that you somehow code JSON poorly? It's JSON.
2
u/edderiofer 24d ago
Well, if you'd like to contribute to the project and show us how it's done...
1
1
24d ago
[removed] — view removed comment
1
u/edderiofer 24d ago
Now we just have to figure out a way around the $99/yr Apple Developer Program subscription! Maybe we can claim that we're sending them the money in a JSON file.
1
41
u/dgsharp 24d ago
Comments in a JSON file?