even better parser

This commit is contained in:
2023-12-26 13:41:14 +01:00
parent 27cbe8a200
commit a0febfba0e
5 changed files with 22 additions and 27 deletions

View File

@@ -2,6 +2,6 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_library(vm src/VM.cpp src/Cell.cpp
src/Parser.cpp src/SParser.cpp src/MemoryContext.cpp src/ConsUtils.cpp)
src/Parser.cpp src/MemoryContext.cpp src/ConsUtils.cpp)
target_include_directories(vm PUBLIC includes)

View File

@@ -1,14 +0,0 @@
//
// Created by Stepan Usatiuk on 25.12.2023.
//
#ifndef PSIL_SPARSER_H
#define PSIL_SPARSER_H
class SParser {
// SParser()
};
#endif//PSIL_SPARSER_H

View File

@@ -4,6 +4,7 @@
#include "Parser.h"
#include <format>
#include <ranges>
#include <stack>
@@ -62,10 +63,23 @@ std::string_view Parser::Tokenizer::peek() const {
}
void Parser::Tokenizer::load(std::string_view input) {
for (const auto &w: input | std::views::split(' ') | std::views::transform([](auto &&rng) {
return std::string_view(&*rng.begin(), std::ranges::distance(rng));
})) {
_tokens.emplace(w);
std::string_view::size_type curpos = input.find_first_not_of(' ');
static const std::string alnum = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
static const std::string special = "().";
while (curpos != std::string_view::npos) {
if (alnum.find(input.at(curpos)) != std::string::npos) {
std::string_view::size_type end = input.find_first_not_of(alnum, curpos);
_tokens.emplace(input.cbegin() + curpos, input.cbegin() + end);
curpos = end;
} else if (special.find(input.at(curpos)) != std::string::npos) {
_tokens.emplace(1, input.at(curpos));
curpos++;
} else {
throw std::invalid_argument("Unexpected symbol " + std::string(1, input.at(curpos)));
}
curpos = input.find_first_not_of(' ', curpos);
}
}

View File

@@ -1,5 +0,0 @@
//
// Created by Stepan Usatiuk on 25.12.2023.
//
#include "SParser.h"