Skip to content
This repository was archived by the owner on Jan 8, 2023. It is now read-only.

Commit dc5fbff

Browse files
author
ApsarasX
committed
feat: add builtin string type
1 parent 62f7597 commit dc5fbff

File tree

19 files changed

+296
-112
lines changed

19 files changed

+296
-112
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ include_directories(include)
2020

2121
add_subdirectory(lib)
2222

23-
add_subdirectory(external)
23+
add_subdirectory(builtin)

builtin/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
set(BUILTIN_SOURCES ss_string.c ss_io.c)
2+
set(BUILTIN_BITCODES ss_string.bc ss_io.bc)
3+
4+
add_custom_command(
5+
OUTPUT ${BUILTIN_BITCODES}
6+
COMMAND clang -c -emit-llvm ${PROJECT_SOURCE_DIR}/builtin/ss_string.c -std=c99 -Os -o ss_string.bc
7+
COMMAND clang -c -emit-llvm ${PROJECT_SOURCE_DIR}/builtin/ss_io.c -std=c99 -Os -o ss_io.bc
8+
DEPENDS ${BUILTIN_SOURCES}
9+
)
10+
11+
add_custom_target(
12+
builtin
13+
DEPENDS ${BUILTIN_BITCODES}
14+
)

builtin/ss_base.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
#define KEEPALIVE __attribute__((used))

builtin/ss_io.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "ss_io.h"
2+
3+
ss_string *KEEPALIVE ss_integer2string(long number) {
4+
size_t capacity = ss_string_get_capacity_with_size(20);
5+
ss_string *str = ss_string_create_with_capacity(capacity);
6+
snprintf(str->buffer, 20, "%ld", number);
7+
str->size = strlen(str->buffer);
8+
return str;
9+
}
10+
11+
long KEEPALIVE ss_string2integer(ss_string *str) {
12+
return strtol(str->buffer, NULL, 10);
13+
}
14+
15+
void KEEPALIVE ss_print_integer(long number) {
16+
printf("%ld", number);
17+
}
18+
19+
void KEEPALIVE ss_println_integer(long number) {
20+
printf("%ld\n", number);
21+
}
22+
23+
void KEEPALIVE ss_print_string(ss_string *str) {
24+
printf("%s", str->buffer);
25+
}
26+
27+
void KEEPALIVE ss_println_string(ss_string *str) {
28+
printf("%s\n", str->buffer);
29+
}

builtin/ss_io.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include <stdlib.h>
4+
#include <stdio.h>
5+
#include "ss_base.h"
6+
#include "ss_string.h"
7+
8+
ss_string *KEEPALIVE ss_integer2string(long number);
9+
10+
long KEEPALIVE ss_string2integer(ss_string *str);
11+
12+
void KEEPALIVE ss_print_integer(long number);
13+
14+
void KEEPALIVE ss_println_integer(long number);
15+
16+
void KEEPALIVE ss_print_string(ss_string *str);
17+
18+
void KEEPALIVE ss_println_string(ss_string *str);
Lines changed: 40 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
1-
#include <sys/types.h>
2-
#include <stdlib.h>
3-
#include <stdio.h>
4-
#include <string.h>
5-
#include <ctype.h>
6-
#define KEEPALIVE __attribute__((used))
7-
8-
typedef struct {
9-
char *buffer;
10-
size_t size;
11-
size_t capacity;
12-
} ss_string;
1+
#include "ss_string.h"
132

