My Pascal Class

Discussion in 'Mac Programming' started by larswik, Jan 25, 2011.

  1. larswik, Jan 25, 2011
    Last edited: Jan 25, 2011

    larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    So I had my first Pascal course yesterday at Santa Barbara City Collage. I think I will stick it out since it will give me a chance to talk to people which the C book does not.

    I found this site http://www.freepascal.org/ but I need some help to see if I can get Pascal working with Xcode, if it can be done? Otherwise I have to dive to the school to use their lab computers and the school is 45 minutes away.

    Any ideas?

    -Lars

    EDIT: I found Xcode information on that site, I will see how it goes.
     
  2. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #2
    FWIW http://www.microbizz.nl/gpc.html GNU Pascal may integrate easier than FreePascal. In case you run into trouble.

    EDIT: Do you really need an IDE though?

    Most everything you do should be doable from within Terminal for an introductory course.

    B
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    Agreed. It's not worth it to try to figure out why the IDE won't let you do X, etc. when you could just bang out the projects they're giving you in an intro class at the terminal.

    -Lee
     
  4. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
  5. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #5
    mfram- Yes, the instructor explained himself yesterday as to why. He said it is a very easy language to learn and it is straight forward and many of the principles you learn here will carry forward.

    Write the code in the terminal? Do I compile from the terminal as well to test the code?

    -Lars
     
  6. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #6
    Yup.

    You could use a GUI editor if you choose, like TextWrangler, but compile and execute the code from the command line in Terminal.

    B
     
  7. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #7
    Balamw - I went to that site that you listed and downloaded and installed everything it said. I have an old G5 with 10.4 and Xcode 2.4 installed on it. Everything seemed to install smooth but when I launched Xcode and the New Project Assistant came up Pascal was not an option I could select.

    -Lars
     
  8. karsten macrumors 6502a

    karsten

    Joined:
    Sep 3, 2010
    #8
    pascal was great, was pretty intuitive to write if i remember correctly
     
  9. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #9
    Borrowing a page from lee1210. Try it without the IDE.

    Install pkg downloaded from the link I provided.
    Open Terminal.app.

    Code:
    $ [B]echo "program HelloWorld;
    
    begin
      writeln('Hello World');
    end.">hello.pp[/B]
    $ [B]gpc hello.pp -o hello[/B]
    $ [B]./hello[/B]
    Hello World
    $ 
    B
     
  10. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #10
    Code:
    Last login: Tue Jan 25 15:21:28 on ttyp1
    Welcome to Darwin!
    larsg5s-power-mac-g5:~ larsg5$ $ echo "program HelloWorld;
    > 
    > begin
    >   writeln('Hello World');
    > end.">hello.pp
    -bash: $: command not found
    larsg5s-power-mac-g5:~ larsg5$ $ gpc hello.pp -o hello
    -bash: $: command not found
    larsg5s-power-mac-g5:~ larsg5$ $ ./hello
    -bash: $: command not found
    larsg5s-power-mac-g5:~ larsg5$ 

    Something is not right? I copy and pasted your test code in there and got a buynch of Command Not Found messages. I have never really used Terminal before so I am sure I am doing it wrong.

    -Lars
     
  11. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #11
    Copy only the bold stuff. Not the $ or anything else not bolded, you get that for free (it's your command prompt). ;)

    B
     
  12. gkarris macrumors 604

    gkarris

    Joined:
    Dec 31, 2004
    Location:
    "No escape from Reality..."
    #12
    WOW...

    Are you using it on a DEC or a CDC? :eek:

    LOL...

    :p
     
  13. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #13
    Yep, that seemed to work so it looks like everything installed fine.
    Code:
    Last login: Tue Jan 25 15:22:02 on ttyp1
    Welcome to Darwin!
    larsg5s-power-mac-g5:~ larsg5$ echo "program HelloWorld;
    > 
    > begin
    >   writeln('Hello World');
    > end.">hello.pp
    larsg5s-power-mac-g5:~ larsg5$ gpc hello.pp -o hello
    larsg5s-power-mac-g5:~ larsg5$ ./hello
    Hello World
    larsg5s-power-mac-g5:~ larsg5$ 
    

    This won't be fun to write code like this at all. If I can get my older version of Xcode working I would rather use an environment I am familiar with.

    -Lars
     
  14. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #14
    Wow. I don't think really you'd use to echo like this to enter your program.

    The sequence most typically would be to create/edit the program source file/s in a GUI editor like TextWrangler. I'm a TextMate fan myself.

    Save your files(s). Then in a terminal compile and run the program. I typically use a command line like this.

    Code:
    gpc hello.pp -o hello && ./hello
    
    This will compile the program and iff there are no compiler errors, run the program as well.

    Once I've typed this command in once, I can use command history to execute it again.

    This can all be done from the keyboard. From the editor: Cmd+Tab, Up, Enter. The program compiles and runs (hopefully). Cmd+Tab back to the editor. Edit away. Then repeat.

    The only thing this lacks if a good debugging environment. I still find gdb frustrating to use vs something like the GUI debugger in Xcode.
     
  15. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #15
    ahhh. I was about to ask what that last line of code was.

    I downloaded TextWrangler and it is installed.
    I then wrote this test code that balamw generously provided :)
    Code:
    echo program "program HelloWorld";
    
    begin
    	writeln ('Hello World');
    end
    Now where do I save it, I am guessing the root directory and call it 'hello' or 'Hello.pp' I then launch Terminal and enter 'gpc hello.pp -o hello && ./hello'. the only thing I really understand is the ./ since I just started reading about writing files in my C book. Does the 'gpc' call the compiler? 'Hello.pp' the name of the file? ' the '-o' I am guessing means open. The other part I don;t know why there is an && logic operator in there?

    -Lars
     
  16. balamw, Jan 25, 2011
    Last edited: Jan 25, 2011

    balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #16
    Yup. This is just to make sure you have a working compiler. I mentioned using a GUI editor earlier in the thread.

    Interestingly, TextWrangler actually doesn't have a plain Pascal mode, only Object Pascal.

    I agree that what you will miss here is easy debugging, though learning how to use gdb is a useful skill.

    EDIT:

    Save it anywhere you want. Say ~/Documents/Pascal/hello.pp. Then, when you open Terminal start your session with:

    Code:
    cd ~/Documents/Pascal
    gpc is the compiler, -o means output. Type
    Code:
    man gpc
    to see the man page for gpc and other options. If you leave -o out, your output is saved in a file called a.out instead.

    You know that "return 0" you put at the end of all your C programs? It's there to facilitate this. If you return anything other than 0, the bit after the && won't run.

    B
     
  17. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #17
    Ok, Thanks for the detailed explanation. I kind of understand it, I will test it tonight with texwrangler

    -Lars
     
  18. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #18
    balamw _ I need a little more help with the Terminal, Sorry. Just the work flow and what I am missing to make it work. The instructor handed out a piece of paper with code for us to type in, here it is.

    Code:
    program carpet(input,output);
    
    const
    width = 15;
    length = 12;
    price = 17;
    
    var
    areaInFeet, areaInYards, cost: integer;
    
    BEGIN
    
    areaInFeet := width * height;
    areaInYards := areaInFeet DIV 9;
    cost := price * AreaInYards;
    writeln('Cost is ',cost );
    
    END.
    I entered this in to TextWrangler and save the file as 'lesson1.pp' into Documents / Pascal folder I created. I then opened The terminal program and type in 'cd ~/Documents/Pascal' and then 'Return'. I can then see the pathway in Terminal to to the Pascal folder. the next part of the 'gpc is confusing as to opening the file and compiling it.

    I tried last night but could not get it to open the file. Can you leave me a few more bread crumbs to solve this problem.

    Thanks!

    -Lars
     
  19. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #19
    Code:
    $ [COLOR="Red"]cd ~/Documents/Pascal/[/COLOR]
    $ [COLOR="Red"]ls[/COLOR]
    deleteme.pas
    $ [COLOR="Red"]gpc -o deleteme deleteme.pas[/COLOR] 
    deleteme.pas: In main program:
    deleteme.pas:13: error: undeclared identifier `height' (first use in this routine)
    deleteme.pas:13: error:  (Each undeclared identifier is reported only once
    deleteme.pas:13: error:  for each routine it appears in.)
    $ [COLOR="red"]gpc -o deleteme deleteme.pas[/COLOR] 
    $ [COLOR="red"]./deleteme[/COLOR] 
    Cost is 340
    $ 
    
    Bits you type in red. I had to change to code a bit between the two calls to gpc.

    Code:
    program carpet(input,output);
    
    const
    width = 15;
    length = 12;
    price = 17;
    
    var
    areaInFeet, areaInYards, cost: integer;
    
    BEGIN
    
    areaInFeet := width * [B]length[/B];
    areaInYards := areaInFeet DIV 9;
    cost := price * AreaInYards;
    writeln('Cost is ',cost );
    
    END.
    Does that help?

    B
     
  20. ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #20

    Think about it like this:

    gpc is just a program that compiles Pascal code into executable code. So you have to get your file into that program, compile it then run it.. SOoo:

    Code:
    gpc hello.pp -o hello
    
    This command tells the terminal to run the program gpc (the Pascal compiler).

    It tells it to pass in the Pascal source code you created named "hello.pp" Then it tells it to output (-o), a binary file named "hello"

    Assuming your program has compiled correctly, you just need to run it with:

    Code:
    ./hello
    
    This basically says "in the current directory, execute the binary named hello". The ./ denotes the current directory. You could potentially put the full path:

    Code:
    ~/Desktop/hello
    
    Would execute a binary named "hello" on the user's desktop

    So when it is all said and done, you can duplicate these steps for any .pp source code file.
     
  21. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #21
    haha... Yes that helps. I typed this in while waiting. $gpc ./lesson1.pp' and I got the same error for 'HEIGHT'. After I fixed it It ran but I did not see the result. Now after your help, again, It works.

    Code:
    larsg5s-power-mac-g5:~ larsg5$ cd ~/Documents/Pascal
    larsg5s-power-mac-g5:~/Documents/Pascal larsg5$ ls
    a.out           hello           hello.pp        lesson1.pp
    larsg5s-power-mac-g5:~/Documents/Pascal larsg5$ ./lesson1
    -bash: ./lesson1: No such file or directory
    larsg5s-power-mac-g5:~/Documents/Pascal larsg5$ ./lesson1.pp
    -bash: ./lesson1.pp: Permission denied
    larsg5s-power-mac-g5:~/Documents/Pascal larsg5$ gpc -o lesson1 lesson1.pp
    larsg5s-power-mac-g5:~/Documents/Pascal larsg5$ ./lesson1
    Cost is 340
    Thank you guys so much! Feed me fish and you fed me for a day. Teach me to fish and I can feed myself for life!

    -Lars
     
  22. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #22
    Once upon a time, I was taking an experimental high energy physics lab and we spent most of the semester assembling various configurations of equipment to perform various experiments. In the last few labs, the experiments got significantly more complicated and the instructor brought out a single piece of equipment that not only replaced every configuration of equipment we had assembled over the whole course but automated the data collection.

    When we asked why had spent all that time using the older, less capable, equipment for all those months the instructor replied "You must learn to walk before you can run".

    Understanding what the individual components did and how they were interconnected and interacted, helped us tell if the results from the automated system made any sense.

    That's basically what it's like using separate editor/compiler/linker/debugger vs. using an IDE. The IDE can make a really complicated task much easier, but it sure helps a lot to understand what it is doing "under the hood".

    B
     
  23. Bill McEnaney macrumors 6502

    Joined:
    Apr 29, 2010
    #23
    Wow! Someone is learning a programming I'm fluent in. If I can help OP in any way, I'll be happy to do that.

    By the way, Free Pascal isn't ISO Standard Pascal. The compiler's authors made Free Pascal compatible with Turbo Pascal.
     
  24. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #24
    The book larswik is using is also Turbo Pascal, though we got him off on to GNU Pascal instead of Free Pascal.

    B
     
  25. Bill McEnaney macrumors 6502

    Joined:
    Apr 29, 2010
    #25
    All right. I hope GNU Pascal's compiler compiles Turbo Pascal's nonstandard statements.

    Bill
     

Share This Page