Skip to content

Instantly share code, notes, and snippets.

@xpqz
Created November 29, 2021 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xpqz/2105fb4144c35f4b982d7cce9e78a03f to your computer and use it in GitHub Desktop.
Save xpqz/2105fb4144c35f4b982d7cce9e78a03f to your computer and use it in GitHub Desktop.
Embed ngn/k or ktye/k in c
/*
This is https://codeberg.org/ngn/k/src/branch/master/x/embed/a.c
with some added commentary.
See: https://github.com/ktye/i/blob/master/kc
*/
#include<stdio.h>
#include"../../k.h"
K add(K x,K y){
/*
The wrapper function would have to test the types of the arguments, maybe signal an error if
they are not right, extract the actual ints from them with iK(x) and iK(y), do whatever f()
is supposed to do, then create a k object for the result with Ki(value) and return it
*/
printf("add()\n");
int a=iK(x);
int b=iK(y);
int c=a+b;
return Ki(c);
}
int main(){
setbuf(stdout,0);
printf("kinit()\n"); kinit();
printf("KR()\n"); K f=KR(add,2,"<add>"); // KR(func,valence,displayName) makes a k function from a c function
printf("KA()\n"); KA("add",f); // KA(name,value) is assignment, like `:`
printf("KC()\n"); K s=KC("`0:$add[2;3]",13); // KC creates a k-value vector (data, length)
printf("K1()\n"); K1('.',s); // K1 calls a k function
printf("return\n"); return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment