Skip to content

Commit

Permalink
/checkerrors endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ParfenovIgor committed Nov 30, 2024
1 parent 4455b88 commit 8284b0a
Show file tree
Hide file tree
Showing 17 changed files with 244 additions and 123 deletions.
10 changes: 5 additions & 5 deletions compiler/httpd/httpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
static int listenfd;
int *clients;
static void start_server(const char *);
static void respond(int);
static void respond(int, struct Settings*);

static char *buf;

Expand All @@ -31,7 +31,7 @@ char *method, // "GET" or "POST"

int payload_size;

void serve_forever(const char *PORT) {
void serve_forever(const char *PORT, struct Settings *settings) {
struct sockaddr_in clientaddr;
socklen_t addrlen;

Expand Down Expand Up @@ -64,7 +64,7 @@ void serve_forever(const char *PORT) {
} else {
if (fork() == 0) {
close(listenfd);
respond(slot);
respond(slot, settings);
close(clients[slot]);
clients[slot] = -1;
exit(0);
Expand Down Expand Up @@ -158,7 +158,7 @@ static void uri_unescape(char *uri) {
}

// client connection
void respond(int slot) {
void respond(int slot, struct Settings *settings) {
int rcvd;

buf = malloc(BUF_SIZE);
Expand Down Expand Up @@ -225,7 +225,7 @@ void respond(int slot) {
close(clientfd);

// call router
route();
route(settings);

// tidy up
fflush(stdout);
Expand Down
4 changes: 3 additions & 1 deletion compiler/include/httpd.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <stdio.h>
#include <string.h>

struct Settings;

// Client request
extern char *method, // "GET" or "POST"
*uri, // "/index.html" things before '?'
Expand All @@ -13,7 +15,7 @@ extern char *method, // "GET" or "POST"
extern int payload_size;

// Server control functions
void serve_forever(const char *PORT);
void serve_forever(const char *PORT, struct Settings*);

char *request_header(const char *name);

Expand Down
3 changes: 2 additions & 1 deletion compiler/include/languageserver.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once
#include <settings.h>

void language_server();
void language_server(struct Settings*);
3 changes: 2 additions & 1 deletion compiler/include/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

struct Settings {
bool language_server;
bool states;
bool validate;
bool compile;
bool assemble;
bool link;
Expand All @@ -15,4 +15,5 @@ struct Settings {
const char *filename_input;
const char *filename_output;
const char *filename_compile_output;
const char *calias_directory;
};
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);
}
95 changes: 92 additions & 3 deletions compiler/src/languageserver.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,95 @@
#include <languageserver.h>
#include <posix.h>
#include <httpd.h>
#include <lexer.h>
#include <string.h>

void language_server(int c, char **v) {
serve_forever("12913");
const char *check_errors(const char *payload, struct Settings *settings) {
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_close(STDOUT);
posix_execve(settings->calias_directory, (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;
const char *filename;
for (int i = 0; i < 2; i++) {
int l = pos;
while (pos + 1 < n && out[pos + 1] != '\n') pos++;
if (i == 1) {
filename = _strndup(out + l, pos - l + 1);
}
pos += 2;
}
for (int i = 0; i < 4; i++) {
while (!_isdigit(out[pos])) pos++;
int x = (int)(out[pos] - '0');
while (pos + 1 < n && _isdigit(out[pos + 1])) {
x = x * 10 + (int)(out[pos + 1] - '0');
pos++;
}
values[i] = x - 1;
pos++;
}
int error_pos = pos;

pos = 0;
char *buffer = (char*)_malloc(1024 * sizeof(char));
pos += _sputs(buffer + pos, "{\"res\":\"fail\"");
pos += _sputs(buffer + pos, ",\"file\":\"");
pos += _sputs(buffer + pos, filename);
pos += _sputs(buffer + pos, "\"");
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, ",\"err\":\"");
pos += _sputs(buffer + pos, out + error_pos);
pos += _sputs(buffer + pos, "\"}");
buffer[pos] = '\0';

return buffer;
}

void language_server(struct Settings *settings) {
serve_forever("12913", settings);
}

void route() {
void route(struct Settings *settings) {
ROUTE_START()

POST("/highlight")
Expand All @@ -16,6 +98,13 @@ void route() {
_puts("HTTP/1.1 200 OK\r\n\r\n");
_puts(output);
}

POST("/checkerrors")
{
const char *output = check_errors(payload, settings);
_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
Loading

0 comments on commit 8284b0a

Please sign in to comment.