visitExpr()

in lib/generator.js [1882:1983]


  visitExpr(ast, level, env) {
    if (ast.type === 'boolean') {
      this.emit(_upperFirst(`${ast.value}`));
    } else if (ast.type === 'null') {
      this.emit('None');
    } else if (ast.type === 'property_access') {
      this.visitPropertyAccess(ast, env);
    } else if (ast.type === 'string') {
      this.emit(`'${(_string(ast.value))}'`);
    } else if (ast.type === 'number') {
      this.emit(ast.value.value);
    } else if (ast.type === 'object') {
      this.visitObject(ast, level);
    } else if (ast.type === 'variable') {
      if(ast.inferred &&  ast.inferred.type === 'basic' && ast.inferred.name === 'class') {
        this.emit(this.getModelName('', _name(ast.id), 'module'));
      } else {
        this.emit(_avoidKeywords(_snakeCase(_name(ast.id))));
      }
    } else if (ast.type === 'virtualVariable') {
      this.emit(`self.${_snakeCase(_vid(ast.vid))}`);
    } else if (ast.type === 'decrement') {
      this.visitExpr(ast.expr, level, { left: true });
      this.emit('-= 1');
    } else if (ast.type === 'increment') {
      this.visitExpr(ast.expr, level, { left: true });
      this.emit('+= 1');
    } else if (ast.type === 'template_string') {
      const tmp = this.output;
      this.output = '';
      for (var i = 0; i < ast.elements.length; i++) {
        var item = ast.elements[i];
        if (item.type === 'element') {
          this.emit((item.value.string).replace(/{/g, '{{').replace(/}/g, '}}'));
        } else if (item.type === 'expr') {
          this.emit('{');
          this.visitExpr(item.expr, level);
          this.emit('}');
        } else {
          throw new Error('unimpelemented');
        }
      }
      const quote = _adaptedQuotes(this.output);
      this.output =  tmp + `f${quote}${this.output}${quote}`;
    } else if (ast.type === 'call') {
      // 调用函数
      this.visitCall(ast, level);
    } else if (ast.type === 'construct') {
      this.visitConstruct(ast, level);
    } else if (ast.type === 'array') {
      this.visitArray(ast, level);
    } else if (ast.type === 'group') {
      this.emit('(');
      this.visitExpr(ast.expr, level, env);
      this.emit(')');
    } else if (_isBinaryOp(ast.type)) {
      this.visitExpr(ast.left, level, env);
      if (ast.type === 'or') {
        this.emit(' or ');
      } else if (ast.type === 'add') {
        this.emit(' + ');
      } else if (ast.type === 'subtract') {
        this.emit(' - ');
      } else if (ast.type === 'div') {
        this.emit(' / ');
      } else if (ast.type === 'multi') {
        this.emit(' * ');
      } else if (ast.type === 'and') {
        this.emit(' and ');
      // eslint-disable-next-line no-dupe-else-if
      } else if (ast.type === 'or') {
        this.emit(' or ');
      } else if (ast.type === 'lte') {
        this.emit(' <= ');
      } else if (ast.type === 'lt') {
        this.emit(' < ');
      } else if (ast.type === 'gte') {
        this.emit(' >= ');
      } else if (ast.type === 'gt') {
        this.emit(' > ');
      } else if (ast.type === 'neq') {
        this.emit(' != ');
      } else if (ast.type === 'eq') {
        this.emit(' == ');
      }
      this.visitExpr(ast.right, level, env);
    } else if (ast.type === 'not') {
      this.emit('not ',level);
      this.visitExpr(ast.expr, level, env);
    } else if (ast.type === 'construct_model') {
      // 生成实例的初始化赋值
      this.visitConstructModel(ast, level);
    } else if (ast.type === 'map_access') {
      this.visitMapAccess(ast, level, env);
    } else if (ast.type === 'array_access') {
      this.visitArrayAccess(ast);
    } else if (ast.type === 'super') {
      this.visitSuper(ast, level);
    } else {
      throw new Error('unimpelemented');
    }
  }