From de06c734ef607877a2f073129b24992bb73a4c18 Mon Sep 17 00:00:00 2001 From: Gusem Fowage Date: Sun, 25 Aug 2024 00:58:29 +0800 Subject: [PATCH] fix* some easy function --- .gitignore | 3 +- README.md | 20 +++++++++- example/Makefile | 25 +++++++++++++ example/example.sh | 14 +++++++ example/main.st | 6 +++ example/test.st | 9 +++++ include/stdc_implemented/String.hpp | 8 ++-- include/stdc_implemented/TypeDef.hpp | 47 ++++++++++++++++++++++- include/template/TypeDef.hpp | 56 +++++++++++++++++++++++++++- src/Stamon.hpp | 5 ++- 10 files changed, 184 insertions(+), 9 deletions(-) create mode 100644 example/Makefile create mode 100644 example/example.sh create mode 100644 example/main.st create mode 100644 example/test.st diff --git a/.gitignore b/.gitignore index 881f91e..19af61a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ bin build CMakeFiles -CMakeCache.txt \ No newline at end of file +CMakeCache.txt +*.stvc \ No newline at end of file diff --git a/README.md b/README.md index 7f62936..226fc89 100644 --- a/README.md +++ b/README.md @@ -40,4 +40,22 @@ * TheFlySong [Github主页](https://github.com/TheFlySong) * XIAOYI12 [Github主页](https://github.com/XIAOYI1212) -* 冻橘 [Github主页](https://github.com/MikanAffine) \ No newline at end of file +* 冻橘 [Github主页](https://github.com/MikanAffine) + +## 或许需要 +在**example**目录下,我们给出了一些组织源码的常用方式
+你可以通过阅读 **demo** 目录下的示例代码来学习Stamon的使用方法。 ++ 使用 **make** +``` console +$ cd example +$ make run R=main +``` ++ 使用 **bash** +``` console +$ cd example +$ bash example.sh main +``` +样例程序中仅给出了一小段示例代码 +可以在demo目录下看到更多的示例代码 +## 最后 +stamon2还在不断的开发中,欢迎大家的参与! \ No newline at end of file diff --git a/example/Makefile b/example/Makefile new file mode 100644 index 0000000..a53095a --- /dev/null +++ b/example/Makefile @@ -0,0 +1,25 @@ +# 为什么要有这个 makefile? +# 这是一个编译的示例 +# 我们可以通过这样的写法来编译st源文件,并生成stvc文件 +# 然后运行stamon来运行stvc文件 + +MAKEFILE_PATH = $(shell pwd) +STAMON_PATH = ${MAKEFILE_PATH}/../bin + +STAMON_EXE = ${STAMON_PATH}/stamon +# 必须导出的环境变量 +export STAMON=${STAMON_PATH} +# 导入的文件 +imports = ${MAKEFILE_PATH} +# 需要运行的文件:注不带后缀 +R = "" +# 生成 +%.stvc: %.st + ${STAMON_EXE} -b $< $@ -I${imports} + @echo +# 运行 +run: ${R}.stvc + @${STAMON_EXE} -r ${R}.stvc +# 获取帮助 +help: + ${STAMON_EXE} help \ No newline at end of file diff --git a/example/example.sh b/example/example.sh new file mode 100644 index 0000000..cd92612 --- /dev/null +++ b/example/example.sh @@ -0,0 +1,14 @@ +#!/bin/bash +# 使用脚本来辅助编译和运行示例程序 +echo "Test to build a example" +echo "--------------------------" + +SHELL_FOLDER=$(cd "$(dirname "$0")";pwd) +# 必要的环境变量 +export STAMON=${SHELL_FOLDER}/../bin +STAMON_EXE=${STAMON}/stamon + +cd ${SHELL_FOLDER} + +$STAMON_EXE -b main.st main.stvc -I./ +$STAMON_EXE -r main.stvc \ No newline at end of file diff --git a/example/main.st b/example/main.st new file mode 100644 index 0000000..7431570 --- /dev/null +++ b/example/main.st @@ -0,0 +1,6 @@ +import test; + +def main = func { + hello(); + return 0; +}(); \ No newline at end of file diff --git a/example/test.st b/example/test.st new file mode 100644 index 0000000..b9ccdac --- /dev/null +++ b/example/test.st @@ -0,0 +1,9 @@ +import std; + +func hello() { + println("example to build sources and run tests"); + def a = input(); + puts("you entered: "); + println(a); + return 0; +} \ No newline at end of file diff --git a/include/stdc_implemented/String.hpp b/include/stdc_implemented/String.hpp index c1c6b35..764ce34 100644 --- a/include/stdc_implemented/String.hpp +++ b/include/stdc_implemented/String.hpp @@ -31,9 +31,9 @@ class String { char_type* str; char_type cache[32] = {0}; - void* StrCalloc(int size) { + void* StrCalloc(size_type size) { if(size*sizeof(char_type)<=32) { - int i = 0; + size_type i = 0; while((cache[i]!=0) && (i<32)) { cache[i] = 0; i++; @@ -55,7 +55,9 @@ class String { str = (char_type*)StrCalloc(1); } //初始化为空字符串 - String(char_type *s) { + // 使用const char_type*是必要的 + // 因为C++中双引号包含的字符串默认类型便是 const char* + String(const char_type *s) { str = (char_type*)StrCalloc(strlen(s)+1); strcpy(str, s); } //初始化,将s复制到this diff --git a/include/stdc_implemented/TypeDef.hpp b/include/stdc_implemented/TypeDef.hpp index c9c0883..9d851a9 100644 --- a/include/stdc_implemented/TypeDef.hpp +++ b/include/stdc_implemented/TypeDef.hpp @@ -35,4 +35,49 @@ template<> struct bytes_get<8, false>{typedef uint64_t type;}; // struct bytes_get<16, false>{typedef uint128_t type;}; -using len_t = bytes_get::type; \ No newline at end of file +using len_t = bytes_get::type; + +// 一下功能仍然在测试阶段 +// 实际加入到程序中仍需要时间 +using nullptr_t = decltype(nullptr); + +template +struct pointer { + typedef T* + ptr_t; + typedef T& + ref_t; + typedef const T& + const_reference; + typedef T + value_type; + typedef bytes_get::type + dif_t; + + pointer() noexcept + : ptr(nullptr) {} + pointer(nullptr_t) noexcept + : ptr(nullptr) {} + pointer(ptr_t p) noexcept + : ptr(p) {} + const_reference operator*() const noexcept + { return *ptr; } + ref_t operator*() noexcept + { return *ptr; } + pointer operator->() const noexcept + { return ptr; } + dif_t operator-(const pointer& other) const noexcept + { return ptr - other.ptr; } + ref_t operator[](dif_t i) noexcept + { return ptr[i]; } + const_reference operator[](dif_t i) const noexcept + { return ptr[i]; } + operator bool() const noexcept + { return ptr!= nullptr; } + operator ptr_t() const noexcept + { return ptr; } + ptr_t get() const noexcept + { return ptr; } +private: + ptr_t ptr; +}; \ No newline at end of file diff --git a/include/template/TypeDef.hpp b/include/template/TypeDef.hpp index d234fdd..ad4dbbd 100644 --- a/include/template/TypeDef.hpp +++ b/include/template/TypeDef.hpp @@ -6,6 +6,14 @@ Description: 跨平台类型定义库 */ +template +struct constant_value { + static constexpr T value = V; +}; + +using true_type = constant_value; +using false_type = constant_value; + //以下宏定义需要填写对应类型 #define INT8 #define INT16 @@ -45,4 +53,50 @@ template<> struct bytes_get<8, false>{typedef UINT64 type;}; using len_t = bytes_get::type; -//等价于#define len_t bytes_get::type \ No newline at end of file +//等价于#define len_t bytes_get::type + +// 这件事情应该由标准库实现,但是为了兼容性,这里还是自己实现 +using nullptr_t = decltype(nullptr); + +// 仅仅只是封装了指针 +// 在C++中应当避免出现直接裸指针 +// 注: +// 可以使用智能指针,但由于项目设计伊始为了可移植性, +// 未导入智能指针,故此处仅仅使用封装指针 +template +struct pointer { + typedef T* + ptr_t; + typedef T& + ref_t; + typedef const T& + const_reference; + typedef T + value_type; + typedef bytes_get::type + dif_t; + + pointer() noexcept; + pointer(nullptr_t) noexcept; + pointer(ptr_t p) noexcept; + const_reference operator*() const noexcept; + ref_t operator*() noexcept; + pointer operator->() const noexcept; + dif_t operator-(const pointer& other) const noexcept; + ref_t operator[](dif_t i) noexcept; + const_reference operator[](dif_t i) const noexcept; + operator bool() const noexcept; + operator ptr_t() const noexcept; + ptr_t get() const noexcept; +protected: // 可在这的基础上封装智能指针 + ptr_t ptr; +}; + +// 判断是否是指针 +// 此功能还未使用,但项目优化有考虑使用 +template +struct is_pointer : false_type{}; +template +struct is_pointer> : true_type{}; +template +struct is_pointer : true_type{}; \ No newline at end of file diff --git a/src/Stamon.hpp b/src/Stamon.hpp index d8ca0a7..f696e7b 100644 --- a/src/Stamon.hpp +++ b/src/Stamon.hpp @@ -82,7 +82,8 @@ namespace stamon { } void compile( - String src, String dst, bool isSupportImport, bool isStrip + // 这里的引用传递是必要的,可以避免拷贝带来的开销,提高效率 + String& src, String& dst, bool isSupportImport, bool isStrip ) { c::Compiler compiler(ex); @@ -104,7 +105,7 @@ namespace stamon { ); } - ast::AstNode* node = new ast::AstProgram(program); + pointer node = new ast::AstProgram(program); //编译为IR