Compare commits
16 Commits
29c6b66b17
...
main
Author | SHA1 | Date | |
---|---|---|---|
573a366766 | |||
6b14c50e65 | |||
6fa0ea91bd | |||
4e15cb9ff1 | |||
1f1a11f886 | |||
2d1c5ee458 | |||
1b7bb91cf1 | |||
828cb3cdcd | |||
cb5ba64ac7 | |||
b87d400471 | |||
6c9f86a88c | |||
31f89f44ec | |||
0358eefcae | |||
3e23f44fc3 | |||
e4dfffa315 | |||
6538b48c48 |
2
README.md
Normal file
2
README.md
Normal file
@ -0,0 +1,2 @@
|
||||
# GolemED
|
||||
A clone of the famous ED text editor in 
|
Submodule lib/IronGolem updated: 0b09f0d262...e417a01f9a
@ -1,4 +1,4 @@
|
||||
cmd_add(text, args)
|
||||
cmd_add(data, args)
|
||||
{
|
||||
local line;
|
||||
local i, j;
|
||||
@ -6,31 +6,35 @@ cmd_add(text, args)
|
||||
|
||||
loop
|
||||
{
|
||||
line = geadline("(add)");
|
||||
line = get_input(data, "(add)", NULL);
|
||||
if (line == NULL)
|
||||
return 1;
|
||||
if (strcmp(line, ".") == 0)
|
||||
return 0;
|
||||
tmp = galloc([text + LOCATION_LENGTH] + 1);
|
||||
tmp = galloc([data + LOCATION_LENGTH] + 1);
|
||||
if (tmp == NULL)
|
||||
return 1;
|
||||
i = 0;
|
||||
j = 0;
|
||||
loop
|
||||
{
|
||||
if (i == [text + LOCATION_CURRENT_LINE] + 1 | i == [text + LOCATION_LENGTH])
|
||||
if (i == [data + LOCATION_CURRENT_LINE] + 1 | i == [data + LOCATION_LENGTH])
|
||||
{
|
||||
[tmp + i] = line;
|
||||
[tmp + i] = galloc(MAP_SIZE);
|
||||
if ([tmp + i] == NULL)
|
||||
return 1;
|
||||
[[tmp + i] + MAP_KEY] = id++;
|
||||
[[tmp + i] + MAP_VALUE] = line;
|
||||
j++;
|
||||
}
|
||||
[tmp + i + j] = [[text + LOCATION_ARRAY] + i];
|
||||
if (i + j == [text + LOCATION_LENGTH] | i == [text + LOCATION_LENGTH])
|
||||
[tmp + i + j] = [[data + LOCATION_ARRAY] + i];
|
||||
if (i + j == [data + LOCATION_LENGTH] | i == [data + LOCATION_LENGTH])
|
||||
break;
|
||||
i++;
|
||||
}
|
||||
free([text + LOCATION_ARRAY]);
|
||||
[text + LOCATION_ARRAY] = tmp;
|
||||
[text + LOCATION_LENGTH] = [text + LOCATION_LENGTH] + 1;
|
||||
[text + LOCATION_CURRENT_LINE] = [text + LOCATION_CURRENT_LINE] + 1;
|
||||
free([data + LOCATION_ARRAY]);
|
||||
[data + LOCATION_ARRAY] = tmp;
|
||||
[data + LOCATION_LENGTH] = [data + LOCATION_LENGTH] + 1;
|
||||
[data + LOCATION_CURRENT_LINE] = [data + LOCATION_CURRENT_LINE] + 1;
|
||||
}
|
||||
}
|
||||
|
7
src/commands/dec.🗿
Normal file
7
src/commands/dec.🗿
Normal file
@ -0,0 +1,7 @@
|
||||
cmd_dec(data, args)
|
||||
{
|
||||
if ([data + LOCATION_CURRENT_LINE] != 0)
|
||||
error("no line");
|
||||
else
|
||||
[data + LOCATION_CURRENT_LINE] = [data + LOCATION_CURRENT_LINE] - 1;
|
||||
}
|
21
src/commands/delete.🗿
Normal file
21
src/commands/delete.🗿
Normal file
@ -0,0 +1,21 @@
|
||||
cmd_delete(data, args)
|
||||
{
|
||||
local i;
|
||||
|
||||
if ([data + LOCATION_LENGTH] == 0)
|
||||
{
|
||||
error(data, "line doesn't exist");
|
||||
return 1;
|
||||
}
|
||||
i = [data + LOCATION_CURRENT_LINE];
|
||||
free ([[[data + LOCATION_ARRAY] + [data + LOCATION_CURRENT_LINE]] + MAP_VALUE]);
|
||||
free ([[data + LOCATION_ARRAY] + [data + LOCATION_CURRENT_LINE]]);
|
||||
[data + LOCATION_LENGTH] = [data + LOCATION_LENGTH] - 1;
|
||||
loop
|
||||
{
|
||||
if (i == [data + LOCATION_LENGTH])
|
||||
return 0;
|
||||
[[data + LOCATION_ARRAY] + i] = [[data + LOCATION_ARRAY] + i + 1];
|
||||
i++;
|
||||
}
|
||||
}
|
4
src/commands/help.🗿
Normal file
4
src/commands/help.🗿
Normal file
@ -0,0 +1,4 @@
|
||||
cmd_help(data, args)
|
||||
{
|
||||
[data + LOCATION_MODE] = [data + LOCATION_MODE] == 0;
|
||||
}
|
7
src/commands/inc.🗿
Normal file
7
src/commands/inc.🗿
Normal file
@ -0,0 +1,7 @@
|
||||
cmd_inc(data, args)
|
||||
{
|
||||
if ([data + LOCATION_CURRENT_LINE] == [data + LOCATION_LENGTH])
|
||||
error(data, "no line");
|
||||
else
|
||||
[data + LOCATION_CURRENT_LINE] = [data + LOCATION_CURRENT_LINE] + 1;
|
||||
}
|
12
src/commands/numered.🗿
Normal file
12
src/commands/numered.🗿
Normal file
@ -0,0 +1,12 @@
|
||||
cmd_numbered(data, args)
|
||||
{
|
||||
if ([data + LOCATION_LENGTH] == 0)
|
||||
{
|
||||
error(data, "Empty buffer");
|
||||
return 1;
|
||||
}
|
||||
putnum([data + LOCATION_CURRENT_LINE] + 1);
|
||||
putchar('\t');
|
||||
putstr([[[data + LOCATION_ARRAY] + [data + LOCATION_CURRENT_LINE]] + MAP_VALUE]);
|
||||
putchar('\n');
|
||||
}
|
@ -1,10 +1,10 @@
|
||||
cmd_print(text, args)
|
||||
cmd_print(data, args)
|
||||
{
|
||||
if ([text + LOCATION_LENGTH] == 0)
|
||||
if ([data + LOCATION_LENGTH] == 0)
|
||||
{
|
||||
putstr("Empty file\n");
|
||||
error(data, "Empty buffer");
|
||||
return 1;
|
||||
}
|
||||
putstr([[text + LOCATION_ARRAY] + [text + LOCATION_CURRENT_LINE]]);
|
||||
putstr([[[data + LOCATION_ARRAY] + [data + LOCATION_CURRENT_LINE]] + MAP_VALUE]);
|
||||
putchar('\n');
|
||||
}
|
||||
|
28
src/commands/write.🗿
Normal file
28
src/commands/write.🗿
Normal file
@ -0,0 +1,28 @@
|
||||
puterr(str)
|
||||
{
|
||||
local ptr = str;
|
||||
|
||||
loop
|
||||
{
|
||||
if ([ptr] == 0)
|
||||
break;
|
||||
err [ptr];
|
||||
ptr++;
|
||||
}
|
||||
err '\n';
|
||||
}
|
||||
|
||||
cmd_write(data, args)
|
||||
{
|
||||
local i = 0;
|
||||
|
||||
if ([data + LOCATION_LENGTH] == 0)
|
||||
return 0;
|
||||
loop
|
||||
{
|
||||
if (i == [data + LOCATION_LENGTH])
|
||||
return 0;
|
||||
puterr([[[data + LOCATION_ARRAY] + i] + MAP_VALUE]);
|
||||
i++;
|
||||
}
|
||||
}
|
178
src/main.🗿
178
src/main.🗿
@ -1,12 +1,96 @@
|
||||
enum LOCATION_LENGTH, LOCATION_CURRENT_LINE, LOCATION_ARRAY;
|
||||
enum LOCATION_LENGTH, LOCATION_CURRENT_LINE, LOCATION_ARRAY, LOCATION_MODE;
|
||||
enum MODE_PRINT, MODE_NO_PRINT;
|
||||
define DATA_SIZE=4;
|
||||
|
||||
parsing(text, cmd_ptr)
|
||||
range(start, stop)
|
||||
{
|
||||
local i;
|
||||
local sign;
|
||||
local tab;
|
||||
|
||||
if (start > stop)
|
||||
sign = 0 - 1;
|
||||
else
|
||||
sign = 0 + 1;
|
||||
tab = galloc((stop - start) * sign + 1 + (start == stop));
|
||||
if (tab == NULL)
|
||||
return NULL;
|
||||
i = 0;
|
||||
loop
|
||||
{
|
||||
[tab + i + 1] = start + (i * sign);
|
||||
if (start + (i * sign) == stop)
|
||||
break;
|
||||
i++;
|
||||
}
|
||||
[tab] = i + (start == stop);
|
||||
return tab;
|
||||
}
|
||||
|
||||
get_number(data, str)
|
||||
{
|
||||
local ranges;
|
||||
local start = 0 -1;
|
||||
local stop = 0 -1;
|
||||
|
||||
if ([[str]] == '%')
|
||||
{
|
||||
ranges = range(0, [data + LOCATION_LENGTH]);
|
||||
[str] = [str] + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
loop
|
||||
{
|
||||
if ([[str]] == '$')
|
||||
{
|
||||
if (start == 0 - 1)
|
||||
start = [data + LOCATION_LENGTH] - 1;
|
||||
else
|
||||
stop = [data + LOCATION_LENGTH] - 1;
|
||||
[str] = [str] + 1;
|
||||
}
|
||||
else if (isdigit([[str]]))
|
||||
{
|
||||
if (start == 0 - 1)
|
||||
start = aton([str]) - 1;
|
||||
else
|
||||
stop = aton([str]) - 1;
|
||||
loop
|
||||
{
|
||||
if (isdigit([[str]]) == 0)
|
||||
break;
|
||||
[str] = [str] + 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
break;
|
||||
if ((start != 0 - 1 & stop != 0 - 1) | [[str]] == ',')
|
||||
[str] = [str] + 1;
|
||||
else
|
||||
break;
|
||||
}
|
||||
if (start != 0 - 1)
|
||||
{
|
||||
if (stop != 0 - 1)
|
||||
ranges = range(start, stop);
|
||||
else
|
||||
ranges = range(start, start);
|
||||
}
|
||||
else
|
||||
ranges = range([data + LOCATION_CURRENT_LINE], [data + LOCATION_CURRENT_LINE]);
|
||||
}
|
||||
return ranges;
|
||||
}
|
||||
|
||||
parsing(data, cmd_ptr, ranges_ptr)
|
||||
{
|
||||
local input;
|
||||
local tmp;
|
||||
local i;
|
||||
local line_application;
|
||||
|
||||
input = geadline("(default)");
|
||||
input = get_input(data, "(default)", NULL);
|
||||
if (input == NULL)
|
||||
return 1;
|
||||
if (strcmp(input, ".") == 0)
|
||||
@ -15,25 +99,8 @@ parsing(text, cmd_ptr)
|
||||
[cmd_ptr] = strdup("p");
|
||||
return [cmd_ptr] == NULL;
|
||||
}
|
||||
if (isdigit([input]) == 1)
|
||||
{
|
||||
line_application = aton(input);
|
||||
if (line_application == 0 | line_application >= [text + LOCATION_LENGTH] + 1)
|
||||
{
|
||||
[cmd_ptr] = NULL;
|
||||
putstr("invalid line\n");
|
||||
free(input);
|
||||
return 0;
|
||||
}
|
||||
[text + LOCATION_CURRENT_LINE] = line_application - 1;
|
||||
}
|
||||
tmp = input;
|
||||
loop
|
||||
{
|
||||
if (isdigit([tmp]) == 0)
|
||||
break;
|
||||
tmp++;
|
||||
}
|
||||
[ranges_ptr] = get_number(data, &tmp);
|
||||
if (strcmp(tmp, "") == 0)
|
||||
[cmd_ptr] = strdup("p");
|
||||
else
|
||||
@ -41,39 +108,80 @@ parsing(text, cmd_ptr)
|
||||
free(input);
|
||||
if ([cmd_ptr] == NULL)
|
||||
return 1;
|
||||
i = 0;
|
||||
loop
|
||||
{
|
||||
if ([[ranges_ptr]] == i)
|
||||
break;
|
||||
[[ranges_ptr] + i + 1] = [[[data + LOCATION_ARRAY] + [[ranges_ptr] + i + 1]] + MAP_KEY];
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
global id=0;
|
||||
|
||||
main()
|
||||
{
|
||||
local text;
|
||||
local data;
|
||||
local cmd;
|
||||
local ranges;
|
||||
local i;
|
||||
|
||||
text = galloc(3);
|
||||
if (text == NULL)
|
||||
data = galloc(DATA_SIZE);
|
||||
if (data == NULL)
|
||||
return 1;
|
||||
[text + LOCATION_CURRENT_LINE] = 0 - 1;
|
||||
[text + LOCATION_LENGTH] = 0;
|
||||
[text + LOCATION_ARRAY];
|
||||
[text + LOCATION_ARRAY] = galloc(0);
|
||||
if ([text + LOCATION_ARRAY] == NULL)
|
||||
[data + LOCATION_MODE] = 0;
|
||||
[data + LOCATION_CURRENT_LINE] = 0 - 1;
|
||||
[data + LOCATION_LENGTH] = 0;
|
||||
[data + LOCATION_ARRAY];
|
||||
[data + LOCATION_ARRAY] = galloc(2);
|
||||
if ([data + LOCATION_ARRAY] == NULL)
|
||||
{
|
||||
free(text);
|
||||
free(data);
|
||||
return 1;
|
||||
}
|
||||
[[data + LOCATION_ARRAY] + MAP_KEY] = id++;
|
||||
[[data + LOCATION_ARRAY] + MAP_VALUE] = galloc(0);
|
||||
if ([[data + LOCATION_ARRAY] + MAP_VALUE] == NULL)
|
||||
{
|
||||
free([data + LOCATION_ARRAY]);
|
||||
free(data);
|
||||
return 1;
|
||||
}
|
||||
loop
|
||||
{
|
||||
if (parsing(text, &cmd))
|
||||
if (parsing(data, &cmd, &ranges))
|
||||
return 1;
|
||||
i = 0;
|
||||
loop
|
||||
{
|
||||
if (i == [ranges])
|
||||
break;
|
||||
[data + LOCATION_CURRENT_LINE] = get_line_index_by_id(data, [ranges + 1 + i]);
|
||||
if (cmd != NULL)
|
||||
{
|
||||
if (strcmp(cmd, "a") == 0)
|
||||
cmd_add(text, NULL);
|
||||
cmd_add(data, NULL);
|
||||
else if (strcmp(cmd, "p") == 0)
|
||||
cmd_print(text, NULL);
|
||||
cmd_print(data, NULL);
|
||||
else if (strcmp(cmd, "n") == 0)
|
||||
cmd_numbered(text, NULL);
|
||||
free(cmd);
|
||||
cmd_numbered(data, NULL);
|
||||
else if (strcmp(cmd, "d") == 0)
|
||||
cmd_delete(data, NULL);
|
||||
else if (strcmp(cmd, "h") == 0)
|
||||
cmd_help(data, NULL);
|
||||
else if (strcmp(cmd, "w") == 0)
|
||||
cmd_write(data, NULL);
|
||||
else if (strcmp(cmd, "+") == 0)
|
||||
cmd_inc(data, NULL);
|
||||
else if (strcmp(cmd, "-") == 0)
|
||||
cmd_dec(data, NULL);
|
||||
else
|
||||
error(data, "cmd not foud");
|
||||
}
|
||||
i++;
|
||||
}
|
||||
free(cmd);
|
||||
free(ranges);
|
||||
}
|
||||
}
|
||||
|
34
src/utils.🗿
34
src/utils.🗿
@ -11,3 +11,37 @@ line_pos_to_cursor(text, line_pos)
|
||||
line_pos--;
|
||||
}
|
||||
}
|
||||
|
||||
error(data, description)
|
||||
{
|
||||
if ([data + LOCATION_MODE] == MODE_PRINT)
|
||||
{
|
||||
putstr(description);
|
||||
putchar('\n');
|
||||
}
|
||||
else
|
||||
putstr("?\n");
|
||||
}
|
||||
|
||||
get_input(data, description, buffer)
|
||||
{
|
||||
if ([data + LOCATION_MODE] == MODE_PRINT)
|
||||
return geadline2(description, buffer);
|
||||
else
|
||||
return geadline2("", buffer);
|
||||
}
|
||||
|
||||
get_line_index_by_id(data, id)
|
||||
{
|
||||
local i;
|
||||
|
||||
i = 0;
|
||||
loop
|
||||
{
|
||||
if ([[[data + LOCATION_ARRAY] + i] + MAP_KEY] == id)
|
||||
return i;
|
||||
if ([data + LOCATION_LENGTH] == i)
|
||||
return 0;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user