4.17. kink/PROGRAM

Operations on kink programs.

4.17.1. PROGRAM.compile(Program_text ...[$config = {})]

`compile` compiles `Program_text` into a top level fun.

Preconditions:

• `Program_text` must be a str

• $config, if given, must be a fun which takes a conf val

The conf val provides the following methods:

• Conf.binding(Binding) : specifies `Binding` as the binding of the top level fun. If not called, a newly created binding is used.

• Conf.locale(Locale) : sets `Locale` as the locale used to create error messages. The default is LOCALE.root.

• Conf.name(Program_name) : uses `Program_name` as the name of the program. If the method is not called, "(compile)" is used as the default.

• Conf.on_success($success_cont): uses $success_cont as the success cont. If the method is not called, VAL.identity is used as the default.

• Conf.on_error($error_cont) : uses $error_cont as the error cont. If the method is not called, the default error cont raises an exception on an compile error.

Result:

• If the compilation succeeds, `compile` tail-calls the success cont with the compiled top level fun.

• If the compilation fails with a compile error, `compile` tail-calls the error cont with (Error_msg, Start_loc, End_loc), where Error_msg is the error message, Start_loc and End_loc are locs indicating the region of the compile error. The following condition holds: 0 <= Start_loc.pos <= End_loc.pos <= Program_text.size.

The top level fun is a fun which contains a sequence of instructions translated from the program, replacing every occurrence of `(binding)` by `(val Binding)`, where `Binding` is the binding of the top level fun.

See Language specification → Semantics about translation.

Example:

:PROGRAM.require_from('kink/')
:BINDING.require_from('kink/')
:LOCALE.require_from('kink/')
:fun <- PROGRAM.compile('10 + 20'){(:C)
  C.locale(LOCALE.for('ja-JP'))
  C.name('add.kn')
  C.on_error{(:Msg :Start_loc :End_loc)
    raise('unexpected compile error: {}'.format(Msg))
  }
}
stdout.print_line(fun(BINDING.new).repr)  # => 30

4.17.2. PROGRAM.compile_error_desc(Error_msg Start_loc End_loc)

compile_error_desc makes a typical description str of the compile error.

Parameters:

• Error_msg must be a message str.

• Start_loc must be the start loc of the compile error.

• End_loc must be the end loc of the compile error.

A typical result is: "compile error: unexpected token: [foo.kn L1 C6] 10 + -->+ 20"