2021-09-07
最近正在设计一种类似汇编语言的中间语言,从而将其转化成 brainfuck
,编译器还在设计中,先放个程序:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++><.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]>+>>>>>>>,><<<<[-]>>>[-<<<+>>>]<>++++++++++++++++++++++++++++++++++++++++++++++++><<<<[->>>>>+<<<<<]>>>>>[-<<<<<+>>>>>>+<]>[-<+>]<[-<+>]<>[-]>[-]>[-]>[-]>[-]<<<<<[->>+<<]<[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<>>[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<<<<[->>>+<<<]>>>[[-]>+<]+>[-<->]<[>+<-]>[>+<-]>[[-]<<<<<<->>>->>><<<<<<[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<>>[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<<<<[->>>+<<<]>>>[[-]>+<]+>[-<->]<[>+<-]>[>+<-]>]<<<<<<[[-]>+<]+>[-<->]<>>>[-]<<<><<<<[->>>>>+<<<<<]>>>>>[-<<<<<+>>>>>>+<]>[-<+>]<[-<+>]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++><>[-]>[-]>[-]>[-]>[-]<<<<<[->>+<<]<[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<>>[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<<<<[->>>+<<<]>>>[[-]>+<]+>[-<->]<[>+<-]>[>+<-]>[[-]<<<<<<->>>->>><<<<<<[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<>>[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<<<<[->>>+<<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<<]>>>[[-]>+<]+>[-<->]<[>+<-]>[>+<-]>]<<<<<<[[-]>+<]+>[-<->]<>>>[-]<<<><<[[-]>[[-]>+<]<][-]>[-]>[-<<+>>]<<><<<[-]>>[-<<+>>]<><<[->>>+<<<]>>>[-<<<+>>>>+<]>[-<+>]<[-<+>]<>+<[->-<]>[-<+>]<><<[-]>[-<+>]<><[->>+<<]>>[-<<+>>>+<]>[-<+>]<[-<+>]<]<<[->>>+<<<]>>>[-<<<+>>>>+<]>[-<+>]<[-<+>]<[[-]<<<<[->>>>>+<<<<<]>>>>>[-<<<<<+>>>>>>+<]>[-<+>]<[-<+>]++++++++++><[-<[->>+>+<<<]>>[-<<+>>]<<>]<[-]>>>[-<<<+>>>]<<<><<<<[->>>>>+<<<<<]>>>>>[-<<<<<+>>>>>>+<]>[-<+>]<[-<+>]<[-<+>]<>++++++++++++++++++++++++++++++++++++++++++++++++><[-<->]<><<<<<[-]>>>>[-<<<<+>>>>]<>,><<<<[-]>>>[-<<<+>>>]<>++++++++++++++++++++++++++++++++++++++++++++++++><<<<[->>>>>+<<<<<]>>>>>[-<<<<<+>>>>>>+<]>[-<+>]<[-<+>]<>[-]>[-]>[-]>[-]>[-]<<<<<[->>+<<]<[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<>>[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<<<<[->>>+<<<]>>>[[-]>+<]+>[-<->]<[>+<-]>[>+<-]>[[-]<<<<<<->>>->>><<<<<<[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<>>[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<<<<[->>>+<<<]>>>[[-]>+<]+>[-<->]<[>+<-]>[>+<-]>]<<<<<<[[-]>+<]+>[-<->]<>>>[-]<<<><<<<[->>>>>+<<<<<]>>>>>[-<<<<<+>>>>>>+<]>[-<+>]<[-<+>]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++><>[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+>>>+<]>[-<+>]<[-<+>]<[[-]<<[->>>+<<<]>>>[-<<<+>>>>+<]>[-<+>]<[-<+>]<<<<<<[->>>>>>>+<<<<<<<]>>>>>>>[-<<<<<<<+>>>>>>>>+<]>[-<+>]<[-<+>]<[-<+>]<><<<[-]>>[-<<+>>]<><<<<<[->>>>>>+<<<<<<]>>>>>>[-<<<<<<+>>>>>>>+<]>[-<+>]<[-<+>]+><[-<+>]<><<<<<<[-]>>>>>[-<<<<<+>>>>>]<><<<<<[->>>>>>+<<<<<<]>>>>>>[-<<<<<<+>>>>>>>+<]>[-<+>]<[-<+>]<<<<[->>>>>+<<<<<]>>>>>[-<<<<<+>>>>>>+<]>[-<+>]<[-<+>]<>[-]>[-]>[-]>[-]>[-]<<<<<[->>+<<]<[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<>>[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<<<<[->>>+<<<]>>>[[-]>+<]+>[-<->]<[>+<-]>[>+<-]>[[-]<<<<<<->>>->>><<<<<<[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<>>[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<<<<[->>>+<<<]>>>[[-]>+<]+>[-<->]<[>+<-]>[>+<-]>]<<<<<<[[-]>+<]+>[-<->]<>>>[-]<<<><<[-]>[-<+>]<><[->>+<<]>>[-<<+>>>+<]>[-<+>]<[-<+>]<]<[-]++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++><.[-]<.[-]<.[-]<.[-]<.[-]<.[-]>><<<[->>>>+<<<<]>>>>[-<<<<+>>>>>+<]>[-<+>]<[-<+>]<<[-]>[-<+>]<>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++><<[->>>+<<<]>>>[-<<<+>>>>+<]>[-<+>]<[-<+>]<>[-]>[-]>[-]>[-]>[-]<<<<<[->>+<<]<[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<>>[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<<<<[->>>+<<<]>>>[[-]>+<]+>[-<->]<[>+<-]>[>+<-]>[[-]<<<<<<->>>->>><<<<<<[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<>>[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<<<<[->>>+<<<]>>>[[-]>+<]+>[-<->]<[>+<-]>[>+<-]>]<<<<<<[[-]>+<]+>[-<->]<>>>[-]<<<><<<[-]>>[-<<+>>]<><<[->>>+<<<]>>>[-<<<+>>>>+<]>[-<+>]<[-<+>]<[[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++><<[->>>+<<<]>>>[-<<<+>>>>+<]>[-<+>]<[-<+>]<<[->>>+>+<<<<]>>>>[-<<<<+>>>>]<<<<>[->>>+>+<<<<]>>>>[-<<<<+>>>>]<<<<>>><[->>+>+<<<]>>>[-<<<+>>>]<<<>>[[-]>+<]>[-<+>]<<[->>+>+<<<]>>>[-<<<+>>>]<<<>>[[-]>+<]>[-<+>]<<[[-]>[[-]>+<]<][-]>[-]>[-<<+>>]<<[[-]<-<-><[->>+>+<<<]>>>[-<<<+>>>]<<<>>[[-]>+<]>[-<+>]<<[->>+>+<<<]>>>[-<<<+>>>]<<<>>[[-]>+<]>[-<+>]<<[[-]>[[-]>+<]<][-]>[-]>[-<<+>>]<<]<[-]<[[-]>+<]+>[-<->]<[[-]<+<<[->>>+>+<<<<]>>>>[-<<<<+>>>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>>]<<<>>[[-]>+<]>[-<+>]<<[->>+>+<<<]>>>[-<<<+>>>]<<<>>[[-]>+<]>[-<+>]<<[[-]>[[-]>+<]<][-]>[-]>[-<<+>>]<<[[-]<-<-><[->>+>+<<<]>>>[-<<<+>>>]<<<>>[[-]>+<]>[-<+>]<<[->>+>+<<<]>>>[-<<<+>>>]<<<>>[[-]>+<]>[-<+>]<<[[-]>[[-]>+<]<][-]>[-]>[-<<+>>]<<]<[-]<[[-]>+<]+>[-<->]<[[-]<+<<[->>>+>+<<<<]>>>>[-<<<<+>>>>]<<<<>>>[-<<->>]<<<[->>>+>+<<<<]>>>>[-<<<<+>>>>]<<<<>[->>>+>+<<<<]>>>>[-<<<<+>>>>]<<<<>>><[->>+>+<<<]>>>[-<<<+>>>]<<<>>[[-]>+<]>[-<+>]<<[->>+>+<<<]>>>[-<<<+>>>]<<<>>[[-]>+<]>[-<+>]<<[[-]>[[-]>+<]<][-]>[-]>[-<<+>>]<<[[-]<-<-><[->>+>+<<<]>>>[-<<<+>>>]<<<>>[[-]>+<]>[-<+>]<<[->>+>+<<<]>>>[-<<<+>>>]<<<>>[[-]>+<]>[-<+>]<<[[-]>[[-]>+<]<][-]>[-]>[-<<+>>]<<]<[-]<[[-]>+<]+>[-<->]<]<<<[-]>>[-<<+>>]<<>><<<[-]>>[-<<+>>]<>++++++++++++++++++++++++++++++++++++++++++++++++><[-<+>]<><.[-]]<[->>+<<]>>[-<<+>>>+<]>[-<+>]<[-<+>]++++++++++++++++++++++++++++++++++++++++++++++++><[-<+>]<><.[-]<[-]<[-]
上文中这个 10KB 的程序的功能是输入一个整数 n ( n ≤ 20 ) n(n\leq 20) n(n≤20),利用循环计算 1 + 2 + ⋯ + n 1+2+\cdots+n 1+2+⋯+n 的值,并输出,运行效果图。
在线的可视化解释器太慢了,顺便给一下解释器吧:
/// brainfuck 的 C 语言解释器
/// author: GGN_2015
/// VERSION: 2021-09-06 alpha
/// 命名规范:所有函数/全局变量 必须采用下划线明名规则
/// 所有函数/头文件/全局变量按照名称字典序排列
#define CODE_EXIT ( 1) /// 退出信号
#define CODE_ERROR (-1) /// 运行时出错
#define DEBUG ( 1) /// 采用调试模式
#define EMPTY() (stack_top <= 0) /// 判断栈是否为空
#define if_debug if(DEBUG) /// 输出调试信息
#define if_show if(1) /// 输出最终状态
#define MAX_CODE_LENGTH (1048576) /// 程序的最大长度
#define min(A,B) ((A)<=(B) ? (A) : (B))
#define PUSH(POS) (stack_arr[stack_top ++] = (POS))
#define POP() (stack_top --)
#define TOP() (stack_arr[stack_top - 1]) /// 返回栈顶元素
/// -------------------------------------------------------------------------------- 头文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/// -------------------------------------------------------------------------------- 全局变量
int end_pos ; /// 记录程序文件结尾的位置
char file_path[256] ; /// 程序文件路径
char* memory ; /// 使用内存
int memory_limit = 1048576 ; /// 默认 1 MB 内存
const int memory_max = 1 << 30 ; /// 内存最大字节数
int pointer_begin = 0 ; /// 程序开始执行时,指针指向的内存坐标
FILE* source = NULL ; /// 程序源代码文件
int status_flag = 0 ; /// 当前程序的运行状态, 只用来接收函数返回值,不允许在 main 函数外赋值
char* source_code = NULL ; /// 储存源代码文件
int* next_pos = NULL ; /// 储存 '[' 与 ']' 的对应关系
int stack_arr[MAX_CODE_LENGTH] ; /// 匹配栈
int stack_top = 0 ; /// 栈顶指针
/// -------------------------------------------------------------------------------- 所有函数
void check_error (const char*) ; /// 检查是否出错
void check_pos (int) ; /// 检查某个位置是否可访问
int delta_argv (int, char**) ; /// 根据命令行参数调整全局变量
char get_ins () ; /// 读取一条指令 (可能是注释)
void help_file () ; /// 输出帮助文件
int init_memory () ; /// 初始化内存
int run_code () ; /// 运行指定的程序
/// -------------------------------------------------------------------------------- 函数实现
void check_error(const char* function_name) { /// 检查程序出错
if(status_flag == CODE_EXIT) { /// 直接退出程序
exit(0);
}else if(status_flag != 0) {
printf("Error When %s. status_flag = %d\n", function_name, status_flag);
exit(status_flag);
}
}
void check_pos(int ptr) {
if(0 <= ptr && ptr <= memory_limit - 1) {
/// 检查该位置是否可访问
}else {
if_debug printf("check_pos: runtime error at ptr = %d\n", ptr); /// 非法访问
exit(-1);
}
}
int delt_argv(int argc, char* argv[]) { /// 处理命令行参数
for(int i = 1; i < argc; i ++) {
if(strcmp(argv[i], "-f") == 0) { /// 指定源代码文件文件
if(i + 1 < argc) {
strcpy(file_path, argv[i + 1]); /// 设置程序路径
i ++;
}else {
printf("delt_argv: -f parament must be followed by a file path.\n");
return CODE_ERROR;
}
}else if(strcmp(argv[i], "-m") == 0) { /// 设置内存限制
if(i + 1 < argc) {
memory_limit = atoi(argv[i + 1]);
i ++;
}else {
printf("delt_argv: -m parament must be followed by a integer (memory limit BYTEs).\n");
return CODE_ERROR;
}
}else if(strcmp(argv[i], "-h") == 0) { /// 显示帮助文档
help_file();
return CODE_EXIT;
}else if(argv[i][0] == '-') { /// 未知参数
printf("delta_argv: %s parament not defined!\n", argv[i]);
return CODE_ERROR;
}else {
strcpy(file_path, argv[i]);
}
}
return 0;
}
char get_ins() { /// 读取一条指令
if(file_path[0] == 0) return getchar();
return fgetc(source);
}
void help_file() { /// 输出帮助文件
printf("brainfuck -f [source_code = ''] -m [memory limit = '1048576']\n");
}
int init_code() {
source_code = (char *) malloc(MAX_CODE_LENGTH * sizeof(char));
next_pos = (int *) malloc(MAX_CODE_LENGTH * sizeof( int)); /// 申请内存
return 0;
}
int init_memory() { /// 初始化 memory 数组
if(memory_limit > memory_max) {
return CODE_ERROR; /// 超过最大内存限制
}else {
memory = (char *)malloc(memory_limit); /// 分配内存
memset(memory, 0x00, memory_limit);
}
return 0;
}
int main(int argc, char* argv[]) { /// 主程序
status_flag = delt_argv(argc, argv); check_error("delt_argv");
status_flag = init_memory(); check_error("init_memory");
status_flag = init_code(); check_error("init_code");
status_flag = run_code(); check_error("run_code");
return 0;
}
int read_func(int pos) { /// 读取一个函数进入内存
for(;; pos ++) {
char ctmp = get_ins();
if(ctmp == EOF) {
end_pos = pos; /// 记录文件结尾的位置
return 0; /// 读到文件结尾
}else {
if(ctmp == '<' || ctmp == '>' || ctmp == '+' || ctmp == '-' || ctmp == ',' || ctmp == '.') {
source_code[pos] = ctmp;
}else if(ctmp == '[') {
source_code[pos] = ctmp;
PUSH(pos);
}else if(ctmp == ']') {
source_code[pos] = ctmp;
if(!EMPTY()) {
int last_pos = TOP(); POP();
next_pos[pos] = last_pos;
next_pos[last_pos] = pos; /// 记录匹配位置
}else {
if_debug printf("read_func: ']' is more than '[', pos = %d\n", pos); /// 右括号比左括号多
return -1;
}
}else pos --; /// 其他字符均视为注释,不计入内存
}
}
if(!EMPTY()) { /// 没有完成匹配
if_debug printf("read_func: '[' is more than ']' at the end of the file.\n"); /// 左括号比右括号多
return -1;
}
return 0;
}
int run_code() { /// 解释运行 .brfk 源代码程序
if(file_path[0] != 0) {
source = fopen(file_path, "r"); /// 读取程序文件
if(source == NULL) {
if_debug printf("run_code: filepath = %s is not accessible.\n", file_path); /// 文件无法打开
return CODE_ERROR;
}
}
status_flag = read_func(0); check_error("read_func"); /// 读入整个程序
int ptr = pointer_begin;
for(int i = 0; i < end_pos; i ++) {
char ctmp = source_code[i];
switch(ctmp) {
case '<': ptr --; break;
case '>': ptr ++; break;
case '+': check_pos(ptr); memory[ptr] ++; break;
case '-': check_pos(ptr); memory[ptr] --; break;
case ',': check_pos(ptr); memory[ptr] = getchar(); break;
case '.': check_pos(ptr); putchar(memory[ptr]); break;
case '[': check_pos(ptr); if(memory[ptr] == 0) i = next_pos[i]; break;
case ']': check_pos(ptr); if(memory[ptr] != 0) i = next_pos[i]; break;
default: if_debug printf("run_code: error char(%d) found at i = %d\n", ctmp, i); return -1;
}
}
if_debug printf("\n--------------------\nrun_code: program end successfully.\n"); /// 程序正常结束
if_show {
printf("ptr = %d\n", ptr);
for(int i = 0; i < min(20, memory_limit); i ++) {
printf("%3d ", (unsigned char)memory[i]);
}
putchar('\n');
}
return 0;
}