{"action":"created","comment":{"url":"https://api.github.com/repos/alexandramedway/odds/pulls/comments/45263302","id":45263302,"diff_hunk":"@@ -0,0 +1,115 @@\n+(*\n+ * COMS4115: Semantic Analyzer\n+ *\n+ * Authors:\n+ *  - Alex Kalicki\n+ *  - Alexandra Medway\n+ *  - Daniel Echikson\n+ *  - Lilly Wang\n+ *)\n+\n+open Ast\n+open Sast\n+open Printf\n+\n+module StringMap = Map.Make(String)\n+\n+(* Environment *)\n+type environment = {\n+  reserved: string list;\n+  scope: string StringMap.t;\n+}\n+\n+let root_env = {\n+  reserved = [\"print\"];\n+  scope = StringMap.empty;\n+}\n+\n+(* Exceptions *)\n+exception Error of string\n+\n+(* Static scoping variable counter *)\n+let ss_counter = ref (-1)\n+let get_ss_id name =\n+  ss_counter := !ss_counter + 1;\n+  sprintf \"%s_%d\" name !ss_counter\n+\n+let add_to_scope env id =\n+  let ss_id = get_ss_id id in\n+  let new_env = {\n+    reserved = env.reserved;\n+    scope = StringMap.add id ss_id env.scope;\n+  } in\n+  new_env, ss_id\n+\n+(* Checks *)\n+let rec check_expr env = function\n+  | Ast.Num_lit(x) -> env, Sast.Num_lit(x)\n+  | Ast.String_lit(s) -> env, Sast.String_lit(s)\n+  | Ast.Bool_lit(b) -> env, Sast.Bool_lit(b)\n+  | Ast.Id(id) -> check_id env id\n+  | Ast.Unop(op, e) -> check_unop env op e\n+  | Ast.Binop(e1, op, e2) -> check_binop env e1 op e2\n+  | Ast.Call(f, args) -> check_func_call env f args\n+  | Ast.Assign(id, e) -> check_assign env id e\n+  | Ast.List(l) -> check_list env l\n+  | Ast.Fdecl(f) -> check_fdecl env \"anon\" f\n+\n+and check_id env id =\n+  if List.mem id env.reserved then env, Sast.Id(id) else\n+  if StringMap.mem id env.scope then env, Sast.Id(StringMap.find id env.scope)\n+  else let error = sprintf \"ID '%s' not found.\" id in raise (Error(error))\n+\n+and check_unop env op e =\n+  let _, e = check_expr env e in env, Sast.Unop(op, e)\n+\n+and check_binop env e1 op e2 =\n+  let _, e1 = check_expr env e1 and _, e2 = check_expr env e2 in\n+  env, Sast.Binop(e1, op, e2)\n+\n+and check_func_call env f args =\n+  let _, id = check_expr env f in\n+  let args = List.map (fun e -> snd(check_expr env e)) args in\n+  env, Sast.Call(id, args)\n+\n+and check_assign env id = function\n+  | Ast.Fdecl(f) -> check_fdecl env id f\n+  | _ as e -> let new_env, e = check_expr env e in\n+      let new_env, name = add_to_scope new_env id in\n+      new_env, Sast.Assign(name, e)","path":"compiler/analyzer.ml","position":79,"original_position":79,"commit_id":"c591e114301ab508a29c6b7817d3b388ded8da1d","original_commit_id":"c591e114301ab508a29c6b7817d3b388ded8da1d","user":{"login":"akalicki","id":3604541,"avatar_url":"https://avatars.githubusercontent.com/u/3604541?v=3","gravatar_id":"","url":"https://api.github.com/users/akalicki","html_url":"https://github.com/akalicki","followers_url":"https://api.github.com/users/akalicki/followers","following_url":"https://api.github.com/users/akalicki/following{/other_user}","gists_url":"https://api.github.com/users/akalicki/gists{/gist_id}","starred_url":"https://api.github.com/users/akalicki/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akalicki/subscriptions","organizations_url":"https://api.github.com/users/akalicki/orgs","repos_url":"https://api.github.com/users/akalicki/repos","events_url":"https://api.github.com/users/akalicki/events{/privacy}","received_events_url":"https://api.github.com/users/akalicki/received_events","type":"User","site_admin":false},"body":":+1: ","created_at":"2015-11-18T21:31:14Z","updated_at":"2015-11-18T21:31:14Z","html_url":"https://github.com/alexandramedway/odds/pull/56#discussion_r45263302","pull_request_url":"https://api.github.com/repos/alexandramedway/odds/pulls/56","_links":{"self":{"href":"https://api.github.com/repos/alexandramedway/odds/pulls/comments/45263302"},"html":{"href":"https://github.com/alexandramedway/odds/pull/56#discussion_r45263302"},"pull_request":{"href":"https://api.github.com/repos/alexandramedway/odds/pulls/56"}}},"pull_request":{"url":"https://api.github.com/repos/alexandramedway/odds/pulls/56","id":51129972,"html_url":"https://github.com/alexandramedway/odds/pull/56","diff_url":"https://github.com/alexandramedway/odds/pull/56.diff","patch_url":"https://github.com/alexandramedway/odds/pull/56.patch","issue_url":"https://api.github.com/repos/alexandramedway/odds/issues/56","number":56,"state":"open","locked":false,"title":"Initial working SAST with scoping","user":{"login":"akalicki","id":3604541,"avatar_url":"https://avatars.githubusercontent.com/u/3604541?v=3","gravatar_id":"","url":"https://api.github.com/users/akalicki","html_url":"https://github.com/akalicki","followers_url":"https://api.github.com/users/akalicki/followers","following_url":"https://api.github.com/users/akalicki/following{/other_user}","gists_url":"https://api.github.com/users/akalicki/gists{/gist_id}","starred_url":"https://api.github.com/users/akalicki/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akalicki/subscriptions","organizations_url":"https://api.github.com/users/akalicki/orgs","repos_url":"https://api.github.com/users/akalicki/repos","events_url":"https://api.github.com/users/akalicki/events{/privacy}","received_events_url":"https://api.github.com/users/akalicki/received_events","type":"User","site_admin":false},"body":"Let's get this merged in to our master-refactor branch - we can build type inference, semantic checking, and the PAST off of what we have so far. Closes #51.","created_at":"2015-11-18T19:29:09Z","updated_at":"2015-11-18T21:31:14Z","closed_at":null,"merged_at":null,"merge_commit_sha":"bbc1adba15ee4ff1782437ee76865b89a4f3945c","assignee":null,"milestone":{"url":"https://api.github.com/repos/alexandramedway/odds/milestones/5","html_url":"https://github.com/alexandramedway/odds/milestones/Functions","labels_url":"https://api.github.com/repos/alexandramedway/odds/milestones/5/labels","id":1415198,"number":5,"title":"Functions","description":"Get functions and anonymous functions working.","creator":{"login":"akalicki","id":3604541,"avatar_url":"https://avatars.githubusercontent.com/u/3604541?v=3","gravatar_id":"","url":"https://api.github.com/users/akalicki","html_url":"https://github.com/akalicki","followers_url":"https://api.github.com/users/akalicki/followers","following_url":"https://api.github.com/users/akalicki/following{/other_user}","gists_url":"https://api.github.com/users/akalicki/gists{/gist_id}","starred_url":"https://api.github.com/users/akalicki/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akalicki/subscriptions","organizations_url":"https://api.github.com/users/akalicki/orgs","repos_url":"https://api.github.com/users/akalicki/repos","events_url":"https://api.github.com/users/akalicki/events{/privacy}","received_events_url":"https://api.github.com/users/akalicki/received_events","type":"User","site_admin":false},"open_issues":6,"closed_issues":1,"state":"open","created_at":"2015-11-17T04:42:45Z","updated_at":"2015-11-18T19:29:09Z","due_on":null,"closed_at":null},"commits_url":"https://api.github.com/repos/alexandramedway/odds/pulls/56/commits","review_comments_url":"https://api.github.com/repos/alexandramedway/odds/pulls/56/comments","review_comment_url":"https://api.github.com/repos/alexandramedway/odds/pulls/comments{/number}","comments_url":"https://api.github.com/repos/alexandramedway/odds/issues/56/comments","statuses_url":"https://api.github.com/repos/alexandramedway/odds/statuses/c591e114301ab508a29c6b7817d3b388ded8da1d","head":{"label":"alexandramedway:sast","ref":"sast","sha":"c591e114301ab508a29c6b7817d3b388ded8da1d","user":{"login":"alexandramedway","id":7786527,"avatar_url":"https://avatars.githubusercontent.com/u/7786527?v=3","gravatar_id":"","url":"https://api.github.com/users/alexandramedway","html_url":"https://github.com/alexandramedway","followers_url":"https://api.github.com/users/alexandramedway/followers","following_url":"https://api.github.com/users/alexandramedway/following{/other_user}","gists_url":"https://api.github.com/users/alexandramedway/gists{/gist_id}","starred_url":"https://api.github.com/users/alexandramedway/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alexandramedway/subscriptions","organizations_url":"https://api.github.com/users/alexandramedway/orgs","repos_url":"https://api.github.com/users/alexandramedway/repos","events_url":"https://api.github.com/users/alexandramedway/events{/privacy}","received_events_url":"https://api.github.com/users/alexandramedway/received_events","type":"User","site_admin":false},"repo":{"id":43025019,"name":"odds","full_name":"alexandramedway/odds","owner":{"login":"alexandramedway","id":7786527,"avatar_url":"https://avatars.githubusercontent.com/u/7786527?v=3","gravatar_id":"","url":"https://api.github.com/users/alexandramedway","html_url":"https://github.com/alexandramedway","followers_url":"https://api.github.com/users/alexandramedway/followers","following_url":"https://api.github.com/users/alexandramedway/following{/other_user}","gists_url":"https://api.github.com/users/alexandramedway/gists{/gist_id}","starred_url":"https://api.github.com/users/alexandramedway/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alexandramedway/subscriptions","organizations_url":"https://api.github.com/users/alexandramedway/orgs","repos_url":"https://api.github.com/users/alexandramedway/repos","events_url":"https://api.github.com/users/alexandramedway/events{/privacy}","received_events_url":"https://api.github.com/users/alexandramedway/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/alexandramedway/odds","description":"A simple mathematical distribution language.","fork":false,"url":"https://api.github.com/repos/alexandramedway/odds","forks_url":"https://api.github.com/repos/alexandramedway/odds/forks","keys_url":"https://api.github.com/repos/alexandramedway/odds/keys{/key_id}","collaborators_url":"https://api.github.com/repos/alexandramedway/odds/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/alexandramedway/odds/teams","hooks_url":"https://api.github.com/repos/alexandramedway/odds/hooks","issue_events_url":"https://api.github.com/repos/alexandramedway/odds/issues/events{/number}","events_url":"https://api.github.com/repos/alexandramedway/odds/events","assignees_url":"https://api.github.com/repos/alexandramedway/odds/assignees{/user}","branches_url":"https://api.github.com/repos/alexandramedway/odds/branches{/branch}","tags_url":"https://api.github.com/repos/alexandramedway/odds/tags","blobs_url":"https://api.github.com/repos/alexandramedway/odds/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/alexandramedway/odds/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/alexandramedway/odds/git/refs{/sha}","trees_url":"https://api.github.com/repos/alexandramedway/odds/git/trees{/sha}","statuses_url":"https://api.github.com/repos/alexandramedway/odds/statuses/{sha}","languages_url":"https://api.github.com/repos/alexandramedway/odds/languages","stargazers_url":"https://api.github.com/repos/alexandramedway/odds/stargazers","contributors_url":"https://api.github.com/repos/alexandramedway/odds/contributors","subscribers_url":"https://api.github.com/repos/alexandramedway/odds/subscribers","subscription_url":"https://api.github.com/repos/alexandramedway/odds/subscription","commits_url":"https://api.github.com/repos/alexandramedway/odds/commits{/sha}","git_commits_url":"https://api.github.com/repos/alexandramedway/odds/git/commits{/sha}","comments_url":"https://api.github.com/repos/alexandramedway/odds/comments{/number}","issue_comment_url":"https://api.github.com/repos/alexandramedway/odds/issues/comments{/number}","contents_url":"https://api.github.com/repos/alexandramedway/odds/contents/{+path}","compare_url":"https://api.github.com/repos/alexandramedway/odds/compare/{base}...{head}","merges_url":"https://api.github.com/repos/alexandramedway/odds/merges","archive_url":"https://api.github.com/repos/alexandramedway/odds/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/alexandramedway/odds/downloads","issues_url":"https://api.github.com/repos/alexandramedway/odds/issues{/number}","pulls_url":"https://api.github.com/repos/alexandramedway/odds/pulls{/number}","milestones_url":"https://api.github.com/repos/alexandramedway/odds/milestones{/number}","notifications_url":"https://api.github.com/repos/alexandramedway/odds/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/alexandramedway/odds/labels{/name}","releases_url":"https://api.github.com/repos/alexandramedway/odds/releases{/id}","created_at":"2015-09-23T20:29:23Z","updated_at":"2015-11-05T18:45:49Z","pushed_at":"2015-11-18T21:11:48Z","git_url":"git://github.com/alexandramedway/odds.git","ssh_url":"git@github.com:alexandramedway/odds.git","clone_url":"https://github.com/alexandramedway/odds.git","svn_url":"https://github.com/alexandramedway/odds","homepage":"","size":184,"stargazers_count":1,"watchers_count":1,"language":"OCaml","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":12,"forks":0,"open_issues":12,"watchers":1,"default_branch":"master"}},"base":{"label":"alexandramedway:master-refactor","ref":"master-refactor","sha":"3a7f4d4e66a45fd97a52f29713d33e6e370448d8","user":{"login":"alexandramedway","id":7786527,"avatar_url":"https://avatars.githubusercontent.com/u/7786527?v=3","gravatar_id":"","url":"https://api.github.com/users/alexandramedway","html_url":"https://github.com/alexandramedway","followers_url":"https://api.github.com/users/alexandramedway/followers","following_url":"https://api.github.com/users/alexandramedway/following{/other_user}","gists_url":"https://api.github.com/users/alexandramedway/gists{/gist_id}","starred_url":"https://api.github.com/users/alexandramedway/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alexandramedway/subscriptions","organizations_url":"https://api.github.com/users/alexandramedway/orgs","repos_url":"https://api.github.com/users/alexandramedway/repos","events_url":"https://api.github.com/users/alexandramedway/events{/privacy}","received_events_url":"https://api.github.com/users/alexandramedway/received_events","type":"User","site_admin":false},"repo":{"id":43025019,"name":"odds","full_name":"alexandramedway/odds","owner":{"login":"alexandramedway","id":7786527,"avatar_url":"https://avatars.githubusercontent.com/u/7786527?v=3","gravatar_id":"","url":"https://api.github.com/users/alexandramedway","html_url":"https://github.com/alexandramedway","followers_url":"https://api.github.com/users/alexandramedway/followers","following_url":"https://api.github.com/users/alexandramedway/following{/other_user}","gists_url":"https://api.github.com/users/alexandramedway/gists{/gist_id}","starred_url":"https://api.github.com/users/alexandramedway/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alexandramedway/subscriptions","organizations_url":"https://api.github.com/users/alexandramedway/orgs","repos_url":"https://api.github.com/users/alexandramedway/repos","events_url":"https://api.github.com/users/alexandramedway/events{/privacy}","received_events_url":"https://api.github.com/users/alexandramedway/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/alexandramedway/odds","description":"A simple mathematical distribution language.","fork":false,"url":"https://api.github.com/repos/alexandramedway/odds","forks_url":"https://api.github.com/repos/alexandramedway/odds/forks","keys_url":"https://api.github.com/repos/alexandramedway/odds/keys{/key_id}","collaborators_url":"https://api.github.com/repos/alexandramedway/odds/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/alexandramedway/odds/teams","hooks_url":"https://api.github.com/repos/alexandramedway/odds/hooks","issue_events_url":"https://api.github.com/repos/alexandramedway/odds/issues/events{/number}","events_url":"https://api.github.com/repos/alexandramedway/odds/events","assignees_url":"https://api.github.com/repos/alexandramedway/odds/assignees{/user}","branches_url":"https://api.github.com/repos/alexandramedway/odds/branches{/branch}","tags_url":"https://api.github.com/repos/alexandramedway/odds/tags","blobs_url":"https://api.github.com/repos/alexandramedway/odds/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/alexandramedway/odds/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/alexandramedway/odds/git/refs{/sha}","trees_url":"https://api.github.com/repos/alexandramedway/odds/git/trees{/sha}","statuses_url":"https://api.github.com/repos/alexandramedway/odds/statuses/{sha}","languages_url":"https://api.github.com/repos/alexandramedway/odds/languages","stargazers_url":"https://api.github.com/repos/alexandramedway/odds/stargazers","contributors_url":"https://api.github.com/repos/alexandramedway/odds/contributors","subscribers_url":"https://api.github.com/repos/alexandramedway/odds/subscribers","subscription_url":"https://api.github.com/repos/alexandramedway/odds/subscription","commits_url":"https://api.github.com/repos/alexandramedway/odds/commits{/sha}","git_commits_url":"https://api.github.com/repos/alexandramedway/odds/git/commits{/sha}","comments_url":"https://api.github.com/repos/alexandramedway/odds/comments{/number}","issue_comment_url":"https://api.github.com/repos/alexandramedway/odds/issues/comments{/number}","contents_url":"https://api.github.com/repos/alexandramedway/odds/contents/{+path}","compare_url":"https://api.github.com/repos/alexandramedway/odds/compare/{base}...{head}","merges_url":"https://api.github.com/repos/alexandramedway/odds/merges","archive_url":"https://api.github.com/repos/alexandramedway/odds/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/alexandramedway/odds/downloads","issues_url":"https://api.github.com/repos/alexandramedway/odds/issues{/number}","pulls_url":"https://api.github.com/repos/alexandramedway/odds/pulls{/number}","milestones_url":"https://api.github.com/repos/alexandramedway/odds/milestones{/number}","notifications_url":"https://api.github.com/repos/alexandramedway/odds/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/alexandramedway/odds/labels{/name}","releases_url":"https://api.github.com/repos/alexandramedway/odds/releases{/id}","created_at":"2015-09-23T20:29:23Z","updated_at":"2015-11-05T18:45:49Z","pushed_at":"2015-11-18T21:11:48Z","git_url":"git://github.com/alexandramedway/odds.git","ssh_url":"git@github.com:alexandramedway/odds.git","clone_url":"https://github.com/alexandramedway/odds.git","svn_url":"https://github.com/alexandramedway/odds","homepage":"","size":184,"stargazers_count":1,"watchers_count":1,"language":"OCaml","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":12,"forks":0,"open_issues":12,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/alexandramedway/odds/pulls/56"},"html":{"href":"https://github.com/alexandramedway/odds/pull/56"},"issue":{"href":"https://api.github.com/repos/alexandramedway/odds/issues/56"},"comments":{"href":"https://api.github.com/repos/alexandramedway/odds/issues/56/comments"},"review_comments":{"href":"https://api.github.com/repos/alexandramedway/odds/pulls/56/comments"},"review_comment":{"href":"https://api.github.com/repos/alexandramedway/odds/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/alexandramedway/odds/pulls/56/commits"},"statuses":{"href":"https://api.github.com/repos/alexandramedway/odds/statuses/c591e114301ab508a29c6b7817d3b388ded8da1d"}}}}