Skip to content

Commit

Permalink
/checkerrors endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ParfenovIgor committed Nov 28, 2024
1 parent 50980e7 commit cdd04b9
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 108 deletions.
4 changes: 3 additions & 1 deletion compiler/src/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ void compile_process(struct Node *node, struct Settings *settings) {
char *program = concat(tmp, str_text);
_free(tmp);
_free(str_text);
write_file(settings->filename_compile_output, program);
if (settings->filename_compile_output) {
write_file(settings->filename_compile_output, program);
}
}

struct TypeNode *CompileBlock(struct Node *node, struct Block *this, struct CPContext *context) {
Expand Down
78 changes: 39 additions & 39 deletions compiler/src/exception.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,52 @@
#include <posix.h>

void error_lexer(const char *value, int line_begin, int position_begin, int line_end, int position_end, const char *filename) {
_fputs(STDOUT, "Error\n");
_fputs(STDOUT, filename);
_fputs(STDOUT, "\n");
_fputi(STDOUT, line_begin + 1);
_fputs(STDOUT, ":");
_fputi(STDOUT, position_begin + 1);
_fputs(STDOUT, "-");
_fputi(STDOUT, line_end + 1);
_fputs(STDOUT, ":");
_fputi(STDOUT, position_end + 1);
_fputs(STDOUT, "\nLexer Error: ");
_fputs(STDOUT, value);
_fputs(STDOUT, "\n");
_fputs(STDERR, "Error\n");
_fputs(STDERR, filename);
_fputs(STDERR, "\n");
_fputi(STDERR, line_begin + 1);
_fputs(STDERR, ":");
_fputi(STDERR, position_begin + 1);
_fputs(STDERR, "-");
_fputi(STDERR, line_end + 1);
_fputs(STDERR, ":");
_fputi(STDERR, position_end + 1);
_fputs(STDERR, "\nLexer Error: ");
_fputs(STDERR, value);
_fputs(STDERR, "\n");
posix_exit(1);
}

void error_syntax(const char *value, struct Token token) {
_fputs(STDOUT, "Error\n");
_fputs(STDOUT, token.filename);
_fputs(STDOUT, "\n");
_fputi(STDOUT, token.line_begin + 1);
_fputs(STDOUT, ":");
_fputi(STDOUT, token.position_begin + 1);
_fputs(STDOUT, "-");
_fputi(STDOUT, token.line_end + 1);
_fputs(STDOUT, ":");
_fputi(STDOUT, token.position_end + 1);
_fputs(STDOUT, "\nSyntax Error: ");
_fputs(STDOUT, value);
_fputs(STDOUT, "\n");
_fputs(STDERR, "Error\n");
_fputs(STDERR, token.filename);
_fputs(STDERR, "\n");
_fputi(STDERR, token.line_begin + 1);
_fputs(STDERR, ":");
_fputi(STDERR, token.position_begin + 1);
_fputs(STDERR, "-");
_fputi(STDERR, token.line_end + 1);
_fputs(STDERR, ":");
_fputi(STDERR, token.position_end + 1);
_fputs(STDERR, "\nSyntax Error: ");
_fputs(STDERR, value);
_fputs(STDERR, "\n");
posix_exit(1);
}

void error_semantic(const char *value, struct Node *node) {
_fputs(STDOUT, "Error\n");
_fputs(STDOUT, node->filename);
_fputs(STDOUT, "\n");
_fputi(STDOUT, node->line_begin + 1);
_fputs(STDOUT, ":");
_fputi(STDOUT, node->position_begin + 1);
_fputs(STDOUT, "-");
_fputi(STDOUT, node->line_end + 1);
_fputs(STDOUT, ":");
_fputi(STDOUT, node->position_end + 1);
_fputs(STDOUT, "\nSemantic Error: ");
_fputs(STDOUT, value);
_fputs(STDOUT, "\n");
_fputs(STDERR, "Error\n");
_fputs(STDERR, node->filename);
_fputs(STDERR, "\n");
_fputi(STDERR, node->line_begin + 1);
_fputs(STDERR, ":");
_fputi(STDERR, node->position_begin + 1);
_fputs(STDERR, "-");
_fputi(STDERR, node->line_end + 1);
_fputs(STDERR, ":");
_fputi(STDERR, node->position_end + 1);
_fputs(STDERR, "\nSemantic Error: ");
_fputs(STDERR, value);
_fputs(STDERR, "\n");
posix_exit(1);
}
75 changes: 75 additions & 0 deletions compiler/src/languageserver.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,76 @@
#include <languageserver.h>
#include <posix.h>
#include <httpd.h>
#include <lexer.h>
#include <string.h>

const char *check_errors(const char *payload) {
int argc = 0;
char *argv[1024];
int n = _strlen(payload);
for (int i = 0; i < n; i++) {
if (payload[i] != ' ') {
int j = i;
while (j + 1 < n && payload[j + 1] != ' ') j++;
int len = j - i + 1;
char *arg = (char*)_malloc((len + 1) * sizeof(char));
_strncpy(arg, payload + i, len);
arg[len] = '\0';
argv[argc] = arg;
argc++;
i = j;
}
}
argv[argc] = NULL;
int fd[2];
posix_pipe(fd);
int pid = posix_fork();
if (pid == 0) {
posix_dup2(fd[1], STDERR);
posix_execve(argv[0], (const char *const*)argv, 0);
}
posix_wait4(pid, 0, 0, 0);
posix_close(fd[1]);
const char *out = read_file_descriptor(fd[0]);

n = _strlen(out);
if (n == 0) {
return "{\"res\":\"ok\"}";
}

int values[4];
int pos = 0;
for (int i = 0; i < 4; i++) {
int x = 0;
while (!_isdigit(out[pos])) pos++;
while (pos + 1 < n && _isdigit(out[pos + 1])) {
x = x * 10 + (int)(out[pos + 1] - '0');
pos++;
}
values[i] = x;
pos++;
}
int error_pos = pos;

pos = 0;
char *buffer = (char*)_malloc(1024 * sizeof(char));
pos += _sputs(buffer + pos, "{\"res\":\"fail\"");
pos += _sputs(buffer + pos, ",\"lb\":");
pos += _sputi(buffer + pos, values[0]);
pos += _sputs(buffer + pos, ",\"pb\":");
pos += _sputi(buffer + pos, values[1]);
pos += _sputs(buffer + pos, ",\"le\":");
pos += _sputi(buffer + pos, values[2]);
pos += _sputs(buffer + pos, ",\"pe\":");
pos += _sputi(buffer + pos, values[3]);
pos += _sputs(buffer + pos, ",\"error\":\"");
pos += _sputs(buffer + pos, out + error_pos);
buffer[pos - 1] = '}';
buffer[pos] = '\0';

return buffer;
}

void language_server(int c, char **v) {
serve_forever("12913");
}
Expand All @@ -16,6 +84,13 @@ void route() {
_puts("HTTP/1.1 200 OK\r\n\r\n");
_puts(output);
}

POST("/checkerrors")
{
const char *output = check_errors(payload);
_puts("HTTP/1.1 200 OK\r\n\r\n");
_puts(output);
}

ROUTE_END()
}
4 changes: 1 addition & 3 deletions compiler/src/lexer.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <lexer.h>
#include <exception.h>
#include <algorithm.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Expand Down Expand Up @@ -450,9 +451,6 @@ const char *TokenColor(enum TokenType type,
return ColorToString(colors[type]);
}

