Compare commits

..

No commits in common. "46d7e9c85cccb6fa9595487a345651f6d01122af" and "6051375f6ad3c1d1827afdbdf5964af540b5fd64" have entirely different histories.

11 changed files with 89 additions and 148 deletions

6
.gitignore vendored
View File

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

4
run.sh
View File

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

View File

@ -1,14 +0,0 @@
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,13 +3,11 @@ 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;
@ -18,94 +16,34 @@ geadline(prompt) {
free(buf); free(buf);
return 0; return 0;
} }
} else if (c == 0x1b) { } else if (c == 0x007f) {
// ESC code if (size) {
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 '\r'; wrt 0x1b;
if (prompt) wrt 0x5b;
putstr(prompt); wrt 0x44;
putstr(buf);
wrt ' '; wrt ' ';
esccode('D'); wrt 0x1b;
a = size - i; wrt 0x5b;
loop { wrt 0x44;
if (a == 0)
break;
a = a - 1;
esccode('D');
}
} }
} else { } else {
size = size + 1; size = size + 1;
if (size >= capacity) { if (size > capacity) {
buf = realloc(buf, capacity * 2); buf = reallocarray(buf, capacity, 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;
}

32
src/get_line.🗿 Normal file
View File

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

View File

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

View File

@ -1,8 +1,6 @@
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);

View File

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

23
tests/get_line.🗿 Normal file
View File

@ -0,0 +1,23 @@
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, "");
}

View File

@ -1,24 +0,0 @@
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");
}