Writing Pascal with VSCode
Last time we talked about Borland Pascal 7 and the way we used to write code for MS-DOS, and while trying different things I remembered about a modern open-source Pascal compiler. It’s called Free Pascal Compiler and I kid you not, it’s actively developed. Since we have a modern compiler, and since we also have an extremely versatile code editor like VSCode, I started thinking about the wondrous possibility. Could it be that someone out there thought to develop a Visual Studio Code plugin for Pascal?
I hurried to open my VSCode install and check out the available extensions. Jackpot! Initial searches were looking good:

When I install a Visual Studio Code plugin that enables support for a given programming language, I usually look first for syntax highlight, automatic code completion next, and finally debugging support. Debugging is last as it’s not always well supported, and I can usually find ways around it by debugging manually.
After testing a few extensions, I settled on OmniPascal. The others were good, but they had many dependencies and weird setups, including setting up environment variables which is not really the VSCode way of handling configurations.

Getting ready to write Pascal code
Ok so far, we downloaded Free Pascal Compiler. In installs by default in the root C drive in the FPC foler, but we can have it anywhere. I just went with the default:

Next, we installed the OmniPascal extension for Visual Studio Code. We will also need a Pascal code formatter, to keep our code straying from the writing style guidelines. I chose Pascal Formatter:

The Pascal Formatter extension needs a bit of configuration. We simply point it to the Free Pascal Formatter engine, included with the Free Pascal Compiler. Note the selection of the formatter engine as ptop, and the location as C:\FPC\3.2.2\bin\i386-win32. If you have a different install location for the Free Pascal Compiler, use that:

We are ready to write our first Pascal program:

Building and debugging with VSCode

As with any other programming language, we need to tell VSCode how to compile and run the Pascal code. This is configured in the tasks.json file:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "PAS build active file",
"command": "fpc",
"args": ["-FE${workspaceFolder}/build", "-g", "-gw", "${file}"],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [],
"dependsOn": "PAS create build folder"
},
{
"type": "shell",
"label": "PAS create build folder",
"command": "md",
"args": ["-force", "${workspaceFolder}/build"],
"group": {
"kind": "build",
"isDefault": false
},
"problemMatcher": []
},
{
"type": "shell",
"label": "PAS run active file",
"command": "${workspaceFolder}/build/${fileBasenameNoExtension}.exe",
"problemMatcher": [],
"dependsOn": "PAS build active file"
}
]
}We defined above three tasks: one to create the build folder using the md command with the -force parameter. This just makes sure that it won’t complain if the build folder already exists. The build folder will be called build and it will be placed in the workspace root folder. This is the second task in the listing above. Note the isDefault property, which marks it as the default build task.
Next, we added the build task, the first one in the listing above, which uses fpc, the Free Pascal Compiler, with the -FE option to specify the output folder. We also add the -g and -gw parameters to enable debugging. The input for fpc is the current file that is being edited, ${file}. Note the dependsOn property, where we configure it to run the build folder creation task in advance.
Finally, we add the run task, the last one in the listing above. This one simply runs the executable resulting in the build step. Again, note the dependsOn property which makes sure we run the build step first. Next, we need to take care of debugging. This is done in the launch.json file:

The launch.json file configures GDB as the debugger for the executable of our choice. In this case, ./build/test.exe:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "gdb",
"request": "launch",
"name": "Launch Program",
"target": "./build/test.exe",
"cwd": "${workspaceRoot}",
"valuesFormatting": "parseText",
"preLaunchTask": "${defaultBuildTask}"
}
]
}Of course, we may have gone with the currently edited executable again, by using the ${file} parameter. The choice is yours. Note again the preLaunchTask option, which makes sure to run the default build task from the tasks.json file. The default build task in our case is “PAS build active file”.
We are done. By pressing F5 we build and launch our small Pascal program in debugging mode:

Ok, this is my setup for writing and debugging Pascal code with VSCode. I am absolutely sure nobody needs this information, but since the last article I just had to try it. Of course, if you really want to write serious applications using Pascal, you should check out Delphi. Maybe we will have an article about it in the future. Until then, thanks for reading and see you next time!






