Skip to content

Commit

Permalink
implemented escape sequenes
Browse files Browse the repository at this point in the history
  • Loading branch information
AnonymousAAArdvark committed Dec 31, 2022
1 parent a09ce43 commit 7a585c2
Show file tree
Hide file tree
Showing 18 changed files with 125 additions and 55 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,12 @@ For a more extensive look into Qi, go to the [Language Guide]().
- [x] Front Page
- [x] Quick Start
- [x] Language Guide
- [ ] Escape Sequences
- [x] Escape Sequences
- [ ] Bitwise operators
- [ ] Remove semicolons
- [ ] Support scientific notation, binary, etc. numbers
- [ ] More string methods
- [ ] More list methods
Expand Down
5 changes: 4 additions & 1 deletion README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,12 @@ All 274 tests passed (641 expectations).
- [x] 首页
- [x] 入门
- [x] 语言指南
- [ ] 转义序列
- [x] 转义序列
- [ ] 按位运算符
- [ ] 删除分号
- [ ] 支持科学记数法、二进制等数字
- [ ] 更多字符串方法
- [ ] 更多列表方法


<!-- CONTRIBUTING -->
Expand Down
5 changes: 4 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,12 @@ For a more extensive look into Qi, go to the [Language Guide](syntax.md).
- [x] Front Page
- [x] Quick Start
- [x] Language Guide
- [ ] Escape Sequences
- [x] Escape Sequences
- [ ] Bitwise operators
- [ ] Remove semicolons
- [ ] Support scientific notation, binary, etc. numbers
- [ ] More string methods
- [ ] More list methods

<!-- LICENSE -->
## License
Expand Down
16 changes: 16 additions & 0 deletions docs/string.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ To declare a literal, surround text in two double quotes ("").
"你好,世界"
```

## Escaping
Most other programming languages use a backslash (\) followed by a letter or combination of digits to declare escape sequences. However, since the backslash is not readily available on the standard pinyin keyboard, Qi uses the middle dot (·) in place of the backslash.
A handful of escape characters are supported:
```c
"·"" // A double quote character.
"··" // A middle dot.
"·a" // Alarm beep.
"·b" // Backspace.
"·f" // Formfeed.
"·n" // Newline.
"·r" // Carriage return.
"·t" // Tab.
"·v" // Vertical tab.
```
> For more information on escape sequences, reference [this article](https://en.wikipedia.org/wiki/Escape_sequences_in_C).
## Static Methods
#### string。**长度**()
Expand Down
6 changes: 4 additions & 2 deletions docs/zh-cn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@
- [x] 首页
- [x] 入门
- [x] 语言指南
- [ ] 转义序列
- [x] 转义序列
- [ ] 按位运算符
- [ ] 删除分号

- [ ] 支持科学记数法、二进制等数字
- [ ] 更多字符串方法
- [ ] 更多列表方法


<!-- LICENSE -->
Expand Down
16 changes: 16 additions & 0 deletions docs/zh-cn/string.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@
"你好,世界"
```

