3.18. kink/PROGRAM

Operations on kink programs.

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

PROGRAM.compile compiles the 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.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, CORE.id 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, PROGRAM.compile tail-calls the success cont with the compiled top level fun. The top level fun takes an env as the top level env, and evaluates the program.

• If the compilation fails with a compile error, PROGRAM.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.

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

3.18.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"