Skip to content

Commit

Permalink
Make main program read a file
Browse files Browse the repository at this point in the history
Just lex it and print the tokens, since that's the only thing we can
print atm. Run this in CI as a sanity check.

I was going to use clipp for cmdline parsing, but it's broken as of
C++20: muellan/clipp#53. It also seems to be
unmaintained, the PR that fixes it has been open for >2 years:
muellan/clipp#54

Will need to look into finding a new cmdline parsing library.
  • Loading branch information
knatten committed Jan 8, 2023
1 parent 472ccd7 commit 7600905
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion ci/build-and-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ cmake -S $SRC_DIR -B $BUILD_DIR -DCMAKE_C_COMPILER=$C_COMPILER -DCMAKE_CXX_COMPI
cmake --build $BUILD_DIR || exit $?

ctest || exit $?
$BUILD_DIR/bin/dumblang || exit $? # Just check that it runs
$BUILD_DIR/bin/dumblang ../../../examples/example.dumb || exit $? # Just check that it runs
2 changes: 2 additions & 0 deletions examples/example.dumb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let x = 42
let y = x + 3
38 changes: 32 additions & 6 deletions src/dumblang/main.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
#include "lexer.h"
#include "parser.h"

#include <filesystem>
#include <fstream>
#include <iostream>
#include <sstream>

int main()
int main(int argc, char *argv[])
{
std::stringstream ss1{"let x = 0\nlet y = 1"};
lexer::print_tokens(lexer::lex(ss1));
std::stringstream ss2;
lexer::print_tokens(lexer::lex(ss2));
// TODO Use CLI library. Clipp is unmaintained, find another one
if (argc != 2)
{
std::cerr << "USAGE: " << argv[0] << "<input file" << std::endl;
return 1;
}

std::filesystem::path infile{argv[1]};
std::cout << "Input file: " << infile << std::endl;

if (!std::filesystem::exists(infile))
{
std::cerr << "Input file " << infile << " does not exist";
return 1;
}

std::ifstream ifs;
ifs.open(infile, std::ifstream::in);
if (!ifs.is_open())
{
std::cerr << "Failed to open " << infile << std::endl;
return 1;
}

std::cout << "-----\n";
lexer::print_tokens(lexer::lex(ifs));
std::cout << "\n-----\n";
}

0 comments on commit 7600905

Please sign in to comment.