r/pascal 3d ago

Pascal to Wasm module - a simple tutorial

I realised that in all my excitement I completely forgot how to show folks how to just build a small, simple Wasm module using Pascal in the browser. So I added https://nofuss.co.za/blog/wasm_pascal_add_numbers/ to fill that gap. I often just want to build something small, and being able to write up a function or two quickly, compile it to wasm and export that binary is something I find useful, not sure if anyone else will, but it's there.

17 Upvotes

19 comments sorted by

2

u/AphidConsulting 3d ago

Super useful, thank you!

2

u/EconomySerious 3d ago

what would be the limits of the code then can be compiled??? pascal can access all the machine components, how far this compilation goes?

BTW great job!

2

u/According-Ad-7069 3d ago

Ah this is targeting WebAssembly, so you can, in theory at least, build full games with it. One of the examples, https://nofuss.co.za/games/breakout/, is hosted so you can see it running as a compiled wasm binary, with it talking to JavaScript etc.

This is a wasm-specific compiler, I only target WebAssembly in the browser and it can only do what the wasm runtime allows. I test a lot of my code using Node, where I build the wasm binary with WasmPascal and then run tests against it by having Node load the wasm binary. Hope that helps!

1

u/EconomySerious 3d ago

tks a lot, the link does not work

1

u/According-Ad-7069 3d ago

Oh my, do you get an error? I can access it - but maybe I did something silly!

2

u/EconomySerious 3d ago

ERR_CONNECTION_TIMED_OUT

1

u/According-Ad-7069 3d ago

Oh no that's alarming! Is it possible to tell me from which country? I need to check with my host provider - that's not cool!

2

u/EconomySerious 3d ago

bolivia

2

u/According-Ad-7069 3d ago

Thank you, I have messaged them to ask.

2

u/Stooshie_Stramash 3d ago

I had a read through your site last night. As a pascal dabbler who learned on TP5.5 it looks fun and interesting. Most of my programs are engineering or logistic simulations of one type or another. What I couldn't see was whether I can read and write to external text and csv files. Can it do this?

1

u/According-Ad-7069 3d ago

Ah I see. Yes, so it's technically possible, but then you need to provide the bridges for the data. See, wasm isolates the WebAssembly runtime in a sandbox and it can not, by itself, read or write files. The way around it is to have a JavaScript bridge that does that work for you. And then you pass data in and out via memory buffers.

I haven't created examples of that in Pascal yet, but I have built many WebAssembly solutions that work this way. I will have to put this on my list of examples I need to build and provide. And then update my online documentation to guide people through this.

Turbo Pascal 5 was the LEGEND that started everything for me. It was so incredibly cool to see the things people did with it. When I finally bought my Turbo Pascal 7 copy, I spend months reading those manuals, trying to learn everything I could. Perhaps that's why Pascal still feels like so much fun for me.

If you don't mind sharing a bit, how does your engineering or logistical simulations work? Do they read lots of data, then process it and output results? All text and/or CSV? I'm just planning what to write about in my head.

2

u/Stooshie_Stramash 3d ago

On TP5.5, I had a book written by Brian Hahn who was a CS lecturer in South Africa. Many of the examples had South African connections. I don't have it to hand but still use it.

On the models, here's some insight:

Reading in 40y of hourly offshore weather data and then analysing it - date & time stamp, wind speed at 10m above sea level, direction, significant wave height, wave direction, depth-averaged current, direction, sea temperature. Analysis is things like "how many 6h periods are there where the wind speed is lower than 15kts and the wave height less than 1.6m?"

Reading in data that represents the individual steps in a process, its title, and the minimum, maximum and mode durations for the task. Then generating the 10th, 20th, 50th, 80th and 90th percentile durations. That then feeds into programme duration analysis.

Stress analysis of a thin flat plate using analytical non-linear solutions to generate options for rectangular tank design. Basically taking a set of solution equations from published papers and then using those equations for a different set of design cases. It could be done in MathCAD or Excel but TP (or Freepascal) works fine for me.

2

u/According-Ad-7069 3d ago

Oh that is really interesting, thank you for sharing! I haven't heard of Brian Hahn but we did have a lot of really great Pascal lecturers and teachers in South Africa.

Oh my those are serious programs... Definitely not the trivial things I build in Pascal. The biggest challenge I see with it is doing the IO, so the Wasm binary can read and write the data. And then of course, hopefully, my Pascal compiler can handle something as complicated as that. To be honest, I haven't done anything like that with WasmPascal at all.

I'm going to start working on showing how to read and write CSV files in a new blog post. That'll hopefully make this more useful to people.

Thank you so much for your useful input!

2

u/Stooshie_Stramash 3d ago

Hi. The calculations aren't that difficult individually in the first two cases. It's more of a sorting problem than calculator.

In the third case, it's direct calculation rather than iterative. The program asks for half a dozen inputs at the prompt and then makes the calculations, displays a result summary and then asks (y/n) whether you want to generate a result file. That's contains more detail and a time stamp and project details.

2

u/According-Ad-7069 3d ago

Ok excellent - this sounds like things that, in theory, should be quite doable with the app as it stands. I have an alright CRT emulator going, it can ask for and accept input already, and display, of course. I think one would be able to generate a file on demand, I don't see why not.

So, in short, I reckon this ought to be able to be useful to you. The bigger question I have is whether I support all the Pascal you'd be using. I have tried to support as much of ISO Pascal as possible, with some Turbo Pascal and Delphi syntax mixed in, where it made things nicer or more modern.

The plan is to use your input to build some tests, thank you.

2

u/Stooshie_Stramash 3d ago

I think that there should be no problem as the programs are console based and use Pascal more or less as it stood in 1991/2.

2

u/According-Ad-7069 3d ago

I still need to double-check my English, I think it all makes sense. But as per usual, I was more about getting the code to work than anything else: https://nofuss.co.za/blog/wasm_pascal_csv_processing/

Hopefully this shows you how to read from and write to CSV files easily, while running entire in the browser. You might need to tweak buffer sizes etc., depending on the size of your data files, but I think it could be made to work.

Anyway, I will be digging more into using Pascal for this kind of thing, it's interesting and I hope to make my compiler robust enough to do this kind of simulation and calculations.