Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Feb 16, 2024
1 parent 7009995 commit 625e050
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 91 deletions.
65 changes: 30 additions & 35 deletions src/julec/compile.jule
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use deadcode
use env::{self, OptLevel, push_opt_level}
use handle::{AnsiEscape, Logger, throw}
use importer
use obj::{IR}
use cxx for obj::cxx
Expand Down Expand Up @@ -45,12 +46,12 @@ fn write_output(path: str, content: str) {

Status.of(dir) else {
Directory.create(dir) else {
exit_err("a problem occurs when code generation")
throw("a problem occurs when code generation")
}
}

File.write(path, []byte(content), 0660) else {
exit_err("a problem occurs when code generation")
throw("a problem occurs when code generation")
}
}

Expand All @@ -75,25 +76,25 @@ fn compile_ir(compiler: str, compiler_cmd: str) {
let status = cmd.spawn() else {
match error {
| ProcessError.Fork:
AnsiEscape.print(AnsiSeq.Red, "back-end compiler could not used because of fork problem")
AnsiEscape.print(AnsiEscape.Red, "back-end compiler could not used because of fork problem")
| ProcessError.NotExist:
AnsiEscape.print(AnsiSeq.Red, "back-end compiler could not used because of compiler path is not exist")
AnsiEscape.print(AnsiEscape.Red, "back-end compiler could not used because of compiler path is not exist")
| ProcessError.NotExec:
AnsiEscape.print(AnsiSeq.Red, "back-end compiler could not used because of compiler path is not in the proper format")
AnsiEscape.print(AnsiEscape.Red, "back-end compiler could not used because of compiler path is not in the proper format")
| ProcessError.Denied:
AnsiEscape.print(AnsiSeq.Red, "back-end compiler could not used because of permission denied")
AnsiEscape.print(AnsiEscape.Red, "back-end compiler could not used because of permission denied")
|:
AnsiEscape.print(AnsiSeq.Red, "back-end compiler could not used because of unkown problem")
AnsiEscape.print(AnsiEscape.Red, "back-end compiler could not used because of unkown problem")
}
exit_err("")
throw("")
use 0
}
if status != 0 {
let error_message = "\n>>> your backend compiler (" + env::COMPILER + `) reports problems
>>> please check errors above
>>> is this a compiler problem, please report us: https://github.com/julelang/jule/issues/new/choose`
AnsiEscape.print(AnsiSeq.Red, error_message)
exit_err("")
AnsiEscape.print(AnsiEscape.Red, error_message)
throw("")
}

clear_objects()
Expand Down Expand Up @@ -224,16 +225,13 @@ fn apply_target_independent_optimizations(mut &ir: &IR) {
fn check_compiler_flag() {
match env::COMPILER {
| "":
exit_err("missing option value: --compiler")

throw("missing option value: --compiler")
| "clang":
env::COMPILER_PATH = "clang++"

| "gcc":
env::COMPILER_PATH = "g++"

|:
exit_err("invalid option value for --compiler: " + env::COMPILER)
throw("invalid option value for --compiler: " + env::COMPILER)
}
}

Expand All @@ -243,8 +241,7 @@ fn check_target_arch(arch: str) {
ret
}
}

exit_err("--target: unsupported/undefined architecture: " + arch)
throw("--target: unsupported/undefined architecture: " + arch)
}

fn check_target_os(os: str) {
Expand All @@ -253,18 +250,17 @@ fn check_target_os(os: str) {
ret
}
}

exit_err("--target: unsupported/undefined operating system: " + os)
throw("--target: unsupported/undefined operating system: " + os)
}

