Procházet zdrojové kódy

Some random progress

master
Ludovic 'Archivist' Lagouardette před 1 rokem
rodič
revize
5b2cc8c9dd
4 změnil soubory, kde provedl 50 přidání a 2 odebrání
  1. +1
    -1
      CMakeLists.txt
  2. +6
    -1
      include/molasses/parser_primitives.h
  3. +13
    -0
      src/main.cpp
  4. +30
    -0
      src/molasses/interpreter.cpp

+ 1
- 1
CMakeLists.txt Zobrazit soubor

@ -13,7 +13,7 @@ add_executable(sugar
include/molasses/lexer.h
src/molasses/parser_primitives.cpp
include/molasses/parser_primitives.h
include/molasses/generator_primitives.h)
include/molasses/generator_primitives.h src/molasses/interpreter.cpp)
function(add_expect_test [testname filename])
add_test(

+ 6
- 1
include/molasses/parser_primitives.h Zobrazit soubor

@ -214,6 +214,7 @@ namespace molasses {
std::vector<std::string> operator>>(std::vector<std::string> current_stack, const operation& next_op);
std::optional<int32_t> try_parse_int32(const std::string& str);
std::optional<int64_t> try_parse_int64(const std::string& str);
struct parser_context {
std::vector<std::shared_ptr<type>> types;
@ -229,7 +230,11 @@ namespace molasses {
parser_context parser;
std::vector<std::shared_ptr<procedure_operation>> procedures;
};
using success_t = bool;
std::tuple<interpreter_stack, generate_context, std::optional<std::shared_ptr<std::runtime_error>>> interpret(interpreter_stack, generate_context, std::string );
generate_context parse(parser_context, const lexed_output&);
std::vector<std::string> generate(const generate_context&);

+ 13
- 0
src/main.cpp Zobrazit soubor

@ -111,6 +111,19 @@ int main(int argc, char** argv) {
compile_stack.emplace(lexer_1);
} else
throw build_system_error("merge-all expects at least 1 lexed outputs\n");
} else if(elem == "interpret") {
if(not compile_stack.empty() and std::holds_alternative<molasses::generate_context>(compile_stack.top())) {
auto context = std::get<molasses::generate_context>(compile_stack.top());
compile_stack.pop();
molasses::interpreter_stack stack;
while(true) {
std::cout << "%> ";
std::string contents;
std::getline(std::cin, contents);
molasses::interpret(stack, context, contents);
}
} else
throw build_system_error("merge-all expects at least 1 lexed outputs\n");
} else if(elem == "assemble") {
if(not compile_stack.empty() and std::holds_alternative<std::string>(compile_stack.top())) {
auto filename = std::get<std::string>(compile_stack.top());

+ 30
- 0
src/molasses/interpreter.cpp Zobrazit soubor

@ -0,0 +1,30 @@
#include <span>
#include "molasses/parser_primitives.h"
namespace molasses {
std::tuple<interpreter_stack, generate_context, std::optional<std::shared_ptr<std::runtime_error>>> interpret(interpreter_stack stack, generate_context context, std::string text){
auto copy_stack = stack;
auto copy_ctx = context;
try {
lexed_output new_lex = lex("%interpreter%", text);
context.lexer = concatenate(context.lexer, new_lex);
auto count_to_execute = new_lex.symbols.size();
auto executable_span = std::span(
context.lexer.symbols.begin()+((intptr_t)context.lexer.symbols.size() - (intptr_t)count_to_execute),
context.lexer.symbols.end()
);
for(const auto& l : executable_span) {
auto token = context.lexer.dictionary[l];
if(auto value_i32 = try_parse_int32(token); value_i32) stack.push(value_i32.value());
else if(auto value_i64 = try_parse_int64(token); value_i64) stack.push(value_i64.value());
else context.parser.lookup_operation(token)->execute(context, stack);
}
} catch (std::runtime_error& err) {
return {copy_stack, copy_ctx, std::make_shared<std::runtime_error>(err)};
}
return {stack, context, std::nullopt};
}
}

Načítá se…
Zrušit
Uložit