Skip to content

Commit

Permalink
Merge pull request #23 from adamhutchings/args
Browse files Browse the repository at this point in the history
Some basic argument parsing
  • Loading branch information
adamhutchings authored Jun 22, 2024
2 parents 914b11c + 56ceabe commit dd50611
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ A C compiler and preprocessor written from scratch in C that targets x86-64 asse

Make sure you have `cmake` installed on your machine, then run
`./scripts/build.sh`. Then, you can run JCCC with `./build/jccc`.

## Usage

Only lexing is supported at this stage. To lex, run the following:
`jccc --token-dump <filename>`
47 changes: 41 additions & 6 deletions src/driver/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,64 @@
*/

#include <stdio.h>
#include <string.h> // strcmp

#include <lexer/lex.h>
#include <util/out.h>

#define TEST_FILE "tests/lextest.c"
int lexer_dump(const char* filename) {

int main(int argc, char **argv) {

// Initialization of everything
Lexer lexer;
FILE * fp = fopen(TEST_FILE, "r");
FILE * fp = fopen(filename, "r");
if (!fp) {
PRINT_ERROR("File %s not found", TEST_FILE);
PRINT_ERROR("File %s not found", filename);
return 1;
}
lexer.fp = fp;
lexer.unlexed_count = 0;

Token t;
do {
lex(&lexer, &t);
// Return if some non-zero (error) code is returned
if (lex(&lexer, &t)) return 1;
printf("Contents: %20s, type: %20s\n", t.contents, ttype_name(t.type));
} while (t.type != TT_EOF);

return 0;

}

int main(int argc, char **argv) {

// TODO -- move this out of main function, perhaps?

// Skip the name of the executable.
--argc, ++argv;

if (argc == 0) {
PRINT_DEFAULT("Usage: --token-dump <filename> to see all tokens");
return 0;
}

if (argc == 1) {
PRINT_DEFAULT("default compilation not supported yet -- try 'jccc --token-dump %s' instead.", argv[0]);
return 1;
}

if (argc > 2) {
PRINT_DEFAULT("expected only two arguments!");
return 1;
}

// Two arguments now.
if (strcmp(argv[0], "--token-dump")) {
PRINT_ERROR("option %s not recognized.", argv[1]);
return 1;
}

// Finally, we can do the lexer test properly!
return lexer_dump(argv[1]);

return 0;
}

0 comments on commit dd50611

Please sign in to comment.