dart-sass/lib/src/visitor/expression/perform.dart

59 lines
2.0 KiB
Dart
Raw Normal View History

2016-05-24 23:01:41 +02:00
// 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 '../../ast/sass/expression.dart';
2016-05-25 00:36:44 +02:00
import '../../environment.dart';
2016-05-24 23:01:41 +02:00
import '../../value.dart';
import '../expression.dart';
class PerformExpressionVisitor extends ExpressionVisitor<Value> {
2016-05-25 00:36:44 +02:00
final Environment _environment;
PerformExpressionVisitor(this._environment);
2016-05-24 23:01:41 +02:00
Value visit(Expression expression) => expression.visit(this);
2016-05-25 00:36:44 +02:00
Value visitVariableExpression(VariableExpression node) {
var result = _environment.getVariable(node.name);
if (result != null) return result;
// TODO: real exception
throw node.span.message("undefined variable");
}
2016-05-25 02:04:16 +02:00
Value visitUnaryOperatorExpression(UnaryOperatorExpression node) {
2016-05-25 05:01:43 +02:00
var operand = node.operand.visit(this);
2016-05-25 02:04:16 +02:00
switch (node.operator) {
2016-05-25 05:01:43 +02:00
case UnaryOperator.plus: return operand.unaryPlus();
case UnaryOperator.minus: return operand.unaryMinus();
case UnaryOperator.divide: return operand.unaryDivide();
case UnaryOperator.not: return operand.unaryNot();
2016-05-25 02:04:16 +02:00
default: throw new StateError("Unknown unary operator ${node.operator}.");
}
}
2016-05-24 23:01:41 +02:00
Identifier visitIdentifierExpression(IdentifierExpression node) =>
new Identifier(visitInterpolationExpression(node.text).text);
2016-05-25 06:20:52 +02:00
Boolean visitBooleanExpression(BooleanExpression node) =>
new Boolean(node.value);
2016-05-25 06:50:44 +02:00
Number visitNumberExpression(NumberExpression node) =>
new Number(node.value);
2016-05-24 23:01:41 +02:00
SassString visitInterpolationExpression(InterpolationExpression node) {
return new SassString(node.contents.map((value) {
if (value is String) return value;
return (value as Expression).visit(this);
}).join());
}
SassList visitListExpression(ListExpression node) => new SassList(
node.contents.map((expression) => expression.visit(this)),
node.separator);
SassString visitStringExpression(StringExpression node) =>
visitInterpolationExpression(node.text);
}