Browse Source

Added command line arguments

- Pass the source file and then the output file, eg. `uxncle main.uxc out.tal`
- removed debug prints from uparse.c
master
Seth Stubbs 3 months ago
parent
commit
cf23e5c72f
  1. 3
      .gitignore
  2. 5
      main.uxc
  3. 59
      src/main.c
  4. 7
      src/uparse.c

3
.gitignore

@ -1,3 +1,4 @@
*.o
bin
.vscode
.vscode
*.tal

5
main.uxc

@ -0,0 +1,5 @@
int i = 0;
while (i < 10) {
prntint i;
i = i + 1;
}

59
src/main.c

@ -1,17 +1,58 @@
#include "uparse.h"
#include "uasm.h"
int main() {
UASTRootNode *tree = UP_parseSource(
"int i = 0;\n"
"while (i < 10) {\n"
" prntint i;\n"
" i = i + 1;\n"
"}\n"
);
UA_genTal(tree, fopen("bin/out.tal", "w"));
char* readFile(const char* path) {
FILE* file = fopen(path, "rb");
if (file == NULL) {
fprintf(stderr, "Could not open file \"%s\".\n", path);
exit(74);
}
/* first, we need to know how big our file is */
fseek(file, 0L, SEEK_END);
size_t fileSize = ftell(file);
rewind(file);
/* allocate our buffer (+1 for NULL byte) */
char *buffer = (char*)malloc(fileSize + 1);
if (buffer == NULL) {
fprintf(stderr, "failed to allocate!");
exit(EXIT_FAILURE);
}
size_t bytesRead = fread(buffer, sizeof(char), fileSize, file);
if (bytesRead < fileSize) {
printf("failed to read file \"%s\"!\n", path);
exit(74);
}
/* place the null terminator to mark the end of the source */
buffer[bytesRead] = '\0';
/* close the file handler and return the source buffer */
fclose(file);
return buffer;
}
int main(int argc, const char *argv[]) {
const char *out, *in;
char *src;
if (argc < 3) {
printf("Usage: %s [SOURCE] [OUT]\nCompiler for the Uxntal assembly language.", argv[0]);
exit(EXIT_FAILURE);
}
in = argv[1];
out = argv[2];
src = readFile(in);
UASTRootNode *tree = UP_parseSource(src);
UA_genTal(tree, fopen(out, "w"));
/* clean up */
UP_freeTree((UASTNode*)tree);
free(src);
return 0;
}

7
src/uparse.c

@ -124,8 +124,6 @@ void advance(UParseState *state) {
state->previous = state->current;
state->current = UL_scanNext(&state->lstate);
printf("consumed '%.*s', with type %d\n", state->current.len, state->current.str, state->current.type);
switch(state->current.type) {
case TOKEN_UNREC: error(state, "Unrecognized symbol '%.*s'!", state->current.len, state->current.str); break;
case TOKEN_ERR: error(state, "%.*s", state->current.len, state->current.str); break;
@ -158,13 +156,11 @@ ParseRule* getRule(UTokenType type) {
UASTNode* number(UParseState *state, UASTNode *left, Precedence currPrec) {
int num = strtol(state->previous.str, NULL, 10);
printf("got number %d! from token '%.*s' [%d]\n", num, state->previous.len, state->previous.str, state->previous.type);
return newNumNode(state, state->previous, NULL, NULL, num);
}
UASTNode* hexnum(UParseState *state, UASTNode *left, Precedence currPrec) {
int num = strtol(state->previous.str + 2, NULL, 16); /* +2 to skip 0x */
printf("got number %d! from token '%.*s' [%d]\n", num, state->previous.len, state->previous.str, state->previous.type);
return newNumNode(state, state->previous, NULL, NULL, num);
}
@ -472,7 +468,6 @@ UASTRootNode *UP_parseSource(const char *src) {
UParseState state;
UASTRootNode *root = NULL;
UScope *scope;
int treeIndent = 16;
UL_initLexState(&state.lstate, src);
advance(&state);
@ -484,7 +479,7 @@ UASTRootNode *UP_parseSource(const char *src) {
root->scope = *scope;
endScope(&state);
printTree((UASTNode*)root, treeIndent);
/* printTree((UASTNode*)root, 16); */
return root;
}

Loading…
Cancel
Save