Skip to content

Commit

Permalink
Merge pull request #286 from andrey-terekhov/status-codes
Browse files Browse the repository at this point in the history
Status codes
  • Loading branch information
Victor-Y-Fadeev authored Apr 1, 2022
2 parents 28b620a + c6485dc commit 950cc88
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 63 deletions.
76 changes: 40 additions & 36 deletions libs/compiler/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,57 +58,60 @@ static inline void make_executable(const char *const path)
#endif
}


static int compile_from_io(const workspace *const ws, universal_io *const io, const encoder enc)
/** Skip linker stage */
static inline bool skip_linker(const workspace *const ws)
{
if (!in_is_correct(io) || !out_is_correct(io))
{
error_msg("некорректные параметры ввода/вывода");
io_erase(io);
return -1;
}

syntax sx = sx_create(ws, io);
int ret = parse(&sx);

bool check_predef = true;
for (size_t i = 0; ; i++)
{
const char *flag = ws_get_flag(ws, i);

if (flag == NULL)
{
break;
return false;
}

if (strcmp(flag, "-c") == 0)
else if (strcmp(flag, "-c") == 0)
{
check_predef = false;
break;
return true;
}
}
}

if (!ret)

static status_t compile_from_io(const workspace *const ws, universal_io *const io, const encoder enc)
{
if (!in_is_correct(io) || !out_is_correct(io))
{
error_msg("некорректные параметры ввода/вывода");
io_erase(io);
return sts_system_error;
}

syntax sx = sx_create(ws, io);
int ret = parse(&sx);
status_t sts = sts_parse_error;

if (!ret && !skip_linker(ws))
{
ret = !sx_is_correct(&sx, check_predef);
ret = !sx_is_correct(&sx);
sts = sts_link_error;
}

if (!ret)
{
ret = enc(ws, &sx);
sts = sts_codegen_error;
}

sx_clear(&sx);
io_erase(io);
return ret;
return ret ? sts : sts_success;
}

static int compile_from_ws(workspace *const ws, const encoder enc)
static status_t compile_from_ws(workspace *const ws, const encoder enc)
{
if (!ws_is_correct(ws) || ws_get_files_num(ws) == 0)
{
error_msg("некорректные входные данные");
return -1;
return sts_system_error;
}

universal_io io = io_create();
Expand All @@ -118,27 +121,27 @@ static int compile_from_ws(workspace *const ws, const encoder enc)
char *const preprocessing = macro(ws); // макрогенерация
if (preprocessing == NULL)
{
return -1;
return sts_macro_error;
}

in_set_buffer(&io, preprocessing);
#else
int ret_macro = macro_to_file(ws, DEFAULT_MACRO);
if (ret_macro)
{
return ret_macro;
return sts_macro_error;
}

in_set_file(&io, DEFAULT_MACRO);
#endif

out_set_file(&io, ws_get_output(ws));
const int ret = compile_from_io(ws, &io, enc);
const status_t sts = compile_from_io(ws, &io, enc);

#ifndef GENERATE_MACRO
free(preprocessing);
#endif
return ret;
return sts;
}


Expand All @@ -151,7 +154,7 @@ static int compile_from_ws(workspace *const ws, const encoder enc)
*/


int compile(workspace *const ws)
status_t compile(workspace *const ws)
{
for (size_t i = 0; ; i++)
{
Expand All @@ -168,30 +171,31 @@ int compile(workspace *const ws)
}
}

int compile_to_vm(workspace *const ws)
status_t compile_to_vm(workspace *const ws)
{
if (ws_get_output(ws) == NULL)
{
ws_set_output(ws, DEFAULT_VM);
}

const int ret = compile_from_ws(ws, &encode_to_vm);
if (!ret)
const status_t sts = compile_from_ws(ws, &encode_to_vm);
if (sts == sts_success)
{
make_executable(ws_get_output(ws));
}

return ret;
return sts == sts_codegen_error ? sts_virtul_error : sts;
}

int compile_to_llvm(workspace *const ws)
status_t compile_to_llvm(workspace *const ws)
{
if (ws_get_output(ws) == NULL)
{
ws_set_output(ws, DEFAULT_LLVM);
}

return compile_from_ws(ws, &encode_to_llvm);
const status_t sts = compile_from_ws(ws, &encode_to_llvm);
return sts == sts_codegen_error ? sts_llvm_error : sts;
}


