mirror of
https://github.com/teo3300/rust-mal.git
synced 2026-01-13 01:35:31 +01:00
Project start: writing step step0_repl
Step 0 alone only act as an echo for the input, define the sructure of the interpreter Signed-off-by: teo3300 <matteo.rogora@live.it>
This commit is contained in:
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rust-mal"
|
||||||
|
version = "0.1.0"
|
||||||
19
src/main.rs
Normal file
19
src/main.rs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
// io lib to read input and print output
|
||||||
|
use std::io::{self, Write};
|
||||||
|
|
||||||
|
mod step0_repl;
|
||||||
|
use step0_repl::rep;
|
||||||
|
|
||||||
|
fn main() -> io::Result<()> {
|
||||||
|
loop {
|
||||||
|
print!("user> ");
|
||||||
|
// Flush the prompt to appear before command
|
||||||
|
let _ = io::stdout().flush();
|
||||||
|
|
||||||
|
let mut input = String::new();
|
||||||
|
|
||||||
|
io::stdin().read_line(&mut input)?;
|
||||||
|
|
||||||
|
print!("{}", rep(&input));
|
||||||
|
}
|
||||||
|
}
|
||||||
29
src/step0_repl.rs
Normal file
29
src/step0_repl.rs
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
// Structure the main functions of the interpreter
|
||||||
|
//
|
||||||
|
// For now just act as an echo, note that each function should not modify the
|
||||||
|
// input, thus this can be referenced by the previous step without the need
|
||||||
|
// to allocate more memory
|
||||||
|
|
||||||
|
#[allow(non_snake_case)]
|
||||||
|
/// Read input and generate an ast
|
||||||
|
fn READ(input: &str) -> String {
|
||||||
|
input.to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(non_snake_case)]
|
||||||
|
/// Evaluate the generated ast
|
||||||
|
fn EVAL(input: &str) -> String {
|
||||||
|
input.to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(non_snake_case)]
|
||||||
|
/// Print out the result of the evaluation
|
||||||
|
fn PRINT(input: &str) -> String {
|
||||||
|
input.to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn rep(input: &str) -> String {
|
||||||
|
let ast = READ(input);
|
||||||
|
let result = EVAL(&ast);
|
||||||
|
PRINT(&result)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user