#include <stdio.h>
#include <sys/time.h>

const char *lexer_highlight(const char *str) {
struct TokenStream *token_stream = lexer_process(str, "");

Expand Down
107 changes: 56 additions & 51 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() {
struct Settings *build_settings(int argc, char **argv) {
struct Settings *settings = (struct Settings*)_malloc(sizeof(struct Settings));
settings->language_server = false;
settings->states = false;
Expand All @@ -31,6 +31,58 @@ struct Settings *build_settings() {
settings->filename_input = NULL;
settings->filename_output = NULL;
settings->filename_compile_output = NULL;

for (int i = 1; i < argc; i++) {
const char *arg = argv[i];
if (_strcmp(arg, "-ls") == 0) {
settings->language_server = true;
}
else if (_strcmp(arg, "-s") == 0) {
settings->states = true;
}
else if(_strcmp(arg, "-c") == 0) {
settings->compile = true;
}
else if(_strcmp(arg, "-a") == 0) {
settings->assemble = true;
}
else if (_strcmp(arg, "-l") == 0) {
settings->link = true;
}
else if (_strcmp(arg, "-i") == 0) {
if (i + 2 >= argc) {
_puts("Name and path expected after -i flag");
return NULL;
}
vpush(&settings->include_names, _strdup(argv[i + 1]));
vpush(&settings->include_paths, _strdup(argv[i + 2]));
i += 2;
}
else if (_strcmp(arg, "-o") == 0) {
if (i + 1 == argc) {
_puts("Filename expected after -o flag");
return NULL;
}
const char *str = _strdup(argv[i + 1]);
settings->filename_output = str;
i++;
}
else if (_strcmp(arg, "-t") == 0) {
settings->testing = true;
}
else {
settings->filename_input = _strdup(arg);
}
}

if (settings->language_server) {
language_server();
}

if (!settings->filename_input) {
return NULL;
}
return settings;
}

int main(int argc, char *argv[]) {
Expand All @@ -39,57 +91,10 @@ int main(int argc, char *argv[]) {
return 0;
}
else {
struct Settings *settings = build_settings();
for (int i = 1; i < argc; i++) {
const char *arg = argv[i];
if (_strcmp(arg, "-ls") == 0) {
settings->language_server = true;
}
else if (_strcmp(arg, "-s") == 0) {
settings->states = true;
}
else if(_strcmp(arg, "-c") == 0) {
settings->compile = true;
}
else if(_strcmp(arg, "-a") == 0) {
settings->assemble = true;
}
else if (_strcmp(arg, "-l") == 0) {
settings->link = true;
}
else if (_strcmp(arg, "-i") == 0) {
if (i + 2 >= argc) {
_puts("Name and path expected after -i flag");
return 1;
}
vpush(&settings->include_names, _strdup(argv[i + 1]));
vpush(&settings->include_paths, _strdup(argv[i + 2]));
i += 2;
}
else if (_strcmp(arg, "-o") == 0) {
if (i + 1 == argc) {
_puts("Filename expected after -o flag");
return 1;
}
const char *str = _strdup(argv[i + 1]);
settings->filename_output = str;
i++;
}
else if (_strcmp(arg, "-t") == 0) {
settings->testing = true;
}
else {
settings->filename_input = _strdup(arg);
}
}

if (settings->language_server) {
language_server();
}

if (!settings->filename_input) {
struct Settings *settings = build_settings(argc, argv);
if (!settings) {
help();
return 0;
return 1;
}

return process(settings);
Expand Down
5 changes: 1 addition & 4 deletions compiler/src/syntax.c
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,7 @@ struct Node *syntax_process_expression(struct TokenStream *ts, struct Settings *
error_syntax("Incorrect expression", tokenstream_get(ts));
}

struct Node *res = primaries.ptr[0];
//vdrop(&primaries);
//vdrop(&operations);

struct Node *res = primaries.ptr[0];

if (tokenstream_get(ts).type == TokenAs) {
struct Node *node = (struct Node*)_malloc(sizeof(struct Node));
Expand Down
6 changes: 6 additions & 0 deletions stdlib/asm/posix.asm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ global posix_close
global posix_mmap
global posix_munmap
global posix_pipe
global posix_dup2
global posix_fork
global posix_execve
global posix_exit
Expand Down Expand Up @@ -47,6 +48,11 @@ posix_pipe:
syscall
ret

posix_dup2:
mov rax, 0x21
syscall
ret

posix_fork:
mov rax, 0x39
syscall
Expand Down
1 change: 1 addition & 0 deletions stdlib/include/posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ int posix_close(int fd);
void *posix_mmap(void *start, int length, int prot, int flags, int fd, int offset);
int posix_munmap(void *start, int length);
int posix_pipe(int *fildes);
int posix_dup2(int oldfd, int newfd);
int posix_fork();
int posix_execve(const char *filename, const char *const *argv, const char *const *envp);
void posix_exit(int error_code);
Expand Down
4 changes: 2 additions & 2 deletions stdlib/include/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ int _fputs2(int fd, const char *str1, const char *str2);
int _fputs3(int fd, const char *str1, const char *str2, const char *str3);
int _fputsi(int fd, const char *str1, int x, const char *str2);
int _fputi(int fd, int n);
int _sputs(char *dest, const char *src);
int _sputi(char *dest, int n);
int _sputs(char *dst, const char *src);
int _sputi(char *dst, int n);
4 changes: 2 additions & 2 deletions stdlib/include/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
bool _isalpha(char c);
bool _isdigit(char c);

char *_strcpy(char *a, char *b);
char *_strncpy(char *a, char *b, int n);
char *_strcpy(char *a, const char *b);
char *_strncpy(char *a, const char *b, int n);
char *_strdup(const char *a);
char *_strndup(const char *a, int n);
int _strcmp(const char *a, const char *b);
Expand Down
Loading

0 comments on commit cdd04b9

Please sign in to comment.