Compare commits
10 commits
e425464aab
...
a84655600d
Author | SHA1 | Date | |
---|---|---|---|
a84655600d | |||
a18612f583 | |||
6b00b2bff9 | |||
04a58f8dec | |||
098a36427e | |||
ab04acdf53 | |||
e104489ee4 | |||
72f36de60c | |||
718829335b | |||
a850aeed63 |
38 changed files with 419 additions and 166 deletions
36
README.md
36
README.md
|
@ -105,24 +105,24 @@ $ bash boot.sh
|
||||||
|
|
||||||
### 支持以下运算符
|
### 支持以下运算符
|
||||||
|
|
||||||
| 运算符 | 含义 | 结合性 |
|
| 运算符 | 含义 | 结合性 |
|
||||||
| --------------------------------- | ----------------------------------------------- | -------- |
|
| ------------------------------------------------------------ | ----------------------------------------------- | -------- |
|
||||||
| `()` | 初等表达式(字面量、标识符、函数调用、括号) | |
|
| `()` | 初等表达式(字面量、标识符、函数调用、括号) | |
|
||||||
| `++` `--` `[]` | 后缀自增自减 数组下标 | 从左到右 |
|
| `++` `--` `[]` | 后缀自增自减 数组下标 | 从左到右 |
|
||||||
| `++` `--` `+` `-` `*` `&` `!` `~` | 前缀自增自减 正负号 取地址 解引用 逻辑非 按位非 | 从右到左 |
|
| `++` `--` `+` `-` `&` `*` `!` `~` | 前缀自增自减 正负号 取地址 解引用 逻辑非 按位非 | 从右到左 |
|
||||||
| `*` `/` `%` | 乘除余 | 从左到右 |
|
| `*` `/` `%` | 乘除余 | 从左到右 |
|
||||||
| `+` `-` | 加减 | 从左到右 |
|
| `+` `-` | 加减 | 从左到右 |
|
||||||
| `<<` `>>` | 左移和算术右移 | 从左到右 |
|
| `<<` `>>` | 左移和算术右移 | 从左到右 |
|
||||||
| `<` `<=` `>` `>=` | 关系比较 | 从左到右 |
|
| `<` `<=` `>` `>=` | 关系比较 | 从左到右 |
|
||||||
| `==` `!=` | 相等比较 | 从左到右 |
|
| `==` `!=` | 相等比较 | 从左到右 |
|
||||||
| `&` | 按位与 | 从左到右 |
|
| `&` | 按位与 | 从左到右 |
|
||||||
| `^` | 按位异或 | 从左到右 |
|
| `^` | 按位异或 | 从左到右 |
|
||||||
| <code>|</code> | 按位或 | 从左到右 |
|
| <code>|</code> | 按位或 | 从左到右 |
|
||||||
| `&&` | 逻辑与 | 从左到右 |
|
| `&&` | 逻辑与 | 从左到右 |
|
||||||
| <code>||</code> | 逻辑或 | 从左到右 |
|
| <code>||</code> | 逻辑或 | 从左到右 |
|
||||||
| `?:` | 条件 | 从右到左 |
|
| `?:` | 条件 | 从右到左 |
|
||||||
| `=` `+=` `-=` `*=` `/=` `%=` `<<=` `>>=` `&=` `^=` <code>|=</code> | 赋值 | 从右到左 |
|
| `=` `+=` `-=` `*=` `/=` `%=` `<<=` `>>=` `&=` `^=` <code>|=</code> | 赋值 复合赋值 | 从右到左 |
|
||||||
| `,` | 逗号 | 从左到右 |
|
| `,` | 逗号 | 从左到右 |
|
||||||
|
|
||||||
- 同级表达式的求值顺序与结合性一致。
|
- 同级表达式的求值顺序与结合性一致。
|
||||||
- 加减号支持整数之间,指针与整数,指针之间的运算。
|
- 加减号支持整数之间,指针与整数,指针之间的运算。
|
||||||
|
|
187
boot.c
187
boot.c
|
@ -1,11 +1,11 @@
|
||||||
/*
|
/*
|
||||||
* RVBTCC By Yaossg
|
* RVBTCC By Yaossg
|
||||||
* A lightweight bootstrapping compiler in 2000 lines.
|
* A lightweight bootstrapping compiler in only ~2000 lines.
|
||||||
*
|
*
|
||||||
* It aims to demonstrate how to write a bootstrapping compiler in no time.
|
* This project aims to demonstrate how to write a bootstrapping compiler in no time.
|
||||||
* Syntax is similar to C, output is RISC-V assembly.
|
* Its syntax resembles C, and its output is RISC-V assembly.
|
||||||
* Only dependent on some glibc functions for I/O.
|
* It relies solely on a few glibc functions for input/output operations.
|
||||||
* Purely for educational purposes. Do not use in production.
|
* Designed purely for educational purposes and not intended for production use.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// glibc dependency
|
// glibc dependency
|
||||||
|
@ -37,19 +37,19 @@ enum {
|
||||||
char echo_buffer[ECHO_BUFFER_SIZE];
|
char echo_buffer[ECHO_BUFFER_SIZE];
|
||||||
int echo_size;
|
int echo_size;
|
||||||
|
|
||||||
int readchar() {
|
int next_char() {
|
||||||
int ch = getchar();
|
int ch = getchar();
|
||||||
if (ch != '\n') {
|
if (ch == '\n' || ch == -1) {
|
||||||
echo_buffer[echo_size++] = ch;
|
|
||||||
} else {
|
|
||||||
echo_buffer[echo_size++] = 0;
|
echo_buffer[echo_size++] = 0;
|
||||||
printf("#@%s\n", echo_buffer);
|
printf("#@%s\n", echo_buffer);
|
||||||
echo_size = 0;
|
echo_size = 0;
|
||||||
|
} else {
|
||||||
|
echo_buffer[echo_size++] = ch;
|
||||||
}
|
}
|
||||||
return ch;
|
return ch;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ungetchar(int ch) {
|
void unget_char(int ch) {
|
||||||
--echo_size;
|
--echo_size;
|
||||||
ungetc(ch, stdin);
|
ungetc(ch, stdin);
|
||||||
}
|
}
|
||||||
|
@ -239,16 +239,16 @@ int is_id_cont(int ch) {
|
||||||
|
|
||||||
int parse_int(int ch) {
|
int parse_int(int ch) {
|
||||||
int num = ch - '0';
|
int num = ch - '0';
|
||||||
while (is_digit(ch = readchar())) {
|
while (is_digit(ch = next_char())) {
|
||||||
num *= 10;
|
num *= 10;
|
||||||
num += ch - '0';
|
num += ch - '0';
|
||||||
}
|
}
|
||||||
ungetchar(ch);
|
unget_char(ch);
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_escaped_char() {
|
int get_escaped_char() {
|
||||||
int ch = readchar();
|
int ch = next_char();
|
||||||
if (ch == 'n') return '\n';
|
if (ch == 'n') return '\n';
|
||||||
if (ch == 't') return '\t';
|
if (ch == 't') return '\t';
|
||||||
if (ch == 'r') return '\r';
|
if (ch == 'r') return '\r';
|
||||||
|
@ -260,7 +260,7 @@ int get_escaped_char() {
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int token_state;
|
int token_unget;
|
||||||
int token_type;
|
int token_type;
|
||||||
int token_data;
|
int token_data;
|
||||||
|
|
||||||
|
@ -271,7 +271,7 @@ int string_lut_size;
|
||||||
int parse_string() {
|
int parse_string() {
|
||||||
int offset = string_offset;
|
int offset = string_offset;
|
||||||
int ch;
|
int ch;
|
||||||
while ((ch = readchar()) != '"') {
|
while ((ch = next_char()) != '"') {
|
||||||
if (ch == -1 || ch == '\n') {
|
if (ch == -1 || ch == '\n') {
|
||||||
fprintf(stderr, "expecting '\"'\n");
|
fprintf(stderr, "expecting '\"'\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
|
@ -286,24 +286,6 @@ int parse_string() {
|
||||||
return string_lut_size++;
|
return string_lut_size++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void rewind_string(int new_data) {
|
|
||||||
string_offset = string_lut[token_data];
|
|
||||||
token_data = new_data;
|
|
||||||
--string_lut_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
void dedup_string() {
|
|
||||||
int last_string = string_lut_size - 1;
|
|
||||||
char* latest = string_table + string_lut[last_string];
|
|
||||||
for (int i = 0; i < last_string; i++) {
|
|
||||||
char* candidate = string_table + string_lut[i];
|
|
||||||
if (streq(candidate, latest)) {
|
|
||||||
rewind_string(i);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
char id_table[ID_TABLE_SIZE];
|
char id_table[ID_TABLE_SIZE];
|
||||||
int id_offset;
|
int id_offset;
|
||||||
int id_lut[ID_LUT_SIZE];
|
int id_lut[ID_LUT_SIZE];
|
||||||
|
@ -311,33 +293,36 @@ int id_lut_size;
|
||||||
int parse_id(int ch) {
|
int parse_id(int ch) {
|
||||||
int offset = id_offset;
|
int offset = id_offset;
|
||||||
id_table[id_offset++] = ch;
|
id_table[id_offset++] = ch;
|
||||||
while (is_id_cont(ch = readchar())) {
|
while (is_id_cont(ch = next_char())) {
|
||||||
id_table[id_offset++] = ch;
|
id_table[id_offset++] = ch;
|
||||||
}
|
}
|
||||||
ungetchar(ch);
|
unget_char(ch);
|
||||||
id_table[id_offset++] = 0;
|
id_table[id_offset++] = 0;
|
||||||
id_lut[id_lut_size] = offset;
|
id_lut[id_lut_size] = offset;
|
||||||
return id_lut_size++;
|
return id_lut_size++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void rewind_id(int new_data) {
|
void dedup(char* table, int* lut, int* lut_size, int* offset) {
|
||||||
id_offset = id_lut[token_data];
|
char* latest = table + lut[*lut_size - 1];
|
||||||
token_data = new_data;
|
for (int i = 0; i < *lut_size - 1; i++) {
|
||||||
--id_lut_size;
|
char* candidate = table + lut[i];
|
||||||
}
|
|
||||||
|
|
||||||
void dedup_id() {
|
|
||||||
int last_id = id_lut_size - 1;
|
|
||||||
char* latest = id_table + id_lut[last_id];
|
|
||||||
for (int i = 0; i < last_id; i++) {
|
|
||||||
char* candidate = id_table + id_lut[i];
|
|
||||||
if (streq(candidate, latest)) {
|
if (streq(candidate, latest)) {
|
||||||
rewind_id(i);
|
*offset = lut[token_data];
|
||||||
|
--*lut_size;
|
||||||
|
token_data = i;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void dedup_string() {
|
||||||
|
dedup(string_table, string_lut, &string_lut_size, &string_offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
void dedup_id() {
|
||||||
|
dedup(id_table, id_lut, &id_lut_size, &id_offset);
|
||||||
|
}
|
||||||
|
|
||||||
void parse_id_like(int ch) {
|
void parse_id_like(int ch) {
|
||||||
token_type = TOKEN_ID;
|
token_type = TOKEN_ID;
|
||||||
token_data = parse_id(ch);
|
token_data = parse_id(ch);
|
||||||
|
@ -370,24 +355,25 @@ void parse_id_like(int ch) {
|
||||||
token_type = TOKEN_ENUM;
|
token_type = TOKEN_ENUM;
|
||||||
}
|
}
|
||||||
if (token_type != TOKEN_ID) {
|
if (token_type != TOKEN_ID) {
|
||||||
rewind_id(0);
|
id_offset = id_lut[token_data];
|
||||||
|
--id_lut_size;
|
||||||
} else {
|
} else {
|
||||||
dedup_id();
|
dedup_id();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void unget_token() {
|
void unget_token() {
|
||||||
token_state = 1;
|
token_unget = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void next_token() {
|
void next_token() {
|
||||||
if (token_state) {
|
if (token_unget) {
|
||||||
token_state = 0;
|
token_unget = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int ch = readchar();
|
int ch = next_char();
|
||||||
while (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n') {
|
while (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n') {
|
||||||
ch = readchar();
|
ch = next_char();
|
||||||
}
|
}
|
||||||
if (ch == -1) {
|
if (ch == -1) {
|
||||||
token_type = TOKEN_EOF;
|
token_type = TOKEN_EOF;
|
||||||
|
@ -404,50 +390,50 @@ void next_token() {
|
||||||
} else if (ch == '}') {
|
} else if (ch == '}') {
|
||||||
token_type = TOKEN_BRACE_RIGHT;
|
token_type = TOKEN_BRACE_RIGHT;
|
||||||
} else if (ch == '+') {
|
} else if (ch == '+') {
|
||||||
int ch2 = readchar();
|
int ch2 = next_char();
|
||||||
if (ch2 == '+') {
|
if (ch2 == '+') {
|
||||||
token_type = TOKEN_INC;
|
token_type = TOKEN_INC;
|
||||||
} else if (ch2 == '=') {
|
} else if (ch2 == '=') {
|
||||||
token_type = TOKEN_ADD_ASSIGN;
|
token_type = TOKEN_ADD_ASSIGN;
|
||||||
} else {
|
} else {
|
||||||
ungetchar(ch2);
|
unget_char(ch2);
|
||||||
token_type = TOKEN_ADD;
|
token_type = TOKEN_ADD;
|
||||||
}
|
}
|
||||||
} else if (ch == '-') {
|
} else if (ch == '-') {
|
||||||
int ch2 = readchar();
|
int ch2 = next_char();
|
||||||
if (ch2 == '-') {
|
if (ch2 == '-') {
|
||||||
token_type = TOKEN_DEC;
|
token_type = TOKEN_DEC;
|
||||||
} else if (ch2 == '=') {
|
} else if (ch2 == '=') {
|
||||||
token_type = TOKEN_SUB_ASSIGN;
|
token_type = TOKEN_SUB_ASSIGN;
|
||||||
} else {
|
} else {
|
||||||
ungetchar(ch2);
|
unget_char(ch2);
|
||||||
token_type = TOKEN_SUB;
|
token_type = TOKEN_SUB;
|
||||||
}
|
}
|
||||||
} else if (ch == '*') {
|
} else if (ch == '*') {
|
||||||
int ch2 = readchar();
|
int ch2 = next_char();
|
||||||
if (ch2 == '=') {
|
if (ch2 == '=') {
|
||||||
token_type = TOKEN_MUL_ASSIGN;
|
token_type = TOKEN_MUL_ASSIGN;
|
||||||
} else {
|
} else {
|
||||||
ungetchar(ch2);
|
unget_char(ch2);
|
||||||
token_type = TOKEN_MUL;
|
token_type = TOKEN_MUL;
|
||||||
}
|
}
|
||||||
} else if (ch == '/') {
|
} else if (ch == '/') {
|
||||||
int ch2 = readchar();
|
int ch2 = next_char();
|
||||||
if (ch2 == '=') {
|
if (ch2 == '=') {
|
||||||
token_type = TOKEN_DIV_ASSIGN;
|
token_type = TOKEN_DIV_ASSIGN;
|
||||||
} else if (ch2 == '/') {
|
} else if (ch2 == '/') {
|
||||||
do ch = readchar(); while (ch != -1 && ch != '\n');
|
do ch = next_char(); while (ch != -1 && ch != '\n');
|
||||||
next_token();
|
next_token();
|
||||||
return;
|
return;
|
||||||
} else if (ch2 == '*') {
|
} else if (ch2 == '*') {
|
||||||
while (1) {
|
while (1) {
|
||||||
ch = readchar();
|
ch = next_char();
|
||||||
if (ch == -1) {
|
if (ch == -1) {
|
||||||
fprintf(stderr, "expecting '*/'\n");
|
fprintf(stderr, "expecting '*/'\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
if (ch == '*') {
|
if (ch == '*') {
|
||||||
ch = readchar();
|
ch = next_char();
|
||||||
if (ch == '/') {
|
if (ch == '/') {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -456,15 +442,15 @@ void next_token() {
|
||||||
next_token();
|
next_token();
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
ungetchar(ch2);
|
unget_char(ch2);
|
||||||
token_type = TOKEN_DIV;
|
token_type = TOKEN_DIV;
|
||||||
}
|
}
|
||||||
} else if (ch == '%') {
|
} else if (ch == '%') {
|
||||||
int ch2 = readchar();
|
int ch2 = next_char();
|
||||||
if (ch2 == '=') {
|
if (ch2 == '=') {
|
||||||
token_type = TOKEN_REM_ASSIGN;
|
token_type = TOKEN_REM_ASSIGN;
|
||||||
} else {
|
} else {
|
||||||
ungetchar(ch2);
|
unget_char(ch2);
|
||||||
token_type = TOKEN_REM;
|
token_type = TOKEN_REM;
|
||||||
}
|
}
|
||||||
} else if (ch == ';') {
|
} else if (ch == ';') {
|
||||||
|
@ -476,90 +462,90 @@ void next_token() {
|
||||||
} else if (ch == ',') {
|
} else if (ch == ',') {
|
||||||
token_type = TOKEN_COMMA;
|
token_type = TOKEN_COMMA;
|
||||||
} else if (ch == '<') {
|
} else if (ch == '<') {
|
||||||
int ch2 = readchar();
|
int ch2 = next_char();
|
||||||
if (ch2 == '=') {
|
if (ch2 == '=') {
|
||||||
token_type = TOKEN_LE;
|
token_type = TOKEN_LE;
|
||||||
} else if (ch2 == '<') {
|
} else if (ch2 == '<') {
|
||||||
int ch3 = readchar();
|
int ch3 = next_char();
|
||||||
if (ch3 == '=') {
|
if (ch3 == '=') {
|
||||||
token_type = TOKEN_LSHIFT_ASSIGN;
|
token_type = TOKEN_LSHIFT_ASSIGN;
|
||||||
} else {
|
} else {
|
||||||
ungetchar(ch3);
|
unget_char(ch3);
|
||||||
token_type = TOKEN_LSHIFT;
|
token_type = TOKEN_LSHIFT;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ungetchar(ch2);
|
unget_char(ch2);
|
||||||
token_type = TOKEN_LT;
|
token_type = TOKEN_LT;
|
||||||
}
|
}
|
||||||
} else if (ch == '>') {
|
} else if (ch == '>') {
|
||||||
int ch2 = readchar();
|
int ch2 = next_char();
|
||||||
if (ch2 == '=') {
|
if (ch2 == '=') {
|
||||||
token_type = TOKEN_GE;
|
token_type = TOKEN_GE;
|
||||||
} else if (ch2 == '>') {
|
} else if (ch2 == '>') {
|
||||||
int ch3 = readchar();
|
int ch3 = next_char();
|
||||||
if (ch3 == '=') {
|
if (ch3 == '=') {
|
||||||
token_type = TOKEN_RSHIFT_ASSIGN;
|
token_type = TOKEN_RSHIFT_ASSIGN;
|
||||||
} else {
|
} else {
|
||||||
ungetchar(ch3);
|
unget_char(ch3);
|
||||||
token_type = TOKEN_RSHIFT;
|
token_type = TOKEN_RSHIFT;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ungetchar(ch2);
|
unget_char(ch2);
|
||||||
token_type = TOKEN_GT;
|
token_type = TOKEN_GT;
|
||||||
}
|
}
|
||||||
} else if (ch == '=') {
|
} else if (ch == '=') {
|
||||||
int ch2 = readchar();
|
int ch2 = next_char();
|
||||||
if (ch2 == '=') {
|
if (ch2 == '=') {
|
||||||
token_type = TOKEN_EQ;
|
token_type = TOKEN_EQ;
|
||||||
} else {
|
} else {
|
||||||
ungetchar(ch2);
|
unget_char(ch2);
|
||||||
token_type = TOKEN_ASSIGN;
|
token_type = TOKEN_ASSIGN;
|
||||||
}
|
}
|
||||||
} else if (ch == '!') {
|
} else if (ch == '!') {
|
||||||
int ch2 = readchar();
|
int ch2 = next_char();
|
||||||
if (ch2 == '=') {
|
if (ch2 == '=') {
|
||||||
token_type = TOKEN_NE;
|
token_type = TOKEN_NE;
|
||||||
} else {
|
} else {
|
||||||
ungetchar(ch2);
|
unget_char(ch2);
|
||||||
token_type = TOKEN_NOT;
|
token_type = TOKEN_NOT;
|
||||||
}
|
}
|
||||||
} else if (ch == '&') {
|
} else if (ch == '&') {
|
||||||
int ch2 = readchar();
|
int ch2 = next_char();
|
||||||
if (ch2 == '=') {
|
if (ch2 == '=') {
|
||||||
token_type = TOKEN_AND_ASSIGN;
|
token_type = TOKEN_AND_ASSIGN;
|
||||||
} else if (ch2 == '&') {
|
} else if (ch2 == '&') {
|
||||||
token_type = TOKEN_LAND;
|
token_type = TOKEN_LAND;
|
||||||
} else {
|
} else {
|
||||||
ungetchar(ch2);
|
unget_char(ch2);
|
||||||
token_type = TOKEN_AND;
|
token_type = TOKEN_AND;
|
||||||
}
|
}
|
||||||
} else if (ch == '|') {
|
} else if (ch == '|') {
|
||||||
int ch2 = readchar();
|
int ch2 = next_char();
|
||||||
if (ch2 == '=') {
|
if (ch2 == '=') {
|
||||||
token_type = TOKEN_OR_ASSIGN;
|
token_type = TOKEN_OR_ASSIGN;
|
||||||
} else if (ch2 == '|') {
|
} else if (ch2 == '|') {
|
||||||
token_type = TOKEN_LOR;
|
token_type = TOKEN_LOR;
|
||||||
} else {
|
} else {
|
||||||
ungetchar(ch2);
|
unget_char(ch2);
|
||||||
token_type = TOKEN_OR;
|
token_type = TOKEN_OR;
|
||||||
}
|
}
|
||||||
} else if (ch == '^') {
|
} else if (ch == '^') {
|
||||||
int ch2 = readchar();
|
int ch2 = next_char();
|
||||||
if (ch2 == '=') {
|
if (ch2 == '=') {
|
||||||
token_type = TOKEN_XOR_ASSIGN;
|
token_type = TOKEN_XOR_ASSIGN;
|
||||||
} else {
|
} else {
|
||||||
ungetchar(ch2);
|
unget_char(ch2);
|
||||||
token_type = TOKEN_XOR;
|
token_type = TOKEN_XOR;
|
||||||
}
|
}
|
||||||
} else if (ch == '~') {
|
} else if (ch == '~') {
|
||||||
token_type = TOKEN_INV;
|
token_type = TOKEN_INV;
|
||||||
} else if (ch == '\'') {
|
} else if (ch == '\'') {
|
||||||
token_type = TOKEN_NUMBER;
|
token_type = TOKEN_NUMBER;
|
||||||
token_data = readchar();
|
token_data = next_char();
|
||||||
if (token_data == '\\') {
|
if (token_data == '\\') {
|
||||||
token_data = get_escaped_char();
|
token_data = get_escaped_char();
|
||||||
}
|
}
|
||||||
if (readchar() != '\'') {
|
if (next_char() != '\'') {
|
||||||
fprintf(stderr, "expecting '\n");
|
fprintf(stderr, "expecting '\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
@ -569,8 +555,8 @@ void next_token() {
|
||||||
dedup_string();
|
dedup_string();
|
||||||
} else if (ch == '.') {
|
} else if (ch == '.') {
|
||||||
token_type = 0;
|
token_type = 0;
|
||||||
if (readchar() == '.') {
|
if (next_char() == '.') {
|
||||||
if (readchar() == '.') {
|
if (next_char() == '.') {
|
||||||
token_type = TOKEN_ELLIPSIS;
|
token_type = TOKEN_ELLIPSIS;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -587,16 +573,6 @@ void next_token() {
|
||||||
fprintf(stderr, "unexpected character: %c(%d)\n", ch, ch);
|
fprintf(stderr, "unexpected character: %c(%d)\n", ch, ch);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
// debug info
|
|
||||||
if (0) {
|
|
||||||
fprintf(stderr, "token: %d\n", token_type);
|
|
||||||
if (token_type == TOKEN_ID) {
|
|
||||||
char* name = id_table + id_lut[token_data];
|
|
||||||
fprintf(stderr, " id: %s\n", name);
|
|
||||||
} else if (token_type == TOKEN_NUMBER) {
|
|
||||||
fprintf(stderr, " number: %d\n", token_data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void expect_token(int expected_type) {
|
void expect_token(int expected_type) {
|
||||||
|
@ -917,7 +893,7 @@ void _asm_ri(char* op, int rd, int rs1, int imm) {
|
||||||
|
|
||||||
void asm_branch(char* op, int rs1, int label) {
|
void asm_branch(char* op, int rs1, int label) {
|
||||||
char* rs1_name = trivialize(rs1, REG_T0);
|
char* rs1_name = trivialize(rs1, REG_T0);
|
||||||
printf(" %s %s, L%d\n", op, rs1_name, label);
|
printf(" %s %s, .L%d\n", op, rs1_name, label);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _asm_i(char* op, int rd, char* prefix1, char* prefix2, int imm) {
|
void _asm_i(char* op, int rd, char* prefix1, char* prefix2, int imm) {
|
||||||
|
@ -1032,7 +1008,7 @@ void asm_bnez(int rs1, int label) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void asm_j(int label) {
|
void asm_j(int label) {
|
||||||
printf(" j L%d\n", label);
|
printf(" j .L%d\n", label);
|
||||||
}
|
}
|
||||||
|
|
||||||
int next_label_id = 0;
|
int next_label_id = 0;
|
||||||
|
@ -1041,7 +1017,7 @@ int next_label() {
|
||||||
}
|
}
|
||||||
|
|
||||||
int asm_label(int label) {
|
int asm_label(int label) {
|
||||||
printf("L%d:\n", label);
|
printf(".L%d:\n", label);
|
||||||
return label;
|
return label;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1721,15 +1697,17 @@ void parse_for() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void parse_do_while() {
|
void parse_do_while() {
|
||||||
|
int body_label = next_label();
|
||||||
int cont_label = next_label();
|
int cont_label = next_label();
|
||||||
int break_label = next_label();
|
int break_label = next_label();
|
||||||
asm_push_label(break_label, cont_label);
|
asm_push_label(break_label, cont_label);
|
||||||
asm_label(cont_label);
|
asm_label(body_label);
|
||||||
parse_stmt(); // body
|
parse_stmt(); // body
|
||||||
expect_token(TOKEN_WHILE);
|
expect_token(TOKEN_WHILE);
|
||||||
expect_token(TOKEN_PAREN_LEFT);
|
expect_token(TOKEN_PAREN_LEFT);
|
||||||
|
asm_label(cont_label);
|
||||||
int cond = parse_expr();
|
int cond = parse_expr();
|
||||||
asm_bnez(cond, cont_label);
|
asm_bnez(cond, body_label);
|
||||||
expect_token(TOKEN_PAREN_RIGHT);
|
expect_token(TOKEN_PAREN_RIGHT);
|
||||||
asm_label(break_label);
|
asm_label(break_label);
|
||||||
asm_pop_label();
|
asm_pop_label();
|
||||||
|
@ -1892,7 +1870,7 @@ void parse_function(char* name) {
|
||||||
|
|
||||||
void parse_global_variable(int id, char* name, int type) {
|
void parse_global_variable(int id, char* name, int type) {
|
||||||
printf(".data\n");
|
printf(".data\n");
|
||||||
printf(".globl %s\n", name);
|
printf(".global %s\n", name);
|
||||||
printf(".align 5\n");
|
printf(".align 5\n");
|
||||||
printf("%s:\n", name);
|
printf("%s:\n", name);
|
||||||
if (token_type == TOKEN_ASSIGN) {
|
if (token_type == TOKEN_ASSIGN) {
|
||||||
|
@ -1993,7 +1971,6 @@ void parse_top_level() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void dump_string_table() {
|
void dump_string_table() {
|
||||||
printf("# string table: \n");
|
|
||||||
printf(".data\n");
|
printf(".data\n");
|
||||||
for (int i = 0; i < string_lut_size; ++i) {
|
for (int i = 0; i < string_lut_size; ++i) {
|
||||||
printf(".LC%d: .string \"", i);
|
printf(".LC%d: .string \"", i);
|
||||||
|
|
1
demo/empty.c
Normal file
1
demo/empty.c
Normal file
|
@ -0,0 +1 @@
|
||||||
|
int main() {}
|
|
@ -2,6 +2,7 @@ int printf(char* format, ...);
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int i;
|
int i;
|
||||||
|
printf("hello for 10 times\n");
|
||||||
for (i = 0;
|
for (i = 0;
|
||||||
i < 10;
|
i < 10;
|
||||||
++i)
|
++i)
|
||||||
|
|
10
test.sh
10
test.sh
|
@ -35,17 +35,23 @@ for D in *; do
|
||||||
if [ -f $i.out ]; then
|
if [ -f $i.out ]; then
|
||||||
../boot.elf < $i.c > $i.s &&
|
../boot.elf < $i.c > $i.s &&
|
||||||
compile $i
|
compile $i
|
||||||
|
gcc $i.c -o $i.ref.elf 2>/dev/null
|
||||||
if [[ $? == 0 ]]; then
|
if [[ $? == 0 ]]; then
|
||||||
if [ -f $i.in ]; then
|
if [ -f $i.in ]; then
|
||||||
run_with_input $i
|
run_with_input $i
|
||||||
|
echo $? >> $i.ans
|
||||||
|
./$i.ref.elf < $i.in > $i.out
|
||||||
|
echo $? >> $i.out
|
||||||
else
|
else
|
||||||
run_without_input $i
|
run_without_input $i
|
||||||
|
echo $? >> $i.ans
|
||||||
|
./$i.ref.elf > $i.out
|
||||||
|
echo $? >> $i.out
|
||||||
fi
|
fi
|
||||||
echo $? >> $i.ans
|
|
||||||
cmp $i.out $i.ans
|
cmp $i.out $i.ans
|
||||||
failed=$?
|
failed=$?
|
||||||
if [[ $failed == 0 ]]; then
|
if [[ $failed == 0 ]]; then
|
||||||
rm $i.ans $i.elf $i.s
|
rm $i.ans $i.elf $i.s $i.ref.elf
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
failed=1
|
failed=1
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
int printf(char* format, ...);
|
int printf(char* format, ...);
|
||||||
int scanf(char* format, ...);
|
|
||||||
int exit(int status);
|
int exit(int status);
|
||||||
|
|
||||||
void assert_eq(int expected, int actual) {
|
void assert_eq(int expected, int actual) {
|
||||||
|
@ -23,9 +22,7 @@ void check(int a[], int i, int j) {
|
||||||
void check_all(int a[], int n) {
|
void check_all(int a[], int n) {
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
for (int j = i + 1; j < n; j++) {
|
for (int j = i + 1; j < n; j++) {
|
||||||
if (a[i] > a[j]) {
|
check(a, i, j);
|
||||||
check(a, i, j);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
54
test/array/bigint.c
Normal file
54
test/array/bigint.c
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
int printf(char* format, ...);
|
||||||
|
int getchar();
|
||||||
|
|
||||||
|
|
||||||
|
void read(int* a) {
|
||||||
|
a[0] = 0;
|
||||||
|
while (1) {
|
||||||
|
int c = getchar();
|
||||||
|
if (c == '\n' || c == -1) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (c >= '0' && c <= '9') {
|
||||||
|
a[++a[0]] = c - '0';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int j = 1; j <= a[0] / 2; j++) {
|
||||||
|
int temp = a[j];
|
||||||
|
a[j] = a[a[0] - j + 1];
|
||||||
|
a[a[0] - j + 1] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void write(int* a) {
|
||||||
|
int n = a[0];
|
||||||
|
for (int i = n; i > 0; --i) {
|
||||||
|
printf("%d", a[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void add(int* a, int* b) {
|
||||||
|
int n = a[0] > b[0] ? a[0] : b[0];
|
||||||
|
int carry = 0;
|
||||||
|
for (int i = 1; i <= n; i++) {
|
||||||
|
int sum = a[i] + b[i] + carry;
|
||||||
|
a[i] = sum % 10;
|
||||||
|
carry = sum / 10;
|
||||||
|
}
|
||||||
|
if (carry) {
|
||||||
|
a[n + 1] = carry;
|
||||||
|
a[0] = n + 1;
|
||||||
|
} else {
|
||||||
|
a[0] = n;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int a[100];
|
||||||
|
int b[100];
|
||||||
|
read(a);
|
||||||
|
read(b);
|
||||||
|
add(a, b);
|
||||||
|
write(a);
|
||||||
|
}
|
2
test/array/bigint.in
Normal file
2
test/array/bigint.in
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
98111111111111111111111111111111107777777777777777777777777777
|
||||||
|
89222222222222222222222222222222208888888888888888888888888888
|
2
test/array/bigint.out
Normal file
2
test/array/bigint.out
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
187333333333333333333333333333333316666666666666666666666666665
|
||||||
|
0
|
|
@ -1,6 +1,5 @@
|
||||||
int printf(char* format, ...);
|
int printf(char* format, ...);
|
||||||
int scanf(char* format, ...);
|
int scanf(char* format, ...);
|
||||||
int exit(int status);
|
|
||||||
|
|
||||||
void swap(int* a, int* b) {
|
void swap(int* a, int* b) {
|
||||||
int t = *a;
|
int t = *a;
|
18
test/array/fib.c
Normal file
18
test/array/fib.c
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
int printf(char* format, ...);
|
||||||
|
int scanf(char* format, ...);
|
||||||
|
|
||||||
|
int fib[100];
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int n;
|
||||||
|
scanf("%d", &n);
|
||||||
|
fib[0] = 0;
|
||||||
|
fib[1] = 1;
|
||||||
|
for (int i = 2; i < n; i++) {
|
||||||
|
fib[i] = fib[i - 1] + fib[i - 2];
|
||||||
|
}
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
printf("%d ", fib[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
1
test/array/fib.in
Normal file
1
test/array/fib.in
Normal file
|
@ -0,0 +1 @@
|
||||||
|
20
|
2
test/array/fib.out
Normal file
2
test/array/fib.out
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
|
||||||
|
0
|
65
test/array/heap.c
Normal file
65
test/array/heap.c
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
int printf(char* format, ...);
|
||||||
|
int scanf(char* format, ...);
|
||||||
|
|
||||||
|
// sort by a max heap
|
||||||
|
|
||||||
|
int heap[100];
|
||||||
|
int heap_size = 0;
|
||||||
|
|
||||||
|
void push_heap(int value) {
|
||||||
|
heap[heap_size] = value;
|
||||||
|
int i = heap_size;
|
||||||
|
heap_size++;
|
||||||
|
while (i > 0) {
|
||||||
|
int parent = (i - 1) / 2;
|
||||||
|
if (heap[i] > heap[parent]) {
|
||||||
|
int temp = heap[i];
|
||||||
|
heap[i] = heap[parent];
|
||||||
|
heap[parent] = temp;
|
||||||
|
i = parent;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int pop_heap() {
|
||||||
|
int value = heap[0];
|
||||||
|
heap_size--;
|
||||||
|
heap[0] = heap[heap_size];
|
||||||
|
int i = 0;
|
||||||
|
while (i < heap_size) {
|
||||||
|
int left = 2 * i + 1;
|
||||||
|
int right = 2 * i + 2;
|
||||||
|
if (left >= heap_size) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
int max_child = left;
|
||||||
|
if (right < heap_size && heap[right] > heap[left]) {
|
||||||
|
max_child = right;
|
||||||
|
}
|
||||||
|
if (heap[i] < heap[max_child]) {
|
||||||
|
int temp = heap[i];
|
||||||
|
heap[i] = heap[max_child];
|
||||||
|
heap[max_child] = temp;
|
||||||
|
i = max_child;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int n;
|
||||||
|
scanf("%d", &n);
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
int value;
|
||||||
|
scanf("%d", &value);
|
||||||
|
push_heap(value);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
printf("%d ", pop_heap());
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
11
test/array/heap.in
Normal file
11
test/array/heap.in
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
100
|
||||||
|
13 49 15 58 24 74 80 81 69 23
|
||||||
|
67 88 59 39 1 12 73 50 55 53
|
||||||
|
71 63 9 90 87 89 51 75 40 84
|
||||||
|
25 94 68 47 48 14 99 33 62 79
|
||||||
|
66 85 56 31 38 29 86 46 70 6
|
||||||
|
10 19 64 72 45 4 11 42 78 7
|
||||||
|
95 27 93 57 21 35 5 22 76 54
|
||||||
|
44 98 61 32 17 92 65 36 20 28
|
||||||
|
83 2 18 60 16 41 30 37 100 97
|
||||||
|
77 3 82 8 26 34 91 43 96 52
|
2
test/array/heap.out
Normal file
2
test/array/heap.out
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
|
||||||
|
0
|
41
test/array/select.c
Normal file
41
test/array/select.c
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
int printf(char* format, ...);
|
||||||
|
int scanf(char* format, ...);
|
||||||
|
|
||||||
|
int a[100];
|
||||||
|
int n;
|
||||||
|
|
||||||
|
void swap(int* a, int* b) {
|
||||||
|
int t = *a;
|
||||||
|
*a = *b;
|
||||||
|
*b = t;
|
||||||
|
}
|
||||||
|
|
||||||
|
int* max_element(int a[], int n) {
|
||||||
|
int* max = a;
|
||||||
|
for (int i = 1; i < n; i++) {
|
||||||
|
if (a[i] > *max) {
|
||||||
|
max = &a[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sort(int a[], int n) {
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
int* max = max_element(a, n - i);
|
||||||
|
swap(max, &a[n - i - 1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
scanf("%d", &n);
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
scanf("%d", &a[i]);
|
||||||
|
}
|
||||||
|
sort(a, n);
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
printf("%d ", a[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
11
test/array/select.in
Normal file
11
test/array/select.in
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
100
|
||||||
|
13 49 15 58 24 74 80 81 69 23
|
||||||
|
67 88 59 39 1 12 73 50 55 53
|
||||||
|
71 63 9 90 87 89 51 75 40 84
|
||||||
|
25 94 68 47 48 14 99 33 62 79
|
||||||
|
66 85 56 31 38 29 86 46 70 6
|
||||||
|
10 19 64 72 45 4 11 42 78 7
|
||||||
|
95 27 93 57 21 35 5 22 76 54
|
||||||
|
44 98 61 32 17 92 65 36 20 28
|
||||||
|
83 2 18 60 16 41 30 37 100 97
|
||||||
|
77 3 82 8 26 34 91 43 96 52
|
2
test/array/select.out
Normal file
2
test/array/select.out
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
||||||
|
0
|
13
test/branch/if-else-if.c
Normal file
13
test/branch/if-else-if.c
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
int printf(char* format, ...);
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int a = 5;
|
||||||
|
if (a == 3) {
|
||||||
|
printf("a is 3\n");
|
||||||
|
} else if (a == 4) {
|
||||||
|
printf("a is 4\n");
|
||||||
|
} else {
|
||||||
|
printf("a is not 3 or 4\n");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
2
test/branch/if-else-if.out
Normal file
2
test/branch/if-else-if.out
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
a is not 3 or 4
|
||||||
|
0
|
10
test/branch/if-else.c
Normal file
10
test/branch/if-else.c
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
int printf(char* format, ...);
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int a = 3;
|
||||||
|
if (a == 3) {
|
||||||
|
printf("a is 3\n");
|
||||||
|
} else {
|
||||||
|
printf("a is not 3\n");
|
||||||
|
}
|
||||||
|
}
|
2
test/branch/if-else.out
Normal file
2
test/branch/if-else.out
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
a is 3
|
||||||
|
0
|
9
test/branch/if.c
Normal file
9
test/branch/if.c
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
int printf(char* format, ...);
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int a = 3;
|
||||||
|
if (a != 3) {
|
||||||
|
printf("a is not 3\n");
|
||||||
|
}
|
||||||
|
printf("a = %d\n", a);
|
||||||
|
}
|
2
test/branch/if.out
Normal file
2
test/branch/if.out
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
a = 3
|
||||||
|
0
|
|
@ -3,34 +3,28 @@ int printf(char* format, ...);
|
||||||
int main() {
|
int main() {
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
// For loop
|
|
||||||
printf("For loop:\n");
|
|
||||||
for (i = 0; i < 5; i++) {
|
for (i = 0; i < 5; i++) {
|
||||||
if (i == 3) {
|
if (i == 3) {
|
||||||
break; // Exit the loop when i is 3
|
break;
|
||||||
}
|
}
|
||||||
printf("%d ", i);
|
printf("%d ", i);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
// While loop
|
|
||||||
printf("While loop:\n");
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < 5) {
|
while (i < 5) {
|
||||||
if (i == 3) {
|
if (i == 3) {
|
||||||
break; // Exit the loop when i is 3
|
break;
|
||||||
}
|
}
|
||||||
printf("%d ", i);
|
printf("%d ", i);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
// Do-while loop
|
|
||||||
printf("Do-while loop:\n");
|
|
||||||
i = 0;
|
i = 0;
|
||||||
do {
|
do {
|
||||||
if (i == 3) {
|
if (i == 3) {
|
||||||
break; // Exit the loop when i is 3
|
break;
|
||||||
}
|
}
|
||||||
printf("%d ", i);
|
printf("%d ", i);
|
||||||
i++;
|
i++;
|
||||||
|
|
|
@ -1,7 +1,4 @@
|
||||||
For loop:
|
|
||||||
0 1 2
|
0 1 2
|
||||||
While loop:
|
|
||||||
0 1 2
|
0 1 2
|
||||||
Do-while loop:
|
|
||||||
0 1 2
|
0 1 2
|
||||||
0
|
0
|
||||||
|
|
|
@ -3,36 +3,33 @@ int printf(char* format, ...);
|
||||||
int main() {
|
int main() {
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
// For loop
|
|
||||||
printf("For loop:\n");
|
printf("For loop:\n");
|
||||||
for (i = 0; i < 5; i++) {
|
for (i = 0; i < 5; i++) {
|
||||||
if (i == 3) {
|
if (i > 3) {
|
||||||
continue; // Skip the rest of the loop when i is 3
|
continue;
|
||||||
}
|
}
|
||||||
printf("%d ", i);
|
printf("%d ", i);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
// While loop
|
|
||||||
printf("While loop:\n");
|
printf("While loop:\n");
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < 5) {
|
while (i < 5) {
|
||||||
if (i == 3) {
|
if (i > 3) {
|
||||||
i++;
|
i++;
|
||||||
continue; // Skip the rest of the loop when i is 3
|
continue;
|
||||||
}
|
}
|
||||||
printf("%d ", i);
|
printf("%d ", i);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
// Do-while loop
|
|
||||||
printf("Do-while loop:\n");
|
printf("Do-while loop:\n");
|
||||||
i = 0;
|
i = 0;
|
||||||
do {
|
do {
|
||||||
if (i == 3) {
|
if (i > 3) {
|
||||||
i++;
|
i++;
|
||||||
continue; // Skip the rest of the loop when i is 3
|
continue;
|
||||||
}
|
}
|
||||||
printf("%d ", i);
|
printf("%d ", i);
|
||||||
i++;
|
i++;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
For loop:
|
For loop:
|
||||||
0 1 2 4
|
0 1 2 3
|
||||||
While loop:
|
While loop:
|
||||||
0 1 2 4
|
0 1 2 3
|
||||||
Do-while loop:
|
Do-while loop:
|
||||||
0 1 2 4
|
0 1 2 3
|
||||||
0
|
0
|
||||||
|
|
11
test/loop/do-while.c
Normal file
11
test/loop/do-while.c
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
int printf(char* format, ...);
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int i = 0;
|
||||||
|
do {
|
||||||
|
printf("%d ", i);
|
||||||
|
i++;
|
||||||
|
} while (i < 5);
|
||||||
|
printf("\n");
|
||||||
|
return 0;
|
||||||
|
}
|
2
test/loop/do-while.out
Normal file
2
test/loop/do-while.out
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
0 1 2 3 4
|
||||||
|
0
|
10
test/loop/for.c
Normal file
10
test/loop/for.c
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
int printf(char* format, ...);
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int i = 0;
|
||||||
|
for (i = 0; i < 5; i++) {
|
||||||
|
printf("%d ", i);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
return 0;
|
||||||
|
}
|
2
test/loop/for.out
Normal file
2
test/loop/for.out
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
0 1 2 3 4
|
||||||
|
0
|
11
test/loop/while.c
Normal file
11
test/loop/while.c
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
int printf(char* format, ...);
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int i = 0;
|
||||||
|
while (i < 5) {
|
||||||
|
printf("%d ", i);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
return 0;
|
||||||
|
}
|
2
test/loop/while.out
Normal file
2
test/loop/while.out
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
0 1 2 3 4
|
||||||
|
0
|
|
@ -1,5 +1,6 @@
|
||||||
import sys
|
import sys
|
||||||
import html
|
import html
|
||||||
|
from string import Template
|
||||||
|
|
||||||
src = ''
|
src = ''
|
||||||
asm = ''
|
asm = ''
|
||||||
|
@ -16,26 +17,22 @@ for line in sys.stdin:
|
||||||
style += 1
|
style += 1
|
||||||
style %= 5
|
style %= 5
|
||||||
flip = False
|
flip = False
|
||||||
elif line.startswith("# string table: "):
|
elif line.startswith(".L") or line.startswith(".data") or line.startswith(".text"):
|
||||||
asm += line
|
asm += line
|
||||||
break
|
|
||||||
else:
|
else:
|
||||||
asm += decorate(line)
|
asm += decorate(line)
|
||||||
flip = True
|
flip = True
|
||||||
|
|
||||||
for line in sys.stdin:
|
|
||||||
asm += line
|
|
||||||
|
|
||||||
title = 'RVBTCC Code Gen Demo'
|
title = 'RVBTCC Code Gen Demo'
|
||||||
|
|
||||||
|
|
||||||
template = '''
|
template = Template('''
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title><%title%></title>
|
<title>${title}</title>
|
||||||
<style>
|
<style>
|
||||||
body {
|
body {
|
||||||
font-family: Arial, sans-serif;
|
font-family: Arial, sans-serif;
|
||||||
|
@ -90,25 +87,25 @@ template = '''
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<header>
|
<header>
|
||||||
<h1><%title%></h1>
|
<h1>${title}</h1>
|
||||||
</header>
|
</header>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<div class="column">
|
<div class="column">
|
||||||
<h2>Source Code</h2>
|
<h2>Source Code</h2>
|
||||||
<pre>
|
<pre>
|
||||||
<code><%src%></code>
|
<code>${src}</code>
|
||||||
</pre>
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="column">
|
<div class="column">
|
||||||
<h2>Assembly</h2>
|
<h2>Assembly</h2>
|
||||||
<pre>
|
<pre>
|
||||||
<code><%asm%></code>
|
<code>${asm}</code>
|
||||||
</pre>
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
'''
|
''')
|
||||||
|
|
||||||
print(template.replace("<%title%>", title).replace("<%src%>", src).replace("<%asm%>", asm))
|
print(template.substitute(title=title, src=src, asm=asm))
|
Loading…
Add table
Add a link
Reference in a new issue