Skip to content

Commit

Permalink
test: do not save errors to a global and instead create an error_prin…
Browse files Browse the repository at this point in the history
…ter object
  • Loading branch information
JaDogg committed Mar 2, 2024
1 parent 0ea78d2 commit c939f4f
Show file tree
Hide file tree
Showing 24 changed files with 213 additions and 190 deletions.
28 changes: 16 additions & 12 deletions compiler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ include_directories(${REPROC_DIR}/reproc++/include)
set(REPROC++ ON)
add_subdirectory(${REPROC_DIR})

## Enable below to use address sanitizer
# add_compile_options(-O1 -g -fsanitize=address -fno-omit-frame-pointer)
# add_link_options(-g -fsanitize=address)
if(DEFINED ENV{YAKSHA_ADDRESS_SANITIZER})
add_compile_options(-O1 -g -fsanitize=address -fno-omit-frame-pointer)
add_link_options(-g -fsanitize=address)
endif()

## Enable below to use undefined behaviour sanitizer
# add_compile_options(-O1 -g -fsanitize=undefined -fsanitize=integer -fsanitize=nullability -fno-omit-frame-pointer)
# add_link_options(-g -fsanitize=undefined -fsanitize=integer -fsanitize=nullability)
if(DEFINED ENV{YAKSHA_UNDEFINED_SANITIZER})
add_compile_options(-O1 -g -fsanitize=undefined -fsanitize=integer -fsanitize=nullability -fno-omit-frame-pointer)
add_link_options(-g -fsanitize=undefined -fsanitize=integer -fsanitize=nullability)
endif()

set(YAKSHA_SOURCE_FILES
src/ast/ast.h src/ast/ast_printer.h src/ast/ast_vis.h src/ast/codefiles.h src/ast/environment.h src/ast/environment_stack.h src/ast/parser.h src/builtins/builtin.h src/builtins/builtins.h src/compiler/codegen.h src/compiler/codegen_c.h src/compiler/codegen_json.h src/compiler/comp_result.h src/compiler/compiler_utils.h src/compiler/const_fold.h src/compiler/datatype_compiler.h src/compiler/datatype_parser.h src/compiler/def_class_visitor.h src/compiler/delete_stack.h src/compiler/delete_stack_stack.h src/compiler/desugaring_compiler.h src/compiler/entry_struct_func_compiler.h src/compiler/function_datatype_extractor.h src/compiler/literal_utils.h src/compiler/multifile_compiler.h src/compiler/return_checker.h src/compiler/slot_matcher.h src/compiler/statement_writer.h src/compiler/to_c_compiler.h src/compiler/type_checker.h src/compiler/usage_analyser.h src/file_formats/ic_tokens_file.h src/file_formats/tokens_file.h src/ic2c/ic2c.h src/ic2c/ic_ast.h src/ic2c/ic_compiler.h src/ic2c/ic_level2_parser.h src/ic2c/ic_level2_tokenizer.h src/ic2c/ic_line_splicer.h src/ic2c/ic_optimizer.h src/ic2c/ic_parser.h src/ic2c/ic_peek_ahead_iter.h src/ic2c/ic_preprocessor.h src/ic2c/ic_simple_character_iter.h src/ic2c/ic_token.h src/ic2c/ic_tokenizer.h src/ic2c/ic_trigraph_translater.h src/tokenizer/block_analyzer.h src/tokenizer/string_utils.h src/tokenizer/token.h src/tokenizer/tokenizer.h src/utilities/annotation.h src/utilities/annotations.h src/utilities/argparser.h src/utilities/colours.h src/utilities/cpp_util.h src/utilities/defer_stack.h src/utilities/defer_stack_stack.h src/utilities/error_printer.h src/utilities/gc_pool.h src/utilities/human_id.h src/utilities/ykdatatype.h src/utilities/ykdt_pool.h src/utilities/ykobject.h src/yaksha_lisp/macro_processor.h src/yaksha_lisp/prelude.h src/yaksha_lisp/yaksha_lisp.h src/yaksha_lisp/yaksha_lisp_builtins.h src/ast/ast.cpp src/ast/ast_printer.cpp src/ast/ast_vis.cpp src/ast/codefiles.cpp src/ast/environment.cpp src/ast/environment_stack.cpp src/ast/parser.cpp src/builtins/builtins.cpp src/compiler/codegen_c.cpp src/compiler/codegen_json.cpp src/compiler/compiler_utils.cpp src/compiler/const_fold.cpp src/compiler/def_class_visitor.cpp src/compiler/delete_stack.cpp src/compiler/delete_stack_stack.cpp src/compiler/desugaring_compiler.cpp src/compiler/entry_struct_func_compiler.cpp src/compiler/literal_utils.cpp src/compiler/multifile_compiler.cpp src/compiler/return_checker.cpp src/compiler/to_c_compiler.cpp src/compiler/type_checker.cpp src/compiler/usage_analyser.cpp src/file_formats/ic_tokens_file.cpp src/file_formats/tokens_file.cpp src/ic2c/ic2c.cpp src/ic2c/ic_ast.cpp src/ic2c/ic_compiler.cpp src/ic2c/ic_level2_parser.cpp src/ic2c/ic_level2_tokenizer.cpp src/ic2c/ic_line_splicer.cpp src/ic2c/ic_optimizer.cpp src/ic2c/ic_parser.cpp src/ic2c/ic_peek_ahead_iter.cpp src/ic2c/ic_preprocessor.cpp src/ic2c/ic_tokenizer.cpp src/ic2c/ic_trigraph_translater.cpp src/tokenizer/block_analyzer.cpp src/tokenizer/string_utils.cpp src/tokenizer/tokenizer.cpp src/utilities/annotation.cpp src/utilities/annotations.cpp src/utilities/colours.cpp src/utilities/cpp_util.cpp src/utilities/defer_stack.cpp src/utilities/defer_stack_stack.cpp src/utilities/error_printer.cpp src/utilities/human_id.cpp src/utilities/ykdatatype.cpp src/utilities/ykdt_pool.cpp src/utilities/ykobject.cpp src/yaksha_lisp/yaksha_lisp.cpp src/yaksha_lisp/yaksha_lisp_builtins.cpp) # update_makefile.py SRC
Expand Down Expand Up @@ -70,8 +72,8 @@ target_compile_features(yakshadmp PRIVATE cxx_std_17)

