#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 Type { enum class Kind { Void, I32, I64, Bool, Str, Ptr, List, Unknown }; Kind kind = Kind::Unknown; TypePtr elem; std::string name; }; TypePtr t_void(); TypePtr t_i32(); TypePtr t_i64(); TypePtr t_bool(); TypePtr t_str(); TypePtr t_ptr(); TypePtr t_list(TypePtr elem); TypePtr t_unknown(const std::string& name); bool same_type(const TypePtr& a, const TypePtr& b); bool is_int(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, }; 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 Expr { enum class Kind { Int, Bool, String, Var, Unary, Binary, Call, If, Block, List, Field, Index, Assign, }; 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; std::vector args; BlockPtr block; }; struct Stmt; using StmtPtr = std::unique_ptr; struct Stmt { enum class Kind { Let, Var, Assign, Return, ForRange, 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; BlockPtr block; }; struct Block { Pos pos; std::vector stmts; }; struct Param { std::string name; TypePtr type; Pos pos; }; struct Contract { enum class Kind { Pre, Post }; Kind kind = Kind::Pre; std::string result_name; ExprPtr expr; Pos pos; }; struct Function { std::string name; std::vector params; TypePtr ret; BlockPtr body; ExprPtr arrow; std::vector contracts; bool pure = false; bool pub = false; Pos pos; }; struct Program { std::vector> functions; }; Program parse_program(const std::string& src); void check_program(Program& program); std::string generate_qbe(Program& program, const std::string& source_name); struct FuncSig { TypePtr ret; std::vector params; }; using SigMap = std::unordered_map; } // namespace haze #endif