kc3-lang/kc3/doc/3_Guides/3.4_Structure.en.md

Download

Structure

libkc3

KC3 is implemented using libkc3 a small C99 library implementing the core of the language.

The library includes a parser and an interpreter for KC3 code in C structures.

Support for large integers provided by libtommath.

Support for C function calls provided by libffi.

Modules are saved as facts databases.

Parser

The parser is recursive descent.

AST

The AST is represented as KC3 data structures and can be meta-programmed in C.

Interpreter

Please see the following functions in libkc3/env.c:

ikc3

Interactive shell. Terminal I/O provided by linenoise.

Example :

$ make test
$ ikc3/ikc3
ikc3> 1 + 1
2
ikc3> 2 + 2
4
ikc3> 3 + 3
6
ikc3> 1 +
ikc3> 1
2
ikc3> double = fn (x) { x * 2 }
fn (x) { x * 2 }
ikc3> double
fn (x) { x * 2 }
ikc3> double(1)
2
ikc3> double(2)
4
ikc3> double(3)
6
ikc3> double(4)
8
ikc3> List.map([1, 2, 3, 4], double)
[2, 4, 6, 8]
ikc3> List.reverse(List.map([1, 2, 3, 4], double))
[8, 6, 4, 2]

Library path

The List.map and List.reverse functions are defined in lib/kc3/0.1/list.kc3 and can be modified in real time.

For example, without closing ikc3 let’s redefine List.reverse, open an editor and change the line in lib/kc3/0.1/list.kc3 from

  def reverse = fn {
    (x) { reverse(x, ()) }
    ([], acc) { acc }
    ([a | b], acc) { reverse(b, [a | acc]) }
  }

to

  def reverse = fn {
    (x) { reverse(x, ()) }
    ([], acc) { [:reversed | acc] }
    ([a | b], acc) { reverse(b, [a | acc]) }
  }
}}

and check the results of the last command (up key) in ikc3/ikc3 :

ikc3> List.reverse(List.map([1, 2, 3, 4], double))
[:reversed, 8, 6, 4, 2]

Don’t forget to revert the changes to list.kc3.

kc3s

Script interpreter. Works the same as ikc3 but is not interactive and does not output results.

HTTPd

HTTP daemon, use make test_httpd.

The http daemon is defined in httpd/httpd.c and lib/kc3/0.1/httpd.kc3.

The http daemon is both a static file server listing directories and serving files for display or download (Web 1.0), and a MVC framework loading KC3 files in ./app. The router is defined in ./conf/router.kc3 .


Top : KC3 guides

Previous : KC3 usage guide

Next : KC3 tutorial and examples guide


Source

Download