# -- ykreload - compiler --
if(MSVC)
add_executable(ykreload src/reloader.cpp)
target_compile_features(yakshac PRIVATE cxx_std_17)
add_executable(ykreload src/reloader.cpp)
target_compile_features(yakshac PRIVATE cxx_std_17)
endif (MSVC)

# -- yaksha binary --
Expand Down Expand Up @@ -100,14 +102,16 @@ catch_discover_tests(YakshaTests)

# Adding Fuzzer Binary
# ---------------------
add_executable(YakshaFuzz ${YAKSHA_SOURCE_FILES} tests/fuzz_main.cpp runtime/whereami.c)
if(DEFINED ENV{YAKSHA_FUZZ})
set(CMAKE_CXX_FLAGS "-fsanitize=fuzzer,address -g -O1")
add_executable(YakshaFuzz ${YAKSHA_SOURCE_FILES} tests/fuzz_main.cpp runtime/whereami.c)
target_compile_definitions(YakshaFuzz PUBLIC YAKSHA_LLVM_FUZZ)
target_link_libraries(YakshaFuzz PUBLIC ${SYS_LIBS})
target_link_libraries(YakshaFuzz PUBLIC reproc++)
target_compile_features(YakshaFuzz PRIVATE cxx_std_17)
endif()
target_compile_definitions(YakshaFuzz PUBLIC TESTING)
target_link_libraries(YakshaFuzz PUBLIC ${SYS_LIBS})
target_link_libraries(YakshaFuzz PUBLIC reproc++)
target_compile_features(YakshaFuzz PRIVATE cxx_std_17)