Expand All @@ -215,7 +219,7 @@ int auto_compile_to_vm(const int argc, const char *const *const argv)
int auto_compile_to_llvm(const int argc, const char *const *const argv)
{
workspace ws = ws_parse_args(argc, argv);
const int ret = compile_to_llvm(&ws);
const status_t ret = compile_to_llvm(&ws);
ws_clear(&ws);
return ret;
}
Expand Down
23 changes: 20 additions & 3 deletions libs/compiler/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,31 @@
extern "C" {
#endif

/** Status codes */
typedef enum STATUS
{
sts_success = 0, /**< Success code */
sts_system_error, /**< System error code */
sts_test_error = 64, /**< Reserved testing system code */
sts_macro_error, /**< Preprocessor error code */
sts_parse_error, /**< Parser error code */
sts_link_error, /**< Linker error code */
sts_optimize_error, /**< Optimization error code */
sts_codegen_error, /**< Default code generator error code */
sts_virtul_error, /**< Virtual Machine generator error code */
sts_llvm_error, /**< LLVM generator error code */
sts_mips_error, /**< MIPS generator error code */
} status_t;


/**
* Compile code from workspace
*
* @param ws Compiler workspace
*
* @return Status code
*/
EXPORTED int compile(workspace *const ws);
EXPORTED status_t compile(workspace *const ws);

/**
* Compile RuC virtual machine code from workspace
Expand All @@ -43,7 +60,7 @@ EXPORTED int compile(workspace *const ws);
*
* @return Status code
*/
EXPORTED int compile_to_vm(workspace *const ws);
EXPORTED status_t compile_to_vm(workspace *const ws);

/**
* Compile LLVM code from workspace
Expand All @@ -52,7 +69,7 @@ EXPORTED int compile_to_vm(workspace *const ws);
*
* @return Status code
*/
EXPORTED int compile_to_llvm(workspace *const ws);
EXPORTED status_t compile_to_llvm(workspace *const ws);


/**
Expand Down
4 changes: 2 additions & 2 deletions libs/compiler/syntax.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,9 @@ syntax sx_create(const workspace *const ws, universal_io *const io)
return sx;
}

bool sx_is_correct(syntax *const sx, const bool check_predef)
bool sx_is_correct(syntax *const sx)
{
if (reporter_get_errors_number(&sx->rprt) || !check_predef)
if (reporter_get_errors_number(&sx->rprt))
{
return true;
}
Expand Down
15 changes: 7 additions & 8 deletions libs/compiler/syntax.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,10 @@ syntax sx_create(const workspace *const ws, universal_io *const io);
* Check if syntax structure is correct
*
* @param sx Syntax structure
* @param check_predef Set if needed to check function definitions
*
* @return @c 1 on true, @c 0 on false
*/
bool sx_is_correct(syntax *const sx, const bool check_predef);
bool sx_is_correct(syntax *const sx);

/**
* Free allocated memory
Expand Down Expand Up @@ -147,20 +146,20 @@ size_t string_add(syntax *const sx, const vector *const str);
const char* string_get(const syntax *const sx, const size_t index);

/**
* Get length of a string
* Get length of a string
*
* @param sx Syntax structure
* @param sx Syntax structure
*
* @return Length of a string
* @return Length of a string
*/
size_t strings_length(const syntax *const sx, const size_t index);

/**
* Get amount of strings
* Get amount of strings
*
* @param sx Syntax structure
* @param sx Syntax structure
*
* @return Amount of strings
* @return Amount of strings
*/
size_t strings_amount(const syntax *const sx);

Expand Down
36 changes: 32 additions & 4 deletions scripts/llvm-baikal.patch
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
--- test.sh 2022-03-09 20:38:58.473520400 +0300
+++ llvm.sh 2022-03-09 20:40:27.502109200 +0300
--- test.sh 2022-04-01 17:47:11.615209900 +0300
+++ llvm.sh 2022-04-01 17:50:50.542879300 +0300
@@ -3,7 +3,9 @@
init()
{
exit_code=1
exit_code=64
- vm_exec=export.txt
+ vm_exec=export.ll
+ llvm_exec=export
Expand Down Expand Up @@ -40,7 +40,35 @@

case $? in
0)
@@ -379,9 +379,9 @@
@@ -292,7 +292,7 @@
message_timeout
let timeout++
;;
- $exit_code)
+ *)
if [[ $path == */$subdir_error/* ]] ; then
if [[ $build_type == "(Debug)" ]] ; then
build_type=""
@@ -312,18 +312,6 @@
fi
fi
;;
- *)
- # Segmentation fault
- # Double free or corruption (!prev)
- # Etcetera
-
- message_failure
- let failure++
-
- if ! [[ -z $debug ]] ; then
- cat $log
- fi
- ;;
esac
else
let success++
@@ -379,9 +367,9 @@
compiling()
{
if [[ -z $ignore || $path != $dir_lexing/* || $path != $dir_preprocessor/* || $path != $dir_semantics/*
Expand Down
36 changes: 32 additions & 4 deletions scripts/llvm-uemu.patch
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
--- test.sh 2022-03-09 20:38:58.473520400 +0300
+++ llvm.sh 2022-03-09 20:38:42.886707100 +0300
--- test.sh 2022-04-01 17:47:11.615209900 +0300
+++ llvm.sh 2022-04-01 17:49:55.186151400 +0300
@@ -3,7 +3,9 @@
init()
{
exit_code=1
exit_code=64
- vm_exec=export.txt
+ vm_exec=export.ll
+ llvm_exec=export
Expand Down Expand Up @@ -40,7 +40,35 @@

case $? in
0)
@@ -379,9 +379,9 @@
@@ -292,7 +292,7 @@
message_timeout
let timeout++
;;
- $exit_code)
+ *)
if [[ $path == */$subdir_error/* ]] ; then
if [[ $build_type == "(Debug)" ]] ; then
build_type=""
@@ -312,18 +312,6 @@
fi
fi
;;
- *)
- # Segmentation fault
- # Double free or corruption (!prev)
- # Etcetera
-
- message_failure
- let failure++
-
- if ! [[ -z $debug ]] ; then
- cat $log
- fi
- ;;
esac
else
let success++
@@ -379,9 +367,9 @@
compiling()
{
if [[ -z $ignore || $path != $dir_lexing/* || $path != $dir_preprocessor/* || $path != $dir_semantics/*
Expand Down
Loading

0 comments on commit 950cc88

Please sign in to comment.