mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-06 06:58:45 +00:00
fixes, more information in errors
This commit is contained in:
parent
a11ddbd25f
commit
b686fe00fb
10 changed files with 356 additions and 65 deletions
|
|
@ -134,7 +134,7 @@ private:
|
|||
|
||||
void Visit(ExtendedName* node) override;
|
||||
|
||||
void Visit(AnyIdentifier* node) override; // std::string
|
||||
// // void Visit(AnyIdentifier* node) override; // std::string
|
||||
|
||||
void Visit(FloatNumberLiteral* node) override;
|
||||
void Visit(NumberLiteral* node) override;
|
||||
|
|
|
|||
|
|
@ -1,11 +1,31 @@
|
|||
#pragma once
|
||||
|
||||
#include "interpreter_tree.hpp"
|
||||
#include <iostream>
|
||||
#include <pthread.h>
|
||||
|
||||
namespace error_handling {
|
||||
|
||||
inline void HandleParsingError(const std::string& message, std::pair<size_t, size_t> place) {
|
||||
std::cout << "Parsing Error: " << message << " at (" << place.first << ", " << place.second << ").\n";
|
||||
inline void PrintPosition(std::ostream& out,
|
||||
std::pair<size_t, size_t> start_position,
|
||||
std::pair<size_t, size_t> end_position) {
|
||||
out << '['
|
||||
<< start_position.first
|
||||
<< ", "
|
||||
<< start_position.second
|
||||
<< "] - ["
|
||||
<< end_position.first
|
||||
<< ", "
|
||||
<< end_position.second
|
||||
<< ']';
|
||||
}
|
||||
|
||||
inline void HandleParsingError(const std::string& message,
|
||||
std::pair<size_t, size_t> start_position,
|
||||
std::pair<size_t, size_t> end_position) {
|
||||
std::cout << "Parsing Error: " << message << " at ";
|
||||
PrintPosition(std::cout, start_position, end_position);
|
||||
std::cout << ".\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
|
@ -14,8 +34,11 @@ inline void HandleInternalError(const std::string& message, const std::string& p
|
|||
exit(1);
|
||||
}
|
||||
|
||||
inline void HandleTypecheckError(const std::string& message) { // TODO: place in code
|
||||
std::cout << "Typecheck Error: " << message << ".\n";
|
||||
inline void HandleTypecheckError(const std::string& message,
|
||||
const interpreter::tokens::BaseNode& node) { // TODO: place in code
|
||||
std::cout << "Typecheck Error: " << message << " at ";
|
||||
PrintPosition(std::cout, node.start_position, node.end_position);
|
||||
std::cout << ".\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,6 +11,11 @@
|
|||
|
||||
namespace interpreter::tokens {
|
||||
|
||||
struct BaseNode {
|
||||
std::pair<size_t, size_t> start_position;
|
||||
std::pair<size_t, size_t> end_position;
|
||||
};
|
||||
|
||||
// ----------------- Declarations -----------------
|
||||
|
||||
using AnyIdentifier = std::string;
|
||||
|
|
@ -236,6 +241,8 @@ struct ParametrizedType;
|
|||
// Identifiers, constants, etc. -----------------
|
||||
|
||||
struct ExtendedName {
|
||||
BaseNode base;
|
||||
|
||||
std::string name;
|
||||
};
|
||||
|
||||
|
|
@ -254,16 +261,22 @@ using Pattern = std::variant<
|
|||
// ----------------- Sources -----------------
|
||||
|
||||
struct SourceFile {
|
||||
BaseNode base;
|
||||
|
||||
std::vector<SourceStatement> statements;
|
||||
};
|
||||
|
||||
// ----------------- Namespaces, partittions -----------------
|
||||
|
||||
struct PartitionSources {
|
||||
BaseNode base;
|
||||
|
||||
std::vector<PartitionStatement> statements;
|
||||
};
|
||||
|
||||
struct Partition {
|
||||
BaseNode base;
|
||||
|
||||
enum PartitionName {
|
||||
Test,
|
||||
Interface,
|
||||
|
|
@ -275,10 +288,14 @@ struct Partition {
|
|||
};
|
||||
|
||||
struct NamespaceSources {
|
||||
BaseNode base;
|
||||
|
||||
std::vector<NamespaceStatement> statements;
|
||||
};
|
||||
|
||||
struct Namespace {
|
||||
BaseNode base;
|
||||
|
||||
enum Modifier { Const, Var };
|
||||
std::optional<Modifier> modifier; // modifier => variable namespace
|
||||
TypeIdentifier type;
|
||||
|
|
@ -291,12 +308,16 @@ struct Namespace {
|
|||
// ----------------- Definitions -----------------
|
||||
|
||||
struct ImportStatement {
|
||||
BaseNode base;
|
||||
|
||||
std::optional<TypeIdentifier> name;
|
||||
std::string module_name;
|
||||
std::vector<ImportSymbol> symbols;
|
||||
};
|
||||
|
||||
struct AliasDefinitionStatement {
|
||||
BaseNode base;
|
||||
|
||||
enum {Alias, Type, Let} modifier;
|
||||
TypeIdentifier type;
|
||||
std::vector<AbstractTypeIdentifier> parameters;
|
||||
|
|
@ -306,6 +327,8 @@ struct AliasDefinitionStatement {
|
|||
};
|
||||
|
||||
struct VariableDefinitionStatement {
|
||||
BaseNode base;
|
||||
|
||||
enum { Var, Const } modifier;
|
||||
enum { Move, Assign } assignment_modifier;
|
||||
AnyName name;
|
||||
|
|
@ -313,6 +336,8 @@ struct VariableDefinitionStatement {
|
|||
};
|
||||
|
||||
struct FunctionDeclaration {
|
||||
BaseNode base;
|
||||
|
||||
ExtendedName name;
|
||||
std::vector<std::unique_ptr<AnnotatedAbstractType>> parameters;
|
||||
std::unique_ptr<FunctionType> type;
|
||||
|
|
@ -321,6 +346,8 @@ struct FunctionDeclaration {
|
|||
};
|
||||
|
||||
struct FunctionDefinitionStatement {
|
||||
BaseNode base;
|
||||
|
||||
bool is_inline;
|
||||
std::unique_ptr<FunctionDefinition> definition;
|
||||
SuperExpression value;
|
||||
|
|
@ -329,6 +356,8 @@ struct FunctionDefinitionStatement {
|
|||
};
|
||||
|
||||
struct TypeDefinitionStatement {
|
||||
BaseNode base;
|
||||
|
||||
enum { Struct, Class } modifier;
|
||||
std::unique_ptr<TypeDefinition> definition;
|
||||
AnyType value;
|
||||
|
|
@ -337,6 +366,8 @@ struct TypeDefinitionStatement {
|
|||
};
|
||||
|
||||
struct AbstractTypeDefinitionStatement {
|
||||
BaseNode base;
|
||||
|
||||
enum { Basic, Abstract } modifier;
|
||||
std::unique_ptr<AnnotatedType> type;
|
||||
|
||||
|
|
@ -345,6 +376,8 @@ struct AbstractTypeDefinitionStatement {
|
|||
};
|
||||
|
||||
struct TypeclassDefinitionStatement {
|
||||
BaseNode base;
|
||||
|
||||
std::unique_ptr<TypeDefinition> definition;
|
||||
std::vector<std::unique_ptr<FunctionDeclaration>> requirements;
|
||||
|
||||
|
|
@ -354,17 +387,23 @@ struct TypeclassDefinitionStatement {
|
|||
// Definition parts -----------------
|
||||
|
||||
struct FunctionDefinition {
|
||||
BaseNode base;
|
||||
|
||||
enum { Operator, Function } modifier;
|
||||
ExtendedName name;
|
||||
std::vector<ExtendedName> arguments;
|
||||
};
|
||||
|
||||
struct TypeDefinition {
|
||||
BaseNode base;
|
||||
|
||||
std::unique_ptr<AnnotatedType> type;
|
||||
std::vector<std::unique_ptr<AnnotatedAbstractType>> parameters;
|
||||
};
|
||||
|
||||
struct AnyAnnotatedType {
|
||||
BaseNode base;
|
||||
|
||||
AnyTypeIdentifier type;
|
||||
std::vector<std::unique_ptr<ParametrizedTypeclass>> typeclasses;
|
||||
};
|
||||
|
|
@ -372,48 +411,66 @@ struct AnyAnnotatedType {
|
|||
// ----------------- Flow control -----------------
|
||||
|
||||
struct TypeConstructorPatternParameter {
|
||||
BaseNode base;
|
||||
|
||||
std::optional<ExtendedName> name;
|
||||
PatternToken value;
|
||||
};
|
||||
|
||||
struct TypeConstructorPattern {
|
||||
BaseNode base;
|
||||
|
||||
std::unique_ptr<TypeExpression> constructor;
|
||||
std::vector<TypeConstructorPatternParameter> parameters;
|
||||
};
|
||||
|
||||
struct MatchCase {
|
||||
BaseNode base;
|
||||
|
||||
Pattern value;
|
||||
std::optional<Expression> condition;
|
||||
std::optional<Expression> statement;
|
||||
};
|
||||
|
||||
struct Match {
|
||||
BaseNode base;
|
||||
|
||||
Expression value;
|
||||
std::vector<MatchCase> matches;
|
||||
};
|
||||
|
||||
struct Condition {
|
||||
BaseNode base;
|
||||
|
||||
std::vector<Expression> conditions; // if, elif
|
||||
std::vector<Expression> statements; // if, elif, else
|
||||
};
|
||||
|
||||
struct DoWhileLoop {
|
||||
BaseNode base;
|
||||
|
||||
Expression condition;
|
||||
Expression statement;
|
||||
};
|
||||
|
||||
struct WhileLoop {
|
||||
BaseNode base;
|
||||
|
||||
Expression condition;
|
||||
Expression statement;
|
||||
};
|
||||
|
||||
struct ForLoop {
|
||||
BaseNode base;
|
||||
|
||||
AnyName variable;
|
||||
Expression interval;
|
||||
Expression statement;
|
||||
};
|
||||
|
||||
struct LoopLoop {
|
||||
BaseNode base;
|
||||
|
||||
Expression statement;
|
||||
};
|
||||
|
||||
|
|
@ -426,32 +483,44 @@ using BlockStatement = std::variant<
|
|||
std::unique_ptr<PrefixedExpression>>;
|
||||
|
||||
struct Block {
|
||||
BaseNode base;
|
||||
|
||||
std::vector<BlockStatement> statements;
|
||||
};
|
||||
|
||||
struct ScopedStatement {
|
||||
BaseNode base;
|
||||
|
||||
SuperExpression statement;
|
||||
};
|
||||
|
||||
// Operators -----------------
|
||||
|
||||
struct BinaryOperatorExpression {
|
||||
BaseNode base;
|
||||
|
||||
OperatorIdentifier operator_name;
|
||||
SubExpression left_expression;
|
||||
SubExpression right_expression;
|
||||
};
|
||||
|
||||
struct UnaryOperatorExpression {
|
||||
BaseNode base;
|
||||
|
||||
OperatorIdentifier operator_name;
|
||||
Expression expression;
|
||||
};
|
||||
|
||||
struct ReferenceExpression {
|
||||
BaseNode base;
|
||||
|
||||
std::vector<utils::ReferenceType> references;
|
||||
std::unique_ptr<ScopedStatement> expression;
|
||||
};
|
||||
|
||||
struct AccessExpression {
|
||||
BaseNode base;
|
||||
|
||||
std::unique_ptr<NameExpression> name;
|
||||
SubExpressionToken id;
|
||||
};
|
||||
|
|
@ -459,6 +528,8 @@ struct AccessExpression {
|
|||
// Other Expressions -----------------
|
||||
|
||||
struct FunctionCallExpression {
|
||||
BaseNode base;
|
||||
|
||||
std::optional<std::variant<std::unique_ptr<SubExpressionToken>,
|
||||
std::unique_ptr<TypeExpression>>> prefix;
|
||||
ExtendedName name;
|
||||
|
|
@ -467,18 +538,26 @@ struct FunctionCallExpression {
|
|||
};
|
||||
|
||||
struct TupleExpression {
|
||||
BaseNode base;
|
||||
|
||||
std::vector<SubExpression> expressions;
|
||||
};
|
||||
|
||||
struct VariantExpression {
|
||||
BaseNode base;
|
||||
|
||||
std::vector<SubExpression> expressions;
|
||||
};
|
||||
|
||||
struct ReturnExpression {
|
||||
BaseNode base;
|
||||
|
||||
Expression expression;
|
||||
};
|
||||
|
||||
struct TypeConstructorParameter {
|
||||
BaseNode base;
|
||||
|
||||
enum AssignmentModifier { Move, Assign };
|
||||
std::optional<ExtendedName> name;
|
||||
std::optional<AssignmentModifier> asignment_modifier;
|
||||
|
|
@ -486,11 +565,15 @@ struct TypeConstructorParameter {
|
|||
};
|
||||
|
||||
struct TypeConstructor {
|
||||
BaseNode base;
|
||||
|
||||
std::unique_ptr<TypeExpression> constructor;
|
||||
std::vector<TypeConstructorParameter> parameters;
|
||||
};
|
||||
|
||||
struct LambdaFunction {
|
||||
BaseNode base;
|
||||
|
||||
std::vector<std::unique_ptr<AnnotatedAbstractType>> parameters;
|
||||
std::vector<ExtendedName> arguments;
|
||||
Expression expression;
|
||||
|
|
@ -500,24 +583,34 @@ struct LambdaFunction {
|
|||
};
|
||||
|
||||
struct ArrayExpression {
|
||||
BaseNode base;
|
||||
|
||||
std::vector<SubExpression> elements;
|
||||
};
|
||||
|
||||
// Name -----------------
|
||||
|
||||
struct NameExpression {
|
||||
BaseNode base;
|
||||
|
||||
std::vector<ExtendedName> names;
|
||||
};
|
||||
|
||||
struct TupleName {
|
||||
BaseNode base;
|
||||
|
||||
std::vector<ScopedAnyName> names;
|
||||
};
|
||||
|
||||
struct VariantName {
|
||||
BaseNode base;
|
||||
|
||||
std::vector<ScopedAnyName> names;
|
||||
};
|
||||
|
||||
struct AnnotatedName {
|
||||
BaseNode base;
|
||||
|
||||
NameIdentifier name;
|
||||
std::optional<ScopedAnyType> type;
|
||||
};
|
||||
|
|
@ -527,20 +620,28 @@ struct AnnotatedName {
|
|||
// Type -----------------
|
||||
|
||||
struct FunctionType {
|
||||
BaseNode base;
|
||||
|
||||
std::vector<ScopedAnyType> types;
|
||||
};
|
||||
|
||||
struct TupleType {
|
||||
BaseNode base;
|
||||
|
||||
std::optional<Constructor> type;
|
||||
std::vector<std::pair<std::optional<ExtendedName>, std::unique_ptr<ExtendedScopedAnyType>>> entities;
|
||||
};
|
||||
|
||||
struct VariantType {
|
||||
BaseNode base;
|
||||
|
||||
std::optional<Constructor> type;
|
||||
std::vector<std::variant<Constructor, std::unique_ptr<TupleType>>> constructors;
|
||||
};
|
||||
|
||||
struct ParametrizedType {
|
||||
BaseNode base;
|
||||
|
||||
AnyTypeIdentifier type;
|
||||
std::vector<std::unique_ptr<TypeExpression>> parameters;
|
||||
|
||||
|
|
@ -548,6 +649,8 @@ struct ParametrizedType {
|
|||
};
|
||||
|
||||
struct TypeExpression {
|
||||
BaseNode base;
|
||||
|
||||
std::vector<ParametrizedType> path;
|
||||
ParametrizedType type;
|
||||
|
||||
|
|
@ -558,6 +661,8 @@ struct TypeExpression {
|
|||
};
|
||||
|
||||
struct ExtendedScopedAnyType {
|
||||
BaseNode base;
|
||||
|
||||
std::vector<utils::ReferenceType> references;
|
||||
AnyType type;
|
||||
};
|
||||
|
|
@ -565,6 +670,8 @@ struct ExtendedScopedAnyType {
|
|||
// Typeclass -----------------
|
||||
|
||||
struct ParametrizedTypeclass {
|
||||
BaseNode base;
|
||||
|
||||
TypeclassIdentifier typeclass;
|
||||
std::vector<std::unique_ptr<TypeExpression>> parameters;
|
||||
|
||||
|
|
@ -575,21 +682,31 @@ struct ParametrizedTypeclass {
|
|||
// ----------------- Identifiers, constants, etc. -----------------
|
||||
|
||||
struct FloatNumberLiteral {
|
||||
BaseNode base;
|
||||
|
||||
double value;
|
||||
};
|
||||
|
||||
struct NumberLiteral {
|
||||
BaseNode base;
|
||||
|
||||
int64_t value;
|
||||
};
|
||||
|
||||
struct StringLiteral {
|
||||
BaseNode base;
|
||||
|
||||
std::string value;
|
||||
};
|
||||
|
||||
struct CharLiteral {
|
||||
BaseNode base;
|
||||
|
||||
char value;
|
||||
};
|
||||
|
||||
struct UnitLiteral {};
|
||||
struct UnitLiteral {
|
||||
BaseNode base;
|
||||
};
|
||||
|
||||
} // namespace interpereter::tokens
|
||||
|
|
|
|||
|
|
@ -121,6 +121,10 @@ public:
|
|||
~ParseTree() {
|
||||
ts_tree_delete(tree_);
|
||||
}
|
||||
|
||||
bool IsProperlyParsed() { // TODO: check
|
||||
return !GetRoot().HasError();
|
||||
}
|
||||
private:
|
||||
TSTree* tree_;
|
||||
std::string source_; // for token value extraction
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue