Added crude typing system support.

This commit is contained in:
Ognjen Milan Robovic 2024-04-01 07:23:49 -04:00
parent 795a055e2c
commit 7a9e5f894c
7 changed files with 254 additions and 58 deletions

View File

@ -2,6 +2,7 @@
set -xe
gcc -g -ansi -Wall -Wextra -Wpedantic -Werror -o xiranda xiranda.c
#~gcc -g -ansi -Wall -Wextra -Wpedantic -Werror -o xiranda xiranda.c
gcc -g -Wall -Wextra -Wpedantic -o xiranda xiranda.c
exit

View File

@ -10,8 +10,9 @@ standard_output = 1
format ELF64 executable 3
segment readable executable
;~section '.text' writable
main:
_start:
mov [echo_0], text_data ; text);
call echo ; echo (
@ -94,6 +95,7 @@ echo: ; echo )
ret ;
segment readable writable
;~section '.data' writable
; variable
string_length_length dd 0 ; natural length = 0;

44
test.c
View File

@ -1,38 +1,10 @@
//~#include <unistd.h>
extern int add (int x, int y, int z) { return x + y + z; }
extern int sub (int x, int y) { return x - y; }
extern int mul (int x, int y) { return x * y; }
extern int div (int x, int y) { return x / y; }
extern int mod (int x, int y) { return x % y; }
extern int zah (int x, int y, int z) { return add (x, z*y, z*x); }
//~int string_length (char * string) {
//~int length = 0;
//~while (* (string + length)) length++;
//~return length;
//~}
extern int GetInt (int X, int Y, char * Z);
//~void echo (char * string) {
//~write (1, string, string_length (string));
//~}
//~int main (void) {
//~echo ("Heyo world!\n");
//~return (0);
//~}
extern int z;
extern int strlen (char * data) {
int l = 0;
while (* (data + l) != 0) l++;
return l;
}
extern int add (int x, int y) {
z = x + y;
return x;
}
extern int zod (int x, int y) {
return x % y;
}
extern int mod (int x, int y) {
x = add (x, y);
return x % y;
}
extern int W (int X) { return (3 + GetInt (X, 1, "A")); }

33
test.x
View File

@ -6,10 +6,10 @@ type character = -128 .. 127;
type integer = -2000000000 .. 2000000000;
type natural % 2000000000;
type structure:
integer a = 0;
boolean b = 0;
natural c = 0;;
type structure (
a: integer = 0;
b: boolean = false;
c: natural = 0)
type system_call (
linux_read_system_call = 0,
@ -22,26 +22,33 @@ type file_descriptor (
standard_input = 0,
standard_output = 1)
main:
x: integer = 1;
main () > integer:
x: integer = 1;
s: structure = (-1, true, 1);
loop (x++ < 101):
if ((x % 3 = 0) && (x % 5 = 0))
echo ("fizzbuzz\n");
echo ("fizzbuzz");
else if (x % 3 = 0):
echo ("fizz\n");
echo ("and again fizz\n");;
echo ("and again fizz");;
else if (x % 5 = 0)
echo ("buzz\n");
else:
echo ("fuck formatting...\n");
echo ("fuck formatting twice...\n");;
echo ("fuck formatting...");
echo ("fuck formatting twice...");;
echo ("\n");;
return (0);;
string_length (character * text):
heyo () echo ("Heyo!");
cyaa () echo ("Heyo!");
name () > characer [] return ("Ognjen");
string_length (character string []) > natural:
length: natural = 0;
loop (* (text + length++));
loop (! string [length])
length++;
return (length);;
echo (character * text):
echo (character text []):
system (linux_write_system_call, standard_output, text, string_length (text));
return;;

139
test_stack.asm Normal file
View File

@ -0,0 +1,139 @@
linux_read_system_call = 0
linux_write_system_call = 1
linux_open_system_call = 2
linux_close_system_call = 3
linux_exit_system_call = 60
standard_input = 0
standard_output = 1
format ELF64 executable 3
segment readable executable
main:
nop
nop
nop
mov rdi, text_data ; text);
call echo ; echo (
loop_1:
cmp [main_x], 16
jnb loop_1e
xor rdx, rdx
mov eax, [main_x]
mov ebx, 15
idiv rbx
cmp edx, 0
jne n3
mov rdi, fb
call echo
jmp n0
n3:
xor rdx, rdx
mov eax, [main_x]
mov ebx, 3
idiv rbx
cmp edx, 0
jne n1
mov rdi, f
call echo
jmp n0
n1:
xor rdx, rdx
mov eax, [main_x]
mov ebx, 5
idiv rbx
cmp edx, 0
jne n2
mov rdi, b
call echo
jmp n0
n2:
mov rdi, n
call echo
n0:
inc [main_x]
jmp loop_1
loop_1e:
;
mov rax, linux_exit_system_call ; <main> return;;
xor rdi, rdi
syscall
;~string_length (character * text):
;~natural length = 0;
;~loop (* (text + length++));
;~return (length);;
;~48 C7 C7 BD 11 40 00 E8 CA 00 00 00 83 3D F3 10 00 00 10 0F 83 83 00 00 00 48 31 D2 8B 05 E4 10 00 00 BB 0F 00 00 00 48 F7 FB 83 FA 00 75 0E 48 C7 C7
;~CA 11 40 00 E8 9B 00 00 00 EB 54 48 31 D2 8B 05 C0 10 00 00 BB 03 00 00 00 48 F7 FB 83 FA 00 75 0E 48 C7 C7 D4 11 40 00 E8 77 00 00 00 EB 30 48 31 D2
;~8B 05 9C 10 00 00 BB 05 00 00 00 48 F7 FB 83 FA 00 75 0E 48 C7 C7 DA 11 40 00 E8 53 00 00 00 EB 0C 48 C7 C7 E0 11 40 00 E8 45 00 00 00 FF 05 6F 10 00
;~00 E9 70 FF FF FF 48 C7 C0 3C 00 00 00 48 31 FF 0F 05
;~57 C8 00 00 00 C7 05 48 10 00 00 00 00 00 00 48 31 C0 80 3F 00 74 0B FF 05 3A 10 00 00 48 FF C7 EB F0 C9 8B 05 2E 10 00 00 5F C3
;~57 C8 00 00 00 E8 C5 FF FF FF 48 89 C2 48 89 FE 48 C7 C7 01 00 00 00 48 C7 C0 01 00 00 00 0F 05 48 31 C0 C9 5F C3
;~00 00 00 00 01 00 00 00 48 65 79 6F 20 77 6F 72 6C 64 21 0A 00 66 69 7A 7A 62 75 7A 7A 0A 00 66 69 7A 7A 0A 00 62 75 7A 7A 0A 00 6E 75 6D 62 65 72 0A 00
;~string_length (character * string) > natural:
;~length: natural = 0;
;~loop (string [length]) length++;
;~return (length);;
string_length: ; string_length
nop
nop
nop
push rdi
enter 0, 0
mov [string_length_length], 0
xor rax, rax
loop_0: ; loop_0
cmp byte [rdi], 0 ; (* (text + length)) != '\0'
je loop_0e
inc [string_length_length] ; text + length
inc rdi ; length++
jmp loop_0 ; if not go loop_0
loop_0e:
leave
mov eax, [string_length_length]
pop rdi
ret ; ;
;~echo (character * text):
;~system (linux_write_system_call, standard_output, text, string_length (text));
;~return;;
echo: ; echo )
nop
nop
nop
push rdi
enter 0, 0
call string_length ; |
mov rdx, rax ; | string_length (text)
mov rsi, rdi ; text
mov rdi, standard_output ; standard_output
mov rax, linux_write_system_call ; linux_write_system_call
syscall ; system (
xor rax, rax ; return;;
leave
pop rdi
ret ;
nop
nop
nop
segment readable writable
; variable
string_length_length dd 0 ; natural length = 0;
main_x dd 1 ; integer x = 0;
; data
text_data db 'Heyo world!', 10, 0
fb db 'fizzbuzz', 10, 0
f db 'fizz', 10, 0
b db 'buzz', 10, 0
n db 'number', 10, 0

54
test_x.x Normal file
View File

@ -0,0 +1,54 @@
--- Heyo
type boolean (false, true)
type character = -128 .. 127;
type integer = -2000000000 .. 2000000000;
type natural % 2000000000;
type structure (
a: integer = 0;
b: boolean = false;
c: natural = 0)
type system_call (
linux_read_system_call = 0,
linux_write_system_call = 1,
linux_open_system_call = 2,
linux_close_system_call = 3,
linux_exit_system_call = 60)
type file_descriptor (
standard_input = 0,
standard_output = 1)
main () > integer:
x: integer = 1;
s: structure = (-1, true, 1);
loop (x++ < 101):
if ((x % 3 = 0) && (x % 5 = 0))
echo ("fizzbuzz");
else if (x % 3 = 0):
echo ("fizz\n");
echo ("and again fizz");;
else if (x % 5 = 0)
echo ("buzz\n");
else:
echo ("fuck formatting...");
echo ("fuck formatting twice...");;
echo ("\n");;
return (0);;
heyo () echo ("Heyo!");
cyaa () echo ("Heyo!");
name () > characer [] return ("Ognjen");
string_length (character string []) > natural:
length: natural = 0;
loop (! string [length])
length++;
return (length);;
echo (character text []):
system (linux_write_system_call, standard_output, text, string_length (text));
return;;

View File

@ -2,7 +2,7 @@
enum {
core_none, core_word, core_marker, core_string, core_number,
core_symbol
core_symbol, core_type
};
enum {
@ -17,16 +17,18 @@ static int token_count = 0;
static int string_code = 0;
static int number_code = 0;
static int marker_code = 0;
static int type_code = 0;
static int symbol_code = 0;
static int * token_data = null;
static int * token_type = null;
static char * * string_name = null;
/*static char * * string_name = null;*/
static char * * string_data = null;
static int * string_size = null;
static char * * number_name = null;
/*static char * * number_name = null;*/
static int * number_data = null;
static char * * marker_name = null;
static char * * type_name = null;
static int * marker_type = null;
static char * symbol_data = null;
@ -69,6 +71,13 @@ static void add_marker (char * name, int type) {
++marker_code;
}
static void add_type (char * name) {
type_name = reallocate (type_name, (type_code + 1) * (int) sizeof (* type_name));
type_name [type_code] = allocate (NAME_LIMIT * (int) sizeof (* type_name));
string_copy (type_name [type_code], name);
++type_code;
}
static void add_symbol (char data) {
symbol_data = reallocate (symbol_data, (symbol_code + 1) * (int) sizeof (* symbol_data));
symbol_data [symbol_code] = data;
@ -126,15 +135,24 @@ int main (void) {
}
}
add_token (core_marker, marker_code);
here:
add_marker (name, token_type [token_count - 1]);
if ((token_type [token_count - 2] == core_word) && (token_data [token_count - 2] == word_type)) { // NEW TYPE
token_type [token_count - 1] = core_type;
add_type (name);
}
for (length = 0; length < type_code; ++length) {
if (string_compare_limit (name, type_name [length], string_length (type_name [length]) + 1) == true) { // EXISTING TYPE
token_type [token_count - 1] = core_type;
}
}
here:
--offset;
} else if (character_compare_array (buffer [offset], ",.;:=<>&|+-*/%(){}") == true) {
} else if (character_compare_array (buffer [offset], ",.;:=<>&|!+-*/%()[]") == true) {
add_token (core_symbol, symbol_code);
add_symbol (buffer [offset]);
} else {
if (character_is_blank (buffer [offset]) == false) {
echo ("\033[1;31mSegmentation fault motherfucker!\033[0m\n");
echo ("\033[1;31mCharacter set exception point: Segmentation\033[0m\n");
printf ("%c -- %i\n", buffer [offset], (int) buffer [offset]);
exit (log_failure);
}
@ -146,6 +164,7 @@ int main (void) {
case core_symbol: printf ("\033[1;34m%c\033[0m ", symbol_data [token_data [length]]); break;
case core_string: printf ("\033[1;31m%s\033[0m ", string_data [token_data [length]]); break;
case core_number: printf ("\033[1;32m%i\033[0m ", number_data [token_data [length]]); break;
case core_type: printf ("\033[1;36m%s\033[0m ", marker_name [token_data [length]]); break;
case core_marker: printf ("\033[1;33m%s\033[0m ", marker_name [token_data [length]]); break;
case core_word: printf ("\033[1;35m%s\033[0m ", word_list [token_data [length]]); break;
default: break;
@ -187,7 +206,7 @@ int main (void) {
}
} else if ((token_type [length] == core_word) && (token_data [length] == word_type)) {
++length;
if (token_type [length] == core_marker) {
if (token_type [length] == core_type) {
echo ("type <name> -- ");
echo (marker_name [token_data [length]]);
echo ("\n");
@ -200,11 +219,13 @@ int main (void) {
for (length = 0; length < string_code; ++length) { string_data [length] = deallocate (string_data [length]); }
for (length = 0; length < marker_code; ++length) { marker_name [length] = deallocate (marker_name [length]); }
for (length = 0; length < type_code; ++length) { type_name [length] = deallocate (type_name [length]); }
string_data = deallocate (string_data);
string_size = deallocate (string_size);
marker_name = deallocate (marker_name);
marker_type = deallocate (marker_type);
type_name = deallocate (type_name);
symbol_data = deallocate (symbol_data);
number_data = deallocate (number_data);
token_data = deallocate (token_data);