fn check_target_flag(&target: str) {
if target == "" {
exit_err("missing option value: --target")
throw("missing option value: --target")
}

let parts = strings::split(target, "-", -1)
if parts.len != 2 {
exit_err("--target: undefined platform target format: " + target)
throw("--target: undefined platform target format: " + target)
}

let (os, arch) = parts[0], parts[1]
Expand All @@ -282,13 +278,13 @@ fn check_target_flag(&target: str) {

fn check_opt_flag(&opt: str) {
if opt == "" {
exit_err("missing option value: --opt")
throw("missing option value: --opt")
}

match opt {
| "L0": // Nothing.
| "L1": push_opt_level(OptLevel.L1)
|: exit_err("--opt: invalid optimization level: " + opt)
|: throw("--opt: invalid optimization level: " + opt)
}
}

Expand All @@ -297,10 +293,9 @@ fn check_cpp_std_flag() {
| "cpp14"
| "cpp17"
| "cpp20":
// Ok.

break
|:
exit_err("--cppstd: invalid optimization level: " + env::CPP_STD)
throw("--cppstd: invalid optimization level: " + env::CPP_STD)
}
}

Expand Down Expand Up @@ -330,7 +325,7 @@ fn check_flags(&args: []str): []str {
fs.add_var[bool](unsafe { (&bool)(&env::OPT_COND) }, "opt-cond", 0, "Conditional optimizations")

let mut content = fs.parse(args) else {
exit_err(str(error))
throw(str(error))
use nil // Avoid error.
}

Expand All @@ -355,22 +350,22 @@ fn build_ir(&args: []str): &IR {
setup_sema_flags(sema_flags)

if content.len == 0 {
exit_err(logf(LogMsg.MissingCompilePath))
throw(logf(LogMsg.MissingCompilePath))
} else if content.len > 1 {
exit_err("undefined content: " + content[1])
throw("undefined content: " + content[1])
}
let (mut path, ok) = path::abs(content[0])
if !ok {
exit_err("compile path could not processed because of a problem")
throw("compile path could not processed because of a problem")
}

// Check standard library.
let inf = Status.of(PATH_STDLIB) else {
exit_err(logf(LogMsg.StdlibNotExist))
throw(logf(LogMsg.StdlibNotExist))
ret nil // Avoid error.
}
if !inf.is_dir() {
exit_err(logf(LogMsg.StdlibNotExist))
throw(logf(LogMsg.StdlibNotExist))
}

// Initialize variables for directive eval.
Expand All @@ -379,12 +374,12 @@ fn build_ir(&args: []str): &IR {
let (mut ir, logs) = IR.build(path, sema_flags)

if ir == nil && logs == nil {
exit_err(logf(LogMsg.NoFileInEntryPackage, path))
throw(logf(LogMsg.NoFileInEntryPackage, path))
}

if logs != nil {
Logger.print_logs(logs)
exit_err("")
throw("")
}

ret ir
Expand All @@ -404,7 +399,7 @@ fn compile_command(mut &args: []str) {
if !env::TEST {
let mut f = ir.main.find_fn(ENTRY_POINT, CPP_LINKED)
if f == nil {
exit_err(logf(LogMsg.NoEntryPoint))
throw(logf(LogMsg.NoEntryPoint))
}
f.statically = true // Mark used for deadcode elimination.
}
Expand Down
25 changes: 25 additions & 0 deletions src/julec/handle/ansi.jule
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2024 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.

pub struct AnsiEscape {}

impl AnsiEscape {
pub const Reset = "\033[0m"
pub const Bold = "\u001b[1m"
pub const Red = "\033[31m"
pub const BrightMagenta = "\033[95m"

// Reset all ANSI formatting.
pub static fn reset() {
out(AnsiEscape.Reset)
}

// Print with ANSI sequence.
// Resets ANSI after print.
pub static fn print(escape: str, text: str) {
out(escape)
out(text)
AnsiEscape.reset()
}
}
44 changes: 15 additions & 29 deletions src/julec/log.jule → src/julec/handle/logger.jule
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,18 @@ use conv for std::conv
use std::jule::build::{Log, LogKind}
use strings for std::strings

enum AnsiSeq: str {
Reset = "\033[0m",
Bold = "\u001b[1m",
Red = "\033[31m",
BrightMagenta = "\033[95m",
}

struct AnsiEscape {}

impl AnsiEscape {
static fn reset() { out(AnsiSeq.Reset) }

static fn print(escape: str, text: str) {
out(escape)
out(text)
AnsiEscape.reset()
}
}

struct Logger{}
// Logger for compiler logs.
pub struct Logger{}

impl Logger {
// Prints flag log.
static fn log_flat(&l: Log) { outln(l.text) }
pub static fn log_flat(&l: Log) {
outln(l.text)
}

// Prints error log.
static fn log_error(&l: Log) {
out(AnsiSeq.Red)
pub static fn log_error(&l: Log) {
out(AnsiEscape.Red)
out("error: ")
out(l.text)
AnsiEscape.reset()
Expand Down Expand Up @@ -70,23 +54,25 @@ impl Logger {
out("\n ")
out(strings::repeat(" ", row.len))
out(" | ")
AnsiEscape.print(AnsiSeq.BrightMagenta, "suggestion: ")
AnsiEscape.print(AnsiEscape.BrightMagenta, "suggestion: ")
out(l.suggestion)
}
}
outln("\n")
}

// Log.
static fn log(&l: Log) {
pub static fn log(&l: Log) {
match l.kind {
| LogKind.Flat: Logger.log_flat(l)
| LogKind.Error: Logger.log_error(l)
| LogKind.Flat:
Logger.log_flat(l)
| LogKind.Error:
Logger.log_error(l)
}
}

// Prints logs.
static fn print_logs(&logs: []Log) {
// Prints all logs.
pub static fn print_logs(&logs: []Log) {
for _, l in logs {
Logger.log(l)
}
Expand Down
12 changes: 12 additions & 0 deletions src/julec/handle/throw.jule
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2024 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.

use process for std::process

const ERROR_EXIT_CODE = 1

pub fn throw(msg: str) {
outln(msg)
process::exit(ERROR_EXIT_CODE)
}
10 changes: 2 additions & 8 deletions src/julec/importer/importer.jule
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// license that can be found in the LICENSE file.

use mod
use handle::{throw}

use std::fs::{OFlag, FsError, DirEntry, File, Directory}
use std::fs::path::{join}
Expand All @@ -22,17 +23,10 @@ use std::jule::sema::{
use std::process::{exit}
use strings for std::strings

fn exit_err(msg: str) {
const ERROR_EXIT_CODE = 1

outln(msg)
exit(ERROR_EXIT_CODE)
}

// Read buffer by file path.
fn read_buff(path: str): []byte {
ret File.read(path) else {
exit_err("error: file cannot read")
throw("error: file cannot read")
ret nil // Avoid error
}
}
Expand Down
8 changes: 0 additions & 8 deletions src/julec/main.jule
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ use std::fs::{File}
use jule for std::jule
use build for std::jule::build
use std::env
use process for std::process
use strings for std::strings

const ERROR_EXIT_CODE = 1

// Compiler commands.
const CMD_HELP = "help"
const CMD_VERSION = "version"
Expand Down Expand Up @@ -161,11 +158,6 @@ fn process_command(&args: []str): bool {
ret true
}

fn exit_err(msg: str) {
outln(msg)
process::exit(ERROR_EXIT_CODE)
}

fn show_info() {
outln(
`JuleC is a tool for Jule source code and developers.
Expand Down
15 changes: 4 additions & 11 deletions std/jule/build/env.jule
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,23 @@ pub static PATH_EXEC = "" // Path of executable file's directory.
pub static PATH_WD = "" // Path of working directory.
pub static PATH_API = "" // Path of main API header file.

fn exit_err(msg: str) {
const ERROR_EXIT_CODE = 1

outln(msg)
process::exit(ERROR_EXIT_CODE)
}

fn init() {
let mut path = process::executable()
if path == "" {
exit_err("std::jule::build: executable file cannot found")
panic("std::jule::build: executable file cannot found")
}

let pwd = env::working_dir() else {
exit_err("std::jule::build: working directory path cannot found")
ret // For avoid assignment error.
panic("std::jule::build: working directory path cannot found")
ret // To avoid assignment error.
}

let path_exec = path::dir(path)

// Go to parent directory.
path = path::join(path_exec, "..")

// Break immutability for assign paths.
// Break immutability to assign paths.
unsafe {
*(&PATH_WD) = pwd
*(&PATH_EXEC) = path_exec
Expand Down

0 comments on commit 625e050

Please sign in to comment.