gpoore / codebraid

Live code in Pandoc Markdown
BSD 3-Clause "New" or "Revised" License
367 stars 13 forks source link

Add support for GAP (www.gap-system.org) #51

Closed ChrisJefferson closed 1 year ago

ChrisJefferson commented 2 years ago

This adds support for the "GAP" programming language.

I'm going to try using this over the next few days, so may improve it, but thought I would put it here for any initial comments.

One thing I wasn't completely sure on is how many (and how much it matters?) newlines I need before + after the delims.

ChrisJefferson commented 2 years ago

If you are curious, GAP can be used as part of Sagemath (and also as a Julia library too!) but is also frequently used as a seperate project.

Known issues: GAP has automatic line wrapping. I made lines 9999 characters long so this doesn't interact with codebraid's output commands.

gpoore commented 2 years ago

I've looked over this and have some comments now. I'll go over it again a final time in more detail to double-check everything once you are ready to merge.

Two final remarks that aren't directly related to this code:

ChrisJefferson commented 2 years ago

Hi, I'll go through your comments, thanks! I'll just mention a few (as they might be generally useful).

GAP doesn't have any explicit 'flush' function. (If you go searching you might find IO_flush, but that isn't useful in this situation). So far codebraid seems to be working fine without it. If flushing really is required, I could investigate adding it to GAP (I could also use the IO library to do output, so then I could use IO_flush, but that raises other issues).

Unfortunately, at the moment we don't get error locations. By default when an error occurs GAP enters a debugger straight away (which hangs codebraid, as you have to type into it). I disable that debugger, but it's the debugger that does most work of printing error locations.

The ();; is because else GAP's parser gets confused, particularly by missing semi-colons, so the output ends up including Codebraid statements. This isn't "broken", but a little unpleasent, like:

 Error, no method found! For debugging hints type ?Recovery from NoMethodFound Error, no 1st choice method found for `+' on 2 arguments Syntax error: ; expected in source.gap:[6] Print("\n #Codebraid(output=stdout, delim=end, chunk=0, output_chunk=0, hash=90eab37110bf9e59cc9614251bd51036172efe075e3bad05bad6e73276915f21,)\n");

I will have a look at the python_repl. It might be interesting to use that when people want to interact with the debugger (which I mentioned earlier is built-in, as soon as you enter an error in interactive mode, the prompt changes to brk>).

gpoore commented 2 years ago

For flushing output: All the existing templates for languages flush stdout and stderr, but that's because there's an assumption that these typically won't be line buffered. If GAP automatically flushes at the end of each line, then this isn't necessary. Actually, the main impacts of not flushing are (1) --live-output might be delayed and (2) some output might be lost due to errors that cause a program to exit before stdout and/or stderr are flushed (which may not be possible or an issue with GAP).

For error locations: It might be possible to modify Codebraid so that it detects the debugger starting by looking for brk>, and then writes something to the program's stdin. I don't know if that would be helpful in getting better output when there are errors.

ChrisJefferson commented 1 year ago

Sorry, dropped this PR on the floor (although I have been using this myself for generating some small documents).

I had a rebase and cleaned up some minor issues. While I'm sure it could continue to be improved, I think it's OK for now.

Random thing I observed while looking over this -- the sage integration seems to have broken, I think it's just not been updated?

gpoore commented 1 year ago

Sorry for the delay...I've been focused on updating the Codebraid VS Code extension recently, and am now back to working on Codebraid itself. This looks good, so I've merged it. It will probably be a few days before a new release incorporating GAP is out. Also, I'll check Sage support and see if something needs to be fixed.