From 95f92bffeb06463c16c7c947258d5625f2629913 Mon Sep 17 00:00:00 2001 From: StunxFS Date: Mon, 25 Dec 2023 18:34:33 -0400 Subject: [PATCH] format python code --- rivetc/src/__init__.py | 16 ++++++++++++---- rivetc/src/ast.py | 10 +++++----- rivetc/src/checker.py | 8 ++++++-- rivetc/src/codegen/__init__.py | 35 +++++++++++++++++----------------- rivetc/src/parser.py | 4 +++- rivetc/src/resolver.py | 11 +++++------ rivetc/src/sym.py | 18 ++++++++--------- 7 files changed, 57 insertions(+), 45 deletions(-) diff --git a/rivetc/src/__init__.py b/rivetc/src/__init__.py index 9a9350d19..dd8bf59a2 100644 --- a/rivetc/src/__init__.py +++ b/rivetc/src/__init__.py @@ -60,7 +60,9 @@ def __init__(self, args): def run(self): # if we are compiling the `core` module, avoid autoloading it if self.prefs.mod_name != "core": - self.parsed_files += self.load_module("core", "core", "", token.NO_POS) + self.parsed_files += self.load_module( + "core", "core", "", token.NO_POS + ) self.load_root_module() self.import_modules() @@ -163,13 +165,17 @@ def load_root_module(self): ) src_dir = path.join(self.prefs.input, "src") if path.isdir(src_dir): # support `src/` directory - files += self.filter_files(glob.glob(path.join(src_dir, "*.ri"))) + files += self.filter_files( + glob.glob(path.join(src_dir, "*.ri")) + ) # if the `--test` option is used and a `tests` directory exists, try to # load files from that directory as well if self.prefs.build_mode == prefs.BuildMode.Test: tests_dir = path.join(self.prefs.input, "tests") if path.isdir(tests_dir): - files += self.filter_files(glob.glob(path.join(tests_dir, "*.ri"))) + files += self.filter_files( + glob.glob(path.join(tests_dir, "*.ri")) + ) else: files = [self.prefs.input] if len(files) == 0: @@ -481,7 +487,9 @@ def evalue_comptime_condition(self, cond): if val != None: return not val return None - elif isinstance(cond, ast.BinaryExpr) and cond.op in [token.Kind.LogicalAnd, token.Kind.LogicalOr]: + elif isinstance(cond, ast.BinaryExpr) and cond.op in [ + token.Kind.LogicalAnd, token.Kind.LogicalOr + ]: left = self.evalue_comptime_condition(cond.left) if left != None: if cond.op == token.Kind.LogicalOr and left: diff --git a/rivetc/src/ast.py b/rivetc/src/ast.py index bd02d0e2e..21f4eb74f 100644 --- a/rivetc/src/ast.py +++ b/rivetc/src/ast.py @@ -40,11 +40,11 @@ def __init__(self, branches, has_else, pos): class ComptimeIfBranch: def __init__(self, cond, is_else, nodes, pos): - self.cond=cond - self.is_else=is_else - self.nodes=nodes - self.pos=pos - self.typ=None + self.cond = cond + self.is_else = is_else + self.nodes = nodes + self.pos = pos + self.typ = None # Used in variable decls/stmts and guard exprs class ObjDecl: diff --git a/rivetc/src/checker.py b/rivetc/src/checker.py index 89d93e314..f47514aae 100644 --- a/rivetc/src/checker.py +++ b/rivetc/src/checker.py @@ -282,7 +282,9 @@ def check_stmt(self, stmt): elif isinstance(stmt, ast.ForStmt): iterable_t = self.check_expr(stmt.iterable) iterable_sym = iterable_t.symbol() - if iterable_sym.kind in (TypeKind.Array, TypeKind.DynArray, TypeKind.Slice): + if iterable_sym.kind in ( + TypeKind.Array, TypeKind.DynArray, TypeKind.Slice + ): elem_typ = self.comp.comptime_number_to_type( iterable_sym.info.elem_typ ) @@ -872,7 +874,9 @@ def check_expr(self, expr): f"expected unsigned integer value, found `{idx_t}`", expr.index.pos ) - if left_sym.kind in (TypeKind.Array, TypeKind.DynArray, TypeKind.Slice): + if left_sym.kind in ( + TypeKind.Array, TypeKind.DynArray, TypeKind.Slice + ): if isinstance(expr.index, ast.RangeExpr): if left_sym.kind == TypeKind.Slice: expr.typ = expr.left_typ diff --git a/rivetc/src/codegen/__init__.py b/rivetc/src/codegen/__init__.py index c4478a7b0..eb772be15 100644 --- a/rivetc/src/codegen/__init__.py +++ b/rivetc/src/codegen/__init__.py @@ -68,15 +68,15 @@ def gen_source_files(self, source_files): # generate '_R4core12init_globalsF' function self.init_global_vars_fn = ir.FuncDecl( - False, ast.Attributes(), False, "_R4core12init_globalsF", [], - False, ir.VOID_T, False + False, ast.Attributes(), False, "_R4core12init_globalsF", [], False, + ir.VOID_T, False ) self.out_rir.decls.append(self.init_global_vars_fn) # generate '_R12drop_globalsZ' function g_fn = ir.FuncDecl( - False, ast.Attributes(), False, "_R4core12drop_globalsF", [], - False, ir.VOID_T, False + False, ast.Attributes(), False, "_R4core12drop_globalsF", [], False, + ir.VOID_T, False ) self.out_rir.decls.append(g_fn) @@ -1632,20 +1632,16 @@ def gen_expr(self, expr, custom_tmp = None): tmp = self.cur_func.local_name() if s.kind in (TypeKind.DynArray, TypeKind.Slice): if end == None: - method_name = "_R4core5Slice10slice_fromM" if s.kind==TypeKind.Slice else "_R4core8DynArray10slice_fromM" + method_name = "_R4core5Slice10slice_fromM" if s.kind == TypeKind.Slice else "_R4core8DynArray10slice_fromM" inst = ir.Inst( - ir.InstKind.Call, [ - ir.Name(method_name), left, - start - ] + ir.InstKind.Call, + [ir.Name(method_name), left, start] ) else: - method_name = "_R4core5Slice5sliceM" if s.kind==TypeKind.Slice else "_R4core8DynArray5sliceM" + method_name = "_R4core5Slice5sliceM" if s.kind == TypeKind.Slice else "_R4core8DynArray5sliceM" inst = ir.Inst( - ir.InstKind.Call, [ - ir.Name(method_name), left, start, - end - ] + ir.InstKind.Call, + [ir.Name(method_name), left, start, end] ) else: size, _ = self.comp.type_size(s.info.elem_typ) @@ -1692,13 +1688,14 @@ def gen_expr(self, expr, custom_tmp = None): elif s.kind in (TypeKind.DynArray, TypeKind.Slice): method_name = "_R4core5Slice3getM" if s.kind == TypeKind.Slice else "_R4core8DynArray3getM" expr_typ_ir2 = expr_typ_ir.ptr() - if s.kind == TypeKind.Slice and not isinstance(left.typ, type.Ptr): + if s.kind == TypeKind.Slice and not isinstance( + left.typ, type.Ptr + ): left = ir.Inst(ir.InstKind.GetPtr, [left], left.typ.ptr()) value = ir.Inst( ir.InstKind.Cast, [ ir.Inst( - ir.InstKind.Call, - [ir.Name(method_name), left, idx] + ir.InstKind.Call, [ir.Name(method_name), left, idx] ), expr_typ_ir2 ], expr_typ_ir2 ) @@ -2447,7 +2444,9 @@ def gen_left_assign(self, expr, right, assign_op): left_ir_typ = self.ir_type(expr.left_typ) left_sym = expr.left_typ.symbol() sym_is_boxed = left_sym.is_boxed() - if left_sym.kind in (TypeKind.DynArray, TypeKind.Slice) and assign_op == Kind.Assign: + if left_sym.kind in ( + TypeKind.DynArray, TypeKind.Slice + ) and assign_op == Kind.Assign: rec = self.gen_expr_with_cast(expr.left_typ, expr.left) if not isinstance(left_ir_typ, ir.Pointer): rec = ir.Inst(ir.InstKind.GetPtr, [rec]) diff --git a/rivetc/src/parser.py b/rivetc/src/parser.py index 2ed377872..df6224188 100644 --- a/rivetc/src/parser.py +++ b/rivetc/src/parser.py @@ -136,7 +136,9 @@ def parse_comptime_if(self, level): self.expect(Kind.KwIf) cond = self.parse_expr() branches.append( - ast.ComptimeIfBranch(cond,False, self.parse_nodes(level), cond.pos) + ast.ComptimeIfBranch( + cond, False, self.parse_nodes(level), cond.pos + ) ) if self.tok.kind != Kind.KwElse: break diff --git a/rivetc/src/resolver.py b/rivetc/src/resolver.py index 8dd0ba288..f0283a753 100644 --- a/rivetc/src/resolver.py +++ b/rivetc/src/resolver.py @@ -420,9 +420,10 @@ def resolve_ident(self, ident): ident.is_sym = True elif self.self_sym: if s := self.self_sym.find(ident.name): - if not (isinstance( - s, sym.Type - ) and s.kind == sym.TypeKind.Placeholder): + if not ( + isinstance(s, sym.Type) + and s.kind == sym.TypeKind.Placeholder + ): ident.sym = s ident.is_sym = True if ident.sym == None and ident.obj == None: @@ -517,9 +518,7 @@ def resolve_type(self, typ): elif isinstance(typ, type.Slice): if self.resolve_type(typ.typ): typ.resolve( - self.comp.universe.add_or_get_slice( - typ.typ, typ.is_mut - ) + self.comp.universe.add_or_get_slice(typ.typ, typ.is_mut) ) return True elif isinstance(typ, type.Tuple): diff --git a/rivetc/src/sym.py b/rivetc/src/sym.py index 8b76fc117..a037c4505 100644 --- a/rivetc/src/sym.py +++ b/rivetc/src/sym.py @@ -242,16 +242,16 @@ def add_or_get_slice(self, elem_typ, is_mut = False): return sym from .type import Type as type_Type slice_sym = Type( - True, unique_name, TypeKind.Slice, info = SliceInfo(elem_typ, is_mut), - fields = [ - Field("len", False, True, type_Type(self[14])) - ] + True, unique_name, TypeKind.Slice, + info = SliceInfo(elem_typ, is_mut), + fields = [Field("len", False, True, type_Type(self[14]))] ) slice_sym.add( Func( - ABI.Rivet, True, False, False, True, False, "to_dynamic_array", [], - type_Type(self.add_or_get_dyn_array(elem_typ, is_mut)), False, True, - NO_POS, False, True, type_Type(slice_sym) + ABI.Rivet, True, False, False, True, False, + "to_dynamic_array", [], + type_Type(self.add_or_get_dyn_array(elem_typ, is_mut)), False, + True, NO_POS, False, True, type_Type(slice_sym) ) ) if core_slice_sym := self.find("core").find("Slice"): @@ -283,8 +283,8 @@ def add_or_get_dyn_array(self, elem_typ, is_mut = False): return sym from .type import Type as type_Type dyn_array_sym = Type( - True, unique_name, TypeKind.DynArray, info = DynArrayInfo(elem_typ, is_mut), - fields = [ + True, unique_name, TypeKind.DynArray, + info = DynArrayInfo(elem_typ, is_mut), fields = [ Field("len", False, True, type_Type(self[14])), Field("cap", False, True, type_Type(self[14])) ]