avatarRadu Zaharia

Summary

The web content describes how to set up Visual Studio Code (VSCode) for writing, building, and debugging Pascal programs using the Free Pascal Compiler and OmniPascal extension.

Abstract

The article outlines the process of configuring VSCode to work with Pascal, leveraging modern tools such as the Free Pascal Compiler and the OmniPascal extension for syntax highlighting and code completion. It details the installation of necessary plugins, the configuration of a Pascal code formatter, and the setup of tasks in tasks.json for compiling and running Pascal code. Additionally, it covers the debugging setup in launch.json using GDB. The author concludes by expressing satisfaction with the setup, despite acknowledging that Pascal may not be widely used for serious application development today, suggesting Delphi as an alternative for professional Pascal development.

Opinions

  • The author is enthusiastic about the possibility of using VSCode for Pascal development, highlighting the modern open-source Pascal compiler, Free Pascal Compiler, and its active development.
  • OmniPascal is favored among other VSCode extensions for Pascal due to its straightforward setup and lack of dependencies or complex configurations.
  • The author values the ability to manually debug code when necessary, indicating that full debugging support is not always a deal-breaker.
  • There is a hint of nostalgia and personal interest in exploring Pascal development in a modern environment, as the author reflects on the previous article about Borland Pascal 7.
  • The author suggests that while the setup described is functional, those interested in serious Pascal application development should consider Delphi, implying that Delphi might offer a more robust and professional environment.

Writing Pascal with VSCode

Photo by Adam Sherez on Unsplash

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:

Visual Studio Code plugins for Pascal

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.

Installing the OmniPascal VSCode extension

Getting ready to write Pascal code

Photo by Clemens van Lay on Unsplash

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:

The Free Pascal Compiler default folder

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:

Installing the Pascal Formatter extension for VSCode

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:

Configuring the Pascal Formatter extension

We are ready to write our first Pascal program:

The first Pascal program in VSCode

Building and debugging with VSCode

The VSCode tasks required for building Pascal code

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 detailing the debugging configuration for Pascal code

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:

Running the Pascal program

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!

Pascal
Vscode
Programming
Recommended from ReadMedium