Skip to content
Browse files

Add unary ! (not) operator

  • Loading branch information...
1 parent 4f57805 commit c44b28c56989afe5435c925caba9107f24fe3a7d @puffnfresh committed
Showing with 21 additions and 0 deletions.
  1. +3 −0 src/compile.js
  2. +1 −0 src/grammar.js
  3. +10 −0 src/nodes.js
  4. +7 −0 src/typeinference.js
View
3 src/compile.js
@@ -345,6 +345,9 @@ var compileNodeWithEnv = function(n, env, opts) {
visitAccess: function() {
return compileNode(n.value) + "[" + compileNode(n.property) + "]";
},
+ visitUnaryBooleanOperator: function() {
+ return [n.name, compileNode(n.value)].join(" ");
+ },
visitBinaryGenericOperator: function() {
return [compileNode(n.left), n.name, compileNode(n.right)].join(" ");
},
View
1 src/grammar.js
@@ -71,6 +71,7 @@ var grammar = {
"callArgument": [
["( expression )", n("$$ = new yy.Expression($2);")],
["& ( expression )", n("$$ = new yy.Replacement($3);")],
+ ["! ( expression )", n("$$ = new yy.UnaryBooleanOperator($1, $3);")],
["[| expression |]", n("$$ = new yy.Quoted($2);")],
["accessor", "$$ = $1;"],
["callArgument @ callArgument", n("$$ = new yy.Access($1, $3);")],
View
10 src/nodes.js
@@ -270,6 +270,16 @@ exports.nodes = {
}
};
},
+ UnaryBooleanOperator: function(name, value) {
+ this.name = name;
+ this.value = value;
+
+ this.accept = function(a) {
+ if(a.visitUnaryBooleanOperator) {
+ return a.visitUnaryBooleanOperator(this);
+ }
+ };
+ },
BinaryGenericOperator: function(name, left, right) {
this.name = name;
this.left = left;
View
7 src/typeinference.js
@@ -522,6 +522,13 @@ var analyse = function(node, env, nonGeneric, aliases, constraints) {
unify(accessType, new t.NumberType(), node.lineno);
return t.prune(valueType).type;
},
+ visitUnaryBooleanOperator: function() {
+ var resultType = new t.BooleanType();
+ var valueType = analyse(node.value, env, nonGeneric, aliases, constraints);
+ unify(valueType, resultType, node.value.lineno);
+
+ return resultType;
+ },
visitBinaryGenericOperator: function() {
var leftType = analyse(node.left, env, nonGeneric, aliases, constraints);
var rightType = analyse(node.right, env, nonGeneric, aliases, constraints);

0 comments on commit c44b28c

Please sign in to comment.
Something went wrong with that request. Please try again.