diff --git a/src/AST.js b/src/AST.js index be69ed2..66b6c82 100644 --- a/src/AST.js +++ b/src/AST.js @@ -58,6 +58,33 @@ ListLiteralNode.prototype.evaluate = function (context) { }; +function TupleNode(expressions) { + AbstractNode.call(this); + this.expressions = expressions; +} + +exports.TupleNode = TupleNode; +extend(AbstractNode, TupleNode); + +TupleNode.prototype.getFreeVariables = function () { + return this.expressions.reduce(function (names, expression) { + return names.union(expression.getFreeVariables()); + }, []); +}; + +TupleNode.prototype.getType = function (context) { + return new TupleType(this.expressions.map(function (expression) { + return expression.getType(context); + })); +}; + +TupleNode.prototype.evaluate = function (context) { + return new TupleValue(this.expressions.map(function (expression) { + return expression.evaluate(context); + })); +}; + + function VariableNode(name) { AbstractNode.call(this); this.name = name; @@ -381,7 +408,7 @@ IfNode.prototype.getFreeVariables = function () { IfNode.prototype.getType = function (context) { var condition = this.condition.getType(context); - if (condition.isSubTypeOf(BooleanType.DEFAULT)) { + if (condition.isBoolean()) { return this.thenExpression.getType(context).merge(this.elseExpression.getType(context)); } else { throw new LambdaTypeError(); @@ -390,14 +417,10 @@ IfNode.prototype.getType = function (context) { IfNode.prototype.evaluate = function (context) { var condition = this.condition.evaluate(context); - if (condition.is(BooleanValue)) { - if (condition.value) { - return this.thenExpression.evaluate(context); - } else { - return this.elseExpression.evaluate(context); - } + if (condition.isTruthy()) { + return this.thenExpression.evaluate(context); } else { - throw new LambdaRuntimeError('\'if\' condition must be boolean'); + return this.elseExpression.evaluate(context); } }; @@ -521,7 +544,7 @@ TryCatchFinallyNode.prototype.evaluate = function (context) { function ChainedComparisonNode(expressions, operators) { - if (operators.length !== expressions.length - 1) { + if (operators.length < 1 || operators.length !== expressions.length - 1) { throw new LambdaInternalError(); } AbstractNode.call(this); @@ -552,7 +575,7 @@ ChainedComparisonNode.prototype.getType = function (context) { var partial = operators[i].bind(expressions[i]); if (partial.is(LambdaType) || partial.is(UnknownType)) { var result = partial.bind(expressions[i + 1]); - if (!result.isSubTypeOf(BooleanType.DEFAULT)) { + if (!result.isBoolean()) { throw new LambdaTypeError(); } } else { @@ -577,12 +600,8 @@ ChainedComparisonNode.prototype.evaluate = function (context) { var partial = operators[i].bind(expressions[i]); if (partial.is(Closure)) { var result = partial.bind(expressions[i + 1]); - if (result.is(BooleanValue)) { - if (!result.value) { - return BooleanValue.FALSE; - } - } else { - throw new LambdaRuntimeError(); + if (!result.isTruthy()) { + return BooleanValue.FALSE; } } else { throw new LambdaRuntimeError(); diff --git a/src/Characters.js b/src/Characters.js index 597bd8e..95be980 100644 --- a/src/Characters.js +++ b/src/Characters.js @@ -7,11 +7,12 @@ var Character = { BOOLEAN: 5, STRING: 6, LIST: 7, - LAMBDA: 8, - UNKNOWN: 9, + TUPLE: 8, + LAMBDA: 9, + UNKNOWN: 10, JS: { - UNDEFINED: 10, - NULL: 11, + UNDEFINED: 11, + NULL: 12, }, }; @@ -29,6 +30,7 @@ function characterToString(character) { 'boolean', 'string', 'list', + 'tuple', 'lambda', 'unknown', 'JavaScript.UNDEFINED', diff --git a/src/DefaultContext.js b/src/DefaultContext.js index 1dab07e..68cf889 100644 --- a/src/DefaultContext.js +++ b/src/DefaultContext.js @@ -36,11 +36,7 @@ DefaultContext.VALUES = Context.EMPTY.addAll({ '<=': Operators.make('<='), '>=': Operators.make('>='), 'not': Closure.fromFunction(function (x) { - if (x.is(BooleanValue)) { - return new BooleanValue(!x.value); - } else { - throw new LambdaRuntimeError(); - } + return x.isFalsey(); }), 'and': Operators.make('and'), 'or': Operators.make('or'), diff --git a/src/Lists.js b/src/Lists.js index e49ac60..4308148 100644 --- a/src/Lists.js +++ b/src/Lists.js @@ -126,6 +126,18 @@ ListType.prototype.context = ListType.prototype.context.addAll({ new VariableType('T')), new VariableType('T')), + zip: new LambdaType( + new ListType( + new VariableType('A')), + new LambdaType( + new ListType( + new VariableType('B')), + new ListType( + new TupleType([ + new VariableType('A'), + new VariableType('B') + ])))), + }); @@ -302,4 +314,19 @@ AbstractListValue.prototype.context = AbstractListValue.prototype.context.addAll throw new LambdaRuntimeError('cannot retrieve the maximum of an empty list'); } }), + zip: Closure.fromFunction(function (list1, list2) { + if (list2.is(AbstractListValue)) { + if (list1.getLength() === list2.getLength()) { + list1 = list1.forceList(); + list2 = list2.forceList(); + return new ListValue(list1.values.map(function (element, index) { + return new TupleValue([element, list2.values[index]]); + })); + } else { + throw new LambdaRuntimeError('cannot zip lists of different lengths'); + } + } else { + throw new LambdaRuntimeError('argument to zip must be a list'); + } + }), }); diff --git a/src/Operators.js b/src/Operators.js index b540078..3ff8026 100644 --- a/src/Operators.js +++ b/src/Operators.js @@ -30,6 +30,19 @@ Operators.select = function (name, left, right) { } }; +Operators._defineForTuples = function (name) { + return Operators._define(name, Character.TUPLE, Character.TUPLE, Character.TUPLE, function (x, y) { + if (x.values.length !== y.values.length) { + throw new LambdaRuntimeError(); + } else { + return new TupleValue(x.values.map(function (left, index) { + var right = y.values[index]; + return Operators.select(name, left.character, right.character).handler(left, right); + })); + } + }); +}; + Operators.make = function (name) { return Closure.fromFunction(function (x, y) { return Operators.select(name, x.character, y.character).handler(x, y); @@ -109,6 +122,8 @@ Operators._define('+', Character.LIST, Character.LIST, Character.LIST, function return new ListValue(x.forceList().values.concat(y.forceList().values)); }); +Operators._defineForTuples('+'); + Operators._define('-', Character.COMPLEX, Character.COMPLEX, Character.COMPLEX, function (x, y) { return new ComplexValue(x.real - y.real, x.imaginary - y.imaginary); @@ -174,6 +189,8 @@ Operators._define('-', Character.NATURAL, Character.NATURAL, Character.INTEGER, return new IntegerValue(x.value - y.value); }); +Operators._defineForTuples('-'); + Operators._define('|', Character.INTEGER, Character.INTEGER, Character.INTEGER, function (x, y) { return new IntegerValue(x.value | y.value); @@ -191,6 +208,8 @@ Operators._define('|', Character.NATURAL, Character.NATURAL, Character.NATURAL, return new NaturalValue(x.value | y.value); }); +Operators._defineForTuples('|'); + Operators._define('^', Character.INTEGER, Character.INTEGER, Character.INTEGER, function (x, y) { return new IntegerValue(x.value ^ y.value); @@ -208,6 +227,8 @@ Operators._define('^', Character.NATURAL, Character.NATURAL, Character.NATURAL, return new NaturalValue(x.value ^ y.value); }); +Operators._defineForTuples('^'); + Operators._define('*', Character.COMPLEX, Character.COMPLEX, Character.COMPLEX, function (x, y) { return new ComplexValue( @@ -275,6 +296,8 @@ Operators._define('*', Character.NATURAL, Character.NATURAL, Character.NATURAL, return new NaturalValue(x.value * y.value); }); +Operators._defineForTuples('*'); + Operators._define('/', Character.COMPLEX, Character.COMPLEX, Character.COMPLEX, function (x, y) { return new ComplexValue( @@ -348,6 +371,8 @@ Operators._define('/', Character.NATURAL, Character.NATURAL, Character.REAL, fun return new RealValue(x.value / y.value); }); +Operators._defineForTuples('/'); + Operators._define('%', Character.REAL, Character.REAL, Character.REAL, function (x, y) { return new RealValue(x.value % y.value); @@ -385,6 +410,8 @@ Operators._define('%', Character.NATURAL, Character.NATURAL, Character.NATURAL, return new NaturalValue(x.value % y.value); }); +Operators._defineForTuples('%'); + Operators._define('&', Character.INTEGER, Character.INTEGER, Character.INTEGER, function (x, y) { return new IntegerValue(x.value & y.value); @@ -402,6 +429,8 @@ Operators._define('&', Character.NATURAL, Character.NATURAL, Character.NATURAL, return new NaturalValue(x.value & y.value); }); +Operators._defineForTuples('&'); + Operators._define('**', Character.REAL, Character.REAL, Character.REAL, function (x, y) { return new RealValue(Math.pow(x.value, y.value)); @@ -439,6 +468,8 @@ Operators._define('**', Character.NATURAL, Character.NATURAL, Character.NATURAL, return new NaturalValue(Math.pow(x.value, y.value)); }); +Operators._defineForTuples('**'); + Operators._define('=', Character.COMPLEX, Character.COMPLEX, Character.BOOLEAN, function (x, y) { return new BooleanValue(x.real === y.real && x.imaginary === y.imaginary); @@ -527,6 +558,8 @@ Operators._define('=', Character.LIST, Character.LIST, Character.BOOLEAN, functi } }); +Operators._defineForTuples('='); + Operators._define('!=', Character.COMPLEX, Character.COMPLEX, Character.BOOLEAN, function (x, y) { return new BooleanValue(x.real !== y.real || x.imaginary !== y.imaginary); @@ -615,6 +648,8 @@ Operators._define('!=', Character.LIST, Character.LIST, Character.BOOLEAN, funct } }); +Operators._defineForTuples('!='); + Operators._define('<', Character.REAL, Character.REAL, Character.BOOLEAN, function (x, y) { return new BooleanValue(x.value < y.value); @@ -660,6 +695,8 @@ Operators._define('<', Character.STRING, Character.STRING, Character.BOOLEAN, fu return new BooleanValue(x.value < y.value); }); +Operators._defineForTuples('<'); + Operators._define('>', Character.REAL, Character.REAL, Character.BOOLEAN, function (x, y) { return new BooleanValue(x.value > y.value); @@ -705,6 +742,8 @@ Operators._define('>', Character.STRING, Character.STRING, Character.BOOLEAN, fu return new BooleanValue(x.value > y.value); }); +Operators._defineForTuples('>'); + Operators._define('<=', Character.REAL, Character.REAL, Character.BOOLEAN, function (x, y) { return new BooleanValue(x.value <= y.value); @@ -750,6 +789,8 @@ Operators._define('<=', Character.STRING, Character.STRING, Character.BOOLEAN, f return new BooleanValue(x.value <= y.value); }); +Operators._defineForTuples('<='); + Operators._define('>=', Character.REAL, Character.REAL, Character.BOOLEAN, function (x, y) { return new BooleanValue(x.value >= y.value); @@ -795,17 +836,25 @@ Operators._define('>=', Character.STRING, Character.STRING, Character.BOOLEAN, f return new BooleanValue(x.value >= y.value); }); +Operators._defineForTuples('>='); + Operators._define('and', Character.BOOLEAN, Character.BOOLEAN, Character.BOOLEAN, function (x, y) { return new BooleanValue(x.value && y.value); }); +Operators._defineForTuples('and'); + Operators._define('or', Character.BOOLEAN, Character.BOOLEAN, Character.BOOLEAN, function (x, y) { return new BooleanValue(x.value || y.value); }); +Operators._defineForTuples('or'); + Operators._define('xor', Character.BOOLEAN, Character.BOOLEAN, Character.BOOLEAN, function (x, y) { return new BooleanValue(x.value != y.value); }); + +Operators._defineForTuples('xor'); diff --git a/src/Parser.js b/src/Parser.js index 6631d25..a526875 100644 --- a/src/Parser.js +++ b/src/Parser.js @@ -55,10 +55,14 @@ Parser.prototype.parseClass0 = function () { this.lexer.next(); return ErrorNode.INSTANCE; case 'left': - this.lexer.next(); - var node = this.parseRoot(['right']); - this.lexer.next(); - return node; + if (this.lexer.next() !== 'right') { + var node = this.parseRoot(['right']); + this.lexer.next(); + return node; + } else { + this.lexer.next(); + return new TupleNode([]); + } case 'left-curly': this.lexer.next(); var expressions = []; @@ -122,9 +126,28 @@ Parser.prototype.parseTypeClass0 = function () { case 'keyword:unknown': return UnknownType.DEFAULT; case 'left': - var type = this.parseTypeClass2(); - this.lexer.expect('right'); - return type; + if (this.lexer.token() !== 'right') { + var type = this.parseTypeClass2(); + if (this.lexer.token() !== 'right') { + var types = [type]; + while ('comma' === this.lexer.token()) { + if (this.lexer.next() !== 'right') { + types.push(this.parseTypeClass2()); + } else { + this.lexer.next(); + return new TupleType(types); + } + } + this.lexer.expect('right'); + return new TupleType(types); + } else { + this.lexer.next(); + return type; + } + } else { + this.lexer.next(); + return new TupleType([]); + } default: return this.lexer.throwSyntaxError(); } @@ -513,8 +536,29 @@ Parser.prototype.parseClass10 = function (terminators) { } }; +Parser.prototype.parseClass11 = function (terminators) { + var nodes = [this.parseClass10(terminators.union('comma'))]; + while ('comma' === this.lexer.token()) { + if (terminators.contains(this.lexer.next())) { + this.lexer.next(); + return new TupleNode(nodes); + } else { + nodes.push(this.parseClass10(terminators.union('comma'))); + } + } + if (nodes.length > 1) { + return new TupleNode(nodes); + } else { + return nodes[0]; + } +}; + Parser.prototype.parseRoot = function (terminators) { - return this.parseClass10(terminators); + if (terminators.contains('comma')) { + return this.parseClass10(terminators); + } else { + return this.parseClass11(terminators); + } }; Parser.prototype.parse = function () { diff --git a/src/Types.js b/src/Types.js index 5827ab6..5ecf035 100644 --- a/src/Types.js +++ b/src/Types.js @@ -10,6 +10,10 @@ AbstractType.prototype.bind = function () { return this; }; +AbstractType.prototype.isBoolean = function () { + return false; +}; + function PrototypedType() { AbstractType.call(this); @@ -303,6 +307,10 @@ BooleanType.prototype.isSubCharacterOf = function (type) { Character.BOOLEAN === type.character; }; +BooleanType.prototype.isBoolean = function () { + return true; +}; + BooleanType.DEFAULT = new BooleanType(); @@ -368,7 +376,7 @@ ListType.prototype.isSubCharacterOf = function (type) { ListType.prototype.isSubTypeOf = function (type) { return UndefinedType.prototype.isSubTypeOf.call(this, type) && - (!type.is(IndexedType) || this.inner.isSubTypeOf(type.inner)); + (!type.is(IndexedType) || this.inner.isSubTypeOf(type.inner)); }; ListType.prototype.instance = function (name, type) { @@ -376,6 +384,56 @@ ListType.prototype.instance = function (name, type) { }; +function TupleType(types) { + UndefinedType.call(this); + this.types = types; +} + +exports.TupleType = TupleType; +extend(UndefinedType, TupleType); + +TupleType.prototype.character = Character.TUPLE; + +TupleType.prototype.toString = function () { + return '(' + this.types.map(function (type) { + return type.toString(); + }).join(', ') + ')'; +}; + +TupleType.prototype.setContext = function (context) { + return new (this._setContext(context))(this.types); +}; + +TupleType.prototype.extend = function (name, type) { + return new (this._extend(name, type))(this.types); +}; + +TupleType.prototype.isSubCharacterOf = function (type) { + return Character.UNDEFINED === type.character || + Character.TUPLE === type.character; +}; + +TupleType.prototype.isSubTypeOf = function (other) { + return UndefinedType.prototype.isSubTypeOf.call(this, other) && ( + !other.is(TupleType) || other.types.length === this.types.length && + this.types.every(function (type, index) { + return type.isSubTypeOf(other.types[index]); + })); +}; + +TupleType.prototype.instance = function (name, type) { + return new (this._setContext(this.context))(this.types.map(function (inner) { + return inner.instance(name, type); + })); +}; + +TupleType.prototype.isBoolean = function () { + return this.types.every(function (type) { + return type.isBoolean(); + }); +}; + + function LambdaType(left, right) { UndefinedType.call(this); this.left = left; diff --git a/src/Values.js b/src/Values.js index 57c9dab..e3eaf1d 100644 --- a/src/Values.js +++ b/src/Values.js @@ -24,6 +24,14 @@ AbstractValue.prototype.bind = function () { return this; }; +AbstractValue.prototype.isTruthy = function () { + throw new LambdaRuntimeException(); +}; + +AbstractValue.prototype.isFalsey = function () { + return !this.isTruthy(); +}; + function UndefinedValue() { AbstractValue.call(this); @@ -199,6 +207,10 @@ BooleanValue.prototype.marshal = function () { return this.value; }; +BooleanValue.prototype.isTruthy = function () { + return !!this.value; +}; + BooleanValue.TRUE = new BooleanValue(true); BooleanValue.FALSE = new BooleanValue(false); @@ -343,6 +355,39 @@ NativeArrayValue.prototype.forceList = function () { }; +function TupleValue(values) { + UndefinedValue.call(this); + this.values = values; +} + +exports.TupleValue = TupleValue; +extend(UndefinedValue, TupleValue); + +TupleValue.prototype.character = Character.TUPLE; + +TupleValue.prototype.toString = function () { + return '(' + this.values.map(function (value) { + return value.toString(); + }).join(', ') + ')'; +}; + +TupleValue.prototype.extend = function (name, value) { + return new (this._extend(name, value))(this.values); +}; + +TupleValue.prototype.marshal = function () { + return this.values.map(function (value) { + return value.marshal(); + }); +}; + +TupleValue.prototype.isTruthy = function () { + return this.values.every(function (value) { + return value.isTruthy(); + }); +}; + + function Closure(lambda, capture) { UndefinedValue.call(this); this.lambda = lambda;