Skip to content

Instantly share code, notes, and snippets.

@onelivesleft
Last active November 5, 2020 02:41
Show Gist options
  • Select an option

  • Save onelivesleft/7bfa11a28753c95f729f7c41e31e43b5 to your computer and use it in GitHub Desktop.

Select an option

Save onelivesleft/7bfa11a28753c95f729f7c41e31e43b5 to your computer and use it in GitHub Desktop.

Revisions

  1. onelivesleft revised this gist Nov 5, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jai_compile_and_run.md
    Original file line number Diff line number Diff line change
    @@ -87,7 +87,7 @@ 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`. Again, change the path specified to the above `.bat` file.
    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.

    ```json
    {
  2. onelivesleft revised this gist Nov 5, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jai_compile_and_run.md
    Original file line number Diff line number Diff line change
    @@ -87,7 +87,7 @@ 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`:
    For VScode I bind this to F5 by adding the following block to my `keybindings.json`. Again, change the path specified to the above `.bat` file.

    ```json
    {
  3. onelivesleft revised this gist Nov 5, 2020. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion jai_compile_and_run.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    # 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, oowever, 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:
    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

    @@ -65,8 +66,12 @@ FOR %%I IN ("%filepath%") DO (
    %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 ----------------------------------------------------
    @@ -76,6 +81,7 @@ echo.
    echo.
    :norun
    ```

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


  4. onelivesleft created this gist Nov 4, 2020.
    93 changes: 93 additions & 0 deletions jai_compile_and_run.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,93 @@
    # 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, oowever, 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.

    ```jai
    #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.

    ```bat
    @ECHO OFF
    set filepath=%1
    FOR %%I IN ("%filepath%") DO (
    set drive=%%~dI
    set path=%%~pI
    set filename=%%~nxI
    )
    %drive%
    cd %path%
    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`:

    ```json
    {
    "key": "f5",
    "command": "runInTerminal.run",
    "args": {"cmd": "c:/repos/jai-tools/jai.bat ${file}", "match": ".*"},
    "when": "resourceLangId == jai"
    }
    ```