Skip to content
This repository
  • 3 commits
  • 3 files changed
  • 0 comments
  • 1 contributor
2  package.json
@@ -2,7 +2,7 @@
2 2
   "name": "isla",
3 3
   "description": "A programming language for children.",
4 4
   "author": "Mary Rose Cook <maryrosecook@maryrosecook.com> (http://maryrosecook.com/)",
5  
-  "version": "0.1.7",
  5
+  "version": "0.1.9",
6 6
   "scripts": {
7 7
     "test": "node_modules/jasmine-node/bin/jasmine-node spec/",
8 8
     "prepublish": "node scripts/minifier.js"
4  src/interpreter.js
@@ -117,7 +117,7 @@
117 117
     })
118 118
 
119 119
     .default(function(ast, env) {
120  
-      throw "You've forgotten a tag type.";
  120
+      throw { message:"You've forgotten a tag type." };
121 121
     });
122 122
 
123 123
   var evaluateValue = multimethod()
@@ -157,7 +157,7 @@
157 157
     });
158 158
 
159 159
   var nonExistentError = function(identifier) {
160  
-    throw "I have not heard of " + identifier.join(" ") + ".";
  160
+    throw { message:"I have not heard of " + identifier.join(" ") + "." };
161 161
   };
162 162
 
163 163
   var checkIdentifierParts = function(objNode, env) {
2  src/isla.min.js
... ...
@@ -1 +1 @@
1  
-(function(exports){var Isla;if(typeof module!=="undefined"&&module.exports){Isla={};Isla.Utils=require("./utils").Utils;Isla.Grammar=require("./grammar").Grammar;Isla.Library=require("./library").Library;Isla.Parser=require("./parser").Parser;Isla.Interpreter=require("./interpreter").Interpreter}else{Isla={}}exports.Isla=Isla})(typeof exports==="undefined"?this:exports);(function(exports){exports.Utils={};exports.Utils.type=function(x){return Object.prototype.toString.call(x).match(/\[object ([^\]]+)\]/)[1]};exports.Utils.merge=function(a,b){var c={};for(var i in a){c[i]=a[i]}for(var i in b){c[i]=b[i]}return c}})(typeof exports==="undefined"?this.Isla:exports);(function(exports){exports.Grammar={};var grammarArr=["{","  var nnode = function(tag, content, column, syntax, code) {","    var node = { tag: tag, c: content, index: column - 1};","    if(syntax !== undefined) {","      node.syntax = syntax;","    }","    if (code !== undefined) {","      node.code = code;","    }","    return node;","  };","  var syn = function(obj, syntax) {","    obj.syntax = syntax;","    return obj;","  };","}","start","  = all:block { return nnode('root', [all], column); }","block","  = _* all:expression+ { return nnode('block', all, column); }","expression","  = all:type_assignment { return nnode('expression', [all], column); }","  / all:value_assignment { return nnode('expression', [all], column); }","  / all:list_assignment { return nnode('expression', [all], column); }","  / all:invocation { return nnode('expression', [all], column); }","type_assignment","  = a:assignee _ ia:is_a _ id:identifier _* nl","    { return nnode('type_assignment', [a, ia, syn(id, 'type')], column); }","value_assignment","  = a:assignee _ i:is _ v:value _* nl","    { return nnode('value_assignment', [a, i, v], column); }","list_assignment","  = lo:list_operation _ va:value _ tf:to_from _ as:assignee _* nl","    { return nnode('list_assignment', [lo, va, tf, as], column); }","invocation","  = a:identifier _ v:value _* nl","    { return nnode('invocation', [syn(a, 'function'), v], column); }","list_operation","  = all:add { return nnode('list_operation', [all], column); }","  / all:take { return nnode('list_operation', [all], column); }","assignee","  = all:object { return nnode('assignee', [all], column); }","  / all:scalar { return nnode('assignee', [all], column); }","value","  = all:literal { return nnode('value', [all], column); }","  / all:variable { return nnode('value', [all], column); }","literal","  = all:string { return nnode('literal', [all], column, 'literal'); }","identifier","  = !keyword init:[a-zA-Z] rest:identifier_char* { return nnode('identifier', [init + rest.join('')], column); }","variable","  = all:object { return nnode('variable', [all], column); }","  / all:scalar { return nnode('variable', [all], column); }","scalar","  = all:identifier { return nnode('scalar', [all], column, 'variable'); }","object","  = id1:identifier _ id2:identifier { return nnode('object', [syn(id1, 'variable'), syn(id2, 'attribute')], column); }","is","  = all:'is' { return nnode('is', [all], column, 'keyword'); }","is_a","  = all:'is a' { return nnode('is_a', [all], column, 'keyword'); }","add","  = all:'add' { return nnode('add', [all], column, 'keyword'); }","take","  = all:'take' { return nnode('take', [all], column, 'keyword'); }","to_from","  = all:to { return nnode('to_from', ['to from'], column, 'keyword', 'to'); }","  / all:from { return nnode('to_from', ['to from'], column, 'keyword', 'from'); }","string","  = \"'\" all:string_char_single* \"'\" { return nnode('string', [all.join('')], column, undefined, \"'\" + all.join('') + \"'\"); }","  / '\"' all:string_char_double* '\"' { return nnode('string', [all.join('')], column, undefined, '\"' + all.join('') + '\"'); }","nl","  = all:[\\n]+ { return nnode('nl', all, column); }","keyword","  = is !identifier_char","  / is_a !identifier_char","  / add !identifier_char","  / take !identifier_char","  / to !identifier_char","  / from !identifier_char","to = 'to'","from = 'from'","_ = [ \\t\\r]+","identifier_char = [a-z0-9]","string_char_double = [A-Za-z0-9., ']",'string_char_single = [A-Za-z0-9., "]'];var peg="";for(var i=0;i<grammarArr.length;i++){peg+=grammarArr[i]+"\n"}exports.Grammar.peg=peg})(typeof exports==="undefined"?this.Isla:exports);(function(exports){var Isla,_,multimethod;if(typeof module!=="undefined"&&module.exports){_=require("Underscore");multimethod=require("multimethod");Isla={};Isla.Utils=require("./utils").Utils}else{_=window._;multimethod=window.multimethod;Isla=window.Isla}exports.Library={};exports.Library.getInitialEnv=function(extraTypes,initialCtx){var islaCtx={write:{fn:function(env,param){if(Isla.Utils.type(param)==="Object"){return param.toString()}else{return param+"\n"}},description:function(p){return"Writes out "+p+"."}},_types:Isla.Utils.merge(extraTypes,{list:function(){return new List},generic:function(){return new Generic}})};if(initialCtx!==undefined){islaCtx=Isla.Utils.merge(islaCtx,initialCtx)}return{ret:null,ctx:islaCtx}};var Generic=function(){this.toString=function(indent){if(!indent){indent="  "}var out="a "+this._meta.type+"\n";for(var i in this){if(Isla.Utils.type(this[i])!=="Function"&&i!=="_meta"){if(Isla.Utils.type(this[i])==="Object"){out+=indent+i+" is "+this[i].toString(indent+"  ")}else{out+=indent+i+" is '"+this[i]+"'\n"}}}return out}};var objsEqual=function(a,b){return a.ref===b.ref&&a.ref||a===b};var List=function(){var data=[];this.add=multimethod().dispatch(function(thing){return Isla.Utils.type(thing)}).when("Object",function(thing){for(var i=0;i<data.length;i++){if(objsEqual(thing,data[i])){return}}data.push(thing)}).default(function(thing){data.push(thing)});this.take=multimethod().dispatch(function(thing){return Isla.Utils.type(thing)}).when("Object",function(thing){for(var i=0;i<data.length;i++){if(objsEqual(thing,data[i])){data.splice(i,1);break}}}).default(function(thing){for(var i=0;i<data.length;i++){if(thing===data[i]){data.splice(i,1);break}}});this.items=function(){return data};this.toString=function(indent){if(!indent){indent="  "}if(this.items().length===0){return"an empty list"}else{var out="a list\n";for(var i=0;i<this.items().length;i++){if(Isla.Utils.type(this.items()[i])==="Object"){out+=indent+this.items()[i].toString(indent+"  ")}else{out+=indent+"'"+this.items()[i]+"'\n"}}return out}}};exports.Library.List=List})(typeof exports==="undefined"?this.Isla:exports);(function(exports){var Isla,_,multimethod,pegjs;if(typeof module!=="undefined"&&module.exports){_=require("Underscore");multimethod=require("multimethod");pegjs=require("pegjs");Isla={};Isla.Grammar=require("./grammar").Grammar}else{_=window._;multimethod=window.multimethod;pegjs=window.PEG;Isla=window.Isla}var pegParser=pegjs.buildParser(Isla.Grammar.peg,{trackLineAndColumn:true});exports.Parser={};exports.Parser.parse=function(str){str+="\n";var ast=pegParser.parse(str);return ast};var extract=multimethod().dispatch(function(__,nxt){return typeof nxt}).when("string",function(ast,nxt){if(ast.tag===nxt){return extractNext(ast.c,arguments)}else{throw new Error(JSON.stringify(ast)+" is not "+nxt)}}).when("number",function(ast,nxt){return extractNext(ast[nxt],arguments)}).when("undefined",function(ast){return ast}).default(function(){throw"Route items must be tags or indices."});var extractNext=function(ast,args){var nextArgs=_.rest(_.rest(_.toArray(args)));nextArgs.unshift(ast);return extract.apply(this,nextArgs)};exports.Parser.extract=extract})(typeof exports==="undefined"?this.Isla:exports);(function(exports){var Isla,_,multimethod;if(typeof module!=="undefined"&&module.exports){_=require("Underscore");multimethod=require("multimethod");Isla={};Isla.Utils=require("./utils").Utils;Isla.Library=require("./library").Library;Isla.Parser=require("./parser").Parser}else{_=window._;multimethod=window.multimethod;Isla=window.Isla}exports.Interpreter={};exports.Interpreter.interpret=function(code,env){return interpretAst(Isla.Parser.parse(code),env)};var interpretAst=multimethod().dispatch(function(ast,env){return ast.tag}).when("root",function(ast,env){if(env===undefined){env=Isla.Library.getInitialEnv()}return runSequence(ast.c,env)}).when("block",function(ast,env){return runSequence(ast.c,env)}).when("expression",function(ast,env){return interpretAst(ast.c[0],env)}).when("value_assignment",function(ast,env){var node=Isla.Parser.extract(ast,"value_assignment");var assignee=node[0];var valueNode=interpretAst(node[2],env);var value=assignmentValue(valueNode);var env=assign(env,assignee,value);return nreturn(env.ctx)}).when("type_assignment",function(ast,env){var node=Isla.Parser.extract(ast,"type_assignment");var assignee=node[0];var typeIdentifier=interpretAst(node[2],env);var typeFn=env.ctx._types[typeIdentifier];if(typeFn===undefined){typeFn=env.ctx._types.generic}var value=instantiateType(typeFn,typeIdentifier);var env=assign(env,assignee,value);return nreturn(env.ctx)}).when("list_assignment",function(ast,env){var node=Isla.Parser.extract(ast,"list_assignment");var assignee=node[3];var currentListEval=evaluateValue(Isla.Parser.extract(assignee,"assignee",0),env);if(currentListEval.val===undefined){var ref=currentListEval.ref;throw"I do not know of a list called "+(Isla.Utils.type(currentListEval.ref)==="Array"?ref[0]+" "+ref[1]:ref)+"."}else{var operation=Isla.Parser.extract(node,0,"list_operation",0).tag;var itemEval=interpretAst(Isla.Parser.extract(node,1),env);var item=assignmentValue(itemEval);var list=currentListEval.val;list[operation](item);var env=assign(env,assignee,currentListEval.val);return nreturn(env.ctx)}}).when("invocation",function(ast,env){var fn=resolve({ref:interpretAst(Isla.Parser.extract(ast,"invocation",0),env)},env).fn;var param=resolve(interpretAst(Isla.Parser.extract(ast,"invocation",1),env).val,env);var returnVal=fn(env,param);return nreturn(env.ctx,returnVal)}).when("value",function(ast,env){var node=Isla.Parser.extract(ast,"value");return evaluateValue(node[0],env)}).when("integer",function(ast,env){return Isla.Parser.extract(ast,"integer",0)}).when("string",function(ast,env){return Isla.Parser.extract(ast,"string",0)}).when("identifier",function(ast,env){return Isla.Parser.extract(ast,"identifier",0)}).default(function(ast,env){throw"You've forgotten a tag type."});var evaluateValue=multimethod().dispatch(function(node,env){return node.tag}).when("literal",function(node,env){return{val:interpretAst(node.c[0],env)}}).when("variable",function(node,env){return evaluateValue(node.c[0],env)}).when("scalar",function(node,env){var identifier=interpretAst(node.c[0],env);return{ref:identifier,val:env.ctx[identifier]}}).when("object",function(node,env){var objId=node.c[0].c[0];var attrId=node.c[1].c[0];var val=env.ctx[objId]!==undefined&&env.ctx[objId][attrId]!==undefined?env.ctx[objId][attrId]:undefined;return{ref:[objId,attrId],val:val}});var assign=multimethod().dispatch(function(__,assigneeNode){return assigneeNode.c[0].tag}).when("scalar",function(env,assigneeNode,value){var identifier=Isla.Parser.extract(assigneeNode,"assignee",0,"scalar",0,"identifier",0);env.ctx[identifier]=value;return env}).when("object",function(env,assigneeNode,value){var objNode=Isla.Parser.extract(assigneeNode,"assignee",0,"object");var initObjIdentifier=Isla.Parser.extract(objNode,0,"identifier",0);var objIdentifier=canonical(initObjIdentifier,env);var slotIdentifier=Isla.Parser.extract(objNode,1,"identifier",0);env.ctx[objIdentifier][slotIdentifier]=value;return env});var assignmentValue=multimethod().dispatch(function(valueNode){return typeof valueNode.val}).when("string",function(valueNode){return valueNode.val}).when("number",function(valueNode){return valueNode.val}).default(function(valueNode){return valueNode.ref===undefined?valueNode.val:{ref:valueNode.ref}});var resolve=multimethod().dispatch(function(thing){if(thing==null){return thing}else if(thing._meta&&thing._meta.type==="list"){return"list"}else if(Isla.Utils.type(thing)==="Object"){return thing.ref===undefined?"object":"ref"}}).when("ref",function(thing,env){return resolve(env.ctx[thing.ref],env)}).when("object",function(thing,env){for(var i in thing){if(i!=="_meta"){thing[i]=resolve(thing[i],env)}}return thing}).when("list",function(thing,env){var items=thing.items();var resolvedList=new Isla.Library.List;for(var i=0;i<items.length;i++){resolvedList.add(resolve(items[i],env))}return resolvedList}).default(function(thing){return thing});var canonical=function(identifier,env){var next=env.ctx[identifier];if(next.ref!==undefined){return canonical(next.ref,env)}else{return identifier}};var runSequence=function(nodes,env){if(nodes.length===0){return env}else{return runSequence(_.rest(nodes),interpretAst(_.first(nodes),rmRet(env)))}};var instantiateType=function(typeFn,identifier){var value=typeFn();value._meta={type:identifier};return value};var rmRet=function(env){env.ret=null;return env};var nreturn=function(ctx,ret){if(ret===undefined){return{ctx:ctx,ret:null}}else{return{ctx:ctx,ret:ret}}};exports.Interpreter.resolve=resolve;exports.Interpreter.evaluateValue=evaluateValue;exports.Interpreter.interpretAst=interpretAst})(typeof exports==="undefined"?this.Isla:exports)
  1
