mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-06 06:58:45 +00:00
type_check_visitor first iteration, value, execution_visitor started
This commit is contained in:
parent
173d50672a
commit
890bd90eba
22 changed files with 481 additions and 452 deletions
|
|
@ -17,6 +17,10 @@ include_directories(include
|
|||
# target_link_libraries(tests PRIVATE Catch2::Catch2WithMain)
|
||||
|
||||
add_executable(lang_interpreter src/main.cpp
|
||||
src/types.cpp
|
||||
src/global_info.cpp
|
||||
include/type_info_contexts.hpp
|
||||
include/definitions.hpp
|
||||
src/visitor.cpp
|
||||
src/build_visitor.cpp
|
||||
src/print_visitor.cpp
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -1,6 +1,5 @@
|
|||
#pragma once
|
||||
|
||||
#include "interpreter_tree.hpp"
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
|
@ -9,6 +8,7 @@
|
|||
#include <memory>
|
||||
|
||||
// for clangd
|
||||
#include "interpreter_tree.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
namespace interpreter {
|
||||
|
|
|
|||
134
include/execute_visitor.hpp
Normal file
134
include/execute_visitor.hpp
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
#pragma once
|
||||
|
||||
#include <ostream>
|
||||
|
||||
// for clangd
|
||||
#include "contexts.hpp"
|
||||
#include "global_info.hpp"
|
||||
#include "type_info_contexts.hpp"
|
||||
#include "visitor.hpp"
|
||||
|
||||
namespace interpreter {
|
||||
|
||||
class ExecuteVisitor : public Visitor {
|
||||
public:
|
||||
explicit ExecuteVisitor(info::GlobalInfo& global_info,
|
||||
info::TypeInfoContextManager& type_info_context_manager,
|
||||
info::ContextManager& context_manager)
|
||||
: namespace_visitor_(global_info.CreateVisitor()),
|
||||
type_info_context_manager_(type_info_context_manager),
|
||||
context_manager_(context_manager) {}
|
||||
|
||||
private:
|
||||
// Sources -----------------
|
||||
|
||||
void Visit(SourceFile* node) override;
|
||||
|
||||
// Namespaces, partitions -----------------
|
||||
|
||||
void Visit(PartitionSources* node) override;
|
||||
void Visit(Partition* node) override;
|
||||
void Visit(NamespaceSources* node) override;
|
||||
void Visit(Namespace* node) override;
|
||||
|
||||
// Definitions -----------------
|
||||
|
||||
void Visit(ImportStatement* node) override;
|
||||
void Visit(AliasDefinitionStatement* node) override;
|
||||
void Visit(VariableDefinitionStatement* node) override;
|
||||
void Visit(FunctionDeclaration* node) override;
|
||||
void Visit(FunctionDefinitionStatement* node) override;
|
||||
void Visit(TypeDefinitionStatement* node) override;
|
||||
void Visit(AbstractTypeDefinitionStatement* node) override;
|
||||
void Visit(TypeclassDefinitionStatement* node) override;
|
||||
|
||||
// Definition parts
|
||||
|
||||
void Visit(FunctionDefinition* node) override;
|
||||
void Visit(TypeDefinition* node) override;
|
||||
void Visit(AnyAnnotatedType* node) override;
|
||||
|
||||
// Flow control -----------------
|
||||
|
||||
void Visit(TypeConstructorPatternParameter* node) override;
|
||||
void Visit(TypeConstructorPattern* node) override;
|
||||
void Visit(MatchCase* node) override;
|
||||
void Visit(Match* node) override;
|
||||
void Visit(Condition* node) override;
|
||||
void Visit(DoWhileLoop* node) override;
|
||||
void Visit(WhileLoop* node) override;
|
||||
void Visit(ForLoop* node) override;
|
||||
void Visit(LoopLoop* node) override;
|
||||
|
||||
// Statements, expressions, blocks, etc. -----------------
|
||||
|
||||
void Visit(Block* node) override;
|
||||
|
||||
void Visit(ScopedStatement* node) override;
|
||||
|
||||
// Operators
|
||||
|
||||
void Visit(BinaryOperatorExpression* node) override;
|
||||
void Visit(UnaryOperatorExpression* node) override;
|
||||
void Visit(ReferenceExpression* node) override;
|
||||
void Visit(AccessExpression* node) override;
|
||||
|
||||
// Simple Expressions
|
||||
|
||||
void Visit(FunctionCallExpression* node) override;
|
||||
|
||||
void Visit(TupleExpression* node) override;
|
||||
void Visit(VariantExpression* node) override;
|
||||
void Visit(ReturnExpression* node) override;
|
||||
void Visit(TypeConstructorParameter* node) override;
|
||||
void Visit(TypeConstructor* node) override;
|
||||
void Visit(LambdaFunction* node) override;
|
||||
void Visit(ArrayExpression* node) override;
|
||||
|
||||
void Visit(LoopControlExpression& node) override; // enum
|
||||
|
||||
// Name
|
||||
|
||||
void Visit(NameExpression* node) override;
|
||||
void Visit(TupleName* node) override;
|
||||
void Visit(VariantName* node) override;
|
||||
void Visit(AnnotatedName* node) override;
|
||||
|
||||
// Type, typeclass, etc. -----------------
|
||||
|
||||
// Type
|
||||
|
||||
void Visit(FunctionType* node) override;
|
||||
void Visit(TupleType* node) override;
|
||||
void Visit(VariantType* node) override;
|
||||
void Visit(TypeExpression* node) override;
|
||||
|
||||
void Visit(ExtendedScopedAnyType* node) override;
|
||||
|
||||
// Typeclass
|
||||
|
||||
void Visit(ParametrizedTypeclass* node) override;
|
||||
|
||||
// Typeclass & Type
|
||||
|
||||
void Visit(ParametrizedType* node) override;
|
||||
|
||||
// Identifiers, constants, etc. -----------------
|
||||
|
||||
void Visit(ExtendedName* node) override;
|
||||
|
||||
void Visit(std::string* node) override; // std::string
|
||||
|
||||
void Visit(FloatNumberLiteral* node) override;
|
||||
void Visit(NumberLiteral* node) override;
|
||||
void Visit(StringLiteral* node) override;
|
||||
void Visit(CharLiteral* node) override;
|
||||
void Visit(UnitLiteral* node) override;
|
||||
|
||||
private:
|
||||
info::GlobalInfo::NamespaceVisitor namespace_visitor_;
|
||||
info::TypeInfoContextManager& type_info_context_manager_;
|
||||
info::ContextManager& context_manager_;
|
||||
};
|
||||
|
||||
} // namespace interpreter
|
||||
|
|
@ -107,10 +107,10 @@ public:
|
|||
definition::Namespace* current_namespace,
|
||||
const std::vector<std::string>& path);
|
||||
private:
|
||||
GlobalInfo& global_info_;
|
||||
|
||||
std::vector<definition::Namespace*> namespace_stack_;
|
||||
std::vector<std::string> current_path_;
|
||||
|
||||
GlobalInfo& global_info_;
|
||||
};
|
||||
|
||||
NamespaceVisitor CreateVisitor() {
|
||||
|
|
@ -123,11 +123,11 @@ public:
|
|||
}
|
||||
|
||||
template<typename T>
|
||||
const std::optional<T>& GetTypeInfo(utils::IdType id) {
|
||||
std::optional<T*> GetTypeInfo(utils::IdType id) {
|
||||
if (!std::holds_alternative<T>(types_.at(id).type)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return std::get<T>(types_[id].type);
|
||||
return &std::get<T>(types_[id].type);
|
||||
}
|
||||
|
||||
// remember about vector realloc
|
||||
|
|
@ -146,6 +146,7 @@ public:
|
|||
}
|
||||
|
||||
private:
|
||||
|
||||
std::vector<definition::Function> functions_;
|
||||
std::vector<definition::Type> types_;
|
||||
std::vector<definition::AbstractType> abstract_types_;
|
||||
|
|
|
|||
|
|
@ -40,8 +40,9 @@ const std::string AnnotatedType = "annotated_type";
|
|||
|
||||
// Flow control -----------------
|
||||
|
||||
const std::string TypeConstructorPatternParameter = "type_constructor_pattern_parameter";
|
||||
const std::string TypeConstructorPattern = "type_constructor_pattern";
|
||||
const std::string PatternToken = "pattern_token";
|
||||
const std::string ScopedPattern = "scoped_pattern";
|
||||
const std::string Pattern = "pattern";
|
||||
const std::string MatchCase = "match_case";
|
||||
const std::string Match = "match";
|
||||
|
|
@ -77,6 +78,7 @@ const std::string FunctionCallExpression = "function_call_expression";
|
|||
const std::string TupleExpression = "tuple_expression";
|
||||
const std::string VariantExpression = "variant_expression";
|
||||
const std::string ReturnExpression = "return_expression";
|
||||
const std::string TypeConstructorParameter = "type_constructor_parameter";
|
||||
const std::string TypeConstructor = "type_constructor";
|
||||
const std::string LambdaFunction = "lambda_function";
|
||||
const std::string ArrayExpression = "array_expression";
|
||||
|
|
@ -88,7 +90,6 @@ const std::string NameExpression = "name_expression";
|
|||
const std::string TupleName = "tuple_name";
|
||||
const std::string VariantName = "variant_name";
|
||||
const std::string AnnotatedName = "annotated_name";
|
||||
const std::string NameSubExpression = "name_subexpression";
|
||||
const std::string AnyName = "any_name";
|
||||
const std::string ScopedAnyName = "scoped_any_name";
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,8 @@ namespace interpreter {
|
|||
|
||||
class TypeCheckVisitor : public Visitor {
|
||||
public:
|
||||
explicit TypeCheckVisitor(info::GlobalInfo& global_info, info::TypeInfoContextManager& context_manager)
|
||||
explicit TypeCheckVisitor(info::GlobalInfo& global_info,
|
||||
info::TypeInfoContextManager& context_manager)
|
||||
: namespace_visitor_(global_info.CreateVisitor()), context_manager_(context_manager) {}
|
||||
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -14,13 +14,17 @@ namespace info {
|
|||
// TODO: remember about type pointers
|
||||
class TypeInfoContextManager {
|
||||
public:
|
||||
TypeInfoContextManager() {
|
||||
contexts_.emplace_back();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
utils::IdType AddType(T&& type, utils::ValueType value_type) {
|
||||
return type_manager_.AddType(std::forward(type), value_type);
|
||||
utils::IdType AddType(const T& type, utils::ValueType value_type) {
|
||||
return type_manager_.AddType(type, value_type);
|
||||
}
|
||||
|
||||
utils::IdType AddAnyType(type::Type&& type, utils::ValueType value_type) {
|
||||
return type_manager_.AddType(std::move(type), value_type);
|
||||
return type_manager_.AddAnyType(std::move(type), value_type);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
|
@ -45,7 +49,8 @@ public:
|
|||
}
|
||||
|
||||
utils::IdType ToModifiedType(utils::IdType type_id, utils::ValueType new_value_type) {
|
||||
return AddAnyType(type::Type(*GetAnyType(type_id)), new_value_type);
|
||||
type::Type type = *GetAnyType(type_id);
|
||||
return AddAnyType(std::move(type), new_value_type);
|
||||
}
|
||||
|
||||
type::TypeManager* GetTypeManager() {
|
||||
|
|
@ -53,7 +58,7 @@ public:
|
|||
}
|
||||
|
||||
void EnterContext() {
|
||||
contexts_.emplace_back(false);
|
||||
contexts_.emplace_back();
|
||||
}
|
||||
|
||||
void ExitContext() {
|
||||
|
|
@ -74,11 +79,10 @@ public:
|
|||
}
|
||||
|
||||
bool DefineLocalAbstractType(const std::string& name, utils::IdType type_id) {
|
||||
if (GetLocalAbstractType(name)) {
|
||||
if (GetLocalAbstractType(name).has_value()) {
|
||||
return false;
|
||||
}
|
||||
contexts_.back().DefineLocalAbstractType(name, type_id);
|
||||
return true;
|
||||
return contexts_.back().DefineLocalAbstractType(name, type_id);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -124,7 +128,7 @@ private:
|
|||
Context() = default;
|
||||
|
||||
bool DefineVariable(const std::string& name, utils::IdType type_id) {
|
||||
if (name == "_") { // placeholder // TODO: check in all places
|
||||
if (name == "_") { // placeholder // TODO: ??
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -225,7 +225,7 @@ public:
|
|||
Type() = default;
|
||||
|
||||
template<typename T>
|
||||
explicit Type(T&& type) : type_(std::forward(type)) {}
|
||||
explicit Type(const T& type) : type_(type) {}
|
||||
|
||||
std::optional<utils::IdType> InContext(const std::unordered_map<std::string, utils::IdType>& context);
|
||||
bool Same(const Type& type) const;
|
||||
|
|
@ -236,6 +236,17 @@ public:
|
|||
|
||||
std::string GetTypeName() const;
|
||||
|
||||
std::variant<AbstractType,
|
||||
DefinedType,
|
||||
InternalType,
|
||||
TupleType,
|
||||
VariantType,
|
||||
ReferenceToType,
|
||||
FunctionType,
|
||||
ArrayType,
|
||||
OptionalType>& GetType() {
|
||||
return type_;
|
||||
}
|
||||
private:
|
||||
std::variant<AbstractType,
|
||||
DefinedType,
|
||||
|
|
@ -251,22 +262,42 @@ private:
|
|||
class TypeManager {
|
||||
public:
|
||||
template<typename T>
|
||||
utils::IdType AddType(T&& type, utils::ValueType value_type);
|
||||
utils::IdType AddType(const T& type, utils::ValueType value_type) {
|
||||
types_.push_back(std::pair<Type, utils::ValueType> {type, value_type});
|
||||
return types_.size() - 1;
|
||||
}
|
||||
|
||||
utils::IdType AddAnyType(Type&& type, utils::ValueType value_type);
|
||||
utils::IdType AddAnyType(Type&& type, utils::ValueType value_type) {
|
||||
types_.push_back(std::pair<Type, utils::ValueType> {std::move(type), value_type});
|
||||
return types_.size() - 1;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::optional<T*> GetType(utils::IdType type_id);
|
||||
std::optional<T*> GetType(utils::IdType type_id) {
|
||||
if (!std::holds_alternative<T>(types_.at(type_id).first.GetType())) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return &std::get<T>(types_.at(type_id).first.GetType());
|
||||
}
|
||||
|
||||
Type* GetAnyType(utils::IdType type_id);
|
||||
Type* GetAnyType(utils::IdType type_id) {
|
||||
return &types_.at(type_id).first;
|
||||
}
|
||||
|
||||
utils::ValueType GetValueType(utils::IdType type_id);
|
||||
utils::ValueType GetValueType(utils::IdType type_id) {
|
||||
return types_.at(type_id).second;
|
||||
}
|
||||
|
||||
bool AddTypeRequirement(utils::IdType type, utils::IdType requrement);
|
||||
bool EqualTypes(utils::IdType first_type, utils::IdType second_type);
|
||||
bool EqualTypes(utils::IdType first_type, utils::IdType second_type) {
|
||||
return GetAnyType(first_type)->Same(*GetAnyType(second_type));
|
||||
}
|
||||
|
||||
bool AddTypeRequirement(utils::IdType type, utils::IdType requrement) {
|
||||
return *GetAnyType(requrement) < *GetAnyType(type);
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::pair<info::type::Type, utils::ValueType>> types_;
|
||||
std::vector<std::pair<Type, utils::ValueType>> types_;
|
||||
};
|
||||
|
||||
} // namespace info::type
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ inline ValueType IsConstModifierToValueType(IsConstModifier modifier) {
|
|||
return ValueType::Var;
|
||||
}
|
||||
// unreachable
|
||||
exit(1);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
|
@ -107,32 +108,32 @@ private:
|
|||
std::vector<size_t> ranks_;
|
||||
};
|
||||
|
||||
static void BackVisitDfs(size_t id,
|
||||
std::vector<size_t>& verticles,
|
||||
std::vector<size_t>& marks,
|
||||
const std::vector<std::vector<size_t>>& edges,
|
||||
size_t mark) {
|
||||
if (marks[id] != 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
marks[id] = mark;
|
||||
verticles.push_back(id);
|
||||
|
||||
for (size_t i = 0; i < edges[id].size(); ++i) {
|
||||
BackVisitDfs(id, verticles, marks, edges, mark);
|
||||
}
|
||||
}
|
||||
|
||||
static std::vector<size_t> BackTopSort(const std::vector<std::vector<size_t>>& edges_) {
|
||||
std::vector<size_t> sorted_verticles;
|
||||
std::vector<size_t> marks(edges_.size(), 0);
|
||||
|
||||
for (size_t i = 0; i < marks.size(); ++i) {
|
||||
BackVisitDfs(i, sorted_verticles, marks, edges_, 1);
|
||||
}
|
||||
|
||||
return sorted_verticles;
|
||||
}
|
||||
// static void BackVisitDfs(size_t id,
|
||||
// std::vector<size_t>& verticles,
|
||||
// std::vector<size_t>& marks,
|
||||
// const std::vector<std::vector<size_t>>& edges,
|
||||
// size_t mark) {
|
||||
// if (marks[id] != 0) {
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// marks[id] = mark;
|
||||
// verticles.push_back(id);
|
||||
//
|
||||
// for (size_t i = 0; i < edges[id].size(); ++i) {
|
||||
// BackVisitDfs(id, verticles, marks, edges, mark);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// static std::vector<size_t> BackTopSort(const std::vector<std::vector<size_t>>& edges_) {
|
||||
// std::vector<size_t> sorted_verticles;
|
||||
// std::vector<size_t> marks(edges_.size(), 0);
|
||||
//
|
||||
// for (size_t i = 0; i < marks.size(); ++i) {
|
||||
// BackVisitDfs(i, sorted_verticles, marks, edges_, 1);
|
||||
// }
|
||||
//
|
||||
// return sorted_verticles;
|
||||
// }
|
||||
|
||||
} // namespace utils
|
||||
|
|
|
|||
143
include/values.hpp
Normal file
143
include/values.hpp
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
|
||||
// for clangd
|
||||
#include "interpreter_tree.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
namespace info::value {
|
||||
|
||||
struct Unit {};
|
||||
|
||||
|
||||
|
||||
struct InternalValue {
|
||||
public:
|
||||
InternalValue() = default;
|
||||
InternalValue(std::variant<double,
|
||||
long long,
|
||||
std::string,
|
||||
char,
|
||||
bool,
|
||||
Unit>&& value) : value(std::move(value)) {}
|
||||
|
||||
public:
|
||||
std::variant<double,
|
||||
long long,
|
||||
std::string,
|
||||
char,
|
||||
bool,
|
||||
Unit> value;
|
||||
};
|
||||
|
||||
struct TupleValue {
|
||||
public:
|
||||
TupleValue() = default;
|
||||
TupleValue(std::unordered_map<std::string, utils::IdType>&& fields) : fields(fields) {}
|
||||
|
||||
public:
|
||||
std::unordered_map<std::string, utils::IdType> fields;
|
||||
};
|
||||
|
||||
struct VariantValue {
|
||||
public:
|
||||
VariantValue() = default;
|
||||
VariantValue(size_t constructor, TupleValue value)
|
||||
: constructor(constructor), value(value) {}
|
||||
|
||||
public:
|
||||
size_t constructor;
|
||||
TupleValue value;
|
||||
};
|
||||
|
||||
struct ReferenceToValue {
|
||||
public:
|
||||
ReferenceToValue() = default;
|
||||
ReferenceToValue(const std::vector<utils::ReferenceModifier>& references,
|
||||
utils::IdType value)
|
||||
: references(references), value(value) {}
|
||||
|
||||
public:
|
||||
std::vector<utils::ReferenceModifier> references;
|
||||
utils::IdType value;
|
||||
};
|
||||
|
||||
struct FunctionValue {
|
||||
public:
|
||||
FunctionValue() = default;
|
||||
FunctionValue(std::variant<interpreter::tokens::FunctionDeclaration*,
|
||||
interpreter::tokens::LambdaFunction*> function)
|
||||
: function(function) {}
|
||||
|
||||
public:
|
||||
std::variant<interpreter::tokens::FunctionDeclaration*,
|
||||
interpreter::tokens::LambdaFunction*> function;
|
||||
};
|
||||
|
||||
|
||||
struct ArrayValue {
|
||||
public:
|
||||
ArrayValue() = default;
|
||||
ArrayValue(const std::vector<utils::IdType>& elements)
|
||||
: elements(elements) {}
|
||||
|
||||
public:
|
||||
std::vector<utils::IdType> elements;
|
||||
};
|
||||
|
||||
struct OptionalValue {
|
||||
public:
|
||||
OptionalValue() = default;
|
||||
OptionalValue(utils::IdType value) : value(value) {}
|
||||
|
||||
public:
|
||||
std::optional<utils::IdType> value;
|
||||
};
|
||||
|
||||
struct Value { // DefinedValue ??
|
||||
std::variant<InternalValue,
|
||||
TupleValue,
|
||||
VariantValue,
|
||||
ReferenceToValue,
|
||||
FunctionValue, // ??
|
||||
ArrayValue,
|
||||
OptionalValue> value;
|
||||
};
|
||||
|
||||
class ValueManager {
|
||||
public:
|
||||
template<typename T>
|
||||
utils::IdType AddType(const T& value, utils::ValueType value_type) {
|
||||
values_.push_back(std::pair<Value, utils::ValueType> {value, value_type});
|
||||
return values_.size() - 1;
|
||||
}
|
||||
|
||||
utils::IdType AddAnyType(Value&& value, utils::ValueType value_type) {
|
||||
values_.push_back(std::pair<Value, utils::ValueType> {std::move(value), value_type});
|
||||
return values_.size() - 1;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::optional<T*> GetType(utils::IdType value_id) {
|
||||
if (!std::holds_alternative<T>(values_.at(value_id).first.value)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return &std::get<T>(values_.at(value_id).first.value);
|
||||
}
|
||||
|
||||
Value* GetAnyType(utils::IdType value_id) {
|
||||
return &values_.at(value_id).first;
|
||||
}
|
||||
|
||||
utils::ValueType GetValueType(utils::IdType value_id) {
|
||||
return values_.at(value_id).second;
|
||||
}
|
||||
private:
|
||||
std::vector<std::pair<Value, utils::ValueType>> values_;
|
||||
};
|
||||
|
||||
} // namespace info::value
|
||||
|
|
@ -487,6 +487,7 @@ void BuildVisitor::Visit(TypeConstructorPattern* node) {
|
|||
auto parse_node = current_node_;
|
||||
|
||||
current_node_ = parse_node.ChildByFieldName("constructor");
|
||||
node->constructor = std::make_unique<TypeExpression>();
|
||||
Visit(node->constructor.get());
|
||||
|
||||
size_t child_count = parse_node.NamedChildCount();
|
||||
|
|
@ -988,8 +989,6 @@ void BuildVisitor::Visit(FunctionCallExpression* node) {
|
|||
if (child_count > excluded_child_count) {
|
||||
bool parameters_ended = false;
|
||||
|
||||
node->arguments.resize(child_count - excluded_child_count);
|
||||
|
||||
for (size_t i = 0; i + excluded_child_count < child_count; ++i) {
|
||||
current_node_ = parse_node.NthNamedChild(i + excluded_child_count);
|
||||
|
||||
|
|
@ -1499,12 +1498,7 @@ void BuildVisitor::Visit(ParametrizedType* node) {
|
|||
void BuildVisitor::Visit(ExtendedName* node) {
|
||||
SetPosition(node->base, current_node_);
|
||||
|
||||
size_t child_count = current_node_.NamedChildCount();
|
||||
if (child_count > 1) {
|
||||
node->name = current_node_.GetValue();
|
||||
} else {
|
||||
node->name = current_node_.NthNamedChild(0).GetValue();
|
||||
}
|
||||
node->name = current_node_.GetValue();
|
||||
}
|
||||
|
||||
// void BuildVisitor::Visit(AnyIdentifier* node) { // std::string
|
||||
|
|
|
|||
0
src/execute_visitor.cpp
Normal file
0
src/execute_visitor.cpp
Normal file
|
|
@ -47,7 +47,8 @@ void GlobalInfo::NamespaceVisitor::EnterNamespace(const std::string& name) {
|
|||
}
|
||||
}
|
||||
|
||||
error_handling::HandleInternalError("Can't find namespace", "GlobalInfo.NamespaceVisitor.EnterNamespace");
|
||||
error_handling::HandleInternalError("Can't find namespace",
|
||||
"GlobalInfo.NamespaceVisitor.EnterNamespace");
|
||||
}
|
||||
|
||||
void GlobalInfo::NamespaceVisitor::ExitNamespace() {
|
||||
|
|
@ -67,8 +68,10 @@ void GlobalInfo::NamespaceVisitor::ToGlobalNamespace() {
|
|||
namespace_stack_.push_back(&global_info_.global_namespace_);
|
||||
}
|
||||
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDeclaration(const std::string& name,
|
||||
definition::FunctionDeclaration&& function_declaration_info) {
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDeclaration(
|
||||
const std::string& name,
|
||||
definition::FunctionDeclaration&& function_declaration_info) {
|
||||
|
||||
size_t id = 0;
|
||||
|
||||
auto function_id_iter = namespace_stack_.back()->functions.find(name);
|
||||
|
|
@ -77,12 +80,13 @@ utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDeclaration(const std::st
|
|||
id = global_info_.functions_.size();
|
||||
namespace_stack_.back()->functions[name] = id;
|
||||
global_info_.functions_.emplace_back();
|
||||
global_info_.functions_.back().argument_count = function_declaration_info.argument_types.size();
|
||||
global_info_.functions_.back().argument_count = function_declaration_info.argument_types.size(); // add return type
|
||||
global_info_.functions_.back().declaration = std::move(function_declaration_info);
|
||||
} else {
|
||||
id = function_id_iter->second;
|
||||
if (global_info_.functions_.back().argument_count != function_declaration_info.argument_types.size()) {
|
||||
error_handling::HandleInternalError("Not same argument count in function definition and declaration", "GlobalInfo");
|
||||
error_handling::HandleInternalError("Not same argument count in function definition and declaration",
|
||||
"GlobalInfo.NamespaceVisitor. AddFunctionDeclaration");
|
||||
}
|
||||
global_info_.functions_[id].declaration = std::move(function_declaration_info);
|
||||
}
|
||||
|
|
@ -100,11 +104,11 @@ utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDefinition(const std::str
|
|||
id = global_info_.functions_.size();
|
||||
namespace_stack_.back()->functions[name] = id;
|
||||
global_info_.functions_.emplace_back();
|
||||
global_info_.functions_.back().argument_count = function_definition_info.argument_names.size();
|
||||
global_info_.functions_.back().argument_count = function_definition_info.argument_names.size() + 1;
|
||||
global_info_.functions_.back().definition = std::move(function_definition_info);
|
||||
} else {
|
||||
id = function_id_iter->second;
|
||||
if (global_info_.functions_.back().argument_count != function_definition_info.argument_names.size()) {
|
||||
if (global_info_.functions_.back().argument_count != function_definition_info.argument_names.size() + 1) {
|
||||
error_handling::HandleInternalError("Not same argument count in function definition and declaration", "GlobalInfo");
|
||||
}
|
||||
global_info_.functions_[id].definition = std::move(function_definition_info);
|
||||
|
|
@ -178,12 +182,15 @@ utils::IdType GlobalInfo::NamespaceVisitor::AddAbstractType(const std::string& a
|
|||
definition::AbstractType&& abstract_type_info,
|
||||
const interpreter::tokens::BaseNode& base_node) {
|
||||
if (!FindAbstractType(abstract_type).has_value()) {
|
||||
size_t id = global_info_.abstract_types_.size();
|
||||
utils::IdType id = global_info_.abstract_types_.size();
|
||||
global_info_.name_to_abstract_type_[abstract_type] = id;
|
||||
global_info_.abstract_types_.push_back(std::move(abstract_type_info));
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
error_handling::HandleTypecheckError("More then one abstract type with the same name in namespace", base_node);
|
||||
error_handling::HandleTypecheckError("More then one abstract type with the same name in namespace",
|
||||
base_node);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -341,6 +348,8 @@ std::optional<T> GlobalInfo::NamespaceVisitor::FindSomething(
|
|||
if (!maybe_namespace.has_value()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
current_namespace = maybe_namespace.value();
|
||||
} else {
|
||||
current_namespace = namespace_stack_[i];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,9 +50,14 @@ void LinkSymbolsVisitor::Visit(TypeExpression* node) { // TODO: check
|
|||
node->type_id_ = namespace_visitor_.FindType(path, node->type.type);
|
||||
node->constructor_id_ = namespace_visitor_.FindConstructor(path, node->type.type);
|
||||
|
||||
// if (!node->type_id_.has_value() && !node->constructor_id_.has_value()) { // TODO: check, that not bastract types
|
||||
// error_handling::HandleTypecheckError("Type or constructor not found", node->base);
|
||||
// }
|
||||
if (path.size() == 0 && namespace_visitor_.FindAbstractType(node->type.type).has_value()) { // TODO
|
||||
// abstract / basic / TODO: local abstract type
|
||||
return;
|
||||
}
|
||||
|
||||
if (!node->type_id_.has_value() && !node->constructor_id_.has_value()) { // TODO: check, that not bastract types
|
||||
error_handling::HandleTypecheckError("Type or constructor not found", node->base);
|
||||
}
|
||||
|
||||
if (node->constructor_id_.has_value()) {
|
||||
utils::IdType constructor_type_id = namespace_visitor_.GetGlobalInfo()->GetConstructorInfo(node->constructor_id_.value()).type_id;
|
||||
|
|
|
|||
|
|
@ -47,15 +47,21 @@ int main(int argc, char** argv) { // TODO, only test version
|
|||
info::TypeInfoContextManager context_manager;
|
||||
|
||||
interpreter::BuildVisitor build_visitor(parse_tree);
|
||||
// interpreter::PrintVisitor print_visitor(std::cout);
|
||||
interpreter::PrintVisitor print_visitor(std::cout);
|
||||
interpreter::FindSymbolsVisitor find_symbols_visitor(global_info);
|
||||
interpreter::LinkSymbolsVisitor link_symbols_visitor(global_info);
|
||||
interpreter::TypeCheckVisitor type_check_visitor(global_info, context_manager);
|
||||
interpreter::TypedPrintVisitor typed_print_visitor(std::cout, context_manager);
|
||||
|
||||
build_visitor.VisitSourceFile(source_file.get());
|
||||
|
||||
std::cout << "\n---------------------------------- Untyped -------------------------------------\n\n";
|
||||
print_visitor.VisitSourceFile(source_file.get());
|
||||
|
||||
find_symbols_visitor.VisitSourceFile(source_file.get());
|
||||
link_symbols_visitor.VisitSourceFile(source_file.get());
|
||||
type_check_visitor.VisitSourceFile(source_file.get());
|
||||
|
||||
std::cout << "\n---------------------------------- Typed -------------------------------------\n\n";
|
||||
typed_print_visitor.VisitSourceFile(source_file.get());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -52,8 +52,6 @@ void TypeCheckVisitor::Visit(NamespaceSources* node) {
|
|||
|
||||
void TypeCheckVisitor::Visit(Namespace* node) { // TODO: two var namespces for class: const and var
|
||||
if (node->modifier.has_value()) {
|
||||
bool is_const = (node->modifier.value() == utils::IsConstModifier::Const);
|
||||
|
||||
if (node->link_typeclass_id_.has_value()) { // TODO: think about typeclass
|
||||
|
||||
std::vector<utils::IdType> requirements {node->link_typeclass_id_.value()};
|
||||
|
|
@ -65,10 +63,10 @@ void TypeCheckVisitor::Visit(Namespace* node) { // TODO: two var namespces for c
|
|||
abstract_type);
|
||||
context_manager_.DefineLocalAbstractType(node->type, abstract_type);
|
||||
} else if (node->link_type_id_.has_value()) {
|
||||
Visitor::Visit(*namespace_visitor_.GetGlobalInfo()->GetTypeInfo<info::definition::AnyType>(node->link_type_id_.value()).value().value); // handle error?
|
||||
Visitor::Visit(*namespace_visitor_.GetGlobalInfo()->GetTypeInfo<info::definition::AnyType>(node->link_type_id_.value()).value()->value); // handle error?
|
||||
|
||||
context_manager_.EnterVariableContext(
|
||||
"self", // TODO: different name ??
|
||||
"self", // TODO: different name
|
||||
context_manager_.AddType(
|
||||
info::type::DefinedType(node->link_type_id_.value(), current_type_, context_manager_.GetTypeManager()),
|
||||
IsConstModifierToValueType(node->modifier.value())));
|
||||
|
|
@ -147,6 +145,20 @@ void TypeCheckVisitor::Visit(FunctionDeclaration* node) {
|
|||
bool was_in_statement = is_in_statement_;
|
||||
is_in_statement_ = true;
|
||||
|
||||
context_manager_.EnterContext();
|
||||
for (auto& parameter : node->parameters) { // declaration correctness check // TODO: needed??
|
||||
std::vector<utils::IdType> requirements;
|
||||
// TODO:
|
||||
current_type_ = context_manager_.AddType(
|
||||
info::type::AbstractType(utils::AbstractTypeModifier::Abstract,
|
||||
parameter->type,
|
||||
requirements), // TODO: typeclasses-requirements
|
||||
utils::ValueType::Tmp);
|
||||
context_manager_.DefineLocalAbstractType(parameter->type, current_type_);
|
||||
}
|
||||
Visit(node->type.get());
|
||||
context_manager_.ExitContext();
|
||||
|
||||
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
|
||||
|
||||
if (!was_in_statement) {
|
||||
|
|
@ -196,7 +208,7 @@ void TypeCheckVisitor::Visit(FunctionDefinitionStatement* node) {
|
|||
Visitor::Visit(*declaration.argument_types.back());
|
||||
utils::IdType return_type = current_type_;
|
||||
|
||||
Visitor::Visit(node->value);
|
||||
// Visitor::Visit(node->value);
|
||||
if (!context_manager_.EqualTypes(return_type, current_type_)) {
|
||||
error_handling::HandleTypecheckError("Wrong function return type", node->base);
|
||||
}
|
||||
|
|
@ -298,7 +310,7 @@ void TypeCheckVisitor::Visit(TypeConstructorPattern* node) { // TODO: match name
|
|||
CollectTypeExpressionContext(*node->constructor, context);
|
||||
|
||||
// TODO: handle alias types
|
||||
const std::optional<info::definition::AnyType>& maybe_type_info =
|
||||
std::optional<info::definition::AnyType*> maybe_type_info =
|
||||
namespace_visitor_.GetGlobalInfo()->GetTypeInfo<info::definition::AnyType>(type_id);
|
||||
|
||||
if (!maybe_type_info.has_value()) { // TODO
|
||||
|
|
@ -306,7 +318,7 @@ void TypeCheckVisitor::Visit(TypeConstructorPattern* node) { // TODO: match name
|
|||
"TypeCheckVisitor.TypeConstructorPattern");
|
||||
}
|
||||
|
||||
const info::definition::AnyType& type_info = maybe_type_info.value();
|
||||
info::definition::AnyType& type_info = *maybe_type_info.value();
|
||||
|
||||
if (constructor_info.constructor_tuple_node.has_value()) {
|
||||
|
||||
|
|
@ -495,8 +507,8 @@ void TypeCheckVisitor::Visit(ForLoop* node) {
|
|||
|
||||
context_manager_.EnterContext();
|
||||
|
||||
current_type_ = context_manager_.AddType(maybe_interval_type.value()->GetElementsType(),
|
||||
utils::IsConstModifierToValueType(node->variable_modifier));
|
||||
current_type_ = context_manager_.ToModifiedType(maybe_interval_type.value()->GetElementsType(),
|
||||
utils::IsConstModifierToValueType(node->variable_modifier));
|
||||
|
||||
is_const_definition_ = node->variable_modifier;
|
||||
Visitor::Visit(node->variable); // type passed to variable definition throught current_type_
|
||||
|
|
@ -625,7 +637,7 @@ void TypeCheckVisitor::Visit(BinaryOperatorExpression* node) {
|
|||
|
||||
auto maybe_left_type_info = namespace_visitor_.GetGlobalInfo()->GetTypeInfo<info::definition::AnyType>(maybe_left_type.value()->GetTypeId());
|
||||
|
||||
std::string left_type_name = maybe_left_type_info.value().type.type;
|
||||
std::string left_type_name = maybe_left_type_info.value()->type.type;
|
||||
|
||||
auto maybe_const_operator_id = namespace_visitor_.FindMethod(std::nullopt,
|
||||
left_type_name,
|
||||
|
|
@ -780,7 +792,7 @@ void TypeCheckVisitor::Visit(FunctionCallExpression* node) {
|
|||
|
||||
utils::IdType type_id = maybe_expression_type.value()->GetTypeId();
|
||||
|
||||
const std::optional<info::definition::AnyType>& maybe_type_info = namespace_visitor_.GetGlobalInfo()->GetTypeInfo<info::definition::AnyType>(type_id);
|
||||
std::optional<info::definition::AnyType*> maybe_type_info = namespace_visitor_.GetGlobalInfo()->GetTypeInfo<info::definition::AnyType>(type_id);
|
||||
|
||||
if (!maybe_type_info.has_value()) {
|
||||
error_handling::HandleInternalError("Functions/Methods implemented only for AnyType",
|
||||
|
|
@ -788,7 +800,7 @@ void TypeCheckVisitor::Visit(FunctionCallExpression* node) {
|
|||
}
|
||||
|
||||
// TODO: better decision ??
|
||||
std::optional<utils::IdType> maybe_const_function_id = maybe_type_info.value().parent_namespace->const_namespaces.at(maybe_type_info->type.type).functions[node->name.name];
|
||||
std::optional<utils::IdType> maybe_const_function_id = maybe_type_info.value()->parent_namespace->const_namespaces.at(maybe_type_info.value()->type.type).functions[node->name.name];
|
||||
|
||||
utils::ValueType expression_value_type = context_manager_.GetValueType(current_type_);
|
||||
|
||||
|
|
@ -799,7 +811,7 @@ void TypeCheckVisitor::Visit(FunctionCallExpression* node) {
|
|||
|
||||
if (expression_value_type == utils::ValueType::Var || expression_value_type == utils::ValueType::Tmp) {
|
||||
// TODO: choose expression value types
|
||||
maybe_function_id = maybe_type_info.value().parent_namespace->var_namespaces.at(maybe_type_info->type.type).functions[node->name.name];
|
||||
maybe_function_id = maybe_type_info.value()->parent_namespace->var_namespaces.at(maybe_type_info.value()->type.type).functions[node->name.name];
|
||||
}
|
||||
|
||||
if (maybe_const_function_id.has_value() && maybe_function_id.has_value()) { // TODO: handle on link_types stage
|
||||
|
|
@ -899,7 +911,7 @@ void TypeCheckVisitor::Visit(VariantExpression* node) {
|
|||
|
||||
// TODO: deal with expression tuple types, etc, ??
|
||||
std::vector<std::pair<std::optional<std::string>, utils::IdType>> constructor_fields {{std::nullopt, current_type_}};
|
||||
constructors.emplace_back(std::nullopt, constructor_fields, context_manager_.GetTypeManager());
|
||||
constructors.push_back(info::type::TupleType(std::nullopt, constructor_fields, context_manager_.GetTypeManager()));
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -937,14 +949,14 @@ void TypeCheckVisitor::Visit(TypeConstructor* node) {
|
|||
CollectTypeExpressionContext(*node->constructor, context);
|
||||
|
||||
// TODO: handle alias types
|
||||
const std::optional<info::definition::AnyType>& maybe_type_info =
|
||||
std::optional<info::definition::AnyType*> maybe_type_info =
|
||||
namespace_visitor_.GetGlobalInfo()->GetTypeInfo<info::definition::AnyType>(type_id);
|
||||
|
||||
if (!maybe_type_info.has_value()) { // TODO
|
||||
error_handling::HandleInternalError("Implemented only for AnyType", "TypeCheckVisitor.TypeConstructor");
|
||||
}
|
||||
|
||||
const info::definition::AnyType& type_info = maybe_type_info.value();
|
||||
info::definition::AnyType& type_info = *maybe_type_info.value();
|
||||
|
||||
if (constructor_info.constructor_tuple_node.has_value()) {
|
||||
|
||||
|
|
@ -1215,7 +1227,11 @@ void TypeCheckVisitor::Visit(TupleType* node) {
|
|||
fields.reserve(node->entities.size());
|
||||
for (auto& entity : node->entities) {
|
||||
Visit(entity.second.get());
|
||||
fields.emplace_back(entity.first, current_type_);
|
||||
if (entity.first.has_value()) {
|
||||
fields.push_back({entity.first.value().name, current_type_});
|
||||
} else {
|
||||
fields.push_back({std::nullopt, current_type_});
|
||||
}
|
||||
}
|
||||
|
||||
current_type_ = context_manager_.AddType(
|
||||
|
|
@ -1232,14 +1248,14 @@ void TypeCheckVisitor::Visit(VariantType* node) {
|
|||
for (auto& constructor : node->constructors) {
|
||||
if (std::holds_alternative<Constructor>(constructor)) {
|
||||
std::vector<std::pair<std::optional<std::string>, utils::IdType>> constructor_fields;
|
||||
constructors.emplace_back(std::get<Constructor>(constructor), constructor_fields, context_manager_.GetTypeManager());
|
||||
constructors.push_back(info::type::TupleType(std::get<Constructor>(constructor), constructor_fields, context_manager_.GetTypeManager()));
|
||||
} else if (std::holds_alternative<std::unique_ptr<TupleType>>(constructor)) {
|
||||
Visit(std::get<std::unique_ptr<TupleType>>(constructor).get());
|
||||
std::optional<TupleType*> maybe_constructor = context_manager_.GetType<TupleType>(current_type_);
|
||||
std::optional<info::type::TupleType*> maybe_constructor = context_manager_.GetType<info::type::TupleType>(current_type_);
|
||||
if (!maybe_constructor.has_value()) {
|
||||
error_handling::HandleInternalError("Entity of VariantType is not TupleType", "TypeCheckVisitor.VariantType");
|
||||
}
|
||||
constructors.emplace_back(maybe_constructor.value());
|
||||
constructors.push_back(*maybe_constructor.value());
|
||||
} else {
|
||||
// error
|
||||
}
|
||||
|
|
@ -1256,20 +1272,23 @@ void TypeCheckVisitor::Visit(TypeExpression* node) {
|
|||
std::unordered_map<std::string, utils::IdType> context;
|
||||
CollectTypeExpressionContext(*node, context);
|
||||
|
||||
const std::optional<info::definition::AnyType>& maybe_type_info =
|
||||
namespace_visitor_.GetGlobalInfo()->GetTypeInfo<info::definition::AnyType>(node->type.type_id_.value());
|
||||
|
||||
if (!maybe_type_info.has_value()) { // TODO: add alias, abstract, ... types
|
||||
error_handling::HandleInternalError("No any type found", "TypeCheckVisitor.TypeExpression");
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> maybe_local_abstract_type = context_manager_.GetLocalAbstractType(node->type.type);
|
||||
if (node->path.size() == 0 && maybe_local_abstract_type.has_value()) {
|
||||
current_type_ = maybe_local_abstract_type.value();
|
||||
}
|
||||
} else if (node->type.type_id_.has_value()) { // TODO: chack that names are different (always true ??)
|
||||
|
||||
Visitor::Visit(*maybe_type_info.value().value);
|
||||
current_type_ = TypeInContext(current_type_, context);
|
||||
std::optional<info::definition::AnyType*> maybe_type_info =
|
||||
namespace_visitor_.GetGlobalInfo()->GetTypeInfo<info::definition::AnyType>(node->type.type_id_.value());
|
||||
|
||||
if (!maybe_type_info.has_value()) { // TODO: add alias, abstract, ... types
|
||||
error_handling::HandleInternalError("No AnyType found", "TypeCheckVisitor.TypeExpression");
|
||||
}
|
||||
|
||||
Visitor::Visit(*maybe_type_info.value()->value);
|
||||
current_type_ = TypeInContext(current_type_, context);
|
||||
} else {
|
||||
error_handling::HandleTypecheckError("Type not found", node->base);
|
||||
}
|
||||
|
||||
if (node->array_size.has_value()) {
|
||||
current_type_ = context_manager_.AddType(
|
||||
|
|
@ -1330,6 +1349,12 @@ void TypeCheckVisitor::Visit(CharLiteral* node) {
|
|||
node->base.type_ = current_type_;
|
||||
}
|
||||
|
||||
void TypeCheckVisitor::Visit(UnitLiteral* node) {
|
||||
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
|
||||
|
||||
node->base.type_ = current_type_;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
void TypeCheckVisitor::CollectTypeContext(const ParametrizedType& type,
|
||||
|
|
@ -1338,14 +1363,14 @@ void TypeCheckVisitor::CollectTypeContext(const ParametrizedType& type,
|
|||
return;
|
||||
}
|
||||
|
||||
const std::optional<info::definition::AnyType>& maybe_type_info =
|
||||
std::optional<info::definition::AnyType*> maybe_type_info =
|
||||
namespace_visitor_.GetGlobalInfo()->GetTypeInfo<info::definition::AnyType>(type.type_id_.value());
|
||||
|
||||
if (!maybe_type_info.has_value()) {
|
||||
error_handling::HandleInternalError("Wrong type id", "TypeCheckVisitor.CollectTypeContext");
|
||||
}
|
||||
|
||||
const info::definition::AnyType& type_info = maybe_type_info.value();
|
||||
info::definition::AnyType& type_info = *maybe_type_info.value();
|
||||
|
||||
if (type_info.parameters.size() != type.parameters.size()) {
|
||||
error_handling::HandleTypecheckError("Wrong type parameters count", type.base);
|
||||
|
|
@ -1372,7 +1397,7 @@ void TypeCheckVisitor::CollectTypeExpressionContext(const TypeExpression& type_e
|
|||
|
||||
utils::IdType TypeCheckVisitor::TypeInContext(utils::IdType type,
|
||||
const std::unordered_map<std::string, utils::IdType>& context) {
|
||||
info::type::Type type_in_context = info::type::Type(context_manager_.GetAnyType(type));
|
||||
info::type::Type type_in_context = *context_manager_.GetAnyType(type);
|
||||
type_in_context.InContext(context);
|
||||
return context_manager_.AddType(std::move(type_in_context), utils::ValueType::Tmp);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -466,42 +466,5 @@ std::string Type::GetTypeName() const {
|
|||
return ""; // ??
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
template<typename T>
|
||||
utils::IdType TypeManager::AddType(T&& type, utils::ValueType value_type) {
|
||||
types_.emplace_back({std::forward(type), value_type});
|
||||
return types_.size() - 1;
|
||||
}
|
||||
|
||||
utils::IdType TypeManager::AddAnyType(Type&& type, utils::ValueType value_type) {
|
||||
types_.push_back({std::move(type), value_type});
|
||||
return types_.size() - 1;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::optional<T*> TypeManager::GetType(utils::IdType type_id) {
|
||||
if (!std::holds_alternative<T>(types_.at(type_id).second)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return &std::get<T>(types_.at(type_id).second);
|
||||
}
|
||||
|
||||
Type* TypeManager::GetAnyType(utils::IdType type_id) {
|
||||
return &types_.at(type_id).first;
|
||||
}
|
||||
|
||||
utils::ValueType TypeManager::GetValueType(utils::IdType type_id) {
|
||||
return types_.at(type_id).second;
|
||||
}
|
||||
|
||||
bool TypeManager::EqualTypes(utils::IdType first_type, utils::IdType second_type) {
|
||||
return GetAnyType(first_type)->Same(*GetAnyType(second_type));
|
||||
}
|
||||
|
||||
bool TypeManager::AddTypeRequirement(utils::IdType type, utils::IdType requrement) {
|
||||
return *GetAnyType(requrement) < *GetAnyType(type);
|
||||
}
|
||||
|
||||
} // namespace info::type
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
namespace Employee {
|
||||
decl gen_employee : Unit -> Employee
|
||||
def gen_employee = {
|
||||
def gen_employee : a = {
|
||||
return
|
||||
$Employee
|
||||
& name = "John"
|
||||
|
|
|
|||
259
tests/match.lang
259
tests/match.lang
|
|
@ -1,12 +1,8 @@
|
|||
================================================================================
|
||||
Match
|
||||
================================================================================
|
||||
|
||||
decl fruit_cost : Fruit -> Int
|
||||
def fruit_cost : fruit = {
|
||||
return (match fruit with
|
||||
| $Banana -> 11
|
||||
| $Apple | $Orange -> 7)
|
||||
return (match fruit with
|
||||
| $Banana -> 11
|
||||
| $Apple | $Orange -> 7)
|
||||
}
|
||||
|
||||
decl amount_to_string : Int -> Bool -> String
|
||||
|
|
@ -19,252 +15,3 @@ def amount_to_string : x is_zero_separated = {
|
|||
| _ -> "Lots"
|
||||
return ans
|
||||
}
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
(source_file
|
||||
(source_statement
|
||||
(partition_statement
|
||||
(namespace_statement
|
||||
(function_declaration
|
||||
(extended_name
|
||||
(name_identifier))
|
||||
(function_type
|
||||
(scoped_any_type
|
||||
(type_expression
|
||||
(parametrized_type
|
||||
(type_identifier))))
|
||||
(scoped_any_type
|
||||
(type_expression
|
||||
(parametrized_type
|
||||
(type_identifier)))))))))
|
||||
(source_statement
|
||||
(partition_statement
|
||||
(namespace_statement
|
||||
(function_definition_statement
|
||||
(function_definition
|
||||
(extended_name
|
||||
(name_identifier))
|
||||
(extended_name
|
||||
(name_identifier)))
|
||||
(superexpression
|
||||
(expression
|
||||
(prefixed_expression
|
||||
(block
|
||||
(block_statement
|
||||
(prefixed_expression
|
||||
(return_expression
|
||||
(expression
|
||||
(subexpression
|
||||
(subexpression_token
|
||||
(scoped_statement
|
||||
(superexpression
|
||||
(flow_control
|
||||
(match
|
||||
(expression
|
||||
(subexpression
|
||||
(subexpression_token
|
||||
(name_expression
|
||||
(extended_name
|
||||
(name_identifier))))))
|
||||
(match_case
|
||||
(pattern
|
||||
(type_constructor_pattern
|
||||
(type_expression
|
||||
(parametrized_type
|
||||
(type_identifier)))))
|
||||
(expression
|
||||
(subexpression
|
||||
(subexpression_token
|
||||
(literal
|
||||
(number_literal))))))
|
||||
(match_case
|
||||
(pattern
|
||||
(type_constructor_pattern
|
||||
(type_expression
|
||||
(parametrized_type
|
||||
(type_identifier))))))
|
||||
(match_case
|
||||
(pattern
|
||||
(type_constructor_pattern
|
||||
(type_expression
|
||||
(parametrized_type
|
||||
(type_identifier)))))
|
||||
(expression
|
||||
(subexpression
|
||||
(subexpression_token
|
||||
(literal
|
||||
(number_literal))))))))))))))))))))))))
|
||||
(source_statement
|
||||
(partition_statement
|
||||
(namespace_statement
|
||||
(function_declaration
|
||||
(extended_name
|
||||
(name_identifier))
|
||||
(function_type
|
||||
(scoped_any_type
|
||||
(type_expression
|
||||
(parametrized_type
|
||||
(type_identifier))))
|
||||
(scoped_any_type
|
||||
(type_expression
|
||||
(parametrized_type
|
||||
(type_identifier))))
|
||||
(scoped_any_type
|
||||
(type_expression
|
||||
(parametrized_type
|
||||
(type_identifier)))))))))
|
||||
(source_statement
|
||||
(partition_statement
|
||||
(namespace_statement
|
||||
(function_definition_statement
|
||||
(function_definition
|
||||
(extended_name
|
||||
(name_identifier))
|
||||
(extended_name
|
||||
(name_identifier))
|
||||
(extended_name
|
||||
(name_identifier)))
|
||||
(superexpression
|
||||
(expression
|
||||
(prefixed_expression
|
||||
(block
|
||||
(block_statement
|
||||
(variable_definition_statement
|
||||
(any_name
|
||||
(annotated_name
|
||||
(extended_name
|
||||
(name_identifier))))
|
||||
(superexpression
|
||||
(flow_control
|
||||
(match
|
||||
(expression
|
||||
(subexpression
|
||||
(subexpression_token
|
||||
(name_expression
|
||||
(extended_name
|
||||
(name_identifier))))))
|
||||
(match_case
|
||||
(pattern
|
||||
(literal
|
||||
(number_literal)))
|
||||
(expression
|
||||
(subexpression
|
||||
(subexpression_token
|
||||
(name_expression
|
||||
(extended_name
|
||||
(name_identifier))))))
|
||||
(expression
|
||||
(subexpression
|
||||
(subexpression_token
|
||||
(literal
|
||||
(string_literal))))))
|
||||
(match_case
|
||||
(pattern
|
||||
(literal
|
||||
(number_literal))))
|
||||
(match_case
|
||||
(pattern
|
||||
(literal
|
||||
(number_literal))))
|
||||
(match_case
|
||||
(pattern
|
||||
(literal
|
||||
(number_literal))))
|
||||
(match_case
|
||||
(pattern
|
||||
(literal
|
||||
(number_literal))))
|
||||
(match_case
|
||||
(pattern
|
||||
(literal
|
||||
(number_literal)))
|
||||
(expression
|
||||
(subexpression
|
||||
(subexpression_token
|
||||
(literal
|
||||
(string_literal))))))
|
||||
(match_case
|
||||
(pattern
|
||||
(extended_name
|
||||
(name_identifier)))
|
||||
(expression
|
||||
(subexpression
|
||||
(function_call_expression
|
||||
(subexpression_token
|
||||
(scoped_statement
|
||||
(superexpression
|
||||
(expression
|
||||
(subexpression
|
||||
(binary_operator_expression
|
||||
(subexpression
|
||||
(subexpression_token
|
||||
(literal
|
||||
(number_literal))))
|
||||
(operator)
|
||||
(subexpression
|
||||
(subexpression_token
|
||||
(literal
|
||||
(number_literal))))))))))
|
||||
(extended_name
|
||||
(name_identifier))
|
||||
(subexpression_token
|
||||
(name_expression
|
||||
(extended_name
|
||||
(name_identifier)))))))
|
||||
(expression
|
||||
(subexpression
|
||||
(subexpression_token
|
||||
(literal
|
||||
(string_literal))))))
|
||||
(match_case
|
||||
(pattern
|
||||
(extended_name
|
||||
(name_identifier)))
|
||||
(expression
|
||||
(subexpression
|
||||
(function_call_expression
|
||||
(subexpression_token
|
||||
(scoped_statement
|
||||
(superexpression
|
||||
(expression
|
||||
(subexpression
|
||||
(binary_operator_expression
|
||||
(subexpression
|
||||
(subexpression_token
|
||||
(literal
|
||||
(number_literal))))
|
||||
(operator)
|
||||
(subexpression
|
||||
(subexpression_token
|
||||
(literal
|
||||
(number_literal))))))))))
|
||||
(extended_name
|
||||
(name_identifier))
|
||||
(subexpression_token
|
||||
(name_expression
|
||||
(extended_name
|
||||
(name_identifier)))))))
|
||||
(expression
|
||||
(subexpression
|
||||
(subexpression_token
|
||||
(literal
|
||||
(string_literal))))))
|
||||
(match_case
|
||||
(pattern
|
||||
(extended_name
|
||||
(name_identifier)))
|
||||
(expression
|
||||
(subexpression
|
||||
(subexpression_token
|
||||
(literal
|
||||
(string_literal)))))))))))
|
||||
(block_statement
|
||||
(prefixed_expression
|
||||
(return_expression
|
||||
(expression
|
||||
(subexpression
|
||||
(subexpression_token
|
||||
(name_expression
|
||||
(extended_name
|
||||
(name_identifier))))))))))))))))))
|
||||
|
|
|
|||
11
tests/test_code.lang
Normal file
11
tests/test_code.lang
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
basic String
|
||||
basic Int
|
||||
basic Unit
|
||||
|
||||
decl print : String -> Unit
|
||||
|
||||
decl func : String -> Int
|
||||
def func : s = {
|
||||
; print: s
|
||||
return 5
|
||||
}
|
||||
|
|
@ -1,7 +1,3 @@
|
|||
================================================================================
|
||||
Types
|
||||
================================================================================
|
||||
|
||||
alias T1 = Int
|
||||
|
||||
abstract (T2 : #A #B #C)
|
||||
|
|
@ -10,50 +6,3 @@ abstract (T2 : #A #B #C)
|
|||
let T2 = Int
|
||||
let T2 = Float
|
||||
let T2 = Complex
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
(source_file
|
||||
(source_statement
|
||||
(partition_statement
|
||||
(namespace_statement
|
||||
(alias_definition_statement
|
||||
(type_identifier)
|
||||
(type_expression
|
||||
(parametrized_type
|
||||
(type_identifier)))))))
|
||||
(source_statement
|
||||
(partition_statement
|
||||
(abstract_type_definition_statement
|
||||
(annotated_type
|
||||
(type_identifier)
|
||||
(parametrized_typeclass
|
||||
(typeclass_identifier))
|
||||
(parametrized_typeclass
|
||||
(typeclass_identifier))
|
||||
(parametrized_typeclass
|
||||
(typeclass_identifier))))))
|
||||
(source_statement
|
||||
(partition_statement
|
||||
(namespace_statement
|
||||
(alias_definition_statement
|
||||
(type_identifier)
|
||||
(type_expression
|
||||
(parametrized_type
|
||||
(type_identifier)))))))
|
||||
(source_statement
|
||||
(partition_statement
|
||||
(namespace_statement
|
||||
(alias_definition_statement
|
||||
(type_identifier)
|
||||
(type_expression
|
||||
(parametrized_type
|
||||
(type_identifier)))))))
|
||||
(source_statement
|
||||
(partition_statement
|
||||
(namespace_statement
|
||||
(alias_definition_statement
|
||||
(type_identifier)
|
||||
(type_expression
|
||||
(parametrized_type
|
||||
(type_identifier))))))))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue