HQ9+ C Function Interface

From Esolang
Jump to navigation Jump to search

An esolang created by User:Oshaboy in 2022. After creating the same SDL2 demo in many different programming languages (COBOL, Awk, C-INTERCAL, Funge-98 and APL) using their C Function Interface. So why not make the same demo in HQ9+? Well, because HQ9+ doesn't have a C Function Interface. But it is trivial to modify HQ9+ to allow that.

Commands

H Q 9 and + are copied verbatim from HQ9+.

S is a "binding" that creates an SDL2 window and renderer and draws random lines in random colors on the screen. Once the window is quit the code resumes.

C Interface

But wait, there's more.

What about Calling HQ9+ code from C. Well, I've created a header and library to do just that.

There is 1 typedef, 7 methods and 5 defines.

Types

HQ9P_State is a structure that contains all the data required by HQ9+. This allows you to create many different HQ9+ functions and execute each in its own environment. Due to data hiding best practices the structure contents are undefined (so you can't just read the accumulator using C code).

Methods

  • HQ9P_State * HQ9P_Initialize(char *): Create a new HQ9P state. The code has to be passed to it for the q function
  • void HQ9P_destroy(HQ9P_State *): Destructor.
  • void HQ9P_parse_char(HQ9P_State * state,char c): takes a char and runs it as an HQ9+ Command.
  • void HQ9P_parse_next_char(HQ9P_State * state): takes the next char of the source code and runs it.
  • char * HQ9P_parse_char_buf( char * buf, size_t n, HQ9P_State * state,char c): This allows you to put the output of an HQ9+ Command in a buffer.
  • char * HQ9P_parse_next_char_buf(char * buf, size_t n, HQ9P_State * state): You get the gist
  • int HQ9P_is_at_end(HQ9P_State * state): Returns 1 if at the end of the code and 0 otherwise.

Defines

The defines are "H" "Q" "N" "P" and "S". each one calling the equivalent HQ9+ Command. The scope used is the last one created by HQ9P_Initialize.

Implementation

https://github.com/oshaboy/HQ9--CFI