4.2. REPL

REPL (read-evel-print loop) is an interactive shell of Kink language. The core functionality of REPL is provided by kink/repl/REPL mod.

4.2.1. Start and stop REPL

To start REPL, execute kink command without spec-and-argv args (see commandline format).

REPL itself does not provide line editing or completion. Thus, in Unix-like environments, using rlwrap is recommended. For example:

$ rlwrap kink

In windows environment, Command Prompt provides a basic line editing function. Thus, it might be enough to just invoking kink command.

> kink

To exit from REPL, kill the JVM process by pressing Ctrl+C.

4.2.2. Usage of REPL

REPL reads a Kink program from stdin, and writes the result of repr method of the result of the program.

kink#0001:0001> 1 + 2 + 3
  6

kink#0002:0001> 'foo' + 'bar'
  "foobar"

REPL reports a compile error if the input is not a valid program.

kink#0001:0001> 1 + 2 + + 3
  compile error: unexpected token: [kink#0001 L1 C9] 1 + 2 + -->+ 3

REPL continues reading from stdin while the program can be valid with more input.

kink#0001:0001> 1 +
kink#0001:0002\ 2 +
kink#0001:0003\ 3
  6
kink#0002:0001> 'foo' +
kink#0002:0002\ 'bar'
  "foobar"

An environment is shared among execution of programs. Thus, you can use local variables to store data.

kink#0001:0001> :dbl <- {(:N)
kink#0001:0002\   N * 2
kink#0001:0003\ }
  nada
kink#0002:0001> :A <- 21
  nada
kink#0003:0001> dbl(A)
  42

If the program raises an exception, the exception traces are printed to stdout.

kink#0001:0001> 10 // 0
  exception traces: from oldest to newest
  [launch]
  [builtin:kink-mods/kink/_launch/LAUNCH_KINK.kn L232 C3 _launch_aux] -->_launch_aux(Args Dep)
  [builtin:kink-mods/kink/_launch/LAUNCH_KINK.kn L215 C11 try] CONTROL.-->try(
  [builtin:kink-mods/kink/CONTROL.kn L201 C14 reset] :handler = KONT.-->reset('kink/CONTROL-try'){
  [builtin:kink-mods/kink/CONTROL.kn L202 C10 body] :R = -->body
  [builtin:kink-mods/kink/_launch/LAUNCH_KINK.kn L217 C7 _start] -->_start(Non_opts Dep)
  [builtin:kink-mods/kink/_launch/LAUNCH_KINK.kn L122 C19 if_else] Non_opts.empty?.-->if_else(
  [builtin:kink-mods/kink/_launch/LAUNCH_KINK.kn L124 C12 run] REPL.-->run{(:C)
  [builtin:kink-mods/kink/repl/REPL.kn L31 C3 _program_loop] -->_program_loop(Conf 1)
  [builtin:kink-mods/kink/repl/REPL.kn L115 C3 _read_program] -->_read_program(Conf Program_no
  [builtin:kink-mods/kink/repl/REPL.kn L167 C3 line_loop] -->line_loop(1 '>' '')
  [builtin:kink-mods/kink/repl/REPL.kn L148 C18 scan_line] Conf.Scanner.-->scan_line{(:C)
  [builtin:kink-mods/kink/io/SCANNER.kn L365 C5 _wrapper_scan_line] -->_wrapper_scan_line(S $present_cont $absent_cont $config_read_bin)
  [builtin:kink-mods/kink/io/SCANNER.kn L473 C3 loop] -->loop('')
  [builtin:kink-mods/kink/io/SCANNER.kn L433 C5 branch] -->branch(
  [builtin:kink-mods/kink/io/SCANNER.kn L456 C11 read_bin] S.-->read_bin(1024){(:C)
  [builtin:kink-mods/kink/io/SCANNER.kn L336 C13 read_bin] S.Input.-->read_bin(Max_size ... Config_fun)
  [builtin:kink-mods/kink/javahost/JAVA_IO.kn L55 C15 call_method] In.Stream.-->call_method('read' [Bytes]){(:C)
  [read_conf]
  [builtin:kink-mods/kink/_java/CALL_AUX.kn L19 C3 cont] -->cont(
  [builtin:kink-mods/kink/javahost/JAVA_IO.kn L63 C23 if_else] Bin.nonempty?.-->if_else(
  [builtin:kink-mods/kink/javahost/JAVA_IO.kn L65 C13 present_cont] -->present_cont(Bin)
  [builtin:kink-mods/kink/io/SCANNER.kn L461 C13 loop] -->loop(Prefix)
  [builtin:kink-mods/kink/io/SCANNER.kn L433 C5 branch] -->branch(
  [builtin:kink-mods/kink/io/SCANNER.kn L435 C40 for_maybe] S.Buf.search_slice(S.Ind "\n").-->for_maybe(
  [builtin:kink-mods/kink/io/SCANNER.kn L440 C13 present_cont] -->present_cont(Result)
  [builtin:kink-mods/kink/repl/REPL.kn L151 C17 compile] PROGRAM.-->compile(Program){(:Conf)
  [builtin:kink-mods/kink/PROGRAM.kn L64 C3 _compile_raw] -->_compile_raw(
  [builtin:kink-mods/kink/PROGRAM.kn L116 C3 handler] -->handler
  [builtin:kink-mods/kink/PROGRAM.kn L123 C16 success_cont] JAVA.wrap{ -->success_cont($compiled_fun) }
  [builtin:kink-mods/kink/repl/REPL.kn L117 C15 try] CONTROL.-->try(
  [builtin:kink-mods/kink/CONTROL.kn L201 C14 reset] :handler = KONT.-->reset('kink/CONTROL-try'){
  [builtin:kink-mods/kink/CONTROL.kn L202 C10 body] :R = -->body
  [builtin:kink-mods/kink/repl/REPL.kn L118 C11 program] { -->program(Conf.Env) }
  [kink#0001 L1 C4 op_intdiv] 10 -->// 0
  exception message: Num.op_intdiv: zero division: 10 is divided by 0

4.2.3. Cancel the input

When you want to cancel the input of a program, insert EOF.

In Unix like systems, type Control+D to insert EOF.

kink#0003:0001> 10 +
kink#0003:0002\ 20 +
kink#0003:0003\ (type Control-D here)
kink#0004:0001>

In Windows, type Control+Z and the return key to insert EOF.

kink#0003:0001> 10 +
kink#0003:0002\ 20 +
kink#0003:0003\ (type Control-Z and the return key here)
kink#0004:0001>

When you insert EOF in the beginning of the program, it terminates REPL.