资源下载地址:https://download.csdn.net/download/sheziqiong/86784192
资源下载地址:https://download.csdn.net/download/sheziqiong/86784192
sudo apt install git
sudo apt install cmake
sudo apt-get install clang lld
sudo apt install ninja-build
git clone https://github.com/Jason048/llvm-project.git
mkdir llvm-project/build
cd llvm-project/build
cmake -G Ninja ../llvm \
-DLLVM_ENABLE_PROJECTS=mlir \
-DLLVM_BUILD_EXAMPLES=ON \
-DLLVM_TARGETS_TO_BUILD="X86;NVPTX;AMDGPU" \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_ENABLE_ASSERTIONS=ON \
-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DLLVM_ENABLE_LLD=ON
cmake --build . --target check-mlir
git clone https://github.com/Jason048/tiny_project.git
mkdir build
cd build
# 将下面的三行命令在build目录下执行
LLVM_DIR=/home/username/llvm-project/build/lib/cmake/llvm \
MLIR_DIR=/home/username/llvm-project/build/lib/cmake/mlir \
cmake -G Ninja ..
对关键词和变量名进行分析。
文件路径:/tiny_project/tiny/include/tiny/Lexer.h
在 Lexer.h 搜索 “TODO”,可以看到需要补充的代码位置。 实现以下功能:
enum Token : int {
tok_semicolon = ';',
tok_parenthese_open = '(',
tok_parenthese_close = ')',
tok_bracket_open = '{',
tok_bracket_close = '}',
tok_sbracket_open = '[',
tok_sbracket_close = ']',
tok_eof = -1,
// commands
tok_return = -2,
tok_var = -3,
tok_def = -4,
tok_struct = -5,
// primary
tok_identifier = -6,
tok_number = -7,
//basic part
tok_invalid_identifier = -8,
};
基础部分:词法分析与语法分析
文件路径:/tiny_project/tiny/include/tiny/Parser.h
在 Parser.h 搜索"TODO",可以看到需要补充的代码位置。 实现以下功能:
TODO:额外支持第三种新的形式。
TODO:语法变量必须以“var”开头,后面为变量名及类型,最后为变量的初始化
TODO:额外支持第三种新的形式。
// Parse a variable declaration,
// 1. it starts with a `var` keyword, followed by a variable name and initialization
// 2. Two methods of initialization have been supported:
// (1) var a = [[1, 2, 3], [4, 5, 6]];
// (2) var a <2,3> = [1, 2, 3, 4, 5, 6];
// You need to support the third method:
// (3) var a [2][3] = [1, 2, 3, 4, 5, 6];
// Some functions may be useful: getLastLocation(); getNextToken();
std::unique_ptr
parseVarDeclaration(bool requiresInitializer) {
// TODO: check to see if this is a 'var' declaration
// If not, report the error with 'parseError', otherwise eat 'var'
/* Write your code here. */
if (lexer.getCurToken() != tok_var)
{
return parseError("var", "to begin declaration");
}
auto loc = lexer.getLastLocation();
lexer.getNextToken(); // eat var
/* Write your code above */
// TODO: check to see if this is a variable name
// If not, report the error with 'parseError'
/* Write your code here. */
if(lexer.getCurToken() != tok_identifier)
{
return parseError("identified", "after 'var' declaration");
}
/* Write your code above */
// eat the variable name
std::string id(lexer.getId());
lexer.getNextToken(); // eat id
std::unique_ptr type;
// TODO: modify code to additionally support the third method: var a[][] = ...
if (lexer.getCurToken() == '<' || lexer.getCurToken() == '[') {
type = parseType();
if (!type)
return nullptr;
}
if (!type)
type = std::make_unique();
std::unique_ptr expr;
if (requiresInitializer) {
lexer.consume(Token('='));
expr = parseExpression();
}
return std::make_unique(std::move(loc), std::move(id),
std::move(*type), std::move(expr));
}
# 在/home/username/tiny_project/build 目录下开始执行
cmake --build . --target tiny
cd ../
build/bin/tiny test/tiny/parser/test_1.tiny -emit=ast
build/bin/tiny test/tiny/parser/test_5.tiny -emit=ast
第三种类型 —— d[2][3] ——已成功识别。
build/bin/tiny test/tiny/parser/test_6.tiny -emit=jit
def transpose_transpose(x) {
return transpose(transpose(x));
}
def main() {
var a<2, 3> = [[1, 2, 3], [4, 5, 6]];
var b = transpose_transpose(a);
print(b);
}
build/bin/tiny test/tiny/parser/test_7.tiny -emit=mlir -opt
build/bin/tiny test/tiny/parser/test_7.tiny -emit=mlir -opt
# 将.tiny文件转换为抽象语法树AST:
build/bin/tiny test/tiny/parser/test_7.tiny -emit=ast
# 或直接得到tiny程序的运行结果,是否进行消除冗余不会影响运行结果
build/bin/tiny test/tiny/parser/test_7.tiny -emit=jit




资源下载地址:https://download.csdn.net/download/sheziqiong/86784192
资源下载地址:https://download.csdn.net/download/sheziqiong/86784192