navid/haze / master / src/haze.h

haze.h

← src · 7156 bytes · raw · download

#ifndef HAZE_H
#define HAZE_H

#include <cstdint>
#include <memory>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

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<Type>;

struct Field {
    std::string name;
    TypePtr type;
    Pos pos;
};

struct Variant {
    std::string name;
    std::vector<Field> fields;
    Pos pos;
};

struct Type {
    enum class Kind {
        Void,
        I8,
        I16,
        I32,
        I64,
        U8,
        U16,
        U32,
        U64,
        F64,
        Bool,
        Str,
        Ptr,
        Bytes,
        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<std::string> type_params;
    std::vector<Field> fields;
    std::vector<Variant> variants;
    std::vector<TypePtr> 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_u8();
TypePtr t_u16();
TypePtr t_u32();
TypePtr t_u64();
TypePtr t_f64();
TypePtr t_bool();
TypePtr t_str();
TypePtr t_ptr();
TypePtr t_bytes();
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<TypePtr> args);
TypePtr instantiate_generic_type(const TypePtr& tmpl, const std::vector<TypePtr>& args);
bool contains_type_param(const TypePtr& t);
TypePtr t_function(std::vector<TypePtr> 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_signed_int(const TypePtr& t);
bool is_unsigned_int(const TypePtr& t);
bool is_float(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 int_constant_fits(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,
    Float,
    Str,
    Arrow,
    EqEq,
    NotEq,
    Le,
    Ge,
    AndAnd,
    OrOr,
    Plus,
    Minus,
    Star,
    Slash,
    Percent,
    Amp,
    Pipe,
    Caret,
    Tilde,
    Shl,
    Shr,
    Assign,
    PlusEq,
    MinusEq,
    StarEq,
    SlashEq,
    PercentEq,
    PlusPlus,
    MinusMinus,
    Dot,
    DotDot,
    Comma,
    LParen,
    RParen,
    LBrace,
    RBrace,
    LBracket,
    RBracket,
    Colon,
    ColonColon,
    Bang,
    Lt,
    Gt,
    Question,
    At,
};

struct Token {
    Tok kind = Tok::End;
    std::string text;
    long long ival = 0;
    unsigned long long uval = 0;
    double fval = 0.0;
    Pos pos;
};

std::vector<Token> lex(const std::string& src);

struct Expr;
using ExprPtr = std::unique_ptr<Expr>;

struct Block;
using BlockPtr = std::unique_ptr<Block>;

struct Param {
    std::string name;
    TypePtr type;
    Pos pos;
};

struct Pattern {
    bool wildcard = false;
    std::string ctor;
    std::vector<std::string> binders;
    std::vector<TypePtr> binder_types;
    Pos pos;
};

struct MatchArm {
    Pattern pat;
    ExprPtr body;
};

struct Expr {
    enum class Kind {
        Int,
        Float,
        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;
    unsigned long long uval = 0;
    double fval = 0.0;
    bool unsigned_literal = false;
    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<ExprPtr> args;
    std::vector<std::string> names;
    std::vector<MatchArm> arms;
    std::vector<Param> params;
    TypePtr lambda_ret;
    BlockPtr block;
    Pattern pattern;
    TypePtr lit_type;
    TypePtr ctor_type;
    TypePtr operand_type;
    const Variant* ctor_variant = nullptr;
    int ctor_index = -1;
};

struct Stmt;
using StmtPtr = std::unique_ptr<Stmt>;

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<ExprPtr> invariants;
    BlockPtr block;
};

struct Block {
    Pos pos;
    std::vector<StmtPtr> stmts;
};

struct Contract {
    enum class Kind { Pre, Post };
    Kind kind = Kind::Pre;
    std::string result_name;
    std::string name;
    std::vector<ExprPtr> args;
    ExprPtr expr;
    Pos pos;
};

struct ContractDecl {
    std::string name;
    std::vector<Param> params;
    std::vector<Contract> bases;
    std::vector<Contract> 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<std::string> names;
    Pos pos;
};

struct Function {
    std::string name;
    std::vector<Param> params;
    TypePtr ret;
    BlockPtr body;
    ExprPtr arrow;
    std::vector<Contract> contracts;
    std::vector<std::string> type_params;
    std::vector<Param> captures;
    std::string module;
    bool pure = false;
    bool pub = false;
    bool is_lambda = false;
    Pos pos;
};

struct Program {
    std::vector<std::unique_ptr<Function>> functions;
    std::vector<TypePtr> types;
    std::vector<std::unique_ptr<ContractDecl>> contracts;
    std::vector<Global> globals;
    std::vector<Use> 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<TypePtr> params;
};

using SigMap = std::unordered_map<std::string, FuncSig>;

} // namespace haze

#endif