143
size_t KEEPALIVE ss_string_get_capacity_with_size(size_t size) {
154
if (size == 0) {
@@ -23,8 +12,8 @@ size_t KEEPALIVE ss_string_get_capacity_with_size(size_t size) {
2312
}
2413
}
2514

26-
ss_string* KEEPALIVE ss_string_create_with_capacity(size_t capacity) {
27-
ss_string* str = (ss_string*) malloc(sizeof(ss_string));
15+
ss_string *KEEPALIVE ss_string_create_with_capacity(size_t capacity) {
16+
ss_string *str = (ss_string *) malloc(sizeof(ss_string));
2817
if (!str) {
2918
return NULL;
3019
}
@@ -35,10 +24,10 @@ ss_string* KEEPALIVE ss_string_create_with_capacity(size_t capacity) {
3524
return str;
3625
}
3726

38-
ss_string* KEEPALIVE ss_string_create(const char *literal) {
27+
ss_string *KEEPALIVE ss_string_create(const char *literal) {
3928
size_t size = strlen(literal);
4029
size_t capacity = ss_string_get_capacity_with_size(size);
41-
ss_string* str = ss_string_create_with_capacity(capacity);
30+
ss_string *str = ss_string_create_with_capacity(capacity);
4231
if (!str) {
4332
return NULL;
4433
}
@@ -47,16 +36,16 @@ ss_string* KEEPALIVE ss_string_create(const char *literal) {
4736
return str;
4837
}
4938

50-
void KEEPALIVE ss_string_delete(ss_string* str) {
39+
void KEEPALIVE ss_string_delete(ss_string *str) {
5140
free(str->buffer);
5241
free(str);
5342
}
5443

55-
size_t KEEPALIVE ss_string_get_size(ss_string* str) {
44+
size_t KEEPALIVE ss_string_get_size(ss_string *str) {
5645
return str->size;
5746
}
5847

59-
int KEEPALIVE ss_string_grow_with_capacity(ss_string* str, size_t new_capacity) {
48+
long KEEPALIVE ss_string_grow_with_capacity(ss_string *str, size_t new_capacity) {
6049
char *new_buffer = (char *) calloc(new_capacity, 1);
6150
if (!new_buffer) {
6251
return -1;
@@ -69,7 +58,7 @@ int KEEPALIVE ss_string_grow_with_capacity(ss_string* str, size_t new_capacity)
6958
return 0;
7059
}
7160

72-
int KEEPALIVE ss_string_grow(ss_string* str) {
61+
long KEEPALIVE ss_string_grow(ss_string *str) {
7362
if (str->capacity < 32) {
7463
str->capacity += 16;
7564
} else {
@@ -78,44 +67,46 @@ int KEEPALIVE ss_string_grow(ss_string* str) {
7867
return ss_string_grow_with_capacity(str, str->capacity);
7968
}
8069

81-
int KEEPALIVE ss_string_append_with_literal(ss_string* str, const char *literal) {
82-
size_t literal_size = strlen(literal);
83-
size_t needed_size = str->size + literal_size;
70+
long KEEPALIVE ss_string_append(ss_string *dest, ss_string *src) {
71+
size_t src_size = strlen(src->buffer);
72+
size_t needed_size = dest->size + src_size;
8473
size_t needed_capacity = ss_string_get_capacity_with_size(needed_size);
85-
if (str->capacity < needed_capacity) {
86-
if (ss_string_grow_with_capacity(str, needed_capacity) == -1) {
74+
if (dest->capacity < needed_capacity) {
75+
if (ss_string_grow_with_capacity(dest, needed_capacity) == -1) {
8776
return -1;
8877
}
8978
}
90-
strncat(str->buffer, literal, literal_size);
91-
str->size = needed_size;
79+
strncat(dest->buffer, src->buffer, src_size);
80+
dest->size = needed_size;
9281
return 0;
9382
}
9483

95-
int KEEPALIVE ss_string_append(ss_string* dest, ss_string* src) {
96-
return ss_string_append_with_literal(dest, src->buffer);
97-
}
98-
99-
int KEEPALIVE ss_string_prepend_with_literal(ss_string* str, const char *literal) {
100-
size_t literal_size = strlen(literal);
101-
size_t needed_size = str->size + literal_size;
84+
long KEEPALIVE ss_string_prepend(ss_string *dest, ss_string *src) {
85+
size_t src_size = strlen(src->buffer);
86+
size_t needed_size = dest->size + src_size;
10287
size_t needed_capacity = ss_string_get_capacity_with_size(needed_size);
103-
if (str->capacity < needed_capacity) {
104-
if (ss_string_grow_with_capacity(str, needed_capacity) == -1) {
88+
if (dest->capacity < needed_capacity) {
89+
if (ss_string_grow_with_capacity(dest, needed_capacity) == -1) {
10590
return -1;
10691
}
10792
}
108-
memmove(str->buffer + literal_size, str->buffer, literal_size + 1);
109-
memcpy(str->buffer, literal, literal_size);
110-
str->size = needed_size;
93+
memmove(dest->buffer + src_size, dest->buffer, src_size + 1);
94+
memcpy(dest->buffer, src->buffer, src_size);
95+
dest->size = needed_size;
11196
return 0;
11297
}
11398

114-
int KEEPALIVE ss_string_prepend(ss_string* dest, ss_string* src) {
115-
return ss_string_prepend_with_literal(dest, src->buffer);
99+
ss_string *ss_string_concat(ss_string *str1, ss_string *str2) {
100+
size_t size = str1->size + str2->size;
101+
size_t capacity = ss_string_get_capacity_with_size(size);
102+
ss_string *str = ss_string_create_with_capacity(capacity);
103+
memcpy(str->buffer, str1->buffer, str1->size);
104+
memcpy(str->buffer + str1->size, str2->buffer, str2->size);
105+
str->size = size;
106+
return str;
116107
}
117108

118-
ss_string* KEEPALIVE ss_string_slice(ss_string* str, ssize_t from, ssize_t to) {
109+
ss_string *KEEPALIVE ss_string_slice(ss_string *str, ssize_t from, ssize_t to) {
119110
if (from < 0) {
120111
from = str->size + from;
121112
}
@@ -127,30 +118,30 @@ ss_string* KEEPALIVE ss_string_slice(ss_string* str, ssize_t from, ssize_t to) {
127118
}
128119
size_t slice_size = to - from;
129120
size_t new_capacity = ss_string_get_capacity_with_size(slice_size);
130-
ss_string* new_str = ss_string_create_with_capacity(new_capacity);
121+
ss_string *new_str = ss_string_create_with_capacity(new_capacity);
131122
memcpy(new_str->buffer, str->buffer + from, slice_size);
132123
new_str->size = slice_size;
133124
return new_str;
134125
}
135126

136-
int KEEPALIVE ss_string_equals(ss_string* str1, ss_string* str2) {
127+
long KEEPALIVE ss_string_equals(ss_string *str1, ss_string *str2) {
137128
size_t max_size = str1->size > str2->size ? str1->size : str2->size;
138129
return strncmp(str1->buffer, str2->buffer, max_size) == 0;
139130
}
140131

141-
ssize_t KEEPALIVE ss_string_index_of_with_literal(ss_string* str, const char *literal) {
132+
ssize_t KEEPALIVE ss_string_index_of_with_literal(ss_string *str, const char *literal) {
142133
char *sub = strstr(str->buffer, literal);
143134
if (!sub) {
144135
return -1;
145136
}
146137
return sub - str->buffer;
147138
}
148139

149-
ssize_t KEEPALIVE ss_string_index_of(ss_string* str, ss_string* substr) {
140+
ssize_t KEEPALIVE ss_string_index_of(ss_string *str, ss_string *substr) {
150141
return ss_string_index_of_with_literal(str, substr->buffer);
151142
}
152143

153-
int KEEPALIVE ss_string_trim_left(ss_string* str) {
144+
long KEEPALIVE ss_string_trim_left(ss_string *str) {
154145
size_t i = 0;
155146
while (isspace(str->buffer[i])) {
156147
i += 1;
@@ -167,7 +158,7 @@ int KEEPALIVE ss_string_trim_left(ss_string* str) {
167158
return 0;
168159
}
169160

170-
void KEEPALIVE ss_string_trim_right(ss_string* str) {
161+
void KEEPALIVE ss_string_trim_right(ss_string *str) {
171162
ssize_t i = str->size - 1;
172163
while (i >= 0 && isspace(str->buffer[i])) {
173164
str->buffer[i] = 0;
@@ -176,11 +167,7 @@ void KEEPALIVE ss_string_trim_right(ss_string* str) {
176167
str->size = i + 1;
177168
}
178169

179-
int KEEPALIVE ss_string_trim(ss_string* str) {
170+
long KEEPALIVE ss_string_trim(ss_string *str) {
180171
ss_string_trim_right(str);
181172
return ss_string_trim_left(str);
182173
}
183-
184-
void KEEPALIVE ss_console_log(ss_string *str) {
185-
printf("%s\n", str->buffer);
186-
}

builtin/ss_string.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#pragma once
2+
3+
#include <sys/types.h>
4+
#include <ctype.h>
5+
#include <string.h>
6+
#include <stdlib.h>
7+
#include "ss_base.h"
8+
9+
typedef struct {
10+
char *buffer;
11+
size_t size;
12+
size_t capacity;
13+
} ss_string;
14+
15+
size_t KEEPALIVE ss_string_get_capacity_with_size(size_t size);
16+
17+
ss_string *KEEPALIVE ss_string_create_with_capacity(size_t capacity);
18+
19+
ss_string *KEEPALIVE ss_string_create(const char *literal);
20+
21+
void KEEPALIVE ss_string_delete(ss_string *str);
22+
23+
size_t KEEPALIVE ss_string_get_size(ss_string *str);
24+
25+
long KEEPALIVE ss_string_grow_with_capacity(ss_string *str, size_t new_capacity);
26+
27+
long KEEPALIVE ss_string_grow(ss_string *str);
28+
29+
long KEEPALIVE ss_string_append(ss_string *dest, ss_string *src);
30+
31+
long KEEPALIVE ss_string_prepend(ss_string *dest, ss_string *src);
32+
33+
ss_string *ss_string_concat(ss_string *str1, ss_string *str2);
34+
35+
ss_string *KEEPALIVE ss_string_slice(ss_string *str, ssize_t from, ssize_t to);
36+
37+
long KEEPALIVE ss_string_equals(ss_string *str1, ss_string *str2);
38+
39+
ssize_t KEEPALIVE ss_string_index_of_with_literal(ss_string *str, const char *literal);
40+
41+
ssize_t KEEPALIVE ss_string_index_of(ss_string *str, ss_string *substr);
42+
43+
long KEEPALIVE ss_string_trim_left(ss_string *str);
44+
45+
void KEEPALIVE ss_string_trim_right(ss_string *str);
46+
47+
long KEEPALIVE ss_string_trim(ss_string *str);

examples/all.ss

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ const s = "string content";
33
let f = true;
44

55
function test() {
6+
ss_print_string(s);
67
return;
78
}
89

910
function func(a: number, b: number): number {
10-
let s = "chenwengang";
11+
let s = "StaticScript";
1112
let x = a + 1;
1213
let y = b - 1;
1314
if (x < y) {
@@ -24,4 +25,5 @@ function func(a: number, b: number): number {
2425
return x + y;
2526
}
2627

27-
func(1, 2);
28+
func(1, 2);
29+
test();

external/CMakeLists.txt

Lines changed: 0 additions & 11 deletions
This file was deleted.

include/AST/DeclNode.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class ParmVarDeclNode : public VarDeclNode {
6363
/// 函数声明节点
6464
class FunctionDeclNode : public DeclNode {
6565
public:
66+
static SharedPtrMap<String, FunctionDeclNode> getBuiltinFunctions();
67+
6668
FunctionDeclNode(
6769
String name,
6870
const SharedPtrVector<ParmVarDeclNode> &params,

0 commit comments

Comments
 (0)