Skip to content

Commit

Permalink
all: stop using static fields of structures
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Jan 18, 2025
1 parent 2644ac5 commit 42d508d
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 121 deletions.
8 changes: 4 additions & 4 deletions src/julec/compile.jule
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ fn compileIr(compiler: str, compilerCmd: str) {
cmd.Spawn() else {
match error {
| os::CmdError.NotExist:
handle::AnsiEscape.Print(handle::AnsiEscape.RedSeq, "back-end compiler could not used because of compiler path is not exist")
handle::AnsiEscape.Print(handle::AnsiRed, "back-end compiler could not used because of compiler path is not exist")
| os::CmdError.Denied:
handle::AnsiEscape.Print(handle::AnsiEscape.RedSeq, "back-end compiler could not used because of permission denied")
handle::AnsiEscape.Print(handle::AnsiRed, "back-end compiler could not used because of permission denied")
|:
handle::AnsiEscape.Print(handle::AnsiEscape.RedSeq, "back-end compiler could not used because of unknown problem")
handle::AnsiEscape.Print(handle::AnsiRed, "back-end compiler could not used because of unknown problem")
}
handle::Throw("")
}
Expand All @@ -77,7 +77,7 @@ fn compileIr(compiler: str, compilerCmd: str) {
>>> please check errors above
>>> is this a compiler problem, please report us: https://github.com/julelang/jule/issues/new/choose`

handle::AnsiEscape.Print(handle::AnsiEscape.RedSeq, errorMessage)
handle::AnsiEscape.Print(handle::AnsiRed, errorMessage)
handle::Throw("")
}

Expand Down
14 changes: 8 additions & 6 deletions src/julec/handle/ansi.jule
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
// Copyright 2024-2025 The Jule Programming Language.
// Use of this source code is governed by a BSD 3-Clause
// license that can be found in the LICENSE file.

// ANSI sequences.
const AnsiReset = "\033[0m"
const AnsiBold = "\u001b[1m"
const AnsiRed = "\033[31m"
const AnsiBrightMagenta = "\033[95m"

struct AnsiEscape{}

impl AnsiEscape {
const ResetSeq = "\033[0m"
const BoldSeq = "\u001b[1m"
const RedSeq = "\033[31m"
const BrightMagentaSeq = "\033[95m"

// Reset all ANSI formatting.
static fn Reset() {
print(AnsiEscape.ResetSeq)
print(AnsiReset)
}

// Print with ANSI sequence.
Expand Down
4 changes: 2 additions & 2 deletions src/julec/handle/logger.jule
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl Logger {

// Prints error log.
static fn LogError(&l: build::Log) {
print(AnsiEscape.RedSeq)
print(AnsiRed)
print("error: ")
print(l.Text)
AnsiEscape.Reset()
Expand Down Expand Up @@ -53,7 +53,7 @@ impl Logger {
print("\n ")
print(strings::Repeat(" ", len(row)))
print(" | ")
AnsiEscape.Print(AnsiEscape.BrightMagentaSeq, "suggestion: ")
AnsiEscape.Print(AnsiBrightMagenta, "suggestion: ")
print(l.Suggestion)
}
}
Expand Down
40 changes: 20 additions & 20 deletions src/julec/obj/cxx/expr.jule
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl exprCoder {
fn string(mut &self, &c: &constant::Const) {
content := c.ReadStr()
if len(content) == 0 { // Empty.
self.oc.write(typeCoder.Str + "()")
self.oc.write(typeStr + "()")
ret
}
len := conv::FmtInt(i64(len(content)), 10)
Expand Down Expand Up @@ -482,15 +482,15 @@ impl exprCoder {
match {
| obj::IsAny(m.Type):
if m.Expr.Type.IsNil() {
self.oc.write(typeCoder.Any + "()")
self.oc.write(typeAny + "()")
ret
}
if m.Expr.Type.TypeEnum() != nil {
self.possibleRefExpr(m.Expr.Model)
ret
}
i := self.oc.pushAnyType(m.Expr.Type)
self.oc.write(typeCoder.Any + "(")
self.oc.write(typeAny + "(")
match type m.Expr.Model {
| &constant::Const:
prim := m.Expr.Type.Prim()
Expand Down Expand Up @@ -558,7 +558,7 @@ impl exprCoder {
self.oc.locInfo(m.Token)
self.oc.write("\", ")
}
self.oc.write("(" + typeCoder.Trait + "::Type*)&")
self.oc.write("(" + typeTrait + "::Type*)&")
identCoder.traitDecl(self.oc.Buf, m.Expr.Type.Trait())
self.oc.write("_mptr_data")
self.oc.write(conv::Itoa(obj::FindTraitTypeOffset(m.Expr.Type.Trait(), m.Type)))
Expand All @@ -568,7 +568,7 @@ impl exprCoder {
self.oc.tc.kind(self.oc.Buf, m.Type)
self.oc.write("(")
self.possibleRefExpr(m.Expr.Model)
self.oc.write(", (" + typeCoder.Trait + "::Type*)&")
self.oc.write(", (" + typeTrait + "::Type*)&")
identCoder.traitDecl(self.oc.Buf, m.Type.Trait())
self.oc.write("_mptr_data")
self.oc.write(conv::Itoa(obj::FindTraitTypeOffset(m.Type.Trait(), m.Expr.Type)))
Expand Down Expand Up @@ -850,7 +850,7 @@ impl exprCoder {
self.oc.write("; if (!")
self.oc.write(argData)
self.oc.write(`) __jule_panic((`)
self.oc.write(typeCoder.U8)
self.oc.write(typeU8)
self.oc.write(`*)"memory allocation failed for concurrent call", 44);` + "\n")
self.oc.indent()

Expand Down Expand Up @@ -900,7 +900,7 @@ impl exprCoder {
self.oc.write(") { delete ")
self.oc.write(argData)
self.oc.write("; __jule_panic((")
self.oc.write(typeCoder.U8)
self.oc.write(typeU8)
self.oc.write(`*)"thread creation failed for concurrent call", 42); }`)
}
self.oc.write("\n")
Expand Down Expand Up @@ -1049,13 +1049,13 @@ impl exprCoder {
match type m.Expr.Model {
| &constant::Const:
// Constant string indexed. Use fast way, avoid making literal.
self.oc.write(typeCoder.Str + "::at(")
self.oc.write(typeStr + "::at(")
if !env::Production {
self.oc.write("\"")
self.oc.locInfo(m.Token)
self.oc.write("\", ")
}
self.oc.write("reinterpret_cast<const " + typeCoder.U8 + "*>(")
self.oc.write("reinterpret_cast<const " + typeU8 + "*>(")
s := (&constant::Const)(m.Expr.Model).ReadStr()
cstrLit(self.oc.Buf, s)
self.oc.write("), ")
Expand Down Expand Up @@ -1136,7 +1136,7 @@ impl exprCoder {
self.oc.write(",")
}
}
self.oc.write("}).as<" + typeCoder.Uintptr + ">(), ")
self.oc.write("}).as<" + typeUintptr + ">(), ")
self.oc.write(ident)
self.oc.write(anonFuncCtxHandlerSuffix + ")")
} else {
Expand Down Expand Up @@ -1355,7 +1355,7 @@ impl exprCoder {
ret
}
}
self.oc.write("jule::VoidExceptional{.error=" + typeCoder.Any + "(")
self.oc.write("jule::VoidExceptional{.error=" + typeAny + "(")
} else {
self.oc.write("jule::Exceptional<")
self.oc.tc.rc.codeMut1(self.oc.Buf, m.Func.Result)
Expand All @@ -1367,7 +1367,7 @@ impl exprCoder {
ret
}
}
self.oc.write(">{.error=" + typeCoder.Any + "(")
self.oc.write(">{.error=" + typeAny + "(")
}
self.possibleRefExpr(m.Err.Model)
i := self.oc.pushAnyType(m.Err.Type)
Expand Down Expand Up @@ -1732,25 +1732,25 @@ impl exprCoder {
}

fn strConcat(mut &self, mut m: &opt::StrConcatExpr) {
self.oc.write("({ " + typeCoder.Int + " size = 0; ")
self.oc.write("({ " + typeInt + " size = 0; ")
for i, p in m.Parts {
ident := "part" + conv::Itoa(i)
self.oc.write(typeCoder.Str + " ")
self.oc.write(typeStr + " ")
self.oc.write(ident)
self.oc.write(" = std::move(")
self.possibleRefExpr(p)
self.oc.write("); size += ")
self.oc.write(ident)
self.oc.write(".len(); ")
}
self.oc.write("jule::Slice<" + typeCoder.U8 + "> buf; buf.alloc_new(0, size); ")
self.oc.write("jule::Slice<" + typeU8 + "> buf; buf.alloc_new(0, size); ")
for i in m.Parts {
ident := "part" + conv::Itoa(i)
self.oc.write("buf.append(")
self.oc.write(ident)
self.oc.write("); ")
}
self.oc.write(typeCoder.Str + " result; result._len = buf._len; result.buffer = std::move(buf.data); result._slice = buf._slice; std::move(result); })")
self.oc.write(typeStr + " result; result._len = buf._len; result.buffer = std::move(buf.data); result._slice = buf._slice; std::move(result); })")
}

fn strFromBytes(mut &self, mut m: &opt::StrFromBytes) {
Expand Down Expand Up @@ -2004,7 +2004,7 @@ impl exprCoder {
} else if prim.IsBool() {
self.oc.write("false")
} else if prim.IsStr() {
self.oc.write(typeCoder.Str + "()")
self.oc.write(typeStr + "()")
}
ret
}
Expand Down Expand Up @@ -2152,7 +2152,7 @@ fn structBinary(mut &buf: strings::Builder, mut &s: &sema::StructIns, &l: str,
// Literal is not including quotes, just string content in bytes.
fn strLit(mut &buf: strings::Builder, lit: str, len: str) {
identCoder.funcIns(buf, meta::Program.Runtime.StrBytePtr)
buf.WriteStr("((" + typeCoder.U8 + "*)")!
buf.WriteStr("((" + typeU8 + "*)")!
cstrLit(buf, lit)
buf.WriteByte(',')!
buf.WriteStr(len)!
Expand All @@ -2163,7 +2163,7 @@ fn strLit(mut &buf: strings::Builder, lit: str, len: str) {
// The lit function should return the length of content.
fn strLitF(mut &buf: strings::Builder, lit: fn(): int) {
identCoder.funcIns(buf, meta::Program.Runtime.StrBytePtr)
buf.WriteStr("((" + typeCoder.U8 + "*)")!
buf.WriteStr("((" + typeU8 + "*)")!
len := conv::Itoa(lit())
buf.WriteByte(',')!
buf.WriteStr(len)!
Expand All @@ -2173,7 +2173,7 @@ fn strLitF(mut &buf: strings::Builder, lit: fn(): int) {
// Like the strLit function, but writes as slice (will not perform allocation).
fn strLitAsSlice(mut &buf: strings::Builder, lit: str, len: str) {
identCoder.funcIns(buf, meta::Program.Runtime.SliceBytePtr)
buf.WriteStr("((" + typeCoder.U8 + "*)")!
buf.WriteStr("((" + typeU8 + "*)")!
cstrLit(buf, lit)
buf.WriteByte(',')!
buf.WriteStr(len)!
Expand Down
8 changes: 4 additions & 4 deletions src/julec/obj/cxx/ident.jule
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ use "std/unsafe"
// Identifier of initialize function caller function.
const initCallerIdent = "__jule_call_initializers"

const identSelf = "_self_"

struct identCoder{}

impl identCoder {
const Self = "_self_"

// Write identifiers to buf. If identifier contains unicode runes,
// handle as ASCII characters. Some backend compilers are not supports
// unicode identifiers and causes compile errors.
Expand Down Expand Up @@ -116,7 +116,7 @@ impl identCoder {
ret
}
if p.IsSelf() {
buf.WriteStr(identCoder.Self)!
buf.WriteStr(identSelf)!
ret
}
if p.Token == nil {
Expand Down Expand Up @@ -169,7 +169,7 @@ impl identCoder {
| v.Ident == token::Kind.Error:
buf.WriteStr("except.error")!
| v.Ident == token::Kind.Self:
buf.WriteStr(identCoder.Self)!
buf.WriteStr(identSelf)!
| v.RetOrder == -1:
// The single return variable is just this one.
// Return directly [resultName] because of generated with this identifier.
Expand Down
20 changes: 10 additions & 10 deletions src/julec/obj/cxx/object.jule
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ impl ObjectCoder {
// Ctx handler function.
self.anonObj.WriteStr("static void ")!
self.anonObj.WriteStr(ident)!
self.anonObj.WriteStr(anonFuncCtxHandlerSuffix + "(jule::Ptr<" + typeCoder.Uintptr + "> &ptr) { ptr.__as<")!
self.anonObj.WriteStr(anonFuncCtxHandlerSuffix + "(jule::Ptr<" + typeUintptr + "> &ptr) { ptr.__as<")!
self.anonObj.WriteStr(ident)!
self.anonObj.WriteStr(anonFuncCtxSuffix + ">().dealloc(); }\n")!
} else {
Expand Down Expand Up @@ -358,17 +358,17 @@ impl ObjectCoder {
di := self.pushDealloc(t.Sptr().Elem)

// Type structure.
self.anyObj.WriteStr("struct " + typeCoder.Any + "::Type ")!
self.anyObj.WriteStr("struct " + typeAny + "::Type ")!
self.anyObj.WriteStr(anyTypeIdent)!
self.anyObj.WriteStr(si)!
self.anyObj.WriteStr("{.dealloc=" + deallocatedTypeIdent)!
self.anyObj.WriteStr(conv::Itoa(di))!
self.anyObj.WriteStr(", .eq=__jule_ptrEqual, .to_str=__jule_ptrToStr};\n")!

// comparison function.
self.anyObj.WriteStr(typeCoder.Bool + " " + anyTypeIdent)!
self.anyObj.WriteStr(typeBool + " " + anyTypeIdent)!
self.anyObj.WriteStr(si)!
self.anyObj.WriteStr("_compare(const " + typeCoder.Any + " &any, const ")!
self.anyObj.WriteStr("_compare(const " + typeAny + " &any, const ")!
self.tc.asSptr(self.anyObj, unsafe { elemKind.Buf() })
self.anyObj.WriteStr(" &other) { return any.type == &" + anyTypeIdent)!
self.anyObj.WriteStr(si)!
Expand All @@ -392,7 +392,7 @@ impl ObjectCoder {
rmodel += "other)"
lmodel += "alloc)"

self.anyObj.WriteStr(typeCoder.Bool + " " + anyTypeIdent)!
self.anyObj.WriteStr(typeBool + " " + anyTypeIdent)!
self.anyObj.WriteStr(si)!
self.anyObj.WriteStr("_eq(void *alloc, void *other) noexcept { ")!
if t.Comparable() {
Expand All @@ -401,7 +401,7 @@ impl ObjectCoder {
self.anyObj.WriteStr("; }\n")!
} else {
self.anyObj.WriteStr(`__jule_panic((`)!
self.anyObj.WriteStr(typeCoder.U8)!
self.anyObj.WriteStr(typeU8)!
self.anyObj.WriteStr(`*)"runtime: comparing uncomparable type " "`)!
mut n := self.anyObj.Len()
cstrBytes(self.anyObj, t.Str())
Expand All @@ -413,7 +413,7 @@ impl ObjectCoder {
}

// to_str function.
self.anyObj.WriteStr(typeCoder.Str + " " + anyTypeIdent)!
self.anyObj.WriteStr(typeStr + " " + anyTypeIdent)!
self.anyObj.WriteStr(si)!
self.anyObj.WriteStr("_to_str(void *alloc) noexcept { return ")!
mut f := obj::FindFuncGenericInstance(meta::Program.Runtime.ToStr, t)
Expand All @@ -429,7 +429,7 @@ impl ObjectCoder {
self.anyObj.WriteStr("*>(alloc)); }\n")!

// Type structure.
self.anyObj.WriteStr("struct " + typeCoder.Any + "::Type ")!
self.anyObj.WriteStr("struct " + typeAny + "::Type ")!
self.anyObj.WriteStr(anyTypeIdent)!
self.anyObj.WriteStr(si)!
self.anyObj.WriteStr("{.dealloc=" + deallocatedTypeIdent)!
Expand Down Expand Up @@ -687,7 +687,7 @@ impl ObjectCoder {
if opt::Inline {
self.write("inline ")
}
self.write(typeCoder.Bool + " ")
self.write(typeBool + " ")
self.write(ident)
self.write(structDefaultEqMethodSuffix)
self.write("(")
Expand Down Expand Up @@ -888,7 +888,7 @@ impl ObjectCoder {
self.write("MptrData {\n")
self.addIndent()
self.indent()
self.write("void (*dealloc)(" + typeCoder.Ptr + "<" + typeCoder.Uintptr + ">&);\n")
self.write("void (*dealloc)(" + typePtr + "<" + typeUintptr + ">&);\n")
self.traitDataTypeMethods(hash)
self.doneIndent()
self.indent()
Expand Down
Loading

0 comments on commit 42d508d

Please sign in to comment.