+(function(exports){var Isla;if(typeof module!=="undefined"&&module.exports){Isla={};Isla.Utils=require("./utils").Utils;Isla.Grammar=require("./grammar").Grammar;Isla.Library=require("./library").Library;Isla.Parser=require("./parser").Parser;Isla.Interpreter=require("./interpreter").Interpreter}else{Isla={}}exports.Isla=Isla})(typeof exports==="undefined"?this:exports);(function(exports){exports.Utils={};exports.Utils.type=function(x){return Object.prototype.toString.call(x).match(/\[object ([^\]]+)\]/)[1]};exports.Utils.merge=function(a,b){var c={};for(var i in a){c[i]=a[i]}for(var i in b){c[i]=b[i]}return c}})(typeof exports==="undefined"?this.Isla:exports);(function(exports){exports.Grammar={};var grammarArr=["{","  var nnode = function(tag, content, column, syntax, code) {","    var node = { tag: tag, c: content, index: column - 1};","    if(syntax !== undefined) {","      node.syntax = syntax;","    }","    if (code !== undefined) {","      node.code = code;","    }","    return node;","  };","  var syn = function(obj, syntax) {","    obj.syntax = syntax;","    return obj;","  };","}","start","  = all:block { return nnode('root', [all], column); }","block","  = _* all:expression+ { return nnode('block', all, column); }","expression","  = all:type_assignment { return nnode('expression', [all], column); }","  / all:value_assignment { return nnode('expression', [all], column); }","  / all:list_assignment { return nnode('expression', [all], column); }","  / all:invocation { return nnode('expression', [all], column); }","type_assignment","  = a:assignee _ ia:is_a _ id:identifier _* nl","    { return nnode('type_assignment', [a, ia, syn(id, 'type')], column); }","value_assignment","  = a:assignee _ i:is _ v:value _* nl","    { return nnode('value_assignment', [a, i, v], column); }","list_assignment","  = lo:list_operation _ va:value _ tf:to_from _ as:assignee _* nl","    { return nnode('list_assignment', [lo, va, tf, as], column); }","invocation","  = a:identifier _ v:value _* nl","    { return nnode('invocation', [syn(a, 'function'), v], column); }","list_operation","  = all:add { return nnode('list_operation', [all], column); }","  / all:take { return nnode('list_operation', [all], column); }","assignee","  = all:object { return nnode('assignee', [all], column); }","  / all:scalar { return nnode('assignee', [all], column); }","value","  = all:literal { return nnode('value', [all], column); }","  / all:variable { return nnode('value', [all], column); }","literal","  = all:string { return nnode('literal', [all], column, 'literal'); }","identifier","  = !keyword init:init_identifier_char rest:identifier_char* { return nnode('identifier', [init + rest.join('')], column); }","variable","  = all:object { return nnode('variable', [all], column); }","  / all:scalar { return nnode('variable', [all], column); }","scalar","  = all:identifier { return nnode('scalar', [all], column, 'variable'); }","object","  = id1:identifier _ id2:identifier rest:object_rest* { return nnode('object', [syn(id1, 'variable'), syn(id2, 'attribute')].concat(rest), column); }","object_rest","  = _ id:identifier { return syn(id, 'attribute'); }","is","  = all:'is' { return nnode('is', [all], column, 'keyword'); }","is_a","  = all:'is a' { return nnode('is_a', [all], column, 'keyword'); }","add","  = all:'add' { return nnode('add', [all], column, 'keyword'); }","take","  = all:'take' { return nnode('take', [all], column, 'keyword'); }","to_from","  = all:to { return nnode('to_from', ['to from'], column, 'keyword', 'to'); }","  / all:from { return nnode('to_from', ['to from'], column, 'keyword', 'from'); }","string","  = \"'\" all:string_char_single* \"'\" { return nnode('string', [all.join('')], column, undefined, \"'\" + all.join('') + \"'\"); }","  / '\"' all:string_char_double* '\"' { return nnode('string', [all.join('')], column, undefined, '\"' + all.join('') + '\"'); }","nl","  = all:[\\n]+ { return nnode('nl', all, column); }","keyword","  = is !identifier_char","  / is_a !identifier_char","  / add !identifier_char","  / take !identifier_char","  / to !identifier_char","  / from !identifier_char","to = 'to'","from = 'from'","_ = [ \\t\\r]+","identifier_char = [a-zA-Z0-9_]","init_identifier_char = [a-zA-Z_]","string_char_double = [A-Za-z0-9., ']",'string_char_single = [A-Za-z0-9., "]'];var peg="";for(var i=0;i<grammarArr.length;i++){peg+=grammarArr[i]+"\n"}exports.Grammar.peg=peg})(typeof exports==="undefined"?this.Isla:exports);(function(exports){var Isla,_,multimethod;if(typeof module!=="undefined"&&module.exports){_=require("Underscore");multimethod=require("multimethod");Isla={};Isla.Utils=require("./utils").Utils}else{_=window._;multimethod=window.multimethod;Isla=window.Isla}exports.Library={};exports.Library.getInitialEnv=function(extraTypes,initialCtx){var islaCtx={write:{fn:function(env,param){if(Isla.Utils.type(param)==="Object"){return param.toString()}else{return param+"\n"}},description:function(p){return"Writes out "+p+"."}},_types:Isla.Utils.merge(extraTypes,{list:function(){return new List},generic:function(){return new Generic}})};if(initialCtx!==undefined){islaCtx=Isla.Utils.merge(islaCtx,initialCtx)}return{ret:null,ctx:islaCtx}};var Generic=function(){this.toString=function(indent){if(!indent){indent="  "}var out="a "+this._meta.type+"\n";for(var i in this){if(Isla.Utils.type(this[i])!=="Function"&&i!=="_meta"){if(Isla.Utils.type(this[i])==="Object"){out+=indent+i+" is "+this[i].toString(indent+"  ")}else{out+=indent+i+" is '"+this[i]+"'\n"}}}return out}};var objsEqual=function(a,b){return a.ref===b.ref&&a.ref||a===b};var List=function(){var data=[];this.add=multimethod().dispatch(function(thing){return Isla.Utils.type(thing)}).when("Object",function(thing){for(var i=0;i<data.length;i++){if(objsEqual(thing,data[i])){return}}data.push(thing)}).default(function(thing){data.push(thing)});this.take=multimethod().dispatch(function(thing){return Isla.Utils.type(thing)}).when("Object",function(thing){for(var i=0;i<data.length;i++){if(objsEqual(thing,data[i])){data.splice(i,1);break}}}).default(function(thing){for(var i=0;i<data.length;i++){if(thing===data[i]){data.splice(i,1);break}}});this.items=function(){return data};this.toString=function(indent){if(!indent){indent="  "}if(this.items().length===0){return"an empty list"}else{var out="a list\n";for(var i=0;i<this.items().length;i++){if(Isla.Utils.type(this.items()[i])==="Object"){out+=indent+this.items()[i].toString(indent+"  ")}else{out+=indent+"'"+this.items()[i]+"'\n"}}return out}}};exports.Library.List=List})(typeof exports==="undefined"?this.Isla:exports);(function(exports){var Isla,_,multimethod,pegjs;if(typeof module!=="undefined"&&module.exports){_=require("Underscore");multimethod=require("multimethod");pegjs=require("pegjs");Isla={};Isla.Grammar=require("./grammar").Grammar}else{_=window._;multimethod=window.multimethod;pegjs=window.PEG;Isla=window.Isla}var pegParser=pegjs.buildParser(Isla.Grammar.peg,{trackLineAndColumn:true});exports.Parser={};exports.Parser.parse=function(str){str+="\n";var ast=pegParser.parse(str);return ast};var extract=multimethod().dispatch(function(__,nxt){return typeof nxt}).when("string",function(ast,nxt){if(ast.tag===nxt){return extractNext(ast.c,arguments)}else{throw new Error(JSON.stringify(ast)+" is not "+nxt)}}).when("number",function(ast,nxt){return extractNext(ast[nxt],arguments)}).when("undefined",function(ast){return ast}).default(function(){throw"Route items must be tags or indices."});var extractNext=function(ast,args){var nextArgs=_.rest(_.rest(_.toArray(args)));nextArgs.unshift(ast);return extract.apply(this,nextArgs)};exports.Parser.extract=extract})(typeof exports==="undefined"?this.Isla:exports);(function(exports){var Isla,_,multimethod;if(typeof module!=="undefined"&&module.exports){_=require("Underscore");multimethod=require("multimethod");Isla={};Isla.Utils=require("./utils").Utils;Isla.Library=require("./library").Library;Isla.Parser=require("./parser").Parser}else{_=window._;multimethod=window.multimethod;Isla=window.Isla}exports.Interpreter={};exports.Interpreter.interpret=function(code,env){return interpretAst(Isla.Parser.parse(code),env)};var interpretAst=multimethod().dispatch(function(ast,env){return ast.tag}).when("root",function(ast,env){if(env===undefined){env=Isla.Library.getInitialEnv()}return runSequence(ast.c,env)}).when("block",function(ast,env){return runSequence(ast.c,env)}).when("expression",function(ast,env){return interpretAst(ast.c[0],env)}).when("value_assignment",function(ast,env){var node=Isla.Parser.extract(ast,"value_assignment");var assignee=node[0];var valueNode=interpretAst(node[2],env);var value=assignmentValue(valueNode);var env=assign(env,assignee,value);return nreturn(env.ctx)}).when("type_assignment",function(ast,env){var node=Isla.Parser.extract(ast,"type_assignment");var assignee=node[0];var typeIdentifier=interpretAst(node[2],env);var typeFn=env.ctx._types[typeIdentifier];if(typeFn===undefined){typeFn=env.ctx._types.generic}var value=instantiateType(typeFn,typeIdentifier);var env=assign(env,assignee,value);return nreturn(env.ctx)}).when("list_assignment",function(ast,env){var node=Isla.Parser.extract(ast,"list_assignment");var assignee=node[3];var currentListEval=evaluateValue(Isla.Parser.extract(assignee,"assignee",0),env);var operation=Isla.Parser.extract(node,0,"list_operation",0).tag;var itemEval=interpretAst(Isla.Parser.extract(node,1),env);var item=assignmentValue(itemEval);var list=currentListEval.val;list[operation](item);var env=assign(env,assignee,currentListEval.val);return nreturn(env.ctx)}).when("invocation",function(ast,env){var fn=resolve({ref:interpretAst(Isla.Parser.extract(ast,"invocation",0),env)},env).fn;var param=resolve(interpretAst(Isla.Parser.extract(ast,"invocation",1),env).val,env);var returnVal=fn(env,param);return nreturn(env.ctx,returnVal)}).when("value",function(ast,env){var node=Isla.Parser.extract(ast,"value");return evaluateValue(node[0],env)}).when("integer",function(ast,env){return Isla.Parser.extract(ast,"integer",0)}).when("string",function(ast,env){return Isla.Parser.extract(ast,"string",0)}).when("identifier",function(ast,env){return Isla.Parser.extract(ast,"identifier",0)}).default(function(ast,env){throw{message:"You've forgotten a tag type."}});var evaluateValue=multimethod().dispatch(function(node,env){return node.tag}).when("literal",function(node,env){return{val:interpretAst(node.c[0],env)}}).when("variable",function(node,env){return evaluateValue(node.c[0],env)}).when("scalar",function(node,env){var identifier=interpretAst(node.c[0],env);var val=env.ctx[identifier];if(val===undefined){nonExistentError([identifier])}else{return{ref:identifier,val:val}}}).when("object",function(node,env){var objNode=Isla.Parser.extract(node,"object");checkIdentifierParts(objNode,env);var parts=identifierParts(objNode,env);var val=env.ctx;for(var i=0;i<objNode.length;i++){var id=Isla.Parser.extract(objNode,i,"identifier",0);val=val[canonical(id,env)]}return{ref:parts,val:val}});var nonExistentError=function(identifier){throw{message:"I have not heard of "+identifier.join(" ")+"."}};var checkIdentifierParts=function(objNode,env){var parts=identifierParts(objNode,env);var val=env.ctx;for(var i=0;i<parts.length;i++){if(val===undefined){nonExistentError(parts.slice(0,i))}else{val=val[canonical(Isla.Parser.extract(objNode,i,"identifier",0),env)]}}};var identifierParts=function(objNode,env){var parts=[];for(var i=0;i<objNode.length;i++){parts.push(Isla.Parser.extract(objNode,i,"identifier",0))}return parts};var assign=multimethod().dispatch(function(__,assigneeNode){return assigneeNode.c[0].tag}).when("scalar",function(env,assigneeNode,value){var identifier=Isla.Parser.extract(assigneeNode,"assignee",0,"scalar",0,"identifier",0);env.ctx[identifier]=value;return env}).when("object",function(env,assigneeNode,value){var objNode=Isla.Parser.extract(assigneeNode,"assignee",0,"object");checkIdentifierParts(objNode,env);var slot=env.ctx;for(var i=0;i<objNode.length-1;i++){var id=canonical(Isla.Parser.extract(objNode,i,"identifier",0),env);slot=slot[id]}slot[Isla.Parser.extract(objNode,i,"identifier",0)]=value;return env});var assignmentValue=multimethod().dispatch(function(valueNode){return typeof valueNode.val}).when("string",function(valueNode){return valueNode.val}).when("number",function(valueNode){return valueNode.val}).default(function(valueNode){return valueNode.ref===undefined?valueNode.val:{ref:valueNode.ref}});var resolve=multimethod().dispatch(function(thing){if(thing==null){return thing}else if(thing._meta&&thing._meta.type==="list"){return"list"}else if(Isla.Utils.type(thing)==="Object"){return thing.ref===undefined?"object":"ref"}}).when("ref",function(thing,env){return resolve(env.ctx[thing.ref],env)}).when("object",function(thing,env){for(var i in thing){if(i!=="_meta"){thing[i]=resolve(thing[i],env)}}return thing}).when("list",function(thing,env){var items=thing.items();var resolvedList=new Isla.Library.List;for(var i=0;i<items.length;i++){resolvedList.add(resolve(items[i],env))}return resolvedList}).default(function(thing){return thing});var canonical=function(identifier,env){var next=env.ctx[identifier];if(next===undefined){return identifier}else if(next.ref!==undefined){return canonical(next.ref,env)}else{return identifier}};var runSequence=function(nodes,env){if(nodes.length===0){return env}else{return runSequence(_.rest(nodes),interpretAst(_.first(nodes),rmRet(env)))}};var instantiateType=function(typeFn,identifier){var value=typeFn();value._meta={type:identifier};return value};var rmRet=function(env){env.ret=null;return env};var nreturn=function(ctx,ret){if(ret===undefined){return{ctx:ctx,ret:null}}else{return{ctx:ctx,ret:ret}}};exports.Interpreter.resolve=resolve;exports.Interpreter.evaluateValue=evaluateValue;exports.Interpreter.interpretAst=interpretAst})(typeof exports==="undefined"?this.Isla:exports)

No commit comments for this range

Something went wrong with that request. Please try again.