Compare commits

...

11 Commits

11 changed files with 148 additions and 89 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

14
src/contain_only.🗿 Normal file
View File

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

View File

@ -3,11 +3,13 @@ geadline(prompt) {
size = 0,
i = 0,
c,
a,
buf = galloc(capacity);
if (prompt)
putstr(prompt);
if (buf == NULL)
return NULL;
[buf] = 0;
loop {
red &c;
@ -16,34 +18,94 @@ geadline(prompt) {
free(buf);
return 0;
}
} else if (c == 0x007f) {
if (size) {
} else if (c == 0x1b) {
// ESC code
red &c; // skip [
red &c; // 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 {
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;
}
}
}
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;
}

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,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,6 +1,8 @@
strlen(str) {
local i = 0;
if (str == NULL)
return 0;
loop {
if ([str + i] == 0)
return (i);

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, "");
}

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");
}