fscript support c++ like comments

This commit is contained in:
lixianjing 2021-01-07 11:34:05 +08:00
parent 7234d9dd25
commit 061ae729a5
4 changed files with 109 additions and 21 deletions

View File

@ -54,6 +54,22 @@ print("hello fscript")
* 字符串类型 (string)。UTF-8 字符串,用英文双引号扩起来。
* 布尔类型 (bool)。标准取值为true 和 false非 0 的数值视为 true。
### 注释
格式:
```
//line comment
/*block comment*/
```
#### 示例
```
//this is line comment
/*this is block comment*/
```
### 函数调用
```
@ -257,23 +273,6 @@ set(a, a+b)
### 4.1 基本函数
#### comment
> 注释
----------------------------
##### 原型
```
#(comment)
```
#### 示例
```
#(this is a comment)
```
#### assert
> 断言

View File

@ -434,7 +434,6 @@ static ret_t fscript_parser_set_error(fscript_parser_t* parser, const char* str)
static ret_t fscript_parser_skip_seperators(fscript_parser_t* parser) {
char c = '\0';
do {
c = fscript_parser_get_char(parser);
} while (isspace(c) || (int)c < 0);
@ -443,6 +442,53 @@ static ret_t fscript_parser_skip_seperators(fscript_parser_t* parser) {
return RET_OK;
}
static ret_t fscript_parser_skip_line_comment(fscript_parser_t* parser) {
char c = '\0';
do {
c = fscript_parser_get_char(parser);
} while (c != '\0' && c != '\r' && c != '\n');
return RET_OK;
}
static ret_t fscript_parser_skip_block_comment(fscript_parser_t* parser) {
char c = '\0';
do {
c = fscript_parser_get_char(parser);
if (c == '*' && parser->cursor[0] == '/') {
c = fscript_parser_get_char(parser);
break;
}
} while (c != '\0');
return RET_OK;
}
static ret_t fscript_parser_skip_seperators_and_comments(fscript_parser_t* parser) {
char c = '\0';
while (TRUE) {
fscript_parser_skip_seperators(parser);
c = fscript_parser_get_char(parser);
if (c == '/') {
if (parser->cursor[0] == '/') {
c = fscript_parser_get_char(parser);
fscript_parser_skip_line_comment(parser);
} else if (parser->cursor[0] == '*') {
c = fscript_parser_get_char(parser);
fscript_parser_skip_block_comment(parser);
} else {
break;
}
} else {
break;
}
}
fscript_parser_unget_char(parser, c);
return RET_OK;
}
#define TOKEN_INIT(t, ttype, str) \
t->type = ttype; \
t->token = str->str; \
@ -509,7 +555,7 @@ static ret_t fscript_parser_parse_id_or_number(fscript_parser_t* parser, token_t
} while (TRUE);
if (isspace(c)) {
fscript_parser_skip_seperators(parser);
fscript_parser_skip_seperators_and_comments(parser);
c = fscript_parser_get_char(parser);
}
@ -532,7 +578,7 @@ static token_t* fscript_parser_get_token_ex(fscript_parser_t* parser, bool_t ope
return t;
}
fscript_parser_skip_seperators(parser);
fscript_parser_skip_seperators_and_comments(parser);
c = fscript_parser_get_char(parser);
str_set_with_len(str, &c, 1);
@ -604,7 +650,7 @@ static token_t* fscript_parser_get_token_ex(fscript_parser_t* parser, bool_t ope
} else {
fscript_parser_unget_char(parser, c);
do {
fscript_parser_skip_seperators(parser);
fscript_parser_skip_seperators_and_comments(parser);
c = fscript_parser_get_char(parser);
if (c == str->str[0]) {
str_append_char(str, c);

View File

@ -88,6 +88,25 @@ TEST(FScript, basic6) {
OBJECT_UNREF(obj);
}
TEST(FScript, basic7) {
value_t v;
object_t* obj = object_default_create();
fscript_eval(obj, "sum(1, 2)//line comments\n", &v);
ASSERT_EQ(value_int(&v), 3);
value_reset(&v);
fscript_eval(obj, "//comment\r\nsum(1, 2)//line comments\n", &v);
ASSERT_EQ(value_int(&v), 3);
value_reset(&v);
fscript_eval(obj, "/*comment*//**/\r\nsum(1, 2)//line comments\n", &v);
ASSERT_EQ(value_int(&v), 3);
value_reset(&v);
OBJECT_UNREF(obj);
}
TEST(FScript, if1) {
value_t v;
object_t* obj = object_default_create();

24
tests/testdata/demo_comments.fs vendored Normal file
View File

@ -0,0 +1,24 @@
//it is a line comment
//it is a line comment
if(1 < 2) {
print("yes");
//it is a line comment
}
assert(sum(1,1) == 2)
/*
*
* block1
*/
assert(sum(1,3) == 4)
/*
block2
*/
/**/
/*block3*/
assert( sum(1,2) == 3)