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:
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);
}
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.
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"
}