sancarn / Extended_MapBasic

This is an Open Source project. We are developing a powerful extension for the MapBasic Window in Pitney Bowes' MapInfo. The extension will give users the ability to compile on demand from the MapBasic Window. Further extensions may include custom MapBasic functions to accomplish common tasks. This project is still very much WIP
MIT License
1 stars 0 forks source link

Pre-Compiler #13

Open sancarn opened 7 years ago

sancarn commented 7 years ago

Interlude

To make this project easy to manage and easy to extend, a pre-compiler may need to be created. In this issue I will explain why this is necessary and what the issues with the idea are, as well as some potential solutions to said problems.

Difficulty to Extend

What are the current steps required when extending (and compiling) the Extended MapBasic Application?

Create transpiling library

Of course the first step is to make the code which takes some source code and converts that into the new transpiled source code. Currently there is no convention to how the transpiling function should be named. Let's assume, for simplicity, it's defined as f().

Ultimately the transpiling library acts a bit like this:

mbsource = f(mbsource)

Editing EMB_Core/transpile.ahk

Writing the functions isn't the only thing that needs to be done before compiling. You also need to link these new transpiling functions to the rest of the application. You do this by modifying the "LIB_Core/transpile.ahk" file with the code above:

mbsource = EMB_Lib_BlockComment(mbsource)
mbsource = EMB_Lib_BlockQuote(mbsource)
mbsource = EMB_Lib_AutoDeclare(mbsource)
mbsource = EMB_Lib_OptionalArgs(mbsource)
mbsource = f(mbsource)

Including the transpiling library in header.ahk

In order to try and simplify this process I decided to use a header file. This file declares all .AHK modules/libraries which are used in the final project. Of course our file needs to be included here also:

#Include "EMB_Lib/BlockComment.ahk"
#Include "EMB_Lib/BlockQuote.ahk"
#Include "EMB_Lib/AutoDeclare.ahk"
#Include "EMB_Lib/myFunctions.ahk"

RegEx for Transpile Requirement Detection

Before Extended MapBasic transpiles the MapBasic application it first detects whether it needs to transpile at all. To do this RegEx is added to the needsTranspiling() function of the file "EMB_Core/transpiler.ahk".

example needed

Other future features which might be required

The Problem

Modifying EMB is not easy!

Having to modify 4+ lines of code is definitely not a difficult task, however it does involve editing files within the "vital organs" of Extended MapBasic. An accidental error could cause serious damage to the compiled exe / cause damage to compiled MBX files which is what we want to avoid most, but it could also cause a lot of false bug-reports with working versions of Extended MapBasic.

The solution

Simple extensibility

The simplest extensibility is a simple "drag and drop" extensibility. E.G. If you want to add a library "MyFunctions.ahk" to Extended MapBasic, you simply drop the file into the "EMB_Lib" folder, make sure the transpilation regex is declared (as in the example below) and run Compile.EXE.

Compile.EXE will run:

Example extension "MyFunctions.AHK"

;RegEx = {i)=\s*myfunction\(\),i)=\s*myNextFunction\(\w+\s+as\s+\w+\)}

EMB_Lib_MyFunctions(mbsource) {
    ;Your AHK library code here
}

Note:

Since now the user doesn't specify the specific transpilation order a problem might occur where transpilation of certain code might be required to run BEFORE other transpilation modules.

E.G.

A EMB module might add a new function TempTable with the definition TempTable(numCol as integer, optional colDefs as string, optional colDelimeter as string)

In this case the EMB module should be transpiled BEFORE EMB_Lib_OptionalArguments() and not after.

Potential Solutions:

One option is to add a Transpile_Before option:

;T_BEFORE = "EMB_Lib_OptionalArguments()"
;RegEx = {TempTable\(}

EMB_Lib_MyFunctions(mbsource) {
    ;Your AHK library code here
}

A standard to be kept

Anyone who is used to standards know that it is innately difficult for people to keep said standards.

A potential solution

A simple AHK-IDE for creating transpilation libraries might be useful. The IDE would enforce users to specify whether their function should be transpiled before certain modules as well as the transpilation regex.

It is also important to note that Error checking should be implemented into the pre-compiler to check validity of modules before implementing changes to the source