6.80. kink/program/PROGRAM¶
6.80.1. type program¶
`program` is a pair of a program name and a program text.
6.80.1.1. P.name¶
`name` returns the program name as a `str`.
6.80.1.2. P.text¶
`text` returns the program text as a `str`.
6.80.1.3. P.compile(...[$config={}])¶
`compile` compiles `P` into a fun.
Config methods:
• C.binding(Binding): default = BINDING.new
• C.locale(Locale): default = LOCALE.root
• C.on_success($success): default = VAL$identity
• C.on_error($error): default = a fun which raises an exception
If the program is compiled to a fun, `compile` tail-calls $success with the fun.
If a compile error occurs, `compile` tail-calls $error with a `compile_error`. `Program` attribute of the `location` values of the `compile_error` will be equal to `P`.
Compile
The program text is transformed to a sequence of abstract instructions, as specified in ”Language specification” chapter of the manual.
Then, every occurrence of `(binding)` instruction in the sequence is replaced with `(val Binding)`. The replacement is done only on the outermost sequence, and not on nested sequences.
The result fun will contain the sequence of abstract instructions after the replacement.
Preconditions
`Binding` must be a `binding`.
`Locale` must be a `locale`.
$success must be a fun which takes a fun.
$error must be a fun which takes a `compile_error`.
Example
:BINDING.require_from('kink/')
:LOCALE.require_from('kink/')
:PROGRAM.require_from('kink/program/')
:P <- PROGRAM.new('add.kn' '10 + 20')
:fun <- P.compile{(:C)
C.locale(LOCALE.for('ja-JP'))
C.on_error{(:Err)
raise('unexpected compile error: {}'.format(Err.desc))
}
}
:R <- fun
stdout.print_line(R.repr) # => 30
6.80.1.4. P1 == P2¶
`==` operator, or `op_eq` method, returns whether `P1` and `P2` have the equal program name and the program text.
Precondition
`P2` must be a `program`.
6.80.2. PROGRAM.new(Name Text)¶
`new` returns a new `program`, with `Name` as the program name, and `Text` as the program text.
Preconditions
`Name` must be a `str`.
`Text` must be a `str`.
6.80.3. PROGRAM.is?(Val)¶
`is?` returns whether `Val` is a `program`.