visitAssignExpr method

  1. @override
Uint8List visitAssignExpr(
  1. AssignExpr expr
)
override

Implementation

@override
Uint8List visitAssignExpr(AssignExpr expr) {
  final bytesBuilder = BytesBuilder();
  if (expr.op == _lexicon.assign) {
    if (expr.left is MemberExpr) {
      final memberExpr = expr.left as MemberExpr;
      final object = compileAST(memberExpr.object);
      bytesBuilder.add(object);
      bytesBuilder.addByte(HTOpCode.register);
      bytesBuilder.addByte(HTRegIdx.postfixObject);
      final key = visitIdentifierExpr(memberExpr.key);
      bytesBuilder.add(key);
      bytesBuilder.addByte(HTOpCode.register);
      bytesBuilder.addByte(HTRegIdx.postfixKey);
      bytesBuilder.addByte(HTOpCode.memberSet);
      bytesBuilder.addByte(memberExpr.isNullable ? 1 : 0);
      final value = compileAST(expr.right, endOfExec: true);
      bytesBuilder.add(_uint16(value.length));
      bytesBuilder.add(value);
    } else if (expr.left is SubExpr) {
      final subExpr = expr.left as SubExpr;
      final array = compileAST(subExpr.object);
      bytesBuilder.add(array);
      bytesBuilder.addByte(HTOpCode.register);
      bytesBuilder.addByte(HTRegIdx.postfixObject);
      bytesBuilder.addByte(HTOpCode.subSet);
      bytesBuilder.addByte(subExpr.isNullable ? 1 : 0);
      // sub get key is after opcode
      // it has to be exec with 'move reg index'
      final key = compileAST(subExpr.key, endOfExec: true);
      final value = compileAST(expr.right, endOfExec: true);
      bytesBuilder.add(_uint16(key.length + value.length));
      bytesBuilder.add(key);
      bytesBuilder.add(value);
    } else {
      final left = compileAST(expr.left);
      final right = compileAST(expr.right);
      bytesBuilder.add(right);
      bytesBuilder.addByte(HTOpCode.register);
      bytesBuilder.addByte(HTRegIdx.assign);
      bytesBuilder.add(left);
      bytesBuilder.addByte(HTOpCode.assign);
    }
  } else if (expr.op == _lexicon.assignIfNull) {
    final ifStmt = IfStmt(
      BinaryExpr(
        expr.left,
        _lexicon.equal,
        ASTLiteralNull(),
      ),
      AssignExpr(
        expr.left,
        _lexicon.assign,
        expr.right,
        source: expr.source,
        line: expr.line,
        column: expr.column,
      ),
    );
    final bytes = visitIf(ifStmt);
    bytesBuilder.add(bytes);
  } else {
    final spreaded = AssignExpr(
        expr.left,
        _lexicon.assign,
        BinaryExpr(
            expr.left, expr.op.substring(0, expr.op.length - 1), expr.right),
        source: expr.source,
        line: expr.line,
        column: expr.column);
    final bytes = visitAssignExpr(spreaded);
    bytesBuilder.add(bytes);
  }
  return bytesBuilder.toBytes();
}