@ -1,57 +1,57 @@ | |||
<component name="ProjectCodeStyleConfiguration"> | |||
<code_scheme name="Project" version="173"> | |||
<option name="LINE_SEPARATOR" value=" " /> | |||
<Objective-C> | |||
<option name="CLASS_CONSTRUCTOR_INIT_LIST_COMMA_ON_NEXT_LINE" value="true" /> | |||
<option name="SUPERCLASS_LIST_COMMA_ON_NEXT_LINE" value="true" /> | |||
</Objective-C> | |||
<Objective-C-extensions> | |||
<rules> | |||
<rule entity="NAMESPACE" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="MACRO" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="CLASS" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="STRUCT" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="ENUM" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="ENUMERATOR" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="TYPEDEF" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="UNION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="CLASS_MEMBER_FUNCTION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="STRUCT_MEMBER_FUNCTION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="CLASS_MEMBER_FIELD" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="STRUCT_MEMBER_FIELD" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="GLOBAL_FUNCTION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="GLOBAL_VARIABLE" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="PARAMETER" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="LOCAL_VARIABLE" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
</rules> | |||
</Objective-C-extensions> | |||
<Objective-C-extensions> | |||
<rules> | |||
<rule entity="NAMESPACE" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="MACRO" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="CLASS" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="STRUCT" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="ENUM" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="ENUMERATOR" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="TYPEDEF" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="UNION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="CLASS_MEMBER_FUNCTION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="STRUCT_MEMBER_FUNCTION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="CLASS_MEMBER_FIELD" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="STRUCT_MEMBER_FIELD" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="GLOBAL_FUNCTION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="GLOBAL_VARIABLE" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="PARAMETER" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="LOCAL_VARIABLE" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
</rules> | |||
</Objective-C-extensions> | |||
<clangFormatSettings> | |||
<option name="ENABLED" value="true" /> | |||
</clangFormatSettings> | |||
<codeStyleSettings language="ObjectiveC"> | |||
<indentOptions> | |||
<option name="CONTINUATION_INDENT_SIZE" value="4" /> | |||
</indentOptions> | |||
</codeStyleSettings> | |||
</code_scheme> | |||
<component name="ProjectCodeStyleConfiguration"> | |||
<code_scheme name="Project" version="173"> | |||
<option name="LINE_SEPARATOR" value=" " /> | |||
<Objective-C> | |||
<option name="CLASS_CONSTRUCTOR_INIT_LIST_COMMA_ON_NEXT_LINE" value="true" /> | |||
<option name="SUPERCLASS_LIST_COMMA_ON_NEXT_LINE" value="true" /> | |||
</Objective-C> | |||
<Objective-C-extensions> | |||
<rules> | |||
<rule entity="NAMESPACE" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="MACRO" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="CLASS" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="STRUCT" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="ENUM" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="ENUMERATOR" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="TYPEDEF" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="UNION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="CLASS_MEMBER_FUNCTION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="STRUCT_MEMBER_FUNCTION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="CLASS_MEMBER_FIELD" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="STRUCT_MEMBER_FIELD" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="GLOBAL_FUNCTION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="GLOBAL_VARIABLE" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="PARAMETER" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="LOCAL_VARIABLE" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
</rules> | |||
</Objective-C-extensions> | |||
<Objective-C-extensions> | |||
<rules> | |||
<rule entity="NAMESPACE" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="MACRO" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="CLASS" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="STRUCT" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="ENUM" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="ENUMERATOR" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="TYPEDEF" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="UNION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="CLASS_MEMBER_FUNCTION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="STRUCT_MEMBER_FUNCTION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="CLASS_MEMBER_FIELD" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="STRUCT_MEMBER_FIELD" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="GLOBAL_FUNCTION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="GLOBAL_VARIABLE" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="PARAMETER" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
<rule entity="LOCAL_VARIABLE" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" /> | |||
</rules> | |||
</Objective-C-extensions> | |||
<clangFormatSettings> | |||
<option name="ENABLED" value="true" /> | |||
</clangFormatSettings> | |||
<codeStyleSettings language="ObjectiveC"> | |||
<indentOptions> | |||
<option name="CONTINUATION_INDENT_SIZE" value="4" /> | |||
</indentOptions> | |||
</codeStyleSettings> | |||
</code_scheme> | |||
</component> |
@ -1,5 +1,5 @@ | |||
<component name="ProjectCodeStyleConfiguration"> | |||
<state> | |||
<option name="USE_PER_PROJECT_SETTINGS" value="true" /> | |||
</state> | |||
<component name="ProjectCodeStyleConfiguration"> | |||
<state> | |||
<option name="USE_PER_PROJECT_SETTINGS" value="true" /> | |||
</state> | |||
</component> |
@ -1,4 +1,4 @@ | |||
<?xml version="1.0" encoding="UTF-8"?> | |||
<project version="4"> | |||
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" /> | |||
<?xml version="1.0" encoding="UTF-8"?> | |||
<project version="4"> | |||
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" /> | |||
</project> |
@ -1,8 +1,8 @@ | |||
<?xml version="1.0" encoding="UTF-8"?> | |||
<project version="4"> | |||
<component name="ProjectModuleManager"> | |||
<modules> | |||
<module fileurl="file://$PROJECT_DIR$/.idea/sugar.iml" filepath="$PROJECT_DIR$/.idea/sugar.iml" /> | |||
</modules> | |||
</component> | |||
<?xml version="1.0" encoding="UTF-8"?> | |||
<project version="4"> | |||
<component name="ProjectModuleManager"> | |||
<modules> | |||
<module fileurl="file://$PROJECT_DIR$/.idea/sugar.iml" filepath="$PROJECT_DIR$/.idea/sugar.iml" /> | |||
</modules> | |||
</component> | |||
</project> |
@ -1,2 +1,2 @@ | |||
<?xml version="1.0" encoding="UTF-8"?> | |||
<?xml version="1.0" encoding="UTF-8"?> | |||
<module classpath="CMake" type="CPP_MODULE" version="4" /> |
@ -1,6 +1,6 @@ | |||
<?xml version="1.0" encoding="UTF-8"?> | |||
<project version="4"> | |||
<component name="VcsDirectoryMappings"> | |||
<mapping directory="$PROJECT_DIR$" vcs="Git" /> | |||
</component> | |||
<?xml version="1.0" encoding="UTF-8"?> | |||
<project version="4"> | |||
<component name="VcsDirectoryMappings"> | |||
<mapping directory="$PROJECT_DIR$" vcs="Git" /> | |||
</component> | |||
</project> |
@ -0,0 +1,85 @@ | |||
#pragma once | |||
#include "lexer.h" | |||
#include <sstream> | |||
#include <stdexcept> | |||
namespace molasses { | |||
namespace details { | |||
template<typename... RestT> | |||
std::string concatenate_builder_impl(std::stringstream& stream) | |||
requires(sizeof...(RestT) == 0) | |||
{ | |||
return stream.str(); | |||
} | |||
template<typename FirstT, typename... RestT> | |||
std::string concatenate_builder_impl(std::stringstream& stream, FirstT first, RestT... rest) { | |||
stream << first; | |||
return concatenate_builder_impl(stream, rest...); | |||
} | |||
template<typename... RestT> | |||
std::string concatenate_builder(RestT... rest) { | |||
std::stringstream stream{}; | |||
return concatenate_builder_impl(stream, rest...); | |||
} | |||
} | |||
struct build_system_error : std::runtime_error { | |||
explicit build_system_error(const std::string& message) | |||
: std::runtime_error(message) {} | |||
}; | |||
struct interpreter_error : std::runtime_error { | |||
explicit interpreter_error(const std::string& message) | |||
: std::runtime_error(message) {} | |||
}; | |||
struct parser_error : public std::runtime_error { | |||
explicit parser_error(const std::string& str) | |||
: std::runtime_error(str) {} | |||
}; | |||
struct type_input_error : public parser_error { | |||
type_input_error() | |||
: parser_error("Bad type provided") {} | |||
// TODO: Better error message | |||
}; | |||
struct value_missing_error : public parser_error { | |||
value_missing_error() | |||
: parser_error("Expected value, none provided") {} | |||
// TODO: Better error message | |||
}; | |||
struct procedure_stack_error : public parser_error { | |||
procedure_stack_error() | |||
: parser_error("Expected the stack to look like the return stack upon completion\n") {} | |||
// TODO: Better error message | |||
}; | |||
struct unexpected_token_error : public parser_error { | |||
unexpected_token_error(const symbol& sym, const std::string& found, const std::string& expected) | |||
: parser_error(details::concatenate_builder( | |||
"Unexpected token encountered\n", "\tAt ", sym.file_name, ":", sym.line, ":", sym.column, "\n", | |||
"\tExpected ", expected, "\n", "\tFound ", found, "\n" | |||
)) {} | |||
}; | |||
struct expecting_token_error : public parser_error { | |||
expecting_token_error(const std::string& expected, const std::string& context) | |||
: parser_error(details::concatenate_builder( | |||
"An expected token has not been encountered before the end of the input\n", "\tExpected ", expected, | |||
"\n", "\t", context, "\n" | |||
)) {} | |||
// TODO: Better error message | |||
}; | |||
struct unknown_token_error : public parser_error { | |||
explicit unknown_token_error(const symbol& sym) | |||
: parser_error(details::concatenate_builder( | |||
"An unknown token has been encountered\n", "\tAt ", sym.file_name, ":", sym.line, ":", sym.column, | |||
"\n" | |||
)) {} | |||
// TODO: Better error message | |||
}; | |||
struct type_expected_with_modifier_error : public parser_error { | |||
type_expected_with_modifier_error() | |||
: parser_error("A type is expected before a modifier") {} | |||
// TODO: Better error message | |||
}; | |||
} |
@ -0,0 +1,126 @@ | |||
#pragma once | |||
#include "lexer.h" | |||
#include <functional> | |||
#include <memory> | |||
#include <stack> | |||
#include <string> | |||
#include <variant> | |||
#include <vector> | |||
namespace molasses { | |||
struct type { | |||
[[nodiscard]] virtual std::string name() const = 0; | |||
[[nodiscard]] virtual size_t byte_size() const = 0; | |||
}; | |||
inline auto operator<=>(const type& lhs, const type& rhs) { | |||
return lhs.name() <=> rhs.name(); | |||
} | |||
struct primitive_type : public type { | |||
std::string _name; | |||
size_t _byte_size; | |||
primitive_type(std::string name, size_t byte_size) | |||
: _name(std::forward<std::string>(name)) | |||
, _byte_size(byte_size) | |||
{} | |||
[[nodiscard]] std::string name() const final { | |||
return _name; | |||
} | |||
[[nodiscard]] size_t byte_size() const final { | |||
return _byte_size; | |||
}; | |||
}; | |||
struct parser_context; | |||
struct ptr_type{ | |||
void* ptr; | |||
std::shared_ptr<type> pointed; | |||
}; | |||
using stack_element = std::variant<int32_t, int64_t, ptr_type>; | |||
using interpreter_stack = std::stack<stack_element>; | |||
struct generate_context; | |||
struct operation { | |||
[[nodiscard]] virtual std::string name() const = 0; | |||
[[nodiscard]] virtual std::vector<std::string> argument_types() const = 0; | |||
[[nodiscard]] virtual std::vector<std::string> return_types() const = 0; | |||
[[nodiscard]] virtual std::vector<std::string> generate(const parser_context&, const lexed_output& lexer_data) const = 0; | |||
[[nodiscard]] virtual std::vector<std::string> emit(const parser_context&) const = 0; | |||
virtual void execute(const generate_context&, interpreter_stack&) const = 0; | |||
}; | |||
struct primitive_operation : public operation { | |||
std::string _name; | |||
std::vector<std::string> _args; | |||
std::vector<std::string> _rets; | |||
std::vector<std::string> _instructions; | |||
std::function<void(const generate_context&, interpreter_stack&)> _executable; | |||
primitive_operation(std::string name, std::vector<std::string> args, std::vector<std::string> rets, std::vector<std::string> body, std::function<void(const generate_context&,interpreter_stack&)> executable) | |||
: _name(std::forward<std::string>(name)) | |||
, _args(std::forward<std::vector<std::string>>(args)) | |||
, _rets(std::forward<std::vector<std::string>>(rets)) | |||
, _instructions(std::forward<std::vector<std::string>>(body)) | |||
, _executable(std::move(executable)) | |||
{} | |||
[[nodiscard]] std::string name() const final { | |||
return _name; | |||
} | |||
[[nodiscard]] std::vector<std::string> argument_types() const final { | |||
return _args; | |||
} | |||
[[nodiscard]] std::vector<std::string> return_types() const final { | |||
return _rets; | |||
} | |||
[[nodiscard]] std::vector<std::string> generate(const parser_context&, const lexed_output& lexer_data) const final { | |||
return {}; | |||
} | |||
[[nodiscard]] std::vector<std::string> emit(const parser_context&) const final { | |||
return _instructions; | |||
} | |||
void execute(const generate_context& ctx, interpreter_stack& stack) const override { | |||
_executable(ctx, stack); | |||
}; | |||
}; | |||
struct procedure_operation : public operation { | |||
std::string _name; | |||
std::vector<std::string> _args; | |||
std::vector<std::string> _rets; | |||
std::vector<symbol> _body; | |||
procedure_operation(std::string name, std::vector<std::string> args, std::vector<std::string> rets, std::vector<symbol> body) | |||
: _name(std::forward<std::string>(name)) | |||
, _args(std::forward<std::vector<std::string>>(args)) | |||
, _rets(std::forward<std::vector<std::string>>(rets)) | |||
, _body(std::forward<std::vector<symbol>>(body)) | |||
{} | |||
[[nodiscard]] std::string name() const final { | |||
return _name; | |||
} | |||
[[nodiscard]] std::vector<std::string> argument_types() const final { | |||
return _args; | |||
} | |||
[[nodiscard]] std::vector<std::string> return_types() const final { | |||
return _rets; | |||
} | |||
[[nodiscard]] std::vector<std::string> generate(const parser_context&, const lexed_output& lexer_data) const final; | |||
[[nodiscard]] std::vector<std::string> emit(const parser_context&) const final; | |||
void execute(const generate_context&, interpreter_stack&) const final; | |||
}; | |||
inline auto operator<=>(const operation& lhs, const operation& rhs) { | |||
return lhs.name() <=> rhs.name(); | |||
} | |||
} |
@ -0,0 +1,16 @@ | |||
#include "molasses/parser_primitives.h" | |||
namespace molasses { | |||
parser_context register_integers(parser_context ctx) { | |||
ctx.types.push_back(std::make_shared<primitive_type>("i8", 1)); | |||
ctx.types.push_back(std::make_shared<primitive_type>("i16", 2)); | |||
ctx.types.push_back(std::make_shared<primitive_type>("i32", 4)); | |||
ctx.types.push_back(std::make_shared<primitive_type>("i64", 8)); | |||
ctx.types.push_back(std::make_shared<primitive_type>("u8", 1)); | |||
ctx.types.push_back(std::make_shared<primitive_type>("u16", 2)); | |||
ctx.types.push_back(std::make_shared<primitive_type>("u32", 4)); | |||
ctx.types.push_back(std::make_shared<primitive_type>("u64", 8)); | |||
return ctx; | |||
} | |||
} |