Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
bin
build
CMakeFiles
CMakeCache.txt
CMakeCache.txt
*.stvc
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,22 @@

* TheFlySong [Github主页](https://github.com/TheFlySong)
* XIAOYI12 [Github主页](https://github.com/XIAOYI1212)
* 冻橘 [Github主页](https://github.com/MikanAffine)
* 冻橘 [Github主页](https://github.com/MikanAffine)

## 或许需要
在**example**目录下,我们给出了一些组织源码的常用方式 <br>
你可以通过阅读 **demo** 目录下的示例代码来学习Stamon的使用方法。
+ 使用 **make**
``` console
$ cd example
$ make run R=main
```
+ 使用 **bash**
``` console
$ cd example
$ bash example.sh main
```
样例程序中仅给出了一小段示例代码
可以在demo目录下看到更多的示例代码
## 最后
stamon2还在不断的开发中,欢迎大家的参与!
25 changes: 25 additions & 0 deletions example/Makefile
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions example/example.sh
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions example/main.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import test;

def main = func {
hello();
return 0;
}();
9 changes: 9 additions & 0 deletions example/test.st
Original file line number Diff line number Diff line change
@@ -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;
}
8 changes: 5 additions & 3 deletions include/stdc_implemented/String.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand All @@ -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
Expand Down
47 changes: 46 additions & 1 deletion include/stdc_implemented/TypeDef.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<sizeof(size_t), false>::type;
using len_t = bytes_get<sizeof(size_t), false>::type;

// 一下功能仍然在测试阶段
// 实际加入到程序中仍需要时间
using nullptr_t = decltype(nullptr);

template<class T>
struct pointer {
typedef T*
ptr_t;
typedef T&
ref_t;
typedef const T&
const_reference;
typedef T
value_type;
typedef bytes_get<sizeof(void*), false>::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;
};
56 changes: 55 additions & 1 deletion include/template/TypeDef.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
Description: 跨平台类型定义库
*/

template<class T, T V>
struct constant_value {
static constexpr T value = V;
};

using true_type = constant_value<bool, true>;
using false_type = constant_value<bool, false>;

//以下宏定义需要填写对应类型
#define INT8
#define INT16
Expand Down Expand Up @@ -45,4 +53,50 @@ template<>
struct bytes_get<8, false>{typedef UINT64 type;};

using len_t = bytes_get<sizeof(size_t), false>::type;
//等价于#define len_t bytes_get<sizeof(size_t), false>::type
//等价于#define len_t bytes_get<sizeof(size_t), false>::type

// 这件事情应该由标准库实现,但是为了兼容性,这里还是自己实现
using nullptr_t = decltype(nullptr);

// 仅仅只是封装了指针
// 在C++中应当避免出现直接裸指针
// 注:
// 可以使用智能指针,但由于项目设计伊始为了可移植性,
// 未导入智能指针,故此处仅仅使用封装指针
template<class T>
struct pointer {
typedef T*
ptr_t;
typedef T&
ref_t;
typedef const T&
const_reference;
typedef T
value_type;
typedef bytes_get<sizeof(void*), false>::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<class T>
struct is_pointer : false_type{};
template<class T>
struct is_pointer<pointer<T>> : true_type{};
template<class T>
struct is_pointer<T*> : true_type{};
5 changes: 3 additions & 2 deletions src/Stamon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -104,7 +105,7 @@ namespace stamon {
);
}

ast::AstNode* node = new ast::AstProgram(program);
pointer<ast::AstNode> node = new ast::AstProgram(program);

//编译为IR

Expand Down