tran method

  1. @override
String tran(
  1. LanguageTip tip
)
override

transform to target code, the target information is in the tip

Implementation

@override
String tran(LanguageTip tip) {
  String format = tip.getRule('cal');
  OperatorInfo operatorInfo = getOpInfo(tip);
  final leftExp = getLeftExpression();
  final rightExp = getRightExpression();
  // 括弧が要るかどうかをチェック
  if (rightExp is DCalculate) {
    if (rightExp.getOpInfo(tip).preceding < operatorInfo.preceding) {
      rightExp.isPriority = true;
    } else if (rightExp.getOpInfo(tip).preceding == operatorInfo.preceding &&
        operatorInfo.fixity == Fixity.left) {
      // 優先度同じかつ左結合
      rightExp.isPriority = true;
    }
  }
  if (leftExp is DCalculate) {
    if (leftExp.getOpInfo(tip).preceding < operatorInfo.preceding) {
      leftExp.isPriority = true;
    } else if (leftExp.getOpInfo(tip).preceding == operatorInfo.preceding &&
        operatorInfo.fixity == Fixity.right) {
      leftExp.isPriority = true;
    }
  }

  if (isPriority) {
    format = '($format)';
  }
  return sprintf(format, [
    getLeftExpression().tran(tip),
    getOperatorSign(tip),
    getRightExpression().tran(tip),
  ]);
}