navid/haze / dd80043cbfdebf21379913ad485b61a10041969a4e5eed45620594643fb3445f / src/haze.h
haze.h
← src · 5466 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, I32, I64, Bool, Str, Ptr, List, Record, Sum, Function, Param, Unknown };
Kind kind = Kind::Unknown;
TypePtr elem;
std::string name;
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_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_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_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<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;
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,
};
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<ExprPtr> args;
std::vector<std::string> names;
std::vector<MatchArm> arms;
std::vector<Param> params;
TypePtr lambda_ret;
BlockPtr block;
};
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> 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 generate_qbe(Program& program, const std::string& source_name);
struct FuncSig {
TypePtr ret;
std::vector<TypePtr> params;
};
using SigMap = std::unordered_map<std::string, FuncSig>;
} // namespace haze
#endif