1 # An experimental Nit interactive interpreter
3 This tool is outside src/ because:
5 1. Is is greatly experimental
6 2. It can depend on readline (GPL3) whereas the rest of nitc is Apache2.
7 Both are compatible but the final binary result is GLP3.
11 * use GNU readline to read lines
12 * use importation/refinement to handle incremental execution (so basically everything works out of the box)
13 * maintain an interpreter and live objects (the model grows but the interpreter and runtime data are reused)
14 * runtime errors/aborts return to the interactive loop
15 * top-level variables are preserved but are only visible at the top level
20 * No model/object inspection
24 `-->` is the prompt, `...` the continuation prompt. What follows is the user input.
25 The rest is the output.
27 ### Basic instructions
38 ### Complex and control statements
55 You can use `do` blocks to delay the execution and control the scope of variables.
60 ...for i in [0..50[ do
68 ### Classes and methods
82 In case of static errors, the history (up arrow) can be reused and updated.
88 ^: Syntax Error: unexpected keyword 'end'.
89 -->class A **up arrow and update, thanks readline**
91 1,7: Redef Error: `A` is an imported class. Add the `redef` keyword to refine it.
94 -->redef class A **up arrow and update again, thanks readline**
95 redef fun foo do print "Bye"
103 Class refinement is available
106 -->redef class String
107 ...fun foo: String do return self + "foo"
113 Top-level methods automatically refine Sys.
117 1,1--3: Error: method or variable `foo` unknown in `Sys`.
127 Already instantiated objects gain the new methods, attributes and specializations.
136 ...redef fun to_s do return "A"
142 However, the new attributes are left uninitialized (default values or init are not recomputed on existing objects)
147 ...redef fun to_s do return "A:{v}"
150 Runtime error: Uninitialized attribute _v
151 redef fun to_s do return "A:{v}"
153 ,---- Stack trace -- - - -
154 | input-6$A$to_s (13,30)
155 | file$Sys$print (lib/core/file.nit:1694,19--29)
156 | input-7$Sys$main (15,1--7)
157 `------------------- - - -
163 ### Dynamic importation
166 -->print([0..10[.to_a.to_json)
167 1,20--26: Error: method `to_json` does not exists in `Array[Int]`.
168 print([0..10[.to_a.to_json)
171 -->print([0..10[.to_a.to_json)
172 [0,1,2,3,4,5,6,7,8,9]
177 The FFI is handled as with the non-interactive interpreter.
181 ...puts("Hello, world");