From 839ab5fc40d0bbca65f273e555292d0af83ad661 Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Fri, 18 Jul 2025 21:25:22 +0800 Subject: [PATCH] update --- include/Interpreter.hpp | 52 +++++++++++++++++++++++++++++++++-------- include/Lexer.hpp | 4 ++-- include/Tools.hpp | 3 ++- src/main.cpp | 21 ++++++++++++++++- tests/main.ztl | 7 +++--- 5 files changed, 70 insertions(+), 17 deletions(-) diff --git a/include/Interpreter.hpp b/include/Interpreter.hpp index fb152dc..eb0e89f 100644 --- a/include/Interpreter.hpp +++ b/include/Interpreter.hpp @@ -36,7 +36,7 @@ namespace ztl{ ObjectType a=getValueOrLiteral(i); ObjectType b; if(i+1(a) && tokens[i+1].str=="+" && std::holds_alternative(b)){ - res = std::get(a) + std::get(b); + if(std::holds_alternative(a) && std::holds_alternative(b)){ + if(tokens[i+1].str=="+"){ + res = std::get(a) + std::get(b); + }else if(tokens[i+1].str=="-"){ + res = std::get(a) - std::get(b); + } + else{ + throw std::runtime_error(std::format("unknown operator '{}' ",tokens[i+1].str)); + } }else{ - throw std::runtime_error(std::format("unknown operator '{}' or value type '{}' ,'{}'",tokens[i+1].str,tokens[i].str,tokens[i+2].str)); + throw std::runtime_error(std::format("unknown value type '{}' ,'{}'",tokens[i].str,tokens[i+2].str)); } + return {res,3}; }; for(size_t i=0;isecond.index()!=res.first.index()){ + throw std::runtime_error(std::format("cannot give value to {}, because type is not same",valueName)); + } + }else{ + throw std::runtime_error(std::format("error when getting int value named '{}'",valueName)); + } + objects[valueName] = res.first; + i+=res.second; }else{ - throw std::runtime_error(std::format("cannot get ';' after '{}'",tokens[i].str)); + throw std::runtime_error(std::format("error when creatting int value '{}'",tokens[i].str)); } - }else{ + } + else{ throw std::runtime_error(std::format("interpreter error at '{}'",tokens[i].str)); } + eat(";"); } } } \ No newline at end of file diff --git a/include/Lexer.hpp b/include/Lexer.hpp index 8f28590..9c67e0f 100644 --- a/include/Lexer.hpp +++ b/include/Lexer.hpp @@ -14,7 +14,7 @@ #include namespace ztl{ - + namespace readFileToStrType{ struct CannotOpenFile{}; using ReturnType = std::variant< @@ -97,7 +97,7 @@ namespace ztl{ } else if(isspace(s[i])){ continue; - }else if(s[i]=='='||s[i]=='+'){ + }else if(s[i]=='='||s[i]=='+'||s[i]=='-'){ tokens.emplace_back(TokenType::Operator,std::string()+s[i]); }else if(isdigit(s[i])){ size_t begin = i; diff --git a/include/Tools.hpp b/include/Tools.hpp index 91f3d1f..adaa623 100644 --- a/include/Tools.hpp +++ b/include/Tools.hpp @@ -23,6 +23,7 @@ namespace std{ } } namespace ztl { + inline bool isDebug = false; template struct overloaded : Ts... {using Ts::operator()...;}; template @@ -39,7 +40,7 @@ namespace ztl { } },ztl::readFileToStr(v)); - std::cout<<"Lexer() return vector:\n"< #include +#include #include +const std::string usageInfo = R"(Usage : ztl + -v to print the version + -d to print the debug info +)"; + +void printUsage(){ + std::cout<"); + throw std::runtime_error(usageInfo); + } + for(int i=0;i