Add command-line argument & auto testing
Signed-off-by: szdytom <szdytom@qq.com>
This commit is contained in:
parent
e8f85bd3dc
commit
14bc07ec9c
13
.gitignore
vendored
13
.gitignore
vendored
@ -32,3 +32,16 @@
|
|||||||
*.out
|
*.out
|
||||||
*.app
|
*.app
|
||||||
|
|
||||||
|
# Xmake build
|
||||||
|
.xmake/
|
||||||
|
build/
|
||||||
|
CMakeList.txt
|
||||||
|
acpa
|
||||||
|
|
||||||
|
# --> Editors
|
||||||
|
# Vim
|
||||||
|
*.swp
|
||||||
|
|
||||||
|
# VS Code
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
31
NOTICE
Normal file
31
NOTICE
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
Licenses for third party components used by this project can be found below.
|
||||||
|
|
||||||
|
================================================================================
|
||||||
|
The MIT License applies to:
|
||||||
|
|
||||||
|
* argparse in `third-party/argparse/argparse.hpp`
|
||||||
|
Copyright (c) 2019-2022 Pranav Srinivas Kumar <pranav.srinivas.kumar@gmail.com>
|
||||||
|
and other contributors.
|
||||||
|
in GitHub repository p-ranav/argparse (https://github.com/p-ranav/argparse)
|
||||||
|
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
================================================================================
|
||||||
|
|
52
scripts/check.py
Normal file
52
scripts/check.py
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
|
||||||
|
class Color:
|
||||||
|
GREEN = '\033[92m'
|
||||||
|
RED = '\033[91m'
|
||||||
|
RESET = '\033[0m'
|
||||||
|
|
||||||
|
def test_compiler(test_dir, should_fail):
|
||||||
|
total_files = 0
|
||||||
|
passed_files = 0
|
||||||
|
has_failed_tests = False
|
||||||
|
|
||||||
|
print(f'开始测试 {test_dir}:')
|
||||||
|
for file in os.listdir(test_dir):
|
||||||
|
if file.endswith('.ac'):
|
||||||
|
total_files += 1
|
||||||
|
file_path = os.path.join(test_dir, file)
|
||||||
|
|
||||||
|
start_time = time.time()
|
||||||
|
result = subprocess.run(['./acpa', file_path], capture_output=True, text=True)
|
||||||
|
elapsed_time = time.time() - start_time
|
||||||
|
|
||||||
|
if should_fail:
|
||||||
|
if 'error' not in result.stdout:
|
||||||
|
print(f' {Color.RED}测试失败:{Color.RESET} {file_path} 应该编译失败,但是没有发现错误')
|
||||||
|
has_failed_tests = True
|
||||||
|
else:
|
||||||
|
print(f' {Color.GREEN}测试通过:{Color.RESET} {file_path}')
|
||||||
|
passed_files += 1
|
||||||
|
else:
|
||||||
|
if 'error' in result.stdout:
|
||||||
|
print(f' {Color.RED}测试失败:{Color.RESET} {file_path} 应该编译通过,但是发现错误: {result.stderr}')
|
||||||
|
has_failed_tests = True
|
||||||
|
else:
|
||||||
|
print(f' {Color.GREEN}测试通过:{Color.RESET} {file_path}')
|
||||||
|
passed_files += 1
|
||||||
|
|
||||||
|
print(f' - 编译时间: {elapsed_time:.3f} 秒')
|
||||||
|
|
||||||
|
print(f'在 {test_dir} 中共有 {total_files} 个测试文件,通过了 {passed_files} 个。\n')
|
||||||
|
return has_failed_tests
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
has_failed_tests = test_compiler('tests/ok/', should_fail=False)
|
||||||
|
has_failed_tests |= test_compiler('tests/fail/', should_fail=True)
|
||||||
|
|
||||||
|
if has_failed_tests:
|
||||||
|
sys.exit(1)
|
||||||
|
|
22
src/main.cpp
22
src/main.cpp
@ -1,5 +1,5 @@
|
|||||||
#include <bits/stdc++.h>
|
#include <bits/stdc++.h>
|
||||||
|
#include <argparse/argparse.hpp>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
enum class TokenType {
|
enum class TokenType {
|
||||||
@ -817,11 +817,25 @@ void work() {
|
|||||||
createVar();
|
createVar();
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main(int argc, char *argv[]) {
|
||||||
freopen("test.txt", "r", stdin);
|
argparse::ArgumentParser program("acpa", APP_VERSION
|
||||||
|
, argparse::default_arguments::help, false);
|
||||||
|
program.add_argument("input_file")
|
||||||
|
.help("Source proof file")
|
||||||
|
.action([](const std::string &value) { return value; });
|
||||||
|
try {
|
||||||
|
program.parse_args(argc, argv);
|
||||||
|
} catch (const std::runtime_error &err) {
|
||||||
|
cerr << err.what() << std::endl;
|
||||||
|
cerr << program;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto input_file = program.get<std::string>("input_file");
|
||||||
|
freopen(input_file.c_str(), "r", stdin);
|
||||||
|
|
||||||
read();
|
read();
|
||||||
printTokens();
|
printTokens();
|
||||||
// return 0;
|
|
||||||
work();
|
work();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
4
tests/fail/lex-missing-semi.ac
Normal file
4
tests/fail/lex-missing-semi.ac
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
struct {
|
||||||
|
|
||||||
|
} main
|
||||||
|
|
8
tests/fail/lex-missing-semi2.ac
Normal file
8
tests/fail/lex-missing-semi2.ac
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
struct {
|
||||||
|
|
||||||
|
T1 = Fn<A>(h: A) -> A {
|
||||||
|
return h;
|
||||||
|
}
|
||||||
|
|
||||||
|
} main;
|
||||||
|
|
6
tests/fail/logic-no-return.ac
Normal file
6
tests/fail/logic-no-return.ac
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
struct {
|
||||||
|
|
||||||
|
T1 = Fn<A>() -> A {
|
||||||
|
};
|
||||||
|
|
||||||
|
} main;
|
7
tests/ok/simple-logic1.ac
Normal file
7
tests/ok/simple-logic1.ac
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
struct {
|
||||||
|
|
||||||
|
T1 = Fn<A>(h: A) -> A {
|
||||||
|
return h;
|
||||||
|
};
|
||||||
|
|
||||||
|
} main;
|
8
tests/ok/simple-logic2.ac
Normal file
8
tests/ok/simple-logic2.ac
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
struct {
|
||||||
|
|
||||||
|
T1 = Fn<A, B>(h1: A, h2: B) -> A {
|
||||||
|
return h1;
|
||||||
|
};
|
||||||
|
|
||||||
|
} main;
|
||||||
|
|
1727
third-party/argparse/argparse.hpp
vendored
Normal file
1727
third-party/argparse/argparse.hpp
vendored
Normal file
File diff suppressed because it is too large
Load Diff
27
xmake.lua
Normal file
27
xmake.lua
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
set_project("acpa")
|
||||||
|
app_version = "0.1a0"
|
||||||
|
set_version(app_version)
|
||||||
|
set_basename("acpa")
|
||||||
|
set_languages("c++17")
|
||||||
|
set_targetdir(".")
|
||||||
|
|
||||||
|
set_policy("build.warning", true)
|
||||||
|
|
||||||
|
target("build")
|
||||||
|
set_kind("binary")
|
||||||
|
set_default(true)
|
||||||
|
set_warnings("allextra")
|
||||||
|
add_files("src/**.cpp")
|
||||||
|
-- add_includedirs("include/")
|
||||||
|
add_includedirs("third-party/")
|
||||||
|
add_defines("APP_VERSION=\"" .. app_version .. "\"")
|
||||||
|
if is_mode("release") then
|
||||||
|
set_strip("all")
|
||||||
|
set_optimize("faster")
|
||||||
|
end
|
||||||
|
|
||||||
|
if is_mode("debug") then
|
||||||
|
set_optimize("none")
|
||||||
|
set_symbols("debug")
|
||||||
|
add_defines("DEBUG")
|
||||||
|
end
|
Loading…
x
Reference in New Issue
Block a user