# CMake Build for carpntr
add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
Expand Down
14 changes: 3 additions & 11 deletions compiler/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
FROM ubuntu:latest
ENV DEBIAN_FRONTEND=noninteractive
ARG CMAKE_VERSION=3.22.0
ARG ZIG_VERSION=0.9.1
RUN apt-get update && apt-get -y -q install clang-12 clang-tools git wget build-essential python3 libssl-dev lcov gcovr dos2unix

RUN wget https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/cmake-${CMAKE_VERSION}.tar.gz \
&& tar -zxf cmake-${CMAKE_VERSION}.tar.gz \
&& cd cmake-${CMAKE_VERSION} \
&& ./bootstrap \
&& make -j4 \
&& make install

RUN apt-get update && apt-get -y -q install clang-12 clang-tools git wget build-essential python3 libssl-dev lcov gcovr dos2unix cmake ninja-build

RUN ln -s /usr/bin/clang-12 /usr/bin/clang \
&& ln -s /usr/bin/clang++-12 /usr/bin/clang++


RUN apt-get -y -q install python3-pip && pip3 install tqdm

RUN wget https://ziglang.org/download/0.9.1/zig-linux-x86_64-${ZIG_VERSION}.tar.xz \
Expand All @@ -36,5 +26,7 @@ ADD carpntr /app/carpntr
ADD test_libs /app/test_libs
ADD bin/test.txt /app/test.txt
RUN mkdir "/app/bin"
RUN dos2unix /app/scripts/*.py
RUN dos2unix /app/scripts/*.sh

ENTRYPOINT ["/app/scripts/code-coverage.sh"]
15 changes: 9 additions & 6 deletions compiler/scripts/code-coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,31 @@ mkdir build
mkdir bin
mkdir bin/fuzz
cp /app/test.txt bin/test.txt
find . -type f -exec dos2unix {} \;
set -e
cd build || exit 1
# Build project
cmake -S .. -B . -DCMAKE_CXX_FLAGS="-fprofile-arcs -ftest-coverage -fPIC -O0 -fsanitize=address -static-libasan -g"
cmake -G Ninja -S .. -B . -DCMAKE_CXX_FLAGS="-fprofile-arcs -ftest-coverage -fPIC -O0 -fsanitize=address -static-libasan -g"
cmake --build . -- -j 24
# Fix test data by converting them all to unix format
cd ../test_data || exit 1
dos2unix *
cd ../bin || exit 1
# Run test cases
echo "██ ██ ███ ██ ██ ████████ ████████ ███████ ███████ ████████"
echo "██ ██ ████ ██ ██ ██ ██ ██ ██ ██"
echo "██ ██ ██ ██ ██ ██ ██ ██ █████ ███████ ██"
echo "██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██"
echo " ██████ ██ ████ ██ ██ ██ ███████ ███████ ██"
./YakshaTests
ninja test
# Build carpntr
cd ../carpntr
rm -rf build
mkdir build
python3 bootstrap_me.py
# Disable exit on error so we can run the rest of the commands
set +e
cd ../scripts
# Run e2e tests
python3 e2e.py
# Run yaksha ast
../bin/yaksha ast ../carpntr/main.yaka
# Run libdocs.py
mkdir ../comp_output_test
python3 libdocs.py
Expand Down
27 changes: 15 additions & 12 deletions compiler/scripts/kabaraya.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,36 @@
# Note: libs - MIT license, runtime/3rd - various
# ==============================================================================================
# GPLv3:
#
#
# Yaksha - Programming Language.
# Copyright (C) 2020 - 2024 Bhathiya Perera
#
#
# This program is free software: you can redistribute it and/or modify it under the terms
# of the GNU General Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
#
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
#
# You should have received a copy of the GNU General Public License along with this program.
# If not, see https://www.gnu.org/licenses/.
#
#
# ==============================================================================================
# Additional Terms:
#
#
# Please note that any commercial use of the programming language's compiler source code
# (everything except compiler/runtime, compiler/libs and compiler/3rd) require a written agreement
# with author of the language (Bhathiya Perera).
#
#
# If you are using it for an open source project, please give credits.
# Your own project must use GPLv3 license with these additional terms.
#
#
# You may use programs written in Yaksha/YakshaLisp for any legal purpose
# (commercial, open-source, closed-source, etc) as long as it agrees
# to the licenses of linked runtime libraries (see compiler/runtime/README.md).
#
#
# ==============================================================================================
# !/usr/bin/env python
"""
Expand Down Expand Up @@ -121,9 +121,10 @@ def fail(text):
"assert", "del", "not",
"elif", "if", "or", "await", "except", "raise",
"finally", "lambda", "nonlocal", "global", "with", "async",
"yield", "in", "is"]
"yield", "in", "is", "macros!", "ccode",
"Const", "Array", "FixedArr"]

OPERATORS = "+ - * ** / // % @ << >> & | ^ ~ := < > <= >= == != ( ) " \
OPERATORS = "! + - * ** / // % @ << >> & | ^ ~ := < > <= >= == != ( ) " \
"[ ] { } , : . ; @ = -> += -= *= /= //= %= @= &= |= ^= >>= <<= **=".split(" ")

INPUT_DATA = []
Expand Down Expand Up @@ -291,7 +292,9 @@ def add_random_indent(input_text: List[str], _: List[List[str]]) -> List[str]:


def execute(arg: str):
args = [os.path.abspath(BINARY), os.path.abspath(arg)]
return execute_item(arg, [os.path.abspath(BINARY), os.path.abspath(arg)])

def execute_item(arg: str, args: List[str]):
fuzz_process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8",
universal_newlines=True)
so, se = "", ""
Expand Down
10 changes: 5 additions & 5 deletions compiler/src/ast/codefiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
#include "utilities/error_printer.h"
#include <optional>
using namespace yaksha;
codefiles::codefiles(std::filesystem::path &libs_path)
codefiles::codefiles(std::filesystem::path &libs_path, errors::error_printer *ep)
: current_path_(std::filesystem::current_path()), prefixes_(),
path_to_fi_(), libs_path_{libs_path}, pool_() {
path_to_fi_(), libs_path_{libs_path}, pool_(), ep_{ep} {
esc_ = new entry_struct_func_compiler(&pool_);
}
codefiles::~codefiles() {
Expand Down Expand Up @@ -217,22 +217,22 @@ file_data *codefiles::parse_or_null(const std::string &data,
auto *t = new tokenizer{file_name, data, yaksha_macros_.get_yk_token_pool()};
t->tokenize();
if (!t->errors_.empty()) {
errors::print_errors(t->errors_);
ep_->print_errors(t->errors_);
delete (t);
return nullptr;
}
auto *b = new block_analyzer{t->tokens_, yaksha_macros_.get_yk_token_pool()};
b->analyze();
if (!b->errors_.empty()) {
errors::print_errors(b->errors_);
ep_->print_errors(b->errors_);
delete (t);
delete (b);
return nullptr;
}
auto *p = new parser(file_name, b->tokens_, &pool_);
p->step_1_parse_token_soup();
if (!b->errors_.empty()) {
errors::print_errors(b->errors_);
ep_->print_errors(b->errors_);
delete (t);
delete (b);
delete (p);
Expand Down
4 changes: 3 additions & 1 deletion compiler/src/ast/codefiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "ast/ast.h"
#include "compiler/entry_struct_func_compiler.h"
#include "tokenizer/tokenizer.h"
#include "utilities/error_printer.h"
#include "utilities/ykdt_pool.h"
#include "yaksha_lisp/yaksha_lisp.h"
#include <filesystem>
Expand All @@ -52,7 +53,7 @@
#include <vector>
namespace yaksha {
struct codefiles {
explicit codefiles(std::filesystem::path &libs_path);
codefiles(std::filesystem::path &libs_path, errors::error_printer *ep);
~codefiles();
file_info *get_or_null(const std::string &f);
file_info *initialize_parsing_or_null(const std::string &filename);
Expand Down Expand Up @@ -81,6 +82,7 @@ namespace yaksha {
std::filesystem::path libs_path_{};
std::unordered_map<std::intptr_t, std::filesystem::path>
import_to_path_cache_{};
errors::error_printer *ep_{nullptr};
};
}// namespace yaksha
#endif
4 changes: 2 additions & 2 deletions compiler/src/ast_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}
comp_result result;
multifile_compiler mc{};
try {
multifile_compiler mc{};
mc.main_required_ = check_main.is_set_;
mc.check_types_ = check_types.is_set_;
mc.usage_analysis_ = false; // disable usage analysis as JSON will dump all
Expand All @@ -77,7 +77,7 @@ int main(int argc, char *argv[]) {
result = mc.compile(code.value_, lib.value_, &cg);
}
if (result.failed_) { return EXIT_FAILURE; }
} catch (parsing_error &e) { errors::print_errors({e}); }
} catch (parsing_error &e) { mc.error_printer_.print_error(std::cerr, e); }
std::cout << result.code_;
return EXIT_SUCCESS;
}
10 changes: 6 additions & 4 deletions compiler/src/comp_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@
#include "utilities/argparser.h"
using namespace yaksha;
int main(int argc, char *argv[]) {
auto args = argparser::ARGS(PROGRAM_NAME, "Compile Yaksha code to C code", "");
auto args =
argparser::ARGS(PROGRAM_NAME, "Compile Yaksha code to C code", "");
auto help = argparser::OP_BOOL('h', "--help", "Print this help message");
auto no_main = argparser::OP_BOOL('N', "--no-main", "Disable main() check");
args.optional_ = {&help, &no_main};
auto code = argparser::PO("mainfile.yaka", "Yaksha code file.");
auto lib = argparser::PO_OPT("[LIBS_PARENT_PATH]", "Path to the parent directory of the libraries");
auto lib = argparser::PO_OPT("[LIBS_PARENT_PATH]",
"Path to the parent directory of the libraries");
args.positional_ = {&code, &lib};
argparser::parse_args(argc, argv, args);
if (help.is_set_) {
Expand All @@ -64,8 +66,8 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}
comp_result result;
multifile_compiler mc{};
try {
multifile_compiler mc{};
mc.main_required_ = !no_main.is_set_;
codegen_c cg{};
if (!lib.is_set_) {// Just code.yaka is passed
Expand All @@ -74,7 +76,7 @@ int main(int argc, char *argv[]) {
result = mc.compile(code.value_, lib.value_, &cg);
}
if (result.failed_) { return EXIT_FAILURE; }
} catch (parsing_error &e) { errors::print_errors({e}); }
} catch (parsing_error &e) { mc.error_printer_.print_errors({e}); }
std::cout << result.code_;
return EXIT_SUCCESS;
}
21 changes: 11 additions & 10 deletions compiler/src/compiler/codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,49 @@
// Note: libs - MIT license, runtime/3rd - various
// ==============================================================================================
// GPLv3:
//
//
// Yaksha - Programming Language.
// Copyright (C) 2020 - 2024 Bhathiya Perera
//
//
// This program is free software: you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
//
// You should have received a copy of the GNU General Public License along with this program.
// If not, see https://www.gnu.org/licenses/.
//
//
// ==============================================================================================
// Additional Terms:
//
//
// Please note that any commercial use of the programming language's compiler source code
// (everything except compiler/runtime, compiler/libs and compiler/3rd) require a written agreement
// with author of the language (Bhathiya Perera).
//
//
// If you are using it for an open source project, please give credits.
// Your own project must use GPLv3 license with these additional terms.
//
//
// You may use programs written in Yaksha/YakshaLisp for any legal purpose
// (commercial, open-source, closed-source, etc) as long as it agrees
// to the licenses of linked runtime libraries (see compiler/runtime/README.md).
//
//
// ==============================================================================================
// codegen.h
#ifndef CODEGEN_H
#define CODEGEN_H
#include "utilities/error_printer.h"
#include "utilities/gc_pool.h"
namespace yaksha {
struct comp_result;
struct codefiles;
struct token;
struct codegen {
virtual ~codegen() = default;
virtual comp_result emit(codefiles *cf, gc_pool<token> *token_pool) = 0;
virtual comp_result emit(codefiles *cf, gc_pool<token> *token_pool, errors::error_printer* ep) = 0;

private:
};
Expand Down
Loading

0 comments on commit c939f4f

Please sign in to comment.