## 转义
大多数其他编程语言使用反斜杠 (\) 后跟一个字母或数字组合来声明转义序列。但是,由于标准拼音键盘上没有现成的反斜杠,气使用中间点 (·) 代替反斜杠。
支持少数转义字符:
```c
"·"" // 一个双引号字符。
"··" // 中间点。
"·a" // 警报声。
"·b" // 退格。
"·f" // 换页。
"·n" // 换行符。
"·r" // 回车。
"·t" // 选项卡。
"·v" // 垂直制表符。
```
> 有关转义序列的更多信息,请参考 [这篇文章](https://en.wikipedia.org/wiki/Escape_sequences_in_C)。
## 静态方法
#### 字符串。**长度**()
Expand Down
68 changes: 23 additions & 45 deletions src/cmake-build-release/CMakeFiles/qi.dir/C.includecache
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,27 @@ stdint.h
wchar.h
-

/Users/andrewyang/CLionProjects/qi/src/compiler.h
object.h
/Users/andrewyang/CLionProjects/qi/src/object.h
vm.h
/Users/andrewyang/CLionProjects/qi/src/vm.h

/Users/andrewyang/CLionProjects/qi/src/debug.h
chunk.h
/Users/andrewyang/CLionProjects/qi/src/chunk.h

/Users/andrewyang/CLionProjects/qi/src/lib.h
/Users/andrewyang/CLionProjects/qi/src/memory.h
common.h
/Users/andrewyang/CLionProjects/qi/src/common.h
object.h
/Users/andrewyang/CLionProjects/qi/src/object.h
memory.h
/Users/andrewyang/CLionProjects/qi/src/memory.h
vm.h
/Users/andrewyang/CLionProjects/qi/src/vm.h

/Users/andrewyang/CLionProjects/qi/src/object.c
stdio.h
-
string.h
-
memory.h
/Users/andrewyang/CLionProjects/qi/src/memory.h
common.h
/Users/andrewyang/CLionProjects/qi/src/common.h
object.h
/Users/andrewyang/CLionProjects/qi/src/object.h
table.h
/Users/andrewyang/CLionProjects/qi/src/table.h
value.h
/Users/andrewyang/CLionProjects/qi/src/value.h
vm.h
/Users/andrewyang/CLionProjects/qi/src/vm.h

/Users/andrewyang/CLionProjects/qi/src/object.h
common.h
Expand All @@ -58,43 +54,25 @@ table.h
value.h
/Users/andrewyang/CLionProjects/qi/src/value.h

/Users/andrewyang/CLionProjects/qi/src/table.h

/Users/andrewyang/CLionProjects/qi/src/value.h
/Users/andrewyang/CLionProjects/qi/src/scanner.c
stdio.h
-
string.h
-
common.h
/Users/andrewyang/CLionProjects/qi/src/common.h
scanner.h
/Users/andrewyang/CLionProjects/qi/src/scanner.h

/Users/andrewyang/CLionProjects/qi/src/vm.c
stdarg.h
-
stdio.h
-
stdlib.h
-
/Users/andrewyang/CLionProjects/qi/src/scanner.h

/Users/andrewyang/CLionProjects/qi/src/table.h

/Users/andrewyang/CLionProjects/qi/src/value.h
string.h
-
time.h
-
ctype.h
-
math.h
-
common.h
/Users/andrewyang/CLionProjects/qi/src/common.h
compiler.h
/Users/andrewyang/CLionProjects/qi/src/compiler.h
debug.h
/Users/andrewyang/CLionProjects/qi/src/debug.h
object.h
/Users/andrewyang/CLionProjects/qi/src/object.h
memory.h
/Users/andrewyang/CLionProjects/qi/src/memory.h
vm.h
/Users/andrewyang/CLionProjects/qi/src/vm.h
lib.h
/Users/andrewyang/CLionProjects/qi/src/lib.h

/Users/andrewyang/CLionProjects/qi/src/vm.h
chunk.h
Expand Down
Binary file modified src/cmake-build-release/CMakeFiles/qi.dir/compiler.c.o
Binary file not shown.
Binary file modified src/cmake-build-release/CMakeFiles/qi.dir/object.c.o
Binary file not shown.
Binary file modified src/cmake-build-release/CMakeFiles/qi.dir/scanner.c.o
Binary file not shown.
4 changes: 2 additions & 2 deletions src/cmake-build-release/Testing/Temporary/LastTest.log
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Start testing: Dec 25 23:31 MST
Start testing: Dec 30 23:42 MST
----------------------------------------------------------
End testing: Dec 25 23:31 MST
End testing: Dec 30 23:42 MST
Binary file modified src/cmake-build-release/qi
Binary file not shown.
6 changes: 3 additions & 3 deletions src/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ static void binary(bool canAssign) {
static void postfix(bool canAssign) {
TokenType operatorType = parser.previous.type;

switch(parser.previous.type) {
switch (parser.previous.type) {
case TOKEN_PLUS_PLUS:
case TOKEN_MINUS_MINUS: {
uint8_t op1 = -1, op2 = -1;
Expand Down Expand Up @@ -528,8 +528,8 @@ static void or_(bool canAssign) {
}

static void string(bool canAssign) {
emitConstant(OBJ_VAL(copyString(parser.previous.start + 1,
parser.previous.length - 2)));
emitConstant(OBJ_VAL(handleEscapeSequences(copyString(parser.previous.start + 1,
parser.previous.length - 2))));
}

static void list(bool canAssign) {
Expand Down
41 changes: 41 additions & 0 deletions src/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,47 @@ ObjString* copyString(const wchar_t* chars, int length) {
return allocateString(heapChars, length, hash);
}

ObjString* handleEscapeSequences(ObjString* string) {
wchar_t *here = string->chars;
size_t len = string->length;
int num;
int num_len;

while (NULL != (here = wcschr(here,L'·'))) {
num_len = 1;
switch (here[1]) {
case L'r':
*here = L'\r';
break;
case L'b':
*here = L'\b';
break;
case L'f':
*here = L'\f';
break;
case L'n':
*here = L'\n';
break;
case L't':
*here = L'\t';
break;
case L'v':
*here = L'\v';
break;
case L'a':
*here = L'\a';
break;
default:
*here = here[1];
break;
}
num = here - string->chars + num_len;
here++;
wmemmove(here, here + num_len, len - num);
}
return string;
}

void storeToString(ObjString* string, int index, wchar_t value) {
string->chars[index] = value;
}
Expand Down
1 change: 1 addition & 0 deletions src/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ ObjInstance* newInstance(ObjClass* klass);
ObjNative* newNative(NativeFn function, int arity);
ObjString* takeString(wchar_t* chars, int length);
ObjString* copyString(const wchar_t* chars, int length);
ObjString* handleEscapeSequences(ObjString* string);
void storeToString(ObjString* string, int index, wchar_t value);
wchar_t indexFromString(ObjString* string, int index);
bool isValidStringIndex(ObjString* string, int index);
Expand Down
1 change: 1 addition & 0 deletions src/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ static Token number() {
static Token string() {
while (peek() != L'"' && !isAtEnd()) {
if (peek() == L'\n') scanner.line++;
else if (peek() == L'·') advance();
advance();
}

Expand Down
1 change: 1 addition & 0 deletions test/scratch.qi
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// 不考
打印行(四舍五入(1.2));
打印行("··");
5 changes: 5 additions & 0 deletions test/string/escape_sequences.qi
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
打印行("·"你好,世界!·""); // 期待:"你好,世界!"
打印("你好,世界!·n"); // 期待:你好,世界!
打印("你好,世界!·p··"); // 期待:你好,世界!p·


0 comments on commit 7a585c2

Please sign in to comment.