Permalink
Browse files

Separate property access from expression accesses

  • Loading branch information...
1 parent b197c35 commit ebc38fefb7708d1a88d9bcbf3884e98a895b66d2 @puffnfresh committed Jan 26, 2012
Showing with 38 additions and 13 deletions.
  1. +4 −4 src/compile.js
  2. +2 −2 src/grammar.js
  3. +10 −0 src/nodes.js
  4. +15 −7 src/typeinference.js
  5. +2 −0 test/accessors.out
  6. +5 −0 test/accessors.roy
View
8 src/compile.js
@@ -287,12 +287,12 @@ var compileNode = function(n) {
}
return compileNode(n.func) + "(" + _.map(n.args, compileNode).join(", ") + ")";
},
- visitAccess: function() {
- if(n.property.accept) {
- return compileNode(n.value) + "[" + compileNode(n.property) + "]";
- }
+ visitPropertyAccess: function() {
return compileNode(n.value) + "." + n.property;
},
+ visitAccess: function() {
+ return compileNode(n.value) + "[" + compileNode(n.property) + "]";
+ },
visitBinaryGenericOperator: function() {
return [compileNode(n.left), n.name, compileNode(n.right)].join(" ");
},
View
4 src/grammar.js
@@ -220,8 +220,8 @@ var grammar = {
],
"accessor": [
["IDENTIFIER", "$$ = new yy.Identifier($1);"],
- ["accessor . keywordOrIdentifier", "$$ = new yy.Access($1, $3);"],
- ["( expression ) . keywordOrIdentifier", "$$ = new yy.Access($2, $5);"]
+ ["accessor . keywordOrIdentifier", "$$ = new yy.PropertyAccess($1, $3);"],
+ ["( expression ) . keywordOrIdentifier", "$$ = new yy.PropertyAccess($2, $5);"]
],
"outdentOrEof": [
["OUTDENT", ""],
View
10 src/nodes.js
@@ -198,6 +198,16 @@ exports.nodes = {
}
};
},
+ PropertyAccess: function(value, property) {
+ this.value = value;
+ this.property = property;
+
+ this.accept = function(a) {
+ if(a.visitPropertyAccess) {
+ return a.visitPropertyAccess(this);
+ }
+ };
+ },
Access: function(value, property) {
this.value = value;
this.property = property;
View
22 src/typeinference.js
@@ -282,19 +282,14 @@ var analyse = function(node, env, nonGeneric, data, aliases) {
// TODO: Make cleaner
return env[node.value.value].props['return'].types[1];
},
- visitAccess: function() {
+ visitPropertyAccess: function() {
var valueType = analyse(node.value, env, nonGeneric, data, aliases);
if(prune(valueType) instanceof t.NativeType) {
return new t.NativeType();
}
- if(prune(valueType) instanceof t.ArrayType) {
- var accessType = analyse(node.property, env, nonGeneric, data, aliases);
- unify(accessType, new t.NumberType());
- return prune(valueType).type;
- }
-
+ // TODO: Properly generate property constraints
if(valueType instanceof t.ObjectType) {
if(!valueType.props[node.property]) {
valueType.props[node.property] = new t.Variable();
@@ -307,6 +302,19 @@ var analyse = function(node, env, nonGeneric, data, aliases) {
return prune(valueType).getPropertyType(node.property);
},
+ visitAccess: function() {
+ var valueType = analyse(node.value, env, nonGeneric, data, aliases);
+
+ if(prune(valueType) instanceof t.NativeType) {
+ return new t.NativeType();
+ }
+
+ unify(valueType, new t.ArrayType(new t.Variable()));
+
+ var accessType = analyse(node.property, env, nonGeneric, data, aliases);
+ unify(accessType, new t.NumberType());
+ return prune(valueType).type;
+ },
visitBinaryGenericOperator: function() {
var leftType = analyse(node.left, env, nonGeneric, data, aliases);
var rightType = analyse(node.right, env, nonGeneric, data, aliases);
View
2 test/accessors.out
@@ -0,0 +1,2 @@
+2
+42
View
5 test/accessors.roy
@@ -0,0 +1,5 @@
+let arr = [1, 2, 3]
+console.log (arr ! 1)
+
+let obj = {answer: 42}
+console.log obj.answer

0 comments on commit ebc38fe

Please sign in to comment.