Running K/ngnk: Difference between revisions

From The K Language Wiki
Content added Content deleted
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
== Installing ngn/k ==
== Installing ngn/k ==


=== Linux ===
=== Linux package managers ===


==== Arch et al. ====
==== Arch et al. ====


ngn/k is available on the [https://aur.archlinux.org/ AUR] package manager; thus it can be conveniently installed on [https://archlinux.org/ Arch Linux] and related distros, like [https://manjaro.org/ Manjaro].
ngn/k is available on the [https://aur.archlinux.org/ AUR] package manager; thus it can be conveniently installed on [https://archlinux.org/ Arch Linux] and related distros, like [https://manjaro.org/ Manjaro].

=== Compiling from source ===

The ngn/k repo contains a makefile that will build an executable <code>k</code> binary. Run <code>make</code> to build <code>k</code>, which you can then put into your <code>PATH</code>.

It also contains <code>repl.k</code>, which when run provides a convenient REPL environment for interactive programming. It's convenient to add a simple alias such as the following:

<syntaxhighlight lang="sh">
alias krepl='rlwrap k ~/sources/k/repl.k'
</syntaxhighlight>


== Building external libraries ==
== Building external libraries ==
Line 13: Line 23:
Roughly, including external libraries into ngn/k programs consists of three steps:
Roughly, including external libraries into ngn/k programs consists of three steps:


1. Write a K-compatible C program by including the <code>k.h</code> header
# Write a K-compatible C program by including the <code>k.h</code> header
2. Compile the C program into a shared object file
# Compile the C program into a shared object file
3. Inside of a K program, load the shared object file and assign it to a verb.
# Inside of a K program, load the shared object file and assign it to a verb.


Note that <code>k.h</code> and <code>libk.so</code> must be available to your compiler and linker, respectively:
Note that <code>k.h</code> and <code>libk.so</code> must be available to your compiler and linker, respectively:


1. <code>k.h</code> is available in the ngn/k source code;
# <code>k.h</code> is available in the ngn/k source code;
2. The ngn/k makefile includes a target to build <code>libk.so</code>.
# The ngn/k makefile includes a target to build <code>libk.so</code>.


(The AUR package above installs both of the required files into the standard system directories)
(The AUR package above installs both of the required files into the standard system directories)
Line 30: Line 40:
[https://github.com/x86y/onikuruma onikuruma] is an implementation of the [https://github.com/kkos/oniguruma Oniguruma] regular expression library for K. It contains both the required C code and Makefile as examples of how to build K-compatible external libraries, as well as example K code for how to call an external library from K. A short example for reference:
[https://github.com/x86y/onikuruma onikuruma] is an implementation of the [https://github.com/kkos/oniguruma Oniguruma] regular expression library for K. It contains both the required C code and Makefile as examples of how to build K-compatible external libraries, as well as example K code for how to call an external library from K. A short example for reference:


<syntaxhighlight lang="cpp">
<syntaxhighlight lang="k">
m:`"./libregex.so"2:(`match;2)
m:`"./libregex.so"2:(`match;2)
m["asd";"someasdstring"]
m["asd";"someasdstring"]

Latest revision as of 01:15, 29 June 2024

Installing ngn/k[edit]

Linux package managers[edit]

Arch et al.[edit]

ngn/k is available on the AUR package manager; thus it can be conveniently installed on Arch Linux and related distros, like Manjaro.

Compiling from source[edit]

The ngn/k repo contains a makefile that will build an executable k binary. Run make to build k, which you can then put into your PATH.

It also contains repl.k, which when run provides a convenient REPL environment for interactive programming. It's convenient to add a simple alias such as the following:

alias krepl='rlwrap k ~/sources/k/repl.k'

Building external libraries[edit]

ngn/k provides a k.h header file. When this file is included, it describes an interface that C programs can implement in order to provide shared objects that are linkable from K programs.

Roughly, including external libraries into ngn/k programs consists of three steps:

  1. Write a K-compatible C program by including the k.h header
  2. Compile the C program into a shared object file
  3. Inside of a K program, load the shared object file and assign it to a verb.

Note that k.h and libk.so must be available to your compiler and linker, respectively:

  1. k.h is available in the ngn/k source code;
  2. The ngn/k makefile includes a target to build libk.so.

(The AUR package above installs both of the required files into the standard system directories)

References and Examples[edit]

Onikuruma[edit]

onikuruma is an implementation of the Oniguruma regular expression library for K. It contains both the required C code and Makefile as examples of how to build K-compatible external libraries, as well as example K code for how to call an external library from K. A short example for reference:

m:`"./libregex.so"2:(`match;2)
m["asd";"someasdstring"]

Douglas Mennella's ngnk-libs[edit]

README.org