Compare commits

...

11 Commits

11 changed files with 148 additions and 89 deletions

6
.gitignore vendored
View File

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

4
run.sh
View File

@ -1,4 +1,4 @@
cat src/* >tmp.🗿 cat src/* > IronGolem.🗿
golemc tmp.🗿 >tmp.asm golemc IronGolem.🗿 > IronGolem.asm
# orgaasm tmp.asm tmp.rom # orgaasm tmp.asm tmp.rom
# orgaemu 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, size = 0,
i = 0, i = 0,
c, c,
a,
buf = galloc(capacity); buf = galloc(capacity);
if (prompt) if (prompt)
putstr(prompt); putstr(prompt);
if (buf == NULL) if (buf == NULL)
return NULL; return NULL;
[buf] = 0;
loop { loop {
red &c; red &c;
@ -16,34 +18,94 @@ geadline(prompt) {
free(buf); free(buf);
return 0; return 0;
} }
} else if (c == 0x007f) { } else if (c == 0x1b) {
if (size) { // 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; size = size - 1;
i = i - 1; i = i - 1;
[buf + size] = 0; [buf + size] = 0;
wrt 0x1b; wrt '\r';
wrt 0x5b; if (prompt)
wrt 0x44; putstr(prompt);
putstr(buf);
wrt ' '; wrt ' ';
wrt 0x1b; esccode('D');
wrt 0x5b; a = size - i;
wrt 0x44; loop {
if (a == 0)
break;
a = a - 1;
esccode('D');
}
} }
} else { } else {
size = size + 1; size = size + 1;
if (size > capacity) { if (size >= capacity) {
buf = reallocarray(buf, capacity, capacity * 2); buf = realloc(buf, capacity * 2);
if (buf == NULL) if (buf == NULL)
return NULL; return NULL;
capacity = capacity * 2; 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 + 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; i = i + 1;
[buf + i] = 0;
wrt c;
if (c == '\n') if (c == '\n')
return buf; 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 block_ptr;
local start; local new_space;
local i;
start = ptr; new_space = galloc(new_size);
tmp = galloc(size); if (new_space == NULL)
if (tmp == NULL) {
return NULL; free(ptr);
return (NULL);
}
if (ptr == NULL) if (ptr == NULL)
return tmp; return new_space;
block_ptr = ptr - LOCATION_DATA;
i = 0;
loop loop
{ {
if (start - ptr == nmemb) if (i == new_size | i == [block_ptr + LOCATION_SIZE])
break; break;
[tmp + start - ptr] = [start]; [new_space + i] = [ptr + i];
start++; i++;
} }
free(ptr); free(ptr);
return tmp; return new_space;
} }

View File

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

View File

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