navid/haze / 4aab20aa1d3fcd84f6c3892dfa837c7a9acf3b4f132cf92ec13930df341ac679 / src/xyz.h

xyz.h

← src · 3336 bytes · raw · download

#ifndef XYZ_H
#define XYZ_H

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

namespace xyz {

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 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<Token> lex(const std::string &src);

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

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

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

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

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<StmtPtr> 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<Param> params;
  TypePtr ret;
  BlockPtr body;
  ExprPtr arrow;
  std::vector<Contract> contracts;
  bool pure = false;
  bool pub = false;
  Pos pos;
};

struct Program {
  std::vector<std::unique_ptr<Function>> 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<TypePtr> params;
};

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

} // namespace xyz

#endif