REPL Interactive Interpreter
YaoXiang REPL (Read-Eval-Print Loop) is an interactive code execution environment that allows you to input and execute YaoXiang code line by line, making it ideal for learning, testing, and debugging.
Quick Start
Starting the REPL
Run the following command in the terminal to start the REPL:
yx replOr simply run yaoxiang (without any subcommand):
yaoxiangAfter starting, you will see the prompt:
YaoXiang REPL - Type :help for assistance
Press Ctrl+D or :quit to exit
>>Basic Usage
Enter YaoXiang code after the >> prompt and press Enter to execute:
>> 1 + 2
3
>> "Hello, World!"
"Hello, World!"
>> x = 10
>> x * 2
20Exiting the REPL
There are three ways to exit the REPL:
- Keyboard shortcut: Press
Ctrl+D - Command: Enter
:quitor:q - Interrupt: Press
Ctrl+Cto interrupt the current input
Command System
The REPL provides a series of special commands starting with a colon :.
Help Command
>> :helpDisplays help information for all available commands.
Quit Command
>> :quitExits the REPL. You can also use the short form :q.
Clear Command
>> :clearClears all defined variables and functions, resetting the REPL state. You can also use the short form :c.
Type Inspection Command
>> :type xInspects the type information of symbol x. You can also use the short form :t.
Example:
>> name = "YaoXiang"
>> :type name
name: String
>> add: (a: Int, b: Int) -> Int = a + b
>> :type add
add: fn(Int, Int) -> IntSymbol List Command
>> :symbolsLists all defined symbols (variables and functions) in the current REPL. You can also use the short forms :i or :info.
Example:
>> x = 10
>> y = 20
>> greet: (name: String) -> String = "Hello, " + name
>> :symbols
x: Int
y: Int
greet: fn(String) -> StringHistory Command
>> :historyDisplays the command history. You can also use the short form :hist.
Statistics Command
>> :statsDisplays execution statistics, including the number of evaluations and total execution time.
Example:
>> :stats
Eval count: 5
Total time: 12.34msCode Execution
Expression Evaluation
The REPL can evaluate any valid YaoXiang expression:
>> 1 + 2
3
>> 10 * 5 + 3
53
>> "Hello" + " " + "World"
"Hello World"
>> true and false
falseVariable Definition
Define variables directly using the variable name:
>> name = "YaoXiang"
>> age = 25
>> pi = 3.14159You can also explicitly annotate the type:
>> name: String = "YaoXiang"
>> age: Int = 25After definition, variables can be used in subsequent code:
>> name
"YaoXiang"
>> age + 5
30Function Definition
YaoXiang has no fn keyword; functions are simply values with signatures:
>> add: (a: Int, b: Int) -> Int = a + b
>> greet: (name: String) -> String = "Hello, " + nameCalling a function:
>> add(3, 4)
7
>> greet("World")
"Hello World"Multi-line Code
The REPL supports multi-line code input. When it detects incomplete code (such as unclosed parentheses), it automatically enters continuation mode:
>> factorial: (n: Int) -> Int = {
.. if n <= 1 { return 1 }
.. return n * factorial(n - 1)
.. }The continuation prompt is .., indicating that the REPL is currently in multi-line input mode.
Type Definition
>> Point: Type = { x: Float, y: Float }Variant Type Definition (Enum)
>> Color: Type = { red: () -> Color, green: () -> Color, blue: () -> Color }Auto-completion
The REPL provides intelligent auto-completion to help you input code quickly.
Trigger Method
Press the Tab key to trigger auto-completion.
Completion Content
- Keyword completion: YaoXiang language keywords (press Tab to expand)
- Symbol completion: Already defined variable and function names
- Builtin function completion: Builtin functions such as
print,len,range,typeof,assert, etc.
Completion Examples
>> my_variable = 42
>> my_<Tab>
my_variable: Int
>> calculate_sum: (a: Int, b: Int) -> Int = a + b
>> calc<Tab>
calculate_sum: fn(Int, Int) -> IntAdvanced Features
Error Handling
When an error occurs in the code, the REPL displays detailed error information:
>> x = 10 / 0
Error: Runtime error: DivisionByZero
>> undefined_variable
Error: Unknown symbol: undefined_variableErrors do not terminate the REPL session; you can continue entering new code.
History
The REPL automatically saves command history, supporting:
- Up/Down arrows: Browse through command history
- Search: Enter a partial string and use the up/down arrows to search
- History file: History is saved to a file and automatically loaded on the next startup
Execution Statistics
Use the :stats command to view execution statistics:
>> :stats
Eval count: 15
Total time: 45.67msThis helps monitor code performance.
Best Practices
1. Use Meaningful Variable Names
// Good
user_name = "YaoXiang"
max_retries = 3
// Bad
x = "YaoXiang"
n = 32. Define Functions to Reuse Code
>> is_even: (n: Int) -> Bool = n % 2 == 0
>> is_even(4)
true
>> is_even(7)
false3. Use :clear to Reset State
When the REPL state becomes messy, use :clear to reset:
>> :clear
Context cleared4. Leverage Auto-completion for Efficiency
Enter the first few characters and press Tab to quickly complete variable and function names.
5. Use Multi-line Input for Complex Code
>> fibonacci: (n: Int) -> Int = {
.. if n <= 1 { return n }
.. return fibonacci(n - 1) + fibonacci(n - 2)
.. }FAQ
Q: How do I view the definition of a function?
A: Use the :type command to view the function signature:
>> :type my_function
my_function: fn(Int, String) -> BoolQ: How do I clear all definitions?
A: Use the :clear command:
>> :clearQ: Why isn't my multi-line code executing?
A: Check for unclosed parentheses, quotes, or braces. The REPL waits for complete code input.
Q: How do I interrupt long-running code?
A: Press Ctrl+C to interrupt the current execution.
Q: What data types does the REPL support?
A: The REPL supports all YaoXiang data types:
Int: IntegerFloat: Floating-point numberString: StringBool: BooleanVoid: Void type- Custom record types and variant types
Example Session
Here is a complete example of a REPL session:
YaoXiang REPL - Type :help for assistance
Press Ctrl+D or :quit to exit
>> greeting = "Hello"
>> name = "YaoXiang"
>> greeting + ", " + name + "!"
"Hello, YaoXiang!"
>> factorial: (n: Int) -> Int = {
.. if n <= 1 { return 1 }
.. return n * factorial(n - 1)
.. }
..
>> factorial(5)
120
>> :symbols
greeting: String
name: String
factorial: fn(Int) -> Int
>> :stats
Eval count: 4
Total time: 2.34ms
>> :quitRelated Commands
| Command | Short Form | Function |
|---|---|---|
:help | :h | Display help information |
:quit | :q | Exit the REPL |
:clear | :c | Clear all state |
:type | :t | View symbol type |
:symbols | :i | List all symbols |
:history | :hist | Display command history |
:stats | - | Display execution stats |
