A space for me to implement some compillers things
To compile everything, just execute in the root folder:
$ make all
Is possible to only compile the main compiler with:
$ make main
And to only compile the compiler with the server:
$ make server
A file called a.out
will be created with only the compiler to you execute it with:
$ ./a.out
And a file called server.out
with the server to you execute it with:
$ ./server.out
One implementation of a Lexer to generate token from C based code.
- The Lexer needs a Symbols Table to work, create it with:
#include "symbolsTable/symbolsTable.h"
// ...
SymbolsTable* st = symbolsTable_init();
- Now we can create the Lexer, it need 3 things: path to source code, the size of buffer and the Symbols Table:
#include "lexer/lexer.h"
// ...
Lexer* l = lexer_init("code_example.txt", 1024, st);
- And now we use
lexer_hasNext(Lexer*)
to check if has a Token available andlexer_getNextToken(Lexer*)
to get the Token. Follow the example to get all Tokens:
while (lexer_hasNext(l)) {
Token t = lexer_getNextToken(l);
}
- Don't forget to free the Lexer and the Symbols Table at the end:
lexer_free(l);
symbolsTable_free(st);
To a complete example see the main.c file
I created a web server from scratch using only TCP sockets, you can see more about it implementation in extras/server
including the references that was consulted about sockets itself and HTTP responses.
However it's not perfect yet. It works to delivery content in json with simples requests (probably there's some bugs yet), but to serve HTTP pages like in Chrome it doesn't really work well because Chrome and other browers sends a lot of extras request that make the server crash in some point.
Also it's not complete yet, there's a lot of thing missing like some body parser (you can only deal with raw payload yet) and other things.
- First we need to init the server specifying the port it will run.
#include "extras/server/server.h"
// ...
int main() {
Server* s = server_init(8000);
return 0;
}
- Now we define our callback that the server will call. The callbacks always need to return a
ResponseCreator*
and receive aRequest
as argument:
#include "extras/server/server.h"
#include "extras/server/responseCreator/responseCreator.h"
// ...
//Callback definition
ResponseCreator* helloWorld(Request r) {
}
In the Request
we can find some useful information like the HTTP method (GET, POST), the path of the request (/api/helloworld) and the raw payload sent in content
.
- Now we define the response that will send back to the client using the
ResponseCreator
. You only need to useresponseCreator_init
and specifying the content type (JSON, HTML) and the status code (200, 404), and then creting you response apedding strings withresponseCreator_appendContent
. Follow one example:
//Callback definition
ResponseCreator* helloWorld(Request r) {
ResponseCreator* response = responseCreator_init(TYPE_JSON, 200);
responseCreator_appendContent(response, "[");
responseCreator_appendContent(response, "{\"msg\": \"Hello, World!\"},");
responseCreator_appendContent(response, "{\"msg\": \"It works!!\"}");
responseCreator_appendContent(response, "]");
return response;
}
And it will returns:
[
{
"msg": "Hello, World!"
},
{
"msg": "It works!!"
}
]
You don't need to worry about free the
ResponseCreator*
created, the server do it automatically
- Now it just add our route to the server. You use
server_addRoute
to do it and specify the path (/api/helloworld), the method (GET, POST) and the callback:
int main() {
Server* s = server_init(8000);
server_addRoute(s, "/api/helloworld", HTTP_GET, helloWorld);
return 0;
}
- And finally we start the server:
int main() {
//...
//Start the server
server_start(s);
//Free the server
server_free(s);
return 0;
}
You problaly will want to handle SIGINT (ctrl-c) to actualy free the server (currently there's not other way to stop it), see the serverRunner.c file to an example.
This is a simple web page created with Vue, Vuetify and the Monaco Editor to a better code visualization.
- First we need to install all the dependencies, make sure you have NodeJs and NPM installed. in the
/extras/client
folder execute:
$ npm install
- Now we just need to run that with:
$ npm run serve
You also need to run the Compiler Server, see the Basics section
- Acess
localhost:8080/
in your browser.