aboutsummaryrefslogtreecommitdiff
path: root/std/ast.pk
blob: 4b8a78388dc97e8e3391b3dc87d9f56a1aa30135 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
## std.ast: Exposes the AST for building and operating on with macros.

## The `Expr` type represents the abstract syntax tree of Puck itself.
## It notably lacks type information.
## It is, however, syntactically correct-by-construction.
pub type Expr = union
  Ident(string)
  Number(int)
  Float(float)
  Char(chr)
  String(str)
  Struct(list[(field: str, value: Expr)])
  Tuple(list[(field: str?, value: Expr)])
  List(list[Expr])
  Let(id: Pattern, kind: Type?, value: ref Expr)
  Var(id: Pattern, kind: Type?, value: ref Expr?)
  ConstDecl(public: bool, id: Pattern, kind: Type?, value: ref Expr)
  FuncDecl( # effects?
    public: bool,
    id: str,
    generics: list[(id: Pattern, kind: Type?)],
    params: list[(id: Pattern, kind: Type)],
    kind: Type?,
    body: list[Expr])
  MacroDecl(
    public: bool,
    id: str,
    generics: list[(id: Pattern, kind: Type?)],
    params: list[(id: Pattern, kind: Type?)],
    kind: Type?,
    body: list[Expr])
  TypeDecl(public: bool, id: str, generics: list[str], alias: Type)
  Module(public: bool, id: str, body: list[Expr])
  Use(path: str)
  Call(id: str, params: list[Expr])
  Cond(
    branches: list[(cond: Expr, body: list[Expr])],
    else_body: list[Expr]?)
  Try(
    try_body: list[Expr],
    catches: list[(exceptions: list[str], body: list[Expr])],
    finally_body: list[Expr]?)
  Match(
    item: ref Expr,
    branches: list[(pattern: Pattern, guard: Expr?, body: list[Expr])])
  Block(id: str?, body: list[Expr])
  ConstExpr(body: list[Expr]),
  For(binding: Pattern, range: ref Expr, body: list[Expr])
  While(cond: ref Expr, body: list[Expr])
  Loop(body: list[Expr]),

pub type Type = ref union
  Never
  Int(size: uint)
  Dec(size: uint)
  Float(size: uint)
  String
  Func(from: list[Type], to: Type)
  Struct(list[(id: str, kind: Type)])
  Tuple(list[(id: str?, kind: Type)])
  Union(list[(id: str, kind: Type)])
  Class( # todo: generics
    funcs: list[(id: str, from: list[Type], to: Type?)]
    for_type: Type?
  )
  Array(size: uint, kind: Type)
  List(Type)
  Slice(Type) # todo: plus ownership
  Alias(str) # todo: params?? huh?
  Const(Type)
  Lent(Type)
  Mut(Type)
  Ref(Type)
  Refc(Type)
  Ptr(Type)

pub type Pattern = union
  Ident(str)
  Number(int), Float(float), Char(chr), String(str)
  Struct(name: str, params: list[Pattern])
  Tuple(list[Pattern])
  List(list[Pattern])

@[magic]
pub func quote(body): Expr