katsaii / catspeak-lang

A cross-platform modding language for GameMaker games.
https://www.katsaii.com/catspeak-lang/
MIT License
94 stars 6 forks source link

Adding a way of tagging compiled Catspeak programs with a filename #139

Open tabularelf opened 2 months ago

tabularelf commented 2 months ago

What is your feature request?

I'm slowly thinking about what ways Catspeak could be extended slightly to include an artificial stacktrace of potential files or other place/s where the end user might end up doing something that's wrong or invalid. I already have a system in my own project, where I can generate a stacktrace from my own files, and push/pop them. But if you any Catspeak code that also makes functions, the stacktrace part itself gets trickier.

Assuming we have this as our Catspeak program

add = fun(a, b) {
  return a + b;
}

And our GML code

var filename = "gamecode.meow";
var str = load_string_from_file(filename);
var ast = Catspeak.parseString(str);
var mainProgram = Catspeak.compile(ast);

program();
var add = mainProgram.getGlobals().add;
 add(undefined, 2); // Errors as undefined cannot be added together with 2

Please describe in detail how you expect this new feature to behave.

I have some ideas I could experiment with, but I might just do it as my own modifications to Catspeak itself for my own project. Since I can't think of a real universal solution for this. The closest idea I have so far involves modifying how __catspeak_function__ works slightly (to implement a catch callback handler), providing .compile() with some optional arguments for a stacktrace_name (so one could provide "game_code.meow" or "https://gamewebsite.com/game_assets/game_code.meow" or whatever as a name to refer it to for debugging purposes), and appending .getStacktraceName() to each and every Catspeak function.

All of this is something I'll experiment with anyway, but the idea I have in code:

Catspeak.setCatchCallback(function(ex, program) {
  ex.longMessage += $"\nIn {program.getStacktraceName()}";
  // I'd return or throw ex, but my idea would be that Catspeak handles throwing ex still, so no need to do anything else here.
});

// Compiling
var filename = "gamecode.meow";
var str = load_string_from_file(filename);
var ast = Catspeak.parseString(str);
var mainProgram = Catspeak.compile(ast, filename);

program();
var add = mainProgram.getGlobals().add;
add(undefined, 2); // Errors as undefined cannot be added together with 2, but also __catspeak_function__ runs 
// our catch callback function, allowing us to append our stacktrace on top of it. So we end up with "In gamecode.meow" at the end.