Booleans and unary not.

This commit is contained in:
Natalie Weizenbaum 2016-05-24 20:01:43 -07:00
parent 110bdf9efc
commit 8f44bd8daa
5 changed files with 47 additions and 6 deletions

View File

@ -20,13 +20,19 @@ class UnaryOperatorExpression implements Expression {
/*=T*/ visit/*<T>*/(ExpressionVisitor/*<T>*/ visitor) =>
visitor.visitUnaryOperatorExpression(this);
String toString() => "${operator.operator}${operand}";
String toString() {
var buffer = new StringBuffer(operator.operator);
if (operator == UnaryOperator.not) buffer.writeCharCode($space);
buffer.write(operand);
return buffer.toString();
}
}
class UnaryOperator {
static const plus = const UnaryOperator._("plus", "+");
static const minus = const UnaryOperator._("minus", "-");
static const divide = const UnaryOperator._("divide", "/");
static const not = const UnaryOperator._("not", "not");
final String name;

View File

@ -392,7 +392,14 @@ class Parser {
Expression _identifierLike() {
// TODO: url(), functions
return new IdentifierExpression(_interpolatedIdentifier());
var identifier = _interpolatedIdentifier();
if (identifier.asPlain == "not") {
_ignoreComments();
return new UnaryOperatorExpression(
UnaryOperator.not, _singleExpression());
} else {
return new IdentifierExpression(identifier);
}
}
/// Consumes tokens up to "{", "}", ";", or "!".

View File

@ -2,17 +2,23 @@
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
import 'value/boolean.dart';
import 'value/string.dart';
export 'value/boolean.dart';
export 'value/identifier.dart';
export 'value/list.dart';
export 'value/string.dart';
class Value {
abstract class Value {
const Value();
// TODO: call the proper stringifying method
Value unaryPlus() => new SassString("+$this");
Value unaryMinus() => new SassString("-$this");
Value unaryDivide() => new SassString("/$this");
Value unaryNot() => Boolean.sassFalse;
}

View File

@ -0,0 +1,20 @@
// Copyright 2016 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
import '../value.dart';
class Boolean extends Value {
static const sassTrue = const Boolean._(true);
static const sassFalse = const Boolean._(false);
final bool value;
factory Boolean(bool value) => value ? Boolean.sassTrue : Boolean.sassFalse;
const Boolean._(this.value);
Value unaryNot() => value ? sassFalse : sassTrue;
String toString() => value.toString();
}

View File

@ -23,10 +23,12 @@ class PerformExpressionVisitor extends ExpressionVisitor<Value> {
}
Value visitUnaryOperatorExpression(UnaryOperatorExpression node) {
var operand = node.operand.visit(this);
switch (node.operator) {
case UnaryOperator.plus: return node.unaryPlus();
case UnaryOperator.minus: return node.unaryMinus();
case UnaryOperator.divide: return node.unaryDivide();
case UnaryOperator.plus: return operand.unaryPlus();
case UnaryOperator.minus: return operand.unaryMinus();
case UnaryOperator.divide: return operand.unaryDivide();
case UnaryOperator.not: return operand.unaryNot();
default: throw new StateError("Unknown unary operator ${node.operator}.");
}
}