• Free Pascal Lesson #1

    From Norman King@1:261/38 to All on Mon May 21 20:09:46 2012
    I will use Free Pascal as it is free to download and use at: http://www.freepascal.org

    Current version I am using is 2.6.0 and the commands I am using compiled under it.

    I am going to make foolish assumptions that the user has installed it and read the documentation. That they are using a text editor and know how to edit text and save as a file.

    Bare with me as I am relearning this all over again. I got started in Pascal in
    1985 in High School and then later college.

    The first program is a hello world program and looks like this:

    program HelloWorld;

    begin
    writeln('Hello World');
    end.

    If you note Pascal likes to make use of the semi-colon. The first line defines the program by name, in this case "HelloWorld" notice that Pascal naming conventions require an uppercase letter for the beginning of each word and no spaces between words. A space between Hello and World would result in an error.
    The capital letters are there to make it easier to spot each word and keep track of it.

    The semi-colon tells the Pascal compiler where the line stops. In this case:

    program HelloWorld;

    The reserved word "program" defines the start of the program, after that the program is named, after the name is a semi-colon.

    You will notice the reserved words begin and end, this marks the beginning and end of the main area of the program. You will notice that end uses a period instead of a semi-colon to tell the compiler where the program ends.

    I should point out now, that reserved words are special words used by the compiler and cannot be used as names for functions, procedures, variables and the like which you will learn later on. You have already learned "program", "begin", "end" so far and the next one to learn is "writeln".

    The procedure "writeln" writes a line of text to the console and then adds a carriage return and line feed or whatever your console terminal or screen as some call it uses to advance to the next line.

    writeln('Hello World');

    Notice we are using single quotes between the string we are printing out, in this case "Hello World" is 'Hello World' as opposed to other programming languages. The Semi-Colon tells the compiler to end at that line. We could have
    put anything in that area like:

    writeln('Welcome to Pascal!');

    But most programming lessons begin with a hello world program.

    I hope I have explained the Free Pascal programming format, and that the reader
    is able to compile the program and run it to get the screen:

    Hello World

    Printed out like so:


    C:\FPC\2.6.0\bin\i386-win32>ppcrossx64 hello.pas

    C:\FPC\2.6.0\bin\i386-win32>hello
    Hello World

    C:\FPC\2.6.0\bin\i386-win32>

    You will note that in this example I am using the Windows version of the Free Pascal 2.6.0 compiler, I used a command prompt, and I changed to the directory it was installed in and ran the 64 bit version ppcrossx64 program and saved my source code as hello.pas in that directory. It generated a hello.o and hello.exe file and when I typed hello it ran the hello.exe program.

    Now some antivirus programs may flag this program as having a low reputation, because you just compiled it, and nobody was able to send them a review of it yet. There is no virus, but if you have Avast! you can safely run it in a sandbox if you like. The file prevalence/reputation is low is a possible message you will get. This is a new system some antivirus programs are using where the file is rated in their database and if there is no rating it will flag a warning like that. Just thought I'd let you know ahead of time. Just choose "Open in Sandbox" as we aren't really doing anything with this program besides printning text to the screen.

    The hello.exe file is clean, and the hello.o file is an object file that was compiled by Free Pascal and then linked to an EXE file. If paranoid feel free to scan them, but they were created by the Free Pascal Compiler.

    If there is more interest I can post another lesson on Free Pascal.

    --- BBBS/Li6 v4.10 Dada-1
    * Origin: Prism bbs (1:261/38)
  • From Norman King@1:261/38 to Norman King on Tue May 22 00:35:24 2012
    I hope that wasn't too confusing.

    For a text editor you don't want a Word Processor because it will mess up the formatting of your code and save by default in a non-text format.

    If you use Notepad or Wordpad you can save in text only format but if you name it as hello.pas it might save it as hello.pas.txt and then you have to rename it to hello.pas or run the compiler on hello.pas.txt instead.

    You might not want to save source code and binary files in the same directory as the compiler, I only did that to make it easier to get it to compile. Compiling with using long path names can be a big pain. It might be easier to add the compiler into the path in autoexec.bat so you can run it without the long path name. It requires some knowledge of the OS.

    Yeah I know Windows is not DOS, but it runs command line programs and still uses DOS commands like "dir", "copy", "del", "ren", and uses the autoexec.bat file. That the 64 bit version did away with the 16 bit sub system. But Free Pascal compiles into 32 bit or 64 bit executables. While there is no GUI, it is
    still text based like a terminal program or BBS.

    I know we are starting out just compiling text files and printing to the console screen, but we have to start somewhere. I figured the basics was just do a hello world program and then get into variables and then loops and whatnot.

    Free Pascal is good not just because it is free, but because it is cross platform. If we write a Free Pascal program for Windows, it can be compiled under Mac OS X, Linux, OS/2, etc. I figure it is good for BBS Games to be written cross platform. But it is not a RAD language like Delphi, and it does not have an IDE like Visual Studio, but it is free and open sourced. I am sure later we can find some libraries to do graphics and make easier to use programs. I am sure there are IDEs out there and the Lazarus add on to make easier to use programs. But I want to start out at ground zero and work our way
    up one step at a time.

    If you get stuck, don't give up, post your source code here and let me look at it. The FPC gives error messages so I'd need that as well. Usually the first number is the row and the second number is the column in the text file where the problem happened. I'll try to get into debugging some how and show you how to fix typos and other things later.

    --- BBBS/Li6 v4.10 Dada-1
    * Origin: Prism bbs (1:261/38)
  • From mark lewis@1:3634/12 to Norman King on Tue May 22 13:01:38 2012

    The semi-colon tells the Pascal compiler where the line stops.

    minor correction... the semi-colon is a statement separator not a line terminator ;)

    [trim]

    If there is more interest I can post another lesson on Free Pascal.

    very nice! i wish i had the archives of this area from back in the day... it would be nice to go back thru them and build much of that code and those projects with today's FPC... many things should still work but there are a few li'l gotchas here and there...

    )\/(ark


    * Origin: (1:3634/12)
  • From Norman King@1:261/38 to Norman King on Tue May 22 14:38:48 2012
    I know that compiling without a good source code editor is hard work, this should make it a bit easier.

    For Windows try Notepad++ at http://notepad-plus-plus.org/

    It seems to support Pascal syntax and will make debugging easier.

    For Free Pascal 2.6.0 you can add the path to the compiler by entering on the command line:

    set PATH=%PATH%;c:\fpc\2.6.0\bin\i386-win32\

    For Windows NT based Windows like Windows 7 hold down the left Windows key and the R key, you will get a run box now type in CMD to access a command shell or the CLI as some call it.

    Type in the set command that modifies the path. Look in the c:\fpc\2.6.0\bin\i386-win32\ directory and see what exe file is there. For mine
    using Windows 7 64 bit it is called ppcrossx64.exe for the 32 bit version it may be named something else.

    Try this:

    cd c:\fpc\

    mkdir pas
    cd pas

    This will make a pas directory in your main fpc directory where you can store your source code files and run the compiler on them.

    now you can do this:

    cd c:\fpc\pas\
    ppcrossx64 hello.pas

    If you want you can add this line to the Autoexec.bat file after the set PATH statement:

    set PATH=%PATH%;c:\fpc\2.6.0\bin\i386-win32\

    Then after a reboot you will have the Free Pascal 2.6.0 64 bit compiler in your
    path and can run it from anywhere.

    I hope this makes it easier to work with Free Pascal. Enjoy!

    --- BBBS/Li6 v4.10 Dada-1
    * Origin: Prism bbs (1:261/38)
  • From Norman King@1:261/38 to Mark Lewis on Tue May 22 14:41:40 2012
    The semi-colon tells the Pascal compiler where the line stops.
    minor correction... the semi-colon is a statement separator not a line terminator ;)
    [trim]
    If there is more interest I can post another lesson on Free Pascal.
    very nice! i wish i had the archives of this area from back in the day... it would be nice to go back thru them and build much of that code and those
    projects with today's FPC... many things should still work but there are a few
    li'l gotchas here and there...
    )\/(ark

    Thank you Mark. I will try to keep some lessons going. I got confused over the semi-colon, thank you for setting it right.

    I hope to get at least some users compiling Free Pascal code and hopefully kick
    start some Free Pascal development for the BBS community.

    Free Pascal can work with some Turbo Pascal 7 or Borland Pascal code and I think 2.6.0 added some Object Pascal as well. I will just start out with the basics and not get into objects and save objects for later.

    --- BBBS/Li6 v4.10 Dada-1
    * Origin: Prism bbs (1:261/38)
  • From Mike Powell@1:2320/105 to Norman King on Wed Jun 13 20:23:00 2012
    | Bare with me as I am relearning this all over again. I got started in Pascal | in 1985 in High School and then later college.
    +-[NK=>All]

    I took a semester of it in college about that long ago. I am now a COBOL programmer by profession, after taking a self-paced course sometime after college.

    | I hope I have explained the Free Pascal programming format, and that the
    | reader is able to compile the program and run it to get the screen:
    |
    | Hello World
    +-[NK=>All]

    Yes, I was able to. ;-)

    | You will note that in this example I am using the Windows version of the Free | Pascal 2.6.0 compiler, I used a command prompt, and I changed to the directory
    +-[NK=>All]

    I am using the linux version from the debian stable repository. I am also using the fpc ide to edit the programs.

    | If there is more interest I can post another lesson on Free Pascal. +-[NK=>All]

    I would not mind seeing some more lessons. This echo had not seen much
    traffic here until your posts. Thanks!

    Mike

    ##MMR 2.61(beta). !link NK 05-21-12 20:09


    --- GTMail 1.26
    * Origin: Capitol City Online * 502/875-8938 * cco.ath.cx (1:2320/105.0)