Skip to content

Commit

Permalink
Alloc cyclic import
Browse files Browse the repository at this point in the history
  • Loading branch information
ParfenovIgor committed Dec 30, 2024
1 parent 4081b63 commit 5a5d1fb
Show file tree
Hide file tree
Showing 21 changed files with 164 additions and 49 deletions.
4 changes: 4 additions & 0 deletions altlib/memory.al
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
//* Copies the data of length `sz` bytes from `src` pointer to `dest` pointer.
proto ._memcpy(dest #1I, src #1I, sz #I) -> #1I

//* _memmove
//* Copies the data of length `sz` bytes from `src` pointer to `dest` pointer. Handles possible overlapping
proto ._memmove(dest #1I, src #1I, sz #I) -> #1I

//* _memset
//* Sets the data of length `count` bytes to `dest` pointer with value `ch`. Important: only the lowest byte in `ch` argument is used.
proto ._memset(dest #1I, ch #I, count #I) -> #1I
1 change: 1 addition & 0 deletions altlib/posix.al
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ proto .posix_execve(filename #1I, argv #2C, envp #2C) -> #I
proto .posix_exit(error_code #I) -> #I
proto .posix_wait4(pid #I, stat_addr #1I, options #I, rusage #1I) -> #I
proto .posix_unlink(pathname #1I) -> #I
proto .posix_getcwd(buf #1C, size #I) -> #1C
6 changes: 4 additions & 2 deletions altlib/stdlib.al
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
include altlib."string.al"

//* stdlib
//* todo
Expand Down Expand Up @@ -44,7 +46,7 @@ func ^.itoa_(n #I) -> #1C {
}
}

/* func ^.atoi_(str #1C) -> #I {
func ^.atoi_(str #1C) -> #I {
def i := strlen_(str) - 1
def x := 0
eval while (i >= 0) {
Expand All @@ -53,7 +55,7 @@ func ^.itoa_(n #I) -> #1C {
i := i - 1
}
return x
} */
}

func ^.rand_(seed #I) -> #I {
seed := seed * 1103515245 + 12345
Expand Down
8 changes: 8 additions & 0 deletions altlib/string.al
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ func ^.strlen_(a #1C) -> #I {
} else 0
}
func ^.strnlen_(a #1C, n #I) -> #I {
def i := 0
return while (i < n) {
eval if (a[i] = '\0') { break i }
i := i + 1
} else n
}
func ^.concat_(a #1C, b #1C) -> #1C {
def s_a := strlen_(a)
def s_b := strlen_(b)
Expand Down
1 change: 1 addition & 0 deletions compiler/include/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ struct Settings {
const char *filename_output;
const char *filename_compile_output;
const char *calias_directory;
struct Vector included_files;
};
30 changes: 10 additions & 20 deletions compiler/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void help() {
_puts(" -o <file> Set output file name.");
}

struct Settings *build_settings(int argc, char **argv, char **envp) {
struct Settings *build_settings(int argc, char **argv) {
struct Settings *settings = (struct Settings*)_malloc(sizeof(struct Settings));
settings->language_server = false;
settings->validate = false;
Expand All @@ -31,7 +31,7 @@ struct Settings *build_settings(int argc, char **argv, char **envp) {
settings->filename_input = NULL;
settings->filename_output = NULL;
settings->filename_compile_output = NULL;
settings->calias_directory = "/";
settings->calias_directory = _strdup(argv[0]);

for (int i = 1; i < argc; i++) {
const char *arg = argv[i];
Expand Down Expand Up @@ -75,22 +75,6 @@ struct Settings *build_settings(int argc, char **argv, char **envp) {
settings->filename_input = _strdup(arg);
}
}
for (int x = 0; envp[x]; x++) {
const char *str = envp[x];
int delim = -1;
int n = _strlen(str);
for (int i = 0; i < n; i++) {
if (str[i] == '=') {
delim = i;
break;
}
}
if (delim == -1) continue;
if (_strncmp(str, "CALIAS_DIR", delim) == 0) {
settings->calias_directory = _strdup(str + delim + 1);
break;
}
}

if (settings->language_server) {
language_server(settings);
Expand All @@ -99,16 +83,22 @@ struct Settings *build_settings(int argc, char **argv, char **envp) {
if (!settings->filename_input) {
return NULL;
}

settings->included_files = vnew();
const char *filename = _strrchr(settings->filename_input, '/');
if (!filename) filename = settings->filename_input;
else filename++;
vpush(&settings->included_files, _strdup(filename));
return settings;
}

int main(int argc, char *argv[], char *envp[]) {
int main(int argc, char *argv[]) {
if (argc < 2) {
help();
return 0;
}
else {
struct Settings *settings = build_settings(argc, argv, envp);
struct Settings *settings = build_settings(argc, argv);
if (!settings) {
help();
return 1;
Expand Down
56 changes: 39 additions & 17 deletions compiler/src/syntax.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,10 @@ struct Node *syntax_process_block(struct TokenStream *ts, struct Settings *st, b
}

while (tokenstream_get(ts).type != TokenEof && tokenstream_get(ts).type != TokenBraceClose) {
if (tokenstream_get(ts).type == TokenSemicolon) {
tokenstream_next(ts);
continue;
struct Node *node = syntax_process_statement(ts, st);
if (node) {
vpush(&this->statement_list, node);
}
vpush(&this->statement_list, syntax_process_statement(ts, st));
if (one_statement) break;
}
if (braces && !one_statement) {
Expand Down Expand Up @@ -737,6 +736,10 @@ struct FunctionSignature *syntax_process_function_signature(struct TokenStream *
}

struct Node *syntax_process_statement(struct TokenStream *ts, struct Settings *st) {
if (tokenstream_get(ts).type == TokenSemicolon) {
tokenstream_next(ts);
return NULL;
}
if (tokenstream_get(ts).type == TokenEval) {
tokenstream_next(ts);
return syntax_process_expression(ts, st);
Expand Down Expand Up @@ -764,22 +767,41 @@ struct Node *syntax_process_statement(struct TokenStream *ts, struct Settings *s
}
pass_next(ts, TokenDot, ". expected in include");
check_next(ts, TokenString, "String literal expected in include");
char *filename = _concat(include_path, tokenstream_get(ts).value_string);
char *path = _concat(include_path, tokenstream_get(ts).value_string);
tokenstream_next(ts);
int fd = posix_open(filename, 0, 0);
if (fd <= 0) {
const char *buffer = _concat("Could not open file ", filename);
error_syntax(buffer, tokenstream_get(ts));
bool good = true;

const char *filename = _strrchr(path, '/');
if (!filename) filename = path;
else filename++;

int sz = vsize(&st->included_files);
for (int i = 0; i < sz; i++) {
if (_strcmp(filename, st->included_files.ptr[i]) == 0) {
good = false;
break;
}
}
if (good) {
vpush(&st->included_files, (char*)filename);
int fd = posix_open(path, 0, 0);
if (fd <= 0) {
const char *buffer = _concat("Could not open file ", path);
error_syntax(buffer, tokenstream_get(ts));
}
else {
posix_close(fd);
}
struct Node *_node = process_parse(path, st);
struct Block *inc_block = (struct Block*)_node->node_ptr;
this->statement_list = inc_block->statement_list;
_free(inc_block);
_free(_node);
}
else {
posix_close(fd);
}
struct Node *_node = process_parse(filename, st);
struct Block *inc_block = (struct Block*)_node->node_ptr;
this->statement_list = inc_block->statement_list;
_free(filename);
_free(inc_block);
_free(_node);
_free(node);
return NULL;
}
}
else if (tokenstream_get(ts).type == TokenTest) {
struct Test *this = (struct Test*)_malloc(sizeof(struct Test));
Expand Down
23 changes: 19 additions & 4 deletions docs/altlibref.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ <h2>Table of Contents</h2>
<li><a href="#3">memory</a>
<ul>
<li><a href="#3.1">_memcpy</a></li>
<li><a href="#3.2">_memset</a></li>
<li><a href="#3.2">_memmove</a></li>
<li><a href="#3.3">_memset</a></li>
</ul></li>
<li><a href="#4">posix</a>
<ul>
Expand Down Expand Up @@ -151,7 +152,12 @@ <h3 id="3.1">_memcpy</h3>
<pre><code>proto ._memcpy(dest #1I, src #1I, sz #I) -> #1I

</code></pre>
<h3 id="3.2">_memset</h3>
<h3 id="3.2">_memmove</h3>
Copies the data of length <code>sz</code> bytes from <code>src</code> pointer to <code>dest</code> pointer. Handles possible overlapping
<pre><code>proto ._memmove(dest #1I, src #1I, sz #I) -> #1I

</code></pre>
<h3 id="3.3">_memset</h3>
Sets the data of length <code>count</code> bytes to <code>dest</code> pointer with value <code>ch</code>. Important: only the lowest byte in <code>ch</code> argument is used.
<pre><code>proto ._memset(dest #1I, ch #I, count #I) -> #1I
</code></pre>
Expand All @@ -168,6 +174,7 @@ <h3 id="4.1">todo</h3>
proto .posix_exit(error_code #I) -&gt #I
proto .posix_wait4(pid #I, stat_addr #1I, options #I, rusage #1I) -&gt #I
proto .posix_unlink(pathname #1I) -&gt #I
proto .posix_getcwd(buf #1C, size #I) -&gt #1C
</code></pre>
<h2 id="5">stdio</h2>
<h3 id="5.1">fputs_</h3>
Expand Down Expand Up @@ -361,7 +368,7 @@ <h3 id="6.1">todo</h3>
}
}

/* func ^.atoi_(str #1C) -&gt #I {
func ^.atoi_(str #1C) -&gt #I {
def i := strlen_(str) - 1
def x := 0
eval while (i &gt= 0) {
Expand All @@ -370,7 +377,7 @@ <h3 id="6.1">todo</h3>
i := i - 1
}
return x
} */
}

func ^.rand_(seed #I) -&gt #I {
seed := seed * 1103515245 + 12345
Expand Down Expand Up @@ -425,6 +432,14 @@ <h3 id="7.1">todo</h3>
} else 0
}

func ^.strnlen_(a #1C, n #I) -&gt #I {
def i := 0
return while (i &lt n) {
eval if (a[i] = '\0') { break i }
i := i + 1
} else n
}

func ^.concat_(a #1C, b #1C) -&gt #1C {
def s_a := strlen_(a)
def s_b := strlen_(b)
Expand Down
26 changes: 26 additions & 0 deletions stdlib/asm/memory.asm
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
global _memcpy
_memcpy:
call _cpy_dir

global _memmove
_memmove:
mov rax, rdi
sub rax, rsi
jl _call_dir
call _cpy_rev
ret
_call_dir:
call _cpy_dir
ret

_cpy_dir:
mov rcx, rdx
rep movsb
mov rax, rdi
ret

_cpy_rev:
mov rcx, rdx
add rdi, rcx
sub rdi, 1
add rsi, rcx
sub rsi, 1
std
rep movsb
mov rax, rdi
cld
ret

global _memset
Expand Down
6 changes: 6 additions & 0 deletions stdlib/asm/posix.asm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ global posix_execve
global posix_exit
global posix_wait4
global posix_unlink
global posix_getcwd

posix_read:
mov rax, 0x0
Expand Down Expand Up @@ -77,3 +78,8 @@ posix_unlink:
mov rax, 0x57
syscall
ret

posix_getcwd:
mov rax, 0xb7
syscall
ret
1 change: 1 addition & 0 deletions stdlib/include/memory.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once

void *_memcpy(void *dest, const void *src, int count);
void *_memmove(void *dest, const void *src, int count);
void *_memset(void *dest, int ch, int count);
1 change: 1 addition & 0 deletions stdlib/include/posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ int posix_execve(const char *filename, const char *const *argv, const char *cons
void posix_exit(int error_code);
int posix_wait4(int pid, int *stat_addr, int options, void *rusage);
int posix_unlink(const char *pathname);
char *posix_getcwd(char *buf, unsigned long size);
3 changes: 3 additions & 0 deletions stdlib/include/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

bool _isalpha(char c);
bool _isdigit(char c);
char *_strchr(const char *s, int c);
char *_strrchr(const char *s, int c);

char *_strcpy(char *a, const char *b);
char *_strncpy(char *a, const char *b, int n);
Expand All @@ -19,6 +21,7 @@ char *_strndup(const char *a, int n);
int _strcmp(const char *a, const char *b);
int _strncmp(const char *a, const char *b, int num);
int _strlen(const char *a);
int _strnlen(const char *a, int n);
char *_concat(const char *a, const char *b);
char *_concat3(const char *a, const char *b, const char *c);
const char *_substr(const char *a, int n);
Expand Down
5 changes: 4 additions & 1 deletion stdlib/src/stdlib.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include <stdlib.h>
#include <heap.h>
#include <memory.h>
#include <posix.h>
#include <stdlib.h>
#include <string.h>

char *_itoa(int n) {
if (n == 0) {
Expand Down
Loading

0 comments on commit 5a5d1fb

Please sign in to comment.