Skip to content

Commit

Permalink
merge dev
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed Jan 28, 2024
2 parents 04c65c5 + 3c726e5 commit 28c6ca0
Show file tree
Hide file tree
Showing 203 changed files with 169 additions and 397 deletions.
45 changes: 11 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
# Trc
# Trc programming language

## Trc is a stack-based programming language

Trc's syntax is easy,and have full toolchain. It is easy to learn modern c++ and compiler,too.

## language

100% C++

Using c++ 20 standard and use module feature

But here is a developing ```rust``` instance now!

[rust_trc](rust/)
Trc is a easy-learn programming language.It can be fast,safe and effective.

## Goal

Expand All @@ -22,22 +10,16 @@ But here is a developing ```rust``` instance now!

**as small as lua**

## project website

[Gitee](https://gitee.com/li-muyangangel/trc.git)
[GitHub](https://github.com/limuy2022/trc.git)
there are two ways to use it:

## All docs reference
[c++ version](./cpp/)
[rust version](./rust/)

[All docs](doc)

## How to compile

[Compilation guide](doc/developer/build.md)
They have their own adventages.You can choose by your preference.

## International

trc support many languages,the way to change language is that uses the language's dynamic linked library to replace the original language library
Trc supports many different kinds of languages.We use GNU gettext.So it dont't need change the language by hand.

## Simple examples

Expand Down Expand Up @@ -73,7 +55,10 @@ del "c"
println(a * b)
```

[more example](tests/black_test/program)
## project website

[Gitee](https://gitee.com/li-muyangangel/trc.git)
[GitHub](https://github.com/limuy2022/trc.git)

## Contact author

Expand All @@ -82,11 +67,3 @@ Wechat:angelgel2020
QQ:3570249647

email: ```[email protected]```

## Referenced Open-source software

| Library |Usage |
|:-----------|:-------|
| googletest | Unittest |

Express my thanks to Open-source software contributors!
File renamed without changes.
File renamed without changes.
23 changes: 23 additions & 0 deletions cpp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Trc

## language

100% C++

Using c++ 20 standard and use module feature

## All docs reference

[All docs](doc)

## How to compile

[Compilation guide](doc/developer/build.md)

## Referenced Open-source software

| Library |Usage |
|:-----------|:-------|
| googletest | Unittest |

Express my thanks to Open-source software contributors!
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 0 additions & 2 deletions script/clang_format.py → cpp/script/clang_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,3 @@
if tmp == ".hpp" or tmp == ".cpp" or tmp == ".h" or tmp == ".cppm":
path = os.path.join(root, i)
os.system("clang-format -i --style=file %s" % path)

input("ok")
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
/**
* @file test_filesys.cpp
* @brief 测试filesys模块
* @date 2022-05-03
*
* @copyright Copyright (c) 2022
*
*/

#include <gtest/gtest.h>
#include <list>
#include <string>
import filesys;
import filesys_tools;

using namespace trc;

static void test_readfile(const char* path, const char* expect) {
std::string filedata;
utils::readcode(filedata, tools::redefine_path(path));
EXPECT_STREQ(filedata.c_str(), expect);
}

// 测试读取文件的函数
TEST(filesys, readfile) {
// 测试读取多行文件
test_readfile("filesys/readfile/readwithlines.in",
"\n\n\nreadwithlines\n\nreadwithlines\n");
// 测试读取空文件
test_readfile("filesys/readfile/readempty.in", "");
// 测试读取英文文件
test_readfile("filesys/readfile/readenglish.in",
"abcd\nefgh\nijkl\nmnop\nqrst\nuvwx\nyz\n");
// 测试读取中文文件
test_readfile("filesys/readfile/readchinese.in",
"生活就像海洋,只有意志坚强的人才能到达彼岸\n");
std::string filedata;
// 测试读取不存在文件
EXPECT_EQ(1,
utils::readcode_with_code(
filedata, tools::redefine_path("filesys/readfile/failtoread")));
// 测试成功读取文件
filedata.clear();
EXPECT_EQ(0,
utils::readcode_with_code(
filedata, tools::redefine_path("filesys/readfile/readenglish.in")));
// 测试读取超大文件
std::string expect_big_data;
char basestr[] = "pppppppppppp\n";
expect_big_data.reserve(sizeof(basestr) * 10240);
for (int i = 0; i < 10240; ++i) {
expect_big_data += basestr;
}
test_readfile("filesys/readfile/readlonglong.in", expect_big_data.c_str());
}
/**
* @file test_filesys.cpp
* @brief 测试filesys模块
* @date 2022-05-03
*
* @copyright Copyright (c) 2022
*
*/

#include <gtest/gtest.h>
#include <list>
#include <string>
import filesys;
import filesys_tools;

using namespace trc;

static void test_readfile(const char* path, const char* expect) {
std::string filedata;
utils::readcode(filedata, tools::redefine_path(path));
EXPECT_STREQ(filedata.c_str(), expect);
}

// 测试读取文件的函数
TEST(filesys, readfile) {
// 测试读取多行文件
test_readfile("filesys/readfile/readwithlines.in",
"\n\n\nreadwithlines\n\nreadwithlines\n");
// 测试读取空文件
test_readfile("filesys/readfile/readempty.in", "");
// 测试读取英文文件
test_readfile("filesys/readfile/readenglish.in",
"abcd\nefgh\nijkl\nmnop\nqrst\nuvwx\nyz\n");
// 测试读取中文文件
test_readfile("filesys/readfile/readchinese.in",
"生活就像海洋,只有意志坚强的人才能到达彼岸\n");
std::string filedata;
// 测试读取不存在文件
EXPECT_EQ(1,
utils::readcode_with_code(
filedata, tools::redefine_path("filesys/readfile/failtoread")));
// 测试成功读取文件
filedata.clear();
EXPECT_EQ(0,
utils::readcode_with_code(
filedata, tools::redefine_path("filesys/readfile/readenglish.in")));
// 测试读取超大文件
std::string expect_big_data;
char basestr[] = "pppppppppppp\n";
expect_big_data.reserve(sizeof(basestr) * 10240);
for (int i = 0; i < 10240; ++i) {
expect_big_data += basestr;
}
test_readfile("filesys/readfile/readlonglong.in", expect_big_data.c_str());
}
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
/**
* 测试自己封装的io库
*/

#include <cstdio>
#include <cstdlib>
#include <gtest/gtest.h>
#include <string>
#include <vector>
import io;
import filesys_tools;

using namespace trc;

static void check_readstr(
const char* path, const std::vector<const char*>& expectstr) {
FILE* file
= tools::fopen_with_check(tools::redefine_path(path).c_str(), "r");
char* raw_str;
// -1是为了避开最后一个false(eof)值
for (size_t i = 0, n = expectstr.size(); i < n - 1; ++i) {
EXPECT_TRUE(io::readstr(raw_str, file));
EXPECT_STREQ(raw_str, expectstr[i]);
free(raw_str);
}
io::readstr(raw_str, file);
EXPECT_STREQ(raw_str, expectstr.back());
free(raw_str);
fclose(file);
}

// 测试字符串读入
TEST(io, readstr) {
// 测试普通正常多行文件
check_readstr("io/readstr1.in", { "abcdef", "opiuy", "pploi" });
// 测试结尾不为换行符的文件
check_readstr("io/readstr_eof.in", { "io", "ploip" });
// 测试空文件读入
check_readstr("io/readstr_empty.in", { "" });
// 测试长字符串读入
check_readstr("io/readstr_long.in",
{ std::string(120, 'u').c_str(), std::string(120, 'v').c_str() });
}
/**
* 测试自己封装的io库
*/

#include <cstdio>
#include <cstdlib>
#include <gtest/gtest.h>
#include <string>
#include <vector>
import io;
import filesys_tools;

using namespace trc;

static void check_readstr(
const char* path, const std::vector<const char*>& expectstr) {
FILE* file
= tools::fopen_with_check(tools::redefine_path(path).c_str(), "r");
char* raw_str;
// -1是为了避开最后一个false(eof)值
for (size_t i = 0, n = expectstr.size(); i < n - 1; ++i) {
EXPECT_TRUE(io::readstr(raw_str, file));
EXPECT_STREQ(raw_str, expectstr[i]);
free(raw_str);
}
io::readstr(raw_str, file);
EXPECT_STREQ(raw_str, expectstr.back());
free(raw_str);
fclose(file);
}

// 测试字符串读入
TEST(io, readstr) {
// 测试普通正常多行文件
check_readstr("io/readstr1.in", { "abcdef", "opiuy", "pploi" });
// 测试结尾不为换行符的文件
check_readstr("io/readstr_eof.in", { "io", "ploip" });
// 测试空文件读入
check_readstr("io/readstr_empty.in", { "" });
// 测试长字符串读入
check_readstr("io/readstr_long.in",
{ std::string(120, 'u').c_str(), std::string(120, 'v').c_str() });
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
28 changes: 20 additions & 8 deletions rust/src/compiler/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,22 @@ impl<'a> AstBuilder<'a> {

fn statement(&mut self, mut t: Token) -> RunResult<()> {
match t.tp {
super::token::TokenType::ID => {}
super::token::TokenType::ID => {
t = self.token_lexer.next_token()?;
match t.tp {
super::token::TokenType::Assign => {}
super::token::TokenType::Store => {}
_ => {
return Err(RuntimeError::new(
Box::new(self.token_lexer.compiler_data.content.clone()),
ErrorInfo::new(
gettextrs::gettext(SYNTAX_ERROR),
gettext!(UNEXPECTED_TOKEN, t.tp.to_string()),
),
))
}
}
}
_ => {
return Err(RuntimeError::new(
Box::new(self.token_lexer.compiler_data.content.clone()),
Expand All @@ -47,15 +62,12 @@ impl<'a> AstBuilder<'a> {
pub fn generate_code(&mut self) -> RunResult<()> {
loop {
let token = self.token_lexer.next_token()?;
match token {
Some(token) => {
self.statement(token)?;
}
None => {
return Ok(());
}
if token.tp == super::token::TokenType::EndOfFile {
break;
}
self.statement(token)?;
}
return Ok(());
}
}

Expand Down
Loading

0 comments on commit 28c6ca0

Please sign in to comment.