Skip to content

Instantly share code, notes, and snippets.

@onelivesleft
Last active November 5, 2020 02:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onelivesleft/7bfa11a28753c95f729f7c41e31e43b5 to your computer and use it in GitHub Desktop.
Save onelivesleft/7bfa11a28753c95f729f7c41e31e43b5 to your computer and use it in GitHub Desktop.
Jai Compile with options then Run

Compile then Run script for Jai

The way Jai does compiler options, using the Compiler module instead of command line arguments to the compiler, is good for use inside a project. When you're only compiling individual files, however, it is pretty clunky: you have to include effectively the script presented below in each program you write. To get round this, and speed up learning the language and testing it out, I've set up vscode to compile and run the current script with my chosen build options as one action. This involves a jai script (I say script because it never compiles to an exe) and a batch file. The setup is very basic right now, it could easily be expanded to allow you to pass in the exe name, etc. It works like this:

jai-compile.jai

This is effectively a wrapper jai script which is compiled in order to compile the target program.

#import "Basic";
#import "Compiler";
#import "String";

set_options :: (options: *Build_Options) {
    // set whatever Build_Options you want in here.
    options.shorten_filenames_in_error_messages = true;

    //options.stack_trace = false;
    //options.emit_debug_info = Debug_Info_Type.DEFAULT;
}


#run {
    args := compiler_get_command_line_arguments();
    if args.count != 1 || !ends_with_nocase(args[0], ".jai") {
        print("Must specify a .jai src file!");
        exit(1);
    }

    filepath := args[0];
    output_path := path_strip_filename(filepath);
    #if OS == .WINDOWS
        executable := concatenate(path_strip_extension(basename(filepath)), ".exe");
    else
        executable := path_strip_extension(basename(filepath));

    set_working_directory(output_path);

    build_options := get_build_options();
    build_options.output_type = .NO_OUTPUT;
    set_build_options(build_options);

    build_options.output_type = .EXECUTABLE;
    build_options.output_executable_name = executable;
    build_options.output_path = output_path;
    set_options(*build_options);
    workspace := compiler_create_workspace("Main");
    set_build_options(build_options, workspace);
    add_build_file(filepath, workspace);
}

jai.bat

This is a batch file which will compile the specified .jai src file with the options specified in the script above, storing the executable in the same folder as the source. It will then run the program, assuming the compile completed successfully.

@ECHO OFF
set filepath=%1
FOR %%I IN ("%filepath%") DO (
    set drive=%%~dI
    set path=%%~pI
    set filename=%%~nxI
)
%drive%
cd %path%


rem Change these two paths to your jai exe path and the path to the above script.

c:\jai\bin\jai.exe c:\repos\jai-tools\jai-compile.jai -- %filename%


if NOT ["%errorlevel%"]==["0"] goto norun
echo.
echo ----------------------------------------------------
echo.
%filename:~0,-4%.exe
echo.
echo.
:norun

If you're on linux then converting the above to /bin/sh should be trivial.

VSCode

For VScode I bind this to F5 by adding the following block to my keybindings.json. Remember to change the path in "cmd" below to the above .bat file.

{
    "key": "f5",
    "command": "runInTerminal.run",
    "args": {"cmd": "c:/repos/jai-tools/jai.bat ${file}", "match": ".*"},
    "when": "resourceLangId == jai"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment