Skip to content

Instantly share code, notes, and snippets.

@antonijn
Created January 3, 2021 15:37
Show Gist options
  • Save antonijn/56e462faaac6169642488718ef925c24 to your computer and use it in GitHub Desktop.
Save antonijn/56e462faaac6169642488718ef925c24 to your computer and use it in GitHub Desktop.
cheax C++ API concept
#include <cheax.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int result;
CHEAX *c = cheax_init();
/* Loads some "unsafe" functions, i.e. setting the maximum stack depth,
IO functions etc. */
cheax_load_extra_builtins(c, CHEAX_ALL_BUILTINS);
/* Load the standard library */
if (cheax_load_prelude(c)) {
perror("failed to load prelude");
return EXIT_FAILURE;
}
cheax_sync(c, "result", CHEAX_INT, &result);
cheax_eval(c, cheax_readstr(c, "(set result (sum (.. 1 100)))"));
cheax_destroy(c);
printf("1 + 2 + ... + 100 = %d\n", result);
return 0;
}
#include <cheax.hpp>
#include <iostream>
#include <cstdlib>
int main()
{
cheax::vm c;
/* Loads some "unsafe" functions, i.e. setting the maximum stack depth,
IO functions etc. */
c.load_extra_builtins(cheax::ALL_BUILTINS);
/* Load the standard library */
if (c.load_prelude()) {
std::perror("failed to load prelude");
return EXIT_FAILURE;
}
int result;
c.sync("result", &result);
c.eval(c, c.readstr("(set result (sum .. 1 100)))"));
std::cout << "1 + 2 + ... + 100 = " << result << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment