This commit is contained in:
Zengtudor 2025-07-12 14:14:54 +08:00
parent 65bc648860
commit 185c7f785c
4 changed files with 30 additions and 4 deletions

View File

@ -204,7 +204,9 @@ namespace std {
os<<" "<<v[v.size()-1]; os<<" "<<v[v.size()-1];
}else{ }else{
os<<"}"; os<<"}";
return os;
} }
os<<"}";
return os; return os;
} }
} }

View File

@ -6,14 +6,15 @@
#include <utility> #include <utility>
#include <variant> #include <variant>
#include <vector> #include <vector>
#include<fstream>
namespace ztl{ namespace ztl{
template<class ...Ts> template<class ...Ts>
void logger(Ts&&...v){ void logger(Ts&&...v){
#ifndef NDEBUG #ifndef NDEBUG
std::cout<<"log["; std::cout<<"log: ";
(std::cout<<...<<v); (std::cout<<...<<v);
std::cout<<"]\n"; std::cout<<"\n";
#endif #endif
} }
@ -76,5 +77,22 @@ namespace ztl{
} }
}; };
inline Result<std::string> get_argv(size_t idx){
if(idx>=__argc){
return Result<std::string>(Err("argv's index out of range"));
}else{
return Result<std::string>(std::string(__argv[idx]));
}
}
inline Result<std::string> get_string_from_file(const std::string &file_path){
std::ifstream file(file_path);
if (!file) {
return Result<std::string>(Err("Could not open file: " + file_path));
}
std::string content((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
return Result<std::string>(content);
}
} }

View File

@ -1,6 +1,11 @@
#include "Lexer.hpp" #include "Lexer.hpp"
#include <iostream> #include "Tools.hpp"
#include <string>
int main(){ int main(){
std::string content = ztl::get_string_from_file(ztl::get_argv(1).unwrap()).unwrap();
ztl::logger("Reading file: ", ztl::get_argv(1).unwrap());
ztl::logger("File content:\n", content);
std::vector<ztl::lexer::Token> tokens = ztl::lexer::lexer(content);
ztl::logger("Tokens parsed:\n", tokens);
} }

1
tests/main.ztl Normal file
View File

@ -0,0 +1 @@
print("hello world");