Skip to content

Commit

Permalink
format python code
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Dec 25, 2023
1 parent 0f731f0 commit 95f92bf
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 45 deletions.
16 changes: 12 additions & 4 deletions rivetc/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions rivetc/src/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 6 additions & 2 deletions rivetc/src/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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
Expand Down
35 changes: 17 additions & 18 deletions rivetc/src/codegen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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])
Expand Down
4 changes: 3 additions & 1 deletion rivetc/src/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 5 additions & 6 deletions rivetc/src/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
18 changes: 9 additions & 9 deletions rivetc/src/sym.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down Expand Up @@ -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]))
]
Expand Down

0 comments on commit 95f92bf

Please sign in to comment.