Ludovic 'Archivist' Lagouardette 1 年之前
父節點
當前提交
947092c339
共有 6 個檔案被更改,包括 72 行新增8 行删除
  1. +10
    -5
      CMakeLists.txt
  2. +8
    -2
      include/molasses/parser_primitives.h
  3. +13
    -0
      src/main.cpp
  4. +30
    -0
      src/molasses/interpreter.cpp
  5. +10
    -0
      src/molasses/parser_primitives.cpp
  6. +1
    -1
      tests/004.exp

+ 10
- 5
CMakeLists.txt 查看文件

@ -7,13 +7,18 @@ enable_testing()
include_directories(include)
add_executable(sugar
src/main.cpp
src/molasses/lexr">er.cpp
src/molasses/generator_primitives_x86_64_linux.cpp
ispan>ncr">luclass="err">de/generalized_parsing.h
ispan>ncr">luclass="err">de/molasses/errors.h
ispan>ncr">luclass="err">de/molasses/generator_primitives.h
include/molasses/lexer.h
src/molasses/parser_primitives.cpp
include/molasses/parser_primitives.h
include/molasses/generator_primitives.h include/molasses/errors.h include/molasses/parser_types.h src/molasses/generator_primitives.cpp include/generalized_parsing.h)
include/molasses/parser_types.h
src/main.cpp
src/molasses/generator_primitives.cpp
src/molasses/generator_primitives_x86_64_linux.cpp
src/molasses/interpreter.cpp
src/molasses/lexer.cpp
src/molasses/parser_primitives.cpp)
function(add_expect_test [testname filename])
add_test(

+ 8
- 2
include/molasses/parser_primitives.h 查看文件

@ -31,7 +31,10 @@ constexpr size_t architecture_ptr_size = 4;
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;
std::vector<std::shared_ptr<operation>> operations;
@ -47,8 +50,11 @@ namespace molasses {
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 );
std::optional<int32_t> try_parse_int32(const std::string& str);
generate_context parse(parser_context, const lexed_output&);
std::vector<std::string> generate(const generate_context&);

+ 13
- 0
src/main.cpp 查看文件

@ -106,6 +106,19 @@ int main(int argc, char** argv) {
compile_stack.emplace(lexer_1);
} else
throw molasses::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 molasses::build_system_error("interpreter expects 1 generated context\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 查看文件

@ -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};
}
}

+ 10
- 0
src/molasses/parser_primitives.cpp 查看文件

@ -358,7 +358,17 @@ namespace molasses {
}
void procedure_operation::execute(const generate_context& ctx, interpreter_stack& stack) const {
for(const auto& elem : _body) {
if(auto n = ctx.parser.lookup_operation(ctx.lexer.dictionary.at(elem)); n)
n->execute(ctx, stack);
else if(auto maybe_i64 = try_parse_int64(ctx.lexer.dictionary.at(elem)); maybe_i64)
stack.emplace(int64_t(maybe_i64.value()));
else if(auto maybe_i32 = try_parse_int32(ctx.lexer.dictionary.at(elem)); maybe_i32)
stack.emplace(int32_t(maybe_i32.value()));
else
std::cerr << "OPERATION NOT FOUND: " << elem <<"\n";
}
}
}

+ 1
- 1
tests/004.exp 查看文件

@ -86,7 +86,7 @@ expect eof
spawn -noecho $SUGAR_EXECUTABLE tests/004/incomplete-07.mol lex parse
expect {
eof { abort "should display failure line" }
{incomplete-07.mol:2}
{incomplete-07.mol:1}
}
expect {
eof { abort "should display failure in incomplete-07" }

Loading…
取消
儲存