feat: add Console.readLine and Console.readInt, guessing game project
Add readLine and readInt operations to the Console effect for interactive input. Create a number guessing game project demonstrating ADTs, pattern matching, effects, and game state management. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2880,7 +2880,7 @@ impl Interpreter {
|
||||
Ok(Value::Unit)
|
||||
}
|
||||
}
|
||||
("Console", "read") => {
|
||||
("Console", "read") | ("Console", "readLine") => {
|
||||
let mut input = String::new();
|
||||
std::io::stdin()
|
||||
.read_line(&mut input)
|
||||
@@ -2890,6 +2890,23 @@ impl Interpreter {
|
||||
})?;
|
||||
Ok(Value::String(input.trim().to_string()))
|
||||
}
|
||||
("Console", "readInt") => {
|
||||
let mut input = String::new();
|
||||
std::io::stdin()
|
||||
.read_line(&mut input)
|
||||
.map_err(|e| RuntimeError {
|
||||
message: format!("Failed to read input: {}", e),
|
||||
span: None,
|
||||
})?;
|
||||
let trimmed = input.trim();
|
||||
match trimmed.parse::<i64>() {
|
||||
Ok(n) => Ok(Value::Int(n)),
|
||||
Err(_) => Err(RuntimeError {
|
||||
message: format!("Invalid integer: '{}'", trimmed),
|
||||
span: None,
|
||||
}),
|
||||
}
|
||||
}
|
||||
("Fail", "fail") => {
|
||||
let msg = request
|
||||
.args
|
||||
|
||||
10
src/types.rs
10
src/types.rs
@@ -780,6 +780,16 @@ impl TypeEnv {
|
||||
params: Vec::new(),
|
||||
return_type: Type::String,
|
||||
},
|
||||
EffectOpDef {
|
||||
name: "readLine".to_string(),
|
||||
params: Vec::new(),
|
||||
return_type: Type::String,
|
||||
},
|
||||
EffectOpDef {
|
||||
name: "readInt".to_string(),
|
||||
params: Vec::new(),
|
||||
return_type: Type::Int,
|
||||
},
|
||||
],
|
||||
},
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user