{"action":"created","comment":{"url":"https://api.github.com/repos/TyOverby/ares/pulls/comments/40602750","id":40602750,"diff_hunk":"@@ -0,0 +1,98 @@\n+use std::rc::Rc;\n+use std::cell::RefCell;\n+use std::marker::PhantomData;\n+use std::ops::{Deref, DerefMut};\n+\n+use super::{Env, eval};\n+use ::{Value, AresResult, AresError, parse, stdlib, Environment};\n+\n+pub struct Context<T> {\n+    env: Env,\n+    _state: PhantomData<T>,\n+}\n+\n+pub struct LoadedContext<'a, 'b, T: 'a + 'b> {\n+    ctx: &'b mut Context<T>,\n+    state: &'a mut T,\n+}\n+\n+impl <T> Context<T> {\n+    pub fn new() -> Context<T> {\n+        let env = Rc::new(RefCell::new(Environment::new()));\n+        stdlib::load_all(&env);\n+        Context {\n+            env: env,\n+            _state: PhantomData,\n+        }\n+    }\n+\n+    pub fn new_empty() -> Context<T> {\n+        Context {\n+            env: Rc::new(RefCell::new(Environment::new())),\n+            _state: PhantomData,\n+        }\n+    }\n+\n+    pub fn load<'a, 'b>(&'b mut self, state: &'a mut T) -> LoadedContext<'a, 'b, T> {\n+        LoadedContext {\n+            ctx: self,\n+            state: state\n+        }\n+    }\n+\n+    pub fn env(&self) -> &Env {\n+        &self.env\n+    }\n+}\n+\n+impl <'a, 'b,  T> LoadedContext<'a, 'b, T> {\n+    pub fn eval(&mut self, program: &Value) -> AresResult<Value> {\n+        eval(program, self.env())\n+    }\n+\n+    pub fn eval_str(&mut self, program: &str) -> AresResult<Value> {\n+        let trees = try!(parse(program));\n+        let mut last = None;\n+        for tree in trees {\n+            last = Some(try!(eval(&tree, self.env())))\n+        }\n+        match last {\n+            Some(v) => Ok(v),\n+            None => Err(AresError::NoProgram)\n+        }\n+    }\n+\n+    pub fn call(&mut self, func: Value, mut args: Vec<Value>) -> AresResult<Value> {\n+        for arg in args.iter_mut() {\n+            if let a@&mut Value::List(_) = arg {\n+                // TODO: can you get rid of this clone?\n+                *a = Value::new_list(vec![Value::new_ident(\"quote\"), a.clone()]);\n+            }\n+        }\n+\n+        let built = Value::new_list(vec![func, Value::new_list(args)]);\n+        self.eval(&built)","path":"src/eval/context.rs","position":74,"original_position":74,"commit_id":"c1a3e2b279b7a0a41cec6bc81539ae4f0d79df21","original_commit_id":"c1a3e2b279b7a0a41cec6bc81539ae4f0d79df21","user":{"login":"bwo","id":68839,"avatar_url":"https://avatars.githubusercontent.com/u/68839?v=3","gravatar_id":"","url":"https://api.github.com/users/bwo","html_url":"https://github.com/bwo","followers_url":"https://api.github.com/users/bwo/followers","following_url":"https://api.github.com/users/bwo/following{/other_user}","gists_url":"https://api.github.com/users/bwo/gists{/gist_id}","starred_url":"https://api.github.com/users/bwo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bwo/subscriptions","organizations_url":"https://api.github.com/users/bwo/orgs","repos_url":"https://api.github.com/users/bwo/repos","events_url":"https://api.github.com/users/bwo/events{/privacy}","received_events_url":"https://api.github.com/users/bwo/received_events","type":"User","site_admin":false},"body":"I suspect the `clone()` can be got rid of, and this whole method made less roundabout, if the part of `eval` that does applies the function to already-evaluated arguments is abstracted out and called directly here. We already know (or at least assume!) that `func` is a procedure, so we shouldn't need to create a list in `built` just to match on it in `eval`, and could also avoid quoting to avoid double-evaluation of lists in the arguments.","created_at":"2015-09-28T20:42:34Z","updated_at":"2015-09-28T20:42:34Z","html_url":"https://github.com/TyOverby/ares/pull/7#discussion_r40602750","pull_request_url":"https://api.github.com/repos/TyOverby/ares/pulls/7","_links":{"self":{"href":"https://api.github.com/repos/TyOverby/ares/pulls/comments/40602750"},"html":{"href":"https://github.com/TyOverby/ares/pull/7#discussion_r40602750"},"pull_request":{"href":"https://api.github.com/repos/TyOverby/ares/pulls/7"}}},"pull_request":{"url":"https://api.github.com/repos/TyOverby/ares/pulls/7","id":46202118,"html_url":"https://github.com/TyOverby/ares/pull/7","diff_url":"https://github.com/TyOverby/ares/pull/7.diff","patch_url":"https://github.com/TyOverby/ares/pull/7.patch","issue_url":"https://api.github.com/repos/TyOverby/ares/issues/7","number":7,"state":"open","locked":false,"title":"begin work with contexts","user":{"login":"TyOverby","id":573215,"avatar_url":"https://avatars.githubusercontent.com/u/573215?v=3","gravatar_id":"","url":"https://api.github.com/users/TyOverby","html_url":"https://github.com/TyOverby","followers_url":"https://api.github.com/users/TyOverby/followers","following_url":"https://api.github.com/users/TyOverby/following{/other_user}","gists_url":"https://api.github.com/users/TyOverby/gists{/gist_id}","starred_url":"https://api.github.com/users/TyOverby/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TyOverby/subscriptions","organizations_url":"https://api.github.com/users/TyOverby/orgs","repos_url":"https://api.github.com/users/TyOverby/repos","events_url":"https://api.github.com/users/TyOverby/events{/privacy}","received_events_url":"https://api.github.com/users/TyOverby/received_events","type":"User","site_admin":false},"body":"The \"Context\" is the main API level that the user will interact with.  Instead of passing `Env` around all the time, we'll instead pass around Contexts.  There are two types of Contexts: Regular `Context` and a `LoadedContext`.  \r\n\r\n1. The `Context` will allow the library user to read and write into the global environment but not evaluate anything.\r\n2. The `LoadedContext` is \"loaded\" with a mutable reference to some rust state that the library user wants to have mutated by the evaluation of lisp code.\r\n\r\nThe ultimate goal is to have something like this be possible:\r\n\r\n```rust\r\nlet mut ctx = Context::new();\r\nlet mut counter = 0;\r\nctx.env().add_function(\"increment-counter\", |ctx, _args| {\r\n    *ctx.state = ctx.state + 1; // increment the counter.\r\n});\r\n\r\n{\r\n    let loaded = ctx.load(&mut counter);\r\n    for _ in 0 .. 5 {\r\n        loaded.call_global(\"increment-counter\", vec![]);\r\n    }\r\n}\r\n\r\nassert_eq!(counter, 5);\r\n\r\n```\r\n\r\n@bwo : I'm nowhere near done, but I'd like you to be updated and get feedback as soon as possible.","created_at":"2015-09-28T20:11:58Z","updated_at":"2015-09-28T20:42:34Z","closed_at":null,"merged_at":null,"merge_commit_sha":"bb381f6283672cc96fd55bd41c3c4449a1c8ba65","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/TyOverby/ares/pulls/7/commits","review_comments_url":"https://api.github.com/repos/TyOverby/ares/pulls/7/comments","review_comment_url":"https://api.github.com/repos/TyOverby/ares/pulls/comments{/number}","comments_url":"https://api.github.com/repos/TyOverby/ares/issues/7/comments","statuses_url":"https://api.github.com/repos/TyOverby/ares/statuses/c1a3e2b279b7a0a41cec6bc81539ae4f0d79df21","head":{"label":"TyOverby:context-work","ref":"context-work","sha":"c1a3e2b279b7a0a41cec6bc81539ae4f0d79df21","user":{"login":"TyOverby","id":573215,"avatar_url":"https://avatars.githubusercontent.com/u/573215?v=3","gravatar_id":"","url":"https://api.github.com/users/TyOverby","html_url":"https://github.com/TyOverby","followers_url":"https://api.github.com/users/TyOverby/followers","following_url":"https://api.github.com/users/TyOverby/following{/other_user}","gists_url":"https://api.github.com/users/TyOverby/gists{/gist_id}","starred_url":"https://api.github.com/users/TyOverby/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TyOverby/subscriptions","organizations_url":"https://api.github.com/users/TyOverby/orgs","repos_url":"https://api.github.com/users/TyOverby/repos","events_url":"https://api.github.com/users/TyOverby/events{/privacy}","received_events_url":"https://api.github.com/users/TyOverby/received_events","type":"User","site_admin":false},"repo":{"id":42225510,"name":"ares","full_name":"TyOverby/ares","owner":{"login":"TyOverby","id":573215,"avatar_url":"https://avatars.githubusercontent.com/u/573215?v=3","gravatar_id":"","url":"https://api.github.com/users/TyOverby","html_url":"https://github.com/TyOverby","followers_url":"https://api.github.com/users/TyOverby/followers","following_url":"https://api.github.com/users/TyOverby/following{/other_user}","gists_url":"https://api.github.com/users/TyOverby/gists{/gist_id}","starred_url":"https://api.github.com/users/TyOverby/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TyOverby/subscriptions","organizations_url":"https://api.github.com/users/TyOverby/orgs","repos_url":"https://api.github.com/users/TyOverby/repos","events_url":"https://api.github.com/users/TyOverby/events{/privacy}","received_events_url":"https://api.github.com/users/TyOverby/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/TyOverby/ares","description":"A Lisp made for easy integration with Rust.","fork":false,"url":"https://api.github.com/repos/TyOverby/ares","forks_url":"https://api.github.com/repos/TyOverby/ares/forks","keys_url":"https://api.github.com/repos/TyOverby/ares/keys{/key_id}","collaborators_url":"https://api.github.com/repos/TyOverby/ares/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/TyOverby/ares/teams","hooks_url":"https://api.github.com/repos/TyOverby/ares/hooks","issue_events_url":"https://api.github.com/repos/TyOverby/ares/issues/events{/number}","events_url":"https://api.github.com/repos/TyOverby/ares/events","assignees_url":"https://api.github.com/repos/TyOverby/ares/assignees{/user}","branches_url":"https://api.github.com/repos/TyOverby/ares/branches{/branch}","tags_url":"https://api.github.com/repos/TyOverby/ares/tags","blobs_url":"https://api.github.com/repos/TyOverby/ares/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/TyOverby/ares/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/TyOverby/ares/git/refs{/sha}","trees_url":"https://api.github.com/repos/TyOverby/ares/git/trees{/sha}","statuses_url":"https://api.github.com/repos/TyOverby/ares/statuses/{sha}","languages_url":"https://api.github.com/repos/TyOverby/ares/languages","stargazers_url":"https://api.github.com/repos/TyOverby/ares/stargazers","contributors_url":"https://api.github.com/repos/TyOverby/ares/contributors","subscribers_url":"https://api.github.com/repos/TyOverby/ares/subscribers","subscription_url":"https://api.github.com/repos/TyOverby/ares/subscription","commits_url":"https://api.github.com/repos/TyOverby/ares/commits{/sha}","git_commits_url":"https://api.github.com/repos/TyOverby/ares/git/commits{/sha}","comments_url":"https://api.github.com/repos/TyOverby/ares/comments{/number}","issue_comment_url":"https://api.github.com/repos/TyOverby/ares/issues/comments{/number}","contents_url":"https://api.github.com/repos/TyOverby/ares/contents/{+path}","compare_url":"https://api.github.com/repos/TyOverby/ares/compare/{base}...{head}","merges_url":"https://api.github.com/repos/TyOverby/ares/merges","archive_url":"https://api.github.com/repos/TyOverby/ares/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/TyOverby/ares/downloads","issues_url":"https://api.github.com/repos/TyOverby/ares/issues{/number}","pulls_url":"https://api.github.com/repos/TyOverby/ares/pulls{/number}","milestones_url":"https://api.github.com/repos/TyOverby/ares/milestones{/number}","notifications_url":"https://api.github.com/repos/TyOverby/ares/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/TyOverby/ares/labels{/name}","releases_url":"https://api.github.com/repos/TyOverby/ares/releases{/id}","created_at":"2015-09-10T06:04:37Z","updated_at":"2015-09-14T10:18:48Z","pushed_at":"2015-09-28T20:11:58Z","git_url":"git://github.com/TyOverby/ares.git","ssh_url":"git@github.com:TyOverby/ares.git","clone_url":"https://github.com/TyOverby/ares.git","svn_url":"https://github.com/TyOverby/ares","homepage":null,"size":306,"stargazers_count":2,"watchers_count":2,"language":"Rust","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":1,"forks":1,"open_issues":1,"watchers":2,"default_branch":"master"}},"base":{"label":"TyOverby:master","ref":"master","sha":"c31cb55ecadb48654fbe60e41a7a4a4c860084bd","user":{"login":"TyOverby","id":573215,"avatar_url":"https://avatars.githubusercontent.com/u/573215?v=3","gravatar_id":"","url":"https://api.github.com/users/TyOverby","html_url":"https://github.com/TyOverby","followers_url":"https://api.github.com/users/TyOverby/followers","following_url":"https://api.github.com/users/TyOverby/following{/other_user}","gists_url":"https://api.github.com/users/TyOverby/gists{/gist_id}","starred_url":"https://api.github.com/users/TyOverby/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TyOverby/subscriptions","organizations_url":"https://api.github.com/users/TyOverby/orgs","repos_url":"https://api.github.com/users/TyOverby/repos","events_url":"https://api.github.com/users/TyOverby/events{/privacy}","received_events_url":"https://api.github.com/users/TyOverby/received_events","type":"User","site_admin":false},"repo":{"id":42225510,"name":"ares","full_name":"TyOverby/ares","owner":{"login":"TyOverby","id":573215,"avatar_url":"https://avatars.githubusercontent.com/u/573215?v=3","gravatar_id":"","url":"https://api.github.com/users/TyOverby","html_url":"https://github.com/TyOverby","followers_url":"https://api.github.com/users/TyOverby/followers","following_url":"https://api.github.com/users/TyOverby/following{/other_user}","gists_url":"https://api.github.com/users/TyOverby/gists{/gist_id}","starred_url":"https://api.github.com/users/TyOverby/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TyOverby/subscriptions","organizations_url":"https://api.github.com/users/TyOverby/orgs","repos_url":"https://api.github.com/users/TyOverby/repos","events_url":"https://api.github.com/users/TyOverby/events{/privacy}","received_events_url":"https://api.github.com/users/TyOverby/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/TyOverby/ares","description":"A Lisp made for easy integration with Rust.","fork":false,"url":"https://api.github.com/repos/TyOverby/ares","forks_url":"https://api.github.com/repos/TyOverby/ares/forks","keys_url":"https://api.github.com/repos/TyOverby/ares/keys{/key_id}","collaborators_url":"https://api.github.com/repos/TyOverby/ares/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/TyOverby/ares/teams","hooks_url":"https://api.github.com/repos/TyOverby/ares/hooks","issue_events_url":"https://api.github.com/repos/TyOverby/ares/issues/events{/number}","events_url":"https://api.github.com/repos/TyOverby/ares/events","assignees_url":"https://api.github.com/repos/TyOverby/ares/assignees{/user}","branches_url":"https://api.github.com/repos/TyOverby/ares/branches{/branch}","tags_url":"https://api.github.com/repos/TyOverby/ares/tags","blobs_url":"https://api.github.com/repos/TyOverby/ares/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/TyOverby/ares/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/TyOverby/ares/git/refs{/sha}","trees_url":"https://api.github.com/repos/TyOverby/ares/git/trees{/sha}","statuses_url":"https://api.github.com/repos/TyOverby/ares/statuses/{sha}","languages_url":"https://api.github.com/repos/TyOverby/ares/languages","stargazers_url":"https://api.github.com/repos/TyOverby/ares/stargazers","contributors_url":"https://api.github.com/repos/TyOverby/ares/contributors","subscribers_url":"https://api.github.com/repos/TyOverby/ares/subscribers","subscription_url":"https://api.github.com/repos/TyOverby/ares/subscription","commits_url":"https://api.github.com/repos/TyOverby/ares/commits{/sha}","git_commits_url":"https://api.github.com/repos/TyOverby/ares/git/commits{/sha}","comments_url":"https://api.github.com/repos/TyOverby/ares/comments{/number}","issue_comment_url":"https://api.github.com/repos/TyOverby/ares/issues/comments{/number}","contents_url":"https://api.github.com/repos/TyOverby/ares/contents/{+path}","compare_url":"https://api.github.com/repos/TyOverby/ares/compare/{base}...{head}","merges_url":"https://api.github.com/repos/TyOverby/ares/merges","archive_url":"https://api.github.com/repos/TyOverby/ares/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/TyOverby/ares/downloads","issues_url":"https://api.github.com/repos/TyOverby/ares/issues{/number}","pulls_url":"https://api.github.com/repos/TyOverby/ares/pulls{/number}","milestones_url":"https://api.github.com/repos/TyOverby/ares/milestones{/number}","notifications_url":"https://api.github.com/repos/TyOverby/ares/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/TyOverby/ares/labels{/name}","releases_url":"https://api.github.com/repos/TyOverby/ares/releases{/id}","created_at":"2015-09-10T06:04:37Z","updated_at":"2015-09-14T10:18:48Z","pushed_at":"2015-09-28T20:11:58Z","git_url":"git://github.com/TyOverby/ares.git","ssh_url":"git@github.com:TyOverby/ares.git","clone_url":"https://github.com/TyOverby/ares.git","svn_url":"https://github.com/TyOverby/ares","homepage":null,"size":306,"stargazers_count":2,"watchers_count":2,"language":"Rust","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":1,"forks":1,"open_issues":1,"watchers":2,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/TyOverby/ares/pulls/7"},"html":{"href":"https://github.com/TyOverby/ares/pull/7"},"issue":{"href":"https://api.github.com/repos/TyOverby/ares/issues/7"},"comments":{"href":"https://api.github.com/repos/TyOverby/ares/issues/7/comments"},"review_comments":{"href":"https://api.github.com/repos/TyOverby/ares/pulls/7/comments"},"review_comment":{"href":"https://api.github.com/repos/TyOverby/ares/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/TyOverby/ares/pulls/7/commits"},"statuses":{"href":"https://api.github.com/repos/TyOverby/ares/statuses/c1a3e2b279b7a0a41cec6bc81539ae4f0d79df21"}}}}