update
This commit is contained in:
parent
679fe7a960
commit
839ab5fc40
@ -36,7 +36,7 @@ namespace ztl{
|
|||||||
ObjectType a=getValueOrLiteral(i);
|
ObjectType a=getValueOrLiteral(i);
|
||||||
ObjectType b;
|
ObjectType b;
|
||||||
if(i+1<tokens.size()&&tokens[i+1].type==TokenType::Operator){
|
if(i+1<tokens.size()&&tokens[i+1].type==TokenType::Operator){
|
||||||
if(tokens[i+1].str=="+"){
|
if(tokens[i+1].type==TokenType::Operator){
|
||||||
if(i+2<tokens.size()){
|
if(i+2<tokens.size()){
|
||||||
b=getValueOrLiteral(i+2);
|
b=getValueOrLiteral(i+2);
|
||||||
}else{
|
}else{
|
||||||
@ -49,14 +49,29 @@ namespace ztl{
|
|||||||
return {a,1};
|
return {a,1};
|
||||||
}
|
}
|
||||||
ObjectType res;
|
ObjectType res;
|
||||||
if(std::holds_alternative<int64_t>(a) && tokens[i+1].str=="+" && std::holds_alternative<int64_t>(b)){
|
if(std::holds_alternative<int64_t>(a) && std::holds_alternative<int64_t>(b)){
|
||||||
res = std::get<int64_t>(a) + std::get<int64_t>(b);
|
if(tokens[i+1].str=="+"){
|
||||||
|
res = std::get<int64_t>(a) + std::get<int64_t>(b);
|
||||||
|
}else if(tokens[i+1].str=="-"){
|
||||||
|
res = std::get<int64_t>(a) - std::get<int64_t>(b);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
throw std::runtime_error(std::format("unknown operator '{}' ",tokens[i+1].str));
|
||||||
|
}
|
||||||
}else{
|
}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};
|
return {res,3};
|
||||||
};
|
};
|
||||||
for(size_t i=0;i<tokens.size();i++){
|
for(size_t i=0;i<tokens.size();i++){
|
||||||
|
const auto eat = [&i,&tokens](const std::string &s){
|
||||||
|
if(i+1<tokens.size()&&tokens[i+1].str==s){
|
||||||
|
i++;
|
||||||
|
}else{
|
||||||
|
throw std::runtime_error(std::format("cannot get '{}' after '{}'",s,tokens[i].str));
|
||||||
|
}
|
||||||
|
};
|
||||||
if(tokens[i].type==TokenType::Keyword){
|
if(tokens[i].type==TokenType::Keyword){
|
||||||
if(tokens[i].str=="int"){
|
if(tokens[i].str=="int"){
|
||||||
if( i+2<tokens.size()
|
if( i+2<tokens.size()
|
||||||
@ -71,8 +86,8 @@ namespace ztl{
|
|||||||
}else{
|
}else{
|
||||||
throw std::runtime_error(std::format("error when creatting int value '{}'",tokens[i].str));
|
throw std::runtime_error(std::format("error when creatting int value '{}'",tokens[i].str));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}else if(tokens[i].str=="print"){
|
else if(tokens[i].str=="print"){
|
||||||
if(i+1<tokens.size()&&tokens[i+1].str=="("){
|
if(i+1<tokens.size()&&tokens[i+1].str=="("){
|
||||||
i++;
|
i++;
|
||||||
}else{
|
}else{
|
||||||
@ -93,14 +108,31 @@ namespace ztl{
|
|||||||
else{
|
else{
|
||||||
throw std::runtime_error(std::format("interpreter error Cannot find Keyword '{}'",tokens[i].str));
|
throw std::runtime_error(std::format("interpreter error Cannot find Keyword '{}'",tokens[i].str));
|
||||||
}
|
}
|
||||||
if(i+1<tokens.size()&&tokens[i+1].str==";"){
|
|
||||||
i++;
|
}else if(tokens[i].type==TokenType::Identifier){
|
||||||
|
if( i+1<tokens.size()
|
||||||
|
&& tokens[i+1].str=="="
|
||||||
|
){
|
||||||
|
std::string valueName = tokens[i].str;
|
||||||
|
i+=1;
|
||||||
|
auto res = getExprAns(i+1);
|
||||||
|
if(auto it = objects.find(valueName);it!=objects.end()){
|
||||||
|
if(it->second.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{
|
}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));
|
throw std::runtime_error(std::format("interpreter error at '{}'",tokens[i].str));
|
||||||
}
|
}
|
||||||
|
eat(";");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -14,7 +14,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace ztl{
|
namespace ztl{
|
||||||
|
|
||||||
namespace readFileToStrType{
|
namespace readFileToStrType{
|
||||||
struct CannotOpenFile{};
|
struct CannotOpenFile{};
|
||||||
using ReturnType = std::variant<
|
using ReturnType = std::variant<
|
||||||
@ -97,7 +97,7 @@ namespace ztl{
|
|||||||
}
|
}
|
||||||
else if(isspace(s[i])){
|
else if(isspace(s[i])){
|
||||||
continue;
|
continue;
|
||||||
}else if(s[i]=='='||s[i]=='+'){
|
}else if(s[i]=='='||s[i]=='+'||s[i]=='-'){
|
||||||
tokens.emplace_back(TokenType::Operator,std::string()+s[i]);
|
tokens.emplace_back(TokenType::Operator,std::string()+s[i]);
|
||||||
}else if(isdigit(s[i])){
|
}else if(isdigit(s[i])){
|
||||||
size_t begin = i;
|
size_t begin = i;
|
||||||
|
@ -23,6 +23,7 @@ namespace std{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
namespace ztl {
|
namespace ztl {
|
||||||
|
inline bool isDebug = false;
|
||||||
template<class ...Ts>
|
template<class ...Ts>
|
||||||
struct overloaded : Ts... {using Ts::operator()...;};
|
struct overloaded : Ts... {using Ts::operator()...;};
|
||||||
template<class ...Ts>
|
template<class ...Ts>
|
||||||
@ -39,7 +40,7 @@ namespace ztl {
|
|||||||
}
|
}
|
||||||
},ztl::readFileToStr(v));
|
},ztl::readFileToStr(v));
|
||||||
|
|
||||||
std::cout<<"Lexer() return vector:\n"<<lexer.tokens<<'\n';
|
if(isDebug)std::cout<<"Lexer() return vector:\n"<<lexer.tokens<<'\n';;
|
||||||
ztl::interpreter(lexer.tokens);
|
ztl::interpreter(lexer.tokens);
|
||||||
}
|
}
|
||||||
}
|
}
|
21
src/main.cpp
21
src/main.cpp
@ -1,11 +1,30 @@
|
|||||||
#include "Tools.hpp"
|
#include "Tools.hpp"
|
||||||
|
#include <iostream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
|
const std::string usageInfo = R"(Usage : ztl <filepath>
|
||||||
|
-v to print the version
|
||||||
|
-d to print the debug info
|
||||||
|
)";
|
||||||
|
|
||||||
|
void printUsage(){
|
||||||
|
std::cout<<usageInfo<<'\n';
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc,char *argv[]){
|
int main(int argc,char *argv[]){
|
||||||
if(argc<2){
|
if(argc<2){
|
||||||
throw std::runtime_error("Usage : ztl <filepath>");
|
throw std::runtime_error(usageInfo);
|
||||||
|
}
|
||||||
|
for(int i=0;i<argc;i++){
|
||||||
|
if(std::string(argv[i])=="-v"){
|
||||||
|
std::cout<<"Tudor Lang version alpha 0.1 by Zengtudor\nhttps://gitcode.com/Zengtudor/TudorLang\n";
|
||||||
|
}else if(std::string(argv[i])=="-h"){
|
||||||
|
printUsage();
|
||||||
|
}else if(std::string(argv[i])=="-d"){
|
||||||
|
ztl::isDebug=true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ztl::runCodeFromPath(argv[1]);
|
ztl::runCodeFromPath(argv[1]);
|
||||||
}
|
}
|
@ -1,5 +1,6 @@
|
|||||||
int a =1 +100;
|
int a = 1;
|
||||||
print(a);
|
print(a);
|
||||||
int a = a+5;
|
a=a+1;
|
||||||
print(a);
|
print(a);
|
||||||
print(a+a);
|
print(a-2);
|
||||||
|
print(a);
|
Loading…
Reference in New Issue
Block a user