Compare commits

...

32 Commits

Author SHA1 Message Date
kdx
e9bf776069 j'ai cringe 2023-07-26 12:57:10 +02:00
kdx
4bd9f50bd4 fix geadline 2023-07-26 12:53:38 +02:00
cc965024d6 Update wiki/strcpy.md 2023-07-26 10:28:27 +00:00
2421c51116 Add wiki/strcpy 2023-07-26 10:28:14 +00:00
78bc62c044 Add wiki/strcat.md 2023-07-26 10:26:06 +00:00
57de93e799 Add wiki/strlen.md 2023-07-26 10:20:13 +00:00
9a23536491 remove: useless func strchri 2023-07-26 12:13:10 +02:00
54370e872e auto check if file.input exist 2023-07-26 12:05:04 +02:00
106d2da5a0 add: geadline test 2023-07-26 10:20:02 +02:00
c41ebf6ff5 use insane new feature of golem 2023-07-26 09:57:11 +02:00
a84415a953 Merge pull request 'geadline2' (#5) from kdx/IronGOLEM:master into master
Reviewed-on: starnakin/IronGOLEM#5
2023-07-25 07:53:38 +00:00
470b97446b Merge branch 'master' into master 2023-07-25 07:53:20 +00:00
09e0d32c15 clean and opti 2023-07-25 09:48:20 +02:00
2b97d38b7e rename replace func to replace_index 2023-07-25 09:41:07 +02:00
cb41e69bdb clean: remove test to deleted func (reallocarray) 2023-07-25 09:39:27 +02:00
kdx
cc53b0cda5 geadline2 2023-07-25 09:24:38 +02:00
kdx
a432fd32c5 geadline: use getchar 2023-07-25 07:56:22 +02:00
15d00356e4 Merge pull request 'getchar' (#4) from kdx/IronGOLEM:master into master
Reviewed-on: starnakin/IronGOLEM#4
2023-07-25 05:43:57 +00:00
kdx
c4ce5b8566 getchar 2023-07-25 07:43:11 +02:00
kdx
1452a70b85 various optimisations 2023-07-25 05:43:10 +02:00
kdx
575ed7aa64 reduce galloc memory 2023-07-25 05:27:21 +02:00
kdx
46d7e9c85c geadline: move cursor 2023-07-25 05:10:15 +02:00
ee80b05d10 add: tmp in .gitignore 2023-07-24 08:56:12 +02:00
2fab31d822 opti: strchr 2023-07-24 08:55:19 +02:00
a7e050c351 add: contain_only 2023-07-24 08:55:10 +02:00
a39e4a780d strlen: check if the str == NULL 2023-07-24 03:28:42 +02:00
0c782738cc rename the output file from tmp to IronGolem in .gitignore 2023-07-24 03:23:46 +02:00
1a1251b512 rename the output file from tmp to IronGolem 2023-07-24 03:01:08 +02:00
d3e788868a update geadline on realloc 2023-07-24 02:59:08 +02:00
5dcace0cbe remove get_line, replaced by readline 2023-07-24 02:57:44 +02:00
85aeaebc7a add: realloc and remove reallocarray 2023-07-24 02:57:44 +02:00
3443ed76e8 Merge pull request 'geadline first version' (#2) from kdx/IronGOLEM:master into master
Reviewed-on: starnakin/IronGOLEM#2
2023-07-23 23:07:25 +00:00
42 changed files with 314 additions and 211 deletions

6
.gitignore vendored
View File

@ -1,3 +1,3 @@
tmp.rom
tmp.asm
tmp.🗿
IronGolem.asm
IronGolem.🗿
tmp.*

4
run.sh
View File

@ -1,4 +1,4 @@
cat src/* >tmp.🗿
golemc tmp.🗿 >tmp.asm
cat src/* > IronGolem.🗿
golemc IronGolem.🗿 > IronGolem.asm
# orgaasm tmp.asm tmp.rom
# orgaemu tmp.rom

View File

@ -1,18 +1,16 @@
aton(str)
{
local i = 0;
local out = 0;
loop {
if ([str + i] != '+')
if ([str] != '+')
break;
i++;
str++;
}
loop {
if ([str + i] == 0 | isdigit([str + i]) == 0)
break;
out = out * 10 + [str + i] - '0';
i++;
if ([str] == 0 | isdigit([str]) == 0)
return out;
out = out * 10 + [str] - '0';
str++;
}
return out;
}

View File

@ -1,23 +1,19 @@
aton_s(str)
{
local i = 0;
local sign = 0;
local out = 0;
loop {
if ([str + i] == '-')
if ([str] == '-')
sign = sign == 0;
else if ([str + i] != '+')
else if ([str] != '+')
break;
i++;
str++;
}
loop {
if ([str + i] == 0 | isdigit([str + i]) == 0)
break;
out = out * 10 + [str + i] - '0';
i++;
if ([str] == 0 | isdigit([str]) == 0)
return (sign * (0 - out)) | ((sign == 0) * out);
out = out * 10 + [str] - '0';
str++;
}
if (sign)
return (0 - out);
return out;
}

View File

@ -1 +1 @@
bzero(tab, size) memset(tab, size, 0);
bzero(tab, size) => memset(tab, size, 0);

11
src/contain_only.🗿 Normal file
View File

@ -0,0 +1,11 @@
contain_only(to_big, to_find)
{
loop
{
if ([to_big] == 0)
return 1;
if (strchr(to_find, [to_big]) == 0)
return 0;
to_big++;
}
}

View File

@ -9,5 +9,5 @@ free_tab(tab)
free([tmp]);
tmp++;
}
free(tab);
return free(tab);
}

View File

@ -1,4 +1,4 @@
define HEAP_SIZE = 0x8000;
define HEAP_SIZE = 0x4000;
global heap[HEAP_SIZE] = 0;
define PADDING_SIZE = 4;
@ -26,11 +26,7 @@ define HEADER_SIZE = 5;
🗿 Is used to check invalid write
🗿 If a case doesn't equal to 0 it is an invalid write
define LOCATION_INITIALISED = 0;
define LOCATION_USED = 1;
define LOCATION_SIZE = 2;
define LOCATION_PREV = 3;
define LOCATION_NEXT = 4;
enum LOCATION_INITIALISED, LOCATION_USED, LOCATION_SIZE, LOCATION_PREV, LOCATION_NEXT;
define LOCATION_DATA = HEADER_SIZE + PADDING_SIZE;
galloc_setup_header(ptr, used, size, next_block, prev_block)

View File

@ -1,49 +1,132 @@
geadline(prompt) {
geadline2(prompt, text)
{
local capacity = 64,
size = 0,
i = 0,
c,
buf = galloc(capacity);
a,
buf;
if (text) {
size = strlen(text);
i = size;
loop {
if (capacity > size)
break;
capacity = capacity * 2;
}
buf = galloc(capacity);
if (buf == NULL)
return NULL;
strcpy(buf, text);
} else {
buf = galloc(capacity);
if (buf == NULL)
return NULL;
[buf] = 0;
}
if (prompt)
putstr(prompt);
if (buf == NULL)
return NULL;
putstr(buf);
loop {
red &c;
c = getchar();
if ((c == 0xffff) | (c == 0x04)) {
if ((size == 0) | (c == 0xffff)) {
free(buf);
return 0;
}
} else if (c == 0x007f) {
if (size) {
} else if (c == 0x1b) {
// ESC code
getchar(); // skip [
c = getchar(); // value
if ((c == 'A') & (size > 0)) {
loop {
if (i == 0)
break;
i = i - 1;
esccode('D');
}
} else if ((c == 'B') & (i < size)) {
loop {
if (i >= size)
break;
i = i + 1;
esccode('C');
}
} else if ((c == 'C') & (i < size)) {
i = i + 1;
esccode('C');
} else if ((c == 'D') & (i > 0)) {
i = i - 1;
esccode('D');
}
wrt '\a';
} else if (c == 0x7f) {
if (i) {
a = i - 1;
loop {
if (a >= size)
break;
[buf + a] = [buf + a + 1];
a = a + 1;
}
size = size - 1;
i = i - 1;
[buf + size] = 0;
wrt 0x1b;
wrt 0x5b;
wrt 0x44;
wrt '\r';
if (prompt)
putstr(prompt);
putstr(buf);
wrt ' ';
wrt 0x1b;
wrt 0x5b;
wrt 0x44;
esccode('D');
a = size - i;
loop {
if (a == 0)
break;
a = a - 1;
esccode('D');
}
}
} else if (c == '\n') {
wrt '\n';
return buf;
} else {
size = size + 1;
if (size > capacity) {
buf = reallocarray(buf, capacity, capacity * 2);
if (size >= capacity) {
buf = realloc(buf, capacity * 2);
if (buf == NULL)
return NULL;
capacity = capacity * 2;
}
a = size - i - 1;
loop {
if ((a == 0xffff) | (a == 0))
break;
[buf + i + a] = [buf + i + a - 1];
a = a - 1;
}
[buf + i] = c;
[buf + size] = 0;
putstr(buf + i);
a = strlen(buf + i) - 1;
loop {
if ((a == 0xffff) | (a == 0))
break;
a = a - 1;
esccode('D');
}
i = i + 1;
[buf + i] = 0;
wrt c;
if (c == '\n')
return buf;
}
}
}
geadline(prompt) => geadline2(prompt, NULL);
esccode(c)
{
wrt 0x1b;
wrt '[';
wrt c;
}

View File

@ -1,32 +0,0 @@
define GET_LINE_BUFFER=100;
get_line()
{
local tmp;
local out;
local c;
local i;
out = NULL;
i = 0;
loop
{
if (i % GET_LINE_BUFFER == 0)
{
tmp = reallocarray(out, i, i + GET_LINE_BUFFER + 1);
if (tmp == NULL)
return NULL;
}
red &c;
if (c == 0xffff)
return NULL;
[tmp + i] = c;
i++;
if (c == '\n')
break;
}
[tmp + i] = 0;
out = strdup(tmp);
free(tmp);
return out;
}

6
src/getchar.🗿 Normal file
View File

@ -0,0 +1,6 @@
getchar()
{
local c;
red &c;
return c;
}

View File

@ -1 +1 @@
isalpha(c) return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z');
isalpha(c) => (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z');

View File

@ -1 +1 @@
isascii(c) return c < 128;
isascii(c) => c < 128;

View File

@ -1 +1 @@
isdigit(c) return c >= '0' & c <= '9';
isdigit(c) => c >= '0' & c <= '9';

View File

@ -1 +1 @@
isalnum(c) return isalpha(c) | isdigit(c);
isalnum(c) => isalpha(c) | isdigit(c);

View File

@ -1 +1 @@
isprint(c) return c >= ' ' & c <= '~';
isprint(c) => c >= ' ' & c <= '~';

View File

@ -5,6 +5,7 @@ memset(tab, size, value)
loop {
if (i == size)
return (tab);
[tab + i++] = value;
[tab + i] = value;
i++;
}
}

View File

@ -3,7 +3,7 @@ ntoa_get_size(number)
local size = 0;
if (number == 0)
size++;
return 1;
loop
{
if (number == 0)
@ -29,8 +29,8 @@ ntoa(number)
{
if (number == 0)
return (str);
[str + size - 1] = number % 10 + '0';
number = number / 10;
size--;
[str + size] = number % 10 + '0';
number = number / 10;
}
}

View File

@ -1,18 +1,3 @@
ntoa_get_size(number)
{
local size = 0;
if (number == 0)
size++;
loop
{
if (number == 0)
return (size);
number = number / 10;
size++;
}
}
ntoa_s(number)
{
local str, sign, size;

View File

@ -1,6 +1,6 @@
print_raw_bit(number)
{
local tab = get_raw_bit(number), i = 0;
local tab = get_raw_bit(number);
puttab_num(tab, 16);
free(tab);
}

View File

@ -1 +1,5 @@
putchar(c) wrt c;
putchar(c)
{
wrt c;
return c;
}

View File

@ -1,9 +1,6 @@
putnum(number)
{
local str;
str = ntoa(number);
if (str == 0)
return;
local str = ntoa(number);
putstr(str);
free(str);
}

View File

@ -1,10 +1,14 @@
putstr(str)
{
local i = 0;
local tmp = str;
if (str == NULL) {
putstr("(null)");
return NULL;
}
loop {
if ([str + i] == 0)
return;
putchar([str + i]);
i++;
if ([tmp] == 0)
return str;
putchar([tmp]);
tmp++;
}
}

View File

@ -1,20 +1,19 @@
puttab_str(tab)
{
local tmp = tab;
putchar('[');
loop
{
if ([tmp] == 0)
break;
if ([tab] == 0) {
putchar(']');
return;
}
putchar('"');
putstr([tmp]);
putstr([tab]);
putchar('"');
if ([tmp + 1] != 0)
tab++;
if ([tab] != 0)
putstr(", ");
tmp++;
}
putchar(']');
}
puttab_num(tab, size)
@ -24,12 +23,13 @@ puttab_num(tab, size)
putchar('[');
loop
{
if (i == size)
break;
if (i == size) {
putchar(']');
return;
}
putnum([tab + i]);
i++;
if (i != size)
putstr(", ");
}
putchar(']');
}

View File

@ -1,21 +1,26 @@
reallocarray(ptr, nmemb, size)
realloc(ptr, new_size)
{
local tmp;
local start;
local block_ptr;
local new_space;
local i;
start = ptr;
tmp = galloc(size);
if (tmp == NULL)
return NULL;
new_space = galloc(new_size);
if (new_space == NULL)
{
free(ptr);
return (NULL);
}
if (ptr == NULL)
return tmp;
return new_space;
block_ptr = ptr - LOCATION_DATA;
i = 0;
loop
{
if (start - ptr == nmemb)
if (i == new_size | i == [block_ptr + LOCATION_SIZE])
break;
[tmp + start - ptr] = [start];
start++;
[new_space + i] = [ptr + i];
i++;
}
free(ptr);
return tmp;
return new_space;
}

View File

@ -1,4 +1,4 @@
replace(str, fill, start, stop)
replace_index(str, fill, start, stop)
{
local out;
local sum;

View File

@ -1,12 +1,12 @@
strchr(str, c)
{
local i = 0;
local start = str;
loop {
if ([str + i] == c)
return (str + i);
if ([str + i] == 0)
if ([start] == c)
return (start);
if ([start] == 0)
return (0);
i++;
start++;
}
}

View File

@ -1,12 +0,0 @@
strchri(str, c)
{
local i = 0;
loop {
if ([str + i] == c)
return (i);
if ([str + i] == 0)
return (0 - 1);
i++;
}
}

View File

@ -1,6 +1,8 @@
strlen(str) {
local i = 0;
if (str == NULL)
return 0;
loop {
if ([str + i] == 0)
return (i);

View File

@ -6,7 +6,12 @@ tester()
cat src/*.🗿 tests/$val.🗿 tests/test.🗿 >tmp.🗿
golemc tmp.🗿 > tmp.asm
orgaasm tmp.asm tmp.rom
orgaemu tmp.rom
if [ -f tests/$val.input ]
then
orgaemu tmp.rom < tests/$val.input
else
orgaemu tmp.rom
fi
echo
done
}

7
tests/contain_only.🗿 Normal file
View File

@ -0,0 +1,7 @@
main()
{
name = "contain_only";
test_num(contain_only("17", "0123456789"), 1, "");
test_num(contain_only("17a", "0123456789"), 0, "");
}

3
tests/geadline.input Normal file
View File

@ -0,0 +1,3 @@
yo
bozo
zboo

18
tests/geadline.🗿 Normal file
View File

@ -0,0 +1,18 @@
main()
{
local ptr;
name = "geadline";
ptr = geadline("");
test_str(ptr, "yo", "");
ptr = geadline("");
test_str(ptr, "bozo", "");
ptr = geadline("");
test_str(ptr, "bozo", "arrow");
ptr = geadline("");
test_num(ptr, 0, "");
}

View File

@ -1,23 +0,0 @@
define GET_LINE=0;
main()
{
local ptr;
name = "get_line";
if (GET_LINE == 0)
return;
putstr("yo\n");
ptr = get_line();
test_str(ptr, "yo\n", "");
free(ptr);
putstr("ENTER\n");
ptr = get_line();
test_str(ptr, "\n", "");
free(ptr);
putstr("ctrl + D\n");
ptr = get_line();
test_num(ptr, NULL, "");
}

24
tests/realloc.🗿 Normal file
View File

@ -0,0 +1,24 @@
main()
{
local ptr1;
local ptr2;
name = "realloc";
ptr1 = strdup("yo");
ptr2 = realloc(ptr1, 6);
test_str("yo", ptr2, "standart: value");
free(ptr2);
test_num(leaks(), 0, "standart: leaks");
ptr2 = realloc(NULL, 6);
test_num(heap + LOCATION_DATA, ptr2, "NULL: value");
free(ptr2);
test_num(leaks(), 0, "NULL: leaks");
ptr1 = strdup("bonjour");
ptr2 = realloc(ptr1, 2);
test_tab_num(ptr2, "bo", 2, "decrement size: value");
free(ptr2);
test_num(leaks(), 0, "decrement size: leaks");
}

View File

@ -1,11 +0,0 @@
main()
{
local tmp;
name = "reallocarray";
tmp = strdup("yo");
if (tmp == NULL)
return 1;
tmp = reallocarray(tmp, strlen(tmp), 5);
test_str(tmp, "yo", "");
}

View File

@ -1,9 +0,0 @@
main()
{
name = "replace";
test_str(replace("yo ca va ?", "t", 2, 3), "yotca va ?", "");
test_str(replace("yo ca va ?", "", 2, 3), "yoca va ?", "empty fill");
test_str(replace("yo ca va ?", "aaaaa", 2, 3), "yoaaaaaca va ?", "");
test_str(replace("", "aaaaa", 0, 0), "aaaaa", "");
}

9
tests/replace_index.🗿 Normal file
View File

@ -0,0 +1,9 @@
main()
{
name = "replace_index";
test_str(replace_index("yo ca va ?", "t", 2, 3), "yotca va ?", "");
test_str(replace_index("yo ca va ?", "", 2, 3), "yoca va ?", "empty fill");
test_str(replace_index("yo ca va ?", "aaaaa", 2, 3), "yoaaaaaca va ?", "");
test_str(replace_index("", "aaaaa", 0, 0), "aaaaa", "");
}

View File

@ -1,8 +0,0 @@
main()
{
name = "strchri";
test_num(strchri("bozoman", 'm'), 4, "");
test_num(strchri("bozoman", 'v'), 0 - 1, "");
test_num(strchri("", 'v'), 0 - 1, "");
}

14
wiki/strcat.md Normal file
View File

@ -0,0 +1,14 @@
# STRCAT
Strcat (string concatenate) is a function that takes two chars lists as a parameter and write the second at this end of the first (like strcat in C)
## params
1. chars list
2. chars list
## example
```
strcat("y", "o") "y" => "yo"
strcat("y", "") "y" => "y"
strcat("", "o") "" => "o"
strcat("hello ", "world!") => "hello world!"
```

14
wiki/strcpy.md Normal file
View File

@ -0,0 +1,14 @@
# STRCPY
Strcpy (string copy) is a function that takes two chars lists as a parameter and write the second in the first (like strcpy in C)
## params
1. chars list
2. chars list
## example
```
strcpy("y", "o") "y" => "o"
strcpy("y", "") "y" => ""
strcpy("", "o") "" => "o"
strcpy("hello ", "world!") => "world!"
```

16
wiki/strlen.md Normal file
View File

@ -0,0 +1,16 @@
# STRLEN
Strlen (string length) is a function that takes an chars list as a parameter and return an length (like strlen in C)
## params
1. char list
## return
number
## example
```
strlen("ab") => 2
strlen(NULL) => 0
strlen("") => 0
strlen("j'ai les cramptés") => 17
```