#ifndef HAZE_H #define HAZE_H #include #include #include #include #include #include #include namespace haze { struct Pos { int line = 1; int col = 1; }; struct Error : std::runtime_error { Pos pos; Error(Pos p, const std::string& msg) : std::runtime_error(msg), pos(p) {} }; struct Type; using TypePtr = std::shared_ptr; struct Field { std::string name; TypePtr type; Pos pos; }; struct Variant { std::string name; std::vector fields; Pos pos; }; struct Type { enum class Kind { Void, I8, I16, I32, I64, Bool, Str, Ptr, List, Record, Sum, Enum, Option, Result, GenericRef, Function, Param, Unknown }; Kind kind = Kind::Unknown; TypePtr elem; TypePtr err; std::string name; TypePtr base; std::vector type_params; std::vector fields; std::vector variants; std::vector params; TypePtr ret; bool pub = false; std::string module; }; TypePtr t_void(); TypePtr t_i8(); TypePtr t_i16(); TypePtr t_i32(); TypePtr t_i64(); TypePtr t_bool(); TypePtr t_str(); TypePtr t_ptr(); TypePtr t_list(TypePtr elem); TypePtr t_record(const std::string& name); TypePtr t_sum(const std::string& name); TypePtr t_enum(const std::string& name); TypePtr t_option(TypePtr elem); TypePtr t_result(TypePtr ok, TypePtr err); TypePtr t_generic_ref(const std::string& name, std::vector args); TypePtr instantiate_generic_type(const TypePtr& tmpl, const std::vector& args); bool contains_type_param(const TypePtr& t); TypePtr t_function(std::vector params, TypePtr ret); TypePtr t_param(const std::string& name); TypePtr t_unknown(const std::string& name); bool same_type(const TypePtr& a, const TypePtr& b); bool is_int(const TypePtr& t); bool is_narrow_int(const TypePtr& t); int int_rank(const TypePtr& t); long long truncate_int(long long v, const TypePtr& t); bool is_managed(const TypePtr& t); std::string type_name(const TypePtr& t); std::string qbe_type(const TypePtr& t); bool assignable(const TypePtr& to, const TypePtr& from); enum class Tok { End, Ident, Int, Str, Arrow, EqEq, NotEq, Le, Ge, AndAnd, OrOr, Plus, Minus, Star, Slash, Percent, Assign, PlusEq, MinusEq, StarEq, SlashEq, PercentEq, PlusPlus, MinusMinus, Dot, DotDot, Comma, LParen, RParen, LBrace, RBrace, LBracket, RBracket, Colon, ColonColon, Pipe, Bang, Lt, Gt, Question, At, }; struct Token { Tok kind = Tok::End; std::string text; long long ival = 0; Pos pos; }; std::vector lex(const std::string& src); struct Expr; using ExprPtr = std::unique_ptr; struct Block; using BlockPtr = std::unique_ptr; struct Param { std::string name; TypePtr type; Pos pos; }; struct Pattern { bool wildcard = false; std::string ctor; std::vector binders; std::vector binder_types; Pos pos; }; struct MatchArm { Pattern pat; ExprPtr body; }; struct Expr { enum class Kind { Int, Bool, String, Var, Unary, Binary, Call, If, Match, Block, List, RecordLit, Field, Index, Assign, Lambda, Cast, Is, Try, }; Kind kind = Kind::Int; Pos pos; TypePtr ty; long long ival = 0; bool bval = false; std::string text; std::string op; ExprPtr lhs; ExprPtr rhs; ExprPtr cond; ExprPtr then_e; ExprPtr else_e; ExprPtr arrow; std::vector args; std::vector names; std::vector arms; std::vector params; TypePtr lambda_ret; BlockPtr block; Pattern pattern; TypePtr lit_type; TypePtr ctor_type; const Variant* ctor_variant = nullptr; int ctor_index = -1; }; struct Stmt; using StmtPtr = std::unique_ptr; struct Stmt { enum class Kind { Let, Var, Assign, Return, ForRange, ForIn, ForCond, Break, Continue, Expr, Block, }; Kind kind = Kind::Expr; Pos pos; std::string name; std::string op; TypePtr decl_type; ExprPtr expr; ExprPtr rhs; ExprPtr cond; ExprPtr lo; ExprPtr hi; std::vector invariants; BlockPtr block; }; struct Block { Pos pos; std::vector stmts; }; struct Contract { enum class Kind { Pre, Post }; Kind kind = Kind::Pre; std::string result_name; std::string name; std::vector args; ExprPtr expr; Pos pos; }; struct ContractDecl { std::string name; std::vector params; std::vector bases; std::vector clauses; bool pub = false; std::string module; Pos pos; }; struct Global { std::string name; TypePtr type; ExprPtr expr; bool pub = false; std::string module; Pos pos; }; struct Use { std::string module; std::vector names; Pos pos; }; struct Function { std::string name; std::vector params; TypePtr ret; BlockPtr body; ExprPtr arrow; std::vector contracts; std::vector type_params; std::vector captures; std::string module; bool pure = false; bool pub = false; bool is_lambda = false; Pos pos; }; struct Program { std::vector> functions; std::vector types; std::vector> contracts; std::vector globals; std::vector uses; std::string module; bool release = false; }; Program parse_program(const std::string& src); Program load_program(const std::string& entry_path); void check_program(Program& program); void fold_program(Program& program); std::string format_source(const std::string& source); std::string generate_qbe(Program& program, const std::string& source_name); void run_lsp_server(); struct FuncSig { TypePtr ret; std::vector params; }; using SigMap = std::unordered_map; } // namespace haze #endif