read from file or stdin, add readme
This commit is contained in:
36
README.md
36
README.md
@@ -1,3 +1,39 @@
|
|||||||
# grassmudhorse.rs
|
# grassmudhorse.rs
|
||||||
|
|
||||||
Grass Mud Horse in Rust
|
Grass Mud Horse in Rust
|
||||||
|
|
||||||
|
help:
|
||||||
|
```shell
|
||||||
|
$ ./target/debug/grassmudhorse -h
|
||||||
|
grassmudhorse.rs v0.1
|
||||||
|
```
|
||||||
|
|
||||||
|
execute from file:
|
||||||
|
```shell
|
||||||
|
$ ./target/debug/grassmudhorse print-110.gmh
|
||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
4
|
||||||
|
5
|
||||||
|
6
|
||||||
|
7
|
||||||
|
8
|
||||||
|
9
|
||||||
|
10
|
||||||
|
```
|
||||||
|
|
||||||
|
execute from stdin:
|
||||||
|
```shell
|
||||||
|
$ cat print-110.gmh | ./target/debug/grassmudhorse
|
||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
4
|
||||||
|
5
|
||||||
|
6
|
||||||
|
7
|
||||||
|
8
|
||||||
|
9
|
||||||
|
10
|
||||||
|
```
|
||||||
|
|||||||
16
print-110.gmh
Normal file
16
print-110.gmh
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
草草草泥马
|
||||||
|
马草草草泥草草草草泥泥马
|
||||||
|
草马草
|
||||||
|
泥马草泥
|
||||||
|
草草草泥草泥草马
|
||||||
|
泥马草草
|
||||||
|
草草草泥马
|
||||||
|
泥草草草
|
||||||
|
草马草
|
||||||
|
草草草泥草泥泥马
|
||||||
|
泥草草泥
|
||||||
|
马泥草草泥草草草泥草泥马
|
||||||
|
马草马草泥草草草草泥泥马
|
||||||
|
马草草草泥草草草泥草泥马
|
||||||
|
草马马
|
||||||
|
马马马
|
||||||
39
src/main.rs
39
src/main.rs
@@ -4,6 +4,10 @@ mod parser;
|
|||||||
mod context;
|
mod context;
|
||||||
mod vm;
|
mod vm;
|
||||||
|
|
||||||
|
use std::{
|
||||||
|
env,
|
||||||
|
fs::read_to_string,
|
||||||
|
};
|
||||||
use err::*;
|
use err::*;
|
||||||
use parser::*;
|
use parser::*;
|
||||||
use context::*;
|
use context::*;
|
||||||
@@ -13,16 +17,45 @@ use vm::*;
|
|||||||
// https://playsecurity.org/rawfile/grass_mud_horse_language_specification.md
|
// https://playsecurity.org/rawfile/grass_mud_horse_language_specification.md
|
||||||
// https://p.rogram.me/grassmudhorse.js/grassmudhorse.js
|
// https://p.rogram.me/grassmudhorse.js/grassmudhorse.js
|
||||||
fn main() {
|
fn main() {
|
||||||
let input = "草草草泥马 马草草草泥草草草草泥泥马 草马草 泥马草泥 草草草泥草泥草马 泥马草草 草草草泥马 泥草草草 草马草 草草草泥草泥泥马 泥草草泥 马泥草草泥草草草泥草泥马 马草马草泥草草草草泥泥马 马草草草泥草草草泥草泥马 草马马 马马马";
|
let is_help = env::args().any(|a| a == "-h" || a == "--help");
|
||||||
|
if is_help {
|
||||||
|
println!("grassmudhorse.rs v0.1");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let is_debug = env::args().any(|a| a == "-d" || a == "--debug");
|
||||||
|
|
||||||
let instructions = match parse_lang(input) {
|
let arg_file = env::args().filter(|a| !a.starts_with("-")).nth(1);
|
||||||
|
|
||||||
|
let input = match arg_file {
|
||||||
|
Some(f) => match read_to_string(&f) {
|
||||||
|
Ok(s) => s, Err(err) => {
|
||||||
|
println!("[ERROR] Read file {}, error: {}", f, err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
let mut buff = String::new();
|
||||||
|
use std::io::Read;
|
||||||
|
match std::io::stdin().lock().read_to_string(&mut buff) {
|
||||||
|
Ok(_) => buff,
|
||||||
|
Err(err) => {
|
||||||
|
println!("[ERROR] Read stdin, error: {}", err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
let instructions = match parse_lang(&input) {
|
||||||
Ok(i) => i, Err(err) => {
|
Ok(i) => i, Err(err) => {
|
||||||
println!("Parse error: {}", err);
|
println!("Parse error: {}", err);
|
||||||
return;
|
return;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut context = Context::new(false);
|
let mut context = Context::new(is_debug);
|
||||||
|
|
||||||
if context.is_debug() {
|
if context.is_debug() {
|
||||||
println!("[DEBUG] parsed instructions:");
|
println!("[DEBUG] parsed instructions:");
|
||||||
|
|||||||
Reference in New Issue
Block a user