Skip to content

Commit

Permalink
+ request
Browse files Browse the repository at this point in the history
  • Loading branch information
meloalright committed Aug 10, 2023
1 parent a936a3a commit 33c0eb9
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ rand = { version = "0.7.3", features = ["getrandom"] }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
rand = { version = "0.7.3" }
reqwest = { version = "0.11", features = ["blocking"] }

[[bin]]
name = "runtime"
Expand Down
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,20 @@ Playground: [https://rustq.github.io/3body-lang/](https://rustq.github.io/3body-
毁灭();
```

### Request

##### Format

```
寻找(<arg1>): void
```

##### Example

```rust
A 以 寻找("https://raw.githubusercontent.com/rustq/3body-lang/main/LICENSE");
```

## Summary

|Monkey|3body-lang|Explanation|
Expand All @@ -225,6 +239,7 @@ Playground: [https://rustq.github.io/3body-lang/](https://rustq.github.io/3body-
|sleep|冬眠|"hibernation"|
|clear|二向箔清理|"two-way foil cleaning"|
|exit|毁灭|"destroy"|
|request|寻找|"search"|


## System Libraries
Expand All @@ -243,7 +258,7 @@ $ make repl
```

```
$ ./target/debug/runtime ./example/macroatom.3body
$ ./target/debug/runtime ./example/地球.3body
```

```
Expand Down
3 changes: 3 additions & 0 deletions example/寻找.3body
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
给 A 以 寻找("https://raw.githubusercontent.com/rustq/3body-lang/main/LICENSE");

广播(A);
22 changes: 22 additions & 0 deletions src/evaluator/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use lexer::Lexer;
use parser::Parser;

extern crate rand;
#[cfg(not(target_arch = "wasm32"))]
extern crate reqwest;
use evaluator::builtins::rand::distributions::Uniform;
use evaluator::builtins::rand::{thread_rng, Rng};

Expand All @@ -23,6 +25,8 @@ pub fn new_builtins() -> HashMap<String, Object> {
builtins.insert(String::from("rest"), Object::Builtin(1, monkey_rest));
builtins.insert(String::from("push"), Object::Builtin(2, monkey_push));
builtins.insert(String::from("广播"), Object::Builtin(1, three_body_puts));
#[cfg(not(target_arch = "wasm32"))]
builtins.insert(String::from("寻找"), Object::Builtin(1, three_body_request));
builtins.insert(
String::from("二向箔清理"),
Object::Builtin(0, three_body_clear),
Expand Down Expand Up @@ -105,6 +109,24 @@ fn three_body_puts(args: Vec<Object>) -> Object {
Object::Null
}

#[cfg(not(target_arch = "wasm32"))]
fn three_body_request(args: Vec<Object>) -> Object {
match &args[0] {
Object::String(o) => {
let resp = reqwest::blocking::get(o);
match resp {
Ok(res) => {
Object::String(res.text().expect("should be text"))
},
Err(error) => {
Object::Error(format!("request got {}", error))
}
}
}
_ => Object::Null,
}
}

fn three_body_clear(_args: Vec<Object>) -> Object {
std::process::Command::new("clear").status().unwrap();
Object::Null
Expand Down
4 changes: 1 addition & 3 deletions src/evaluator/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,9 @@ mod tests {
fn test_object_hash() {
let mut hash = HashMap::new();
hash.insert(Object::String("a".to_string()), Object::Int(1));
hash.insert(Object::String("c".to_string()), Object::Int(2));
hash.insert(Object::String("b".to_string()), Object::Int(3));

let obj = Object::Hash(hash);
assert_eq!(obj.to_string(), "{\"c\": 2, \"b\": 3, \"a\": 1}");
assert_eq!(obj.to_string(), "{\"a\": 1}");
}

#[test]
Expand Down
21 changes: 21 additions & 0 deletions src/wasm/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ extern "C" {
fn sleep(secs: c_uint) -> c_uint;
fn clear() -> c_void;
fn random(input_uint: c_uint) -> c_uint;
fn request(url: *mut c_char) -> *mut c_char;
}

fn internal_print(msg: &str) {
Expand All @@ -49,6 +50,13 @@ fn internal_random(input: &i64) -> u32 {
}
}

fn internal_request(input: &str) -> String {
unsafe {
let result = request(string_to_ptr(input.to_string())) as *mut c_char;
let c_str = CStr::from_ptr(result);
c_str.to_str().unwrap().to_owned()
}
}


fn string_to_ptr(s: String) -> *mut c_char {
Expand Down Expand Up @@ -141,6 +149,19 @@ pub fn eval(input_ptr: *mut c_char) -> *mut c_char {
}),
);

env.set(
String::from("寻找"),
&Object::Builtin(-1, |args| {
match &args[0] {
Object::String(o) => {
let n = internal_request(&format!("{}", o));
Object::String(n)
},
_ => Object::Null
}
}),
);

let mut evaluator = Evaluator::new(Rc::new(RefCell::new(env)));
let evaluated = evaluator.eval(&program).unwrap_or(Object::Null);
let output = format!("{}", evaluated);
Expand Down
7 changes: 7 additions & 0 deletions web/src/js/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ true
{
label: 'Clear',
value: `二向箔清理();
`,
},
{
label: 'Request',
value: `给 A 以 寻找("https://raw.githubusercontent.com/rustq/3body-lang/main/LICENSE");
A
`,
},
];
12 changes: 12 additions & 0 deletions web/src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,16 @@ Module.load('./monkey.wasm', {
random: (input) => {
return Math.floor(Math.random() * input)
},
request: (value) => {
const str = Module.copyCStr(value);
const xhr = new XMLHttpRequest();
xhr.open("GET", str, false);
xhr.send();
if (xhr.status === 200) {
console.log(xhr.responseText)
return Module.allocStr(xhr.responseText).ptr;
} else {
throw new Error('Request failed: ' + xhr.statusText);
}
},
}).catch((e) => console.error(e));

0 comments on commit 33c0eb9

Please sign in to comment.