Skip to content

Commit 7f0c5aa

Browse files
committed
Add local parser additions for core
1 parent 9155b3c commit 7f0c5aa

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package effekt
2+
package core
3+
package syntax
4+
5+
class Parser
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package effekt
2+
package core
3+
package syntax
4+
5+
type Id = String
6+
7+
sealed trait Tree
8+
9+
case class ModuleDecl(
10+
path: String,
11+
includes: List[String],
12+
declarations: List[Declaration],
13+
externs: List[Extern],
14+
definitions: List[Toplevel],
15+
exports: List[Id]
16+
) extends Tree
17+
18+
/**
19+
* Toplevel data and interface declarations
20+
*/
21+
enum Declaration extends Tree {
22+
def id: Id
23+
24+
case Data(id: Id, tparams: List[Id], constructors: List[Constructor])
25+
case Interface(id: Id, tparams: List[Id], properties: List[Property])
26+
}
27+
export Declaration.*
28+
29+
case class Constructor(id: Id, fields: List[Field]) extends Tree
30+
case class Field(id: Id, tpe: ValueType) extends Tree
31+
case class Property(id: Id, tpe: BlockType) extends Tree
32+
33+
enum FeatureFlag extends Tree {
34+
case NamedFeatureFlag(id: String)
35+
case Default
36+
}
37+
38+
/**
39+
* FFI external definitions
40+
*/
41+
enum Extern extends Tree {
42+
case Def(id: Id, tparams: List[Id], cparams: List[Id], vparams: List[ValueParam], bparams: List[BlockParam], ret: ValueType, body: ExternBody)
43+
case Include(featureFlag: FeatureFlag, contents: String)
44+
}
45+
sealed trait ExternBody extends Tree
46+
object ExternBody {
47+
case class StringExternBody(featureFlag: FeatureFlag, contents: Template[Pure]) extends ExternBody
48+
case class Unsupported(err: util.messages.EffektError) extends ExternBody
49+
}
50+
51+
enum Toplevel {
52+
def id: Id
53+
54+
case Def(id: Id, block: Block)
55+
case Val(id: Id, tpe: ValueType, binding: core.Stmt)
56+
}
57+
58+
59+
/**
60+
* Expressions (with potential IO effects)
61+
*
62+
* - [[DirectApp]]
63+
* - [[Pure]]
64+
*/
65+
sealed trait Expr extends Tree
66+
67+
// invariant, block b is {io}.
68+
case class DirectApp(b: Block.BlockVar, targs: List[ValueType], vargs: List[Pure], bargs: List[Block]) extends Expr
69+
70+
enum Pure extends Expr {
71+
72+
case ValueVar(id: Id)
73+
74+
case Literal(value: Any)
75+
76+
case PureApp(b: Block.BlockVar, targs: List[ValueType], vargs: List[Pure])
77+
78+
case Make(data: ValueType.Data, tag: Id, targs: List[ValueType], vargs: List[Pure])
79+
80+
case Box(b: Block)
81+
}
82+
export Pure.*
83+
84+
enum Block extends Tree {
85+
case BlockVar(id: Id)
86+
case BlockLit(tparams: List[Id], cparams: List[Id], vparams: List[ValueParam], bparams: List[BlockParam], ret: ValueType, body: Stmt)
87+
case Unbox(pure: Pure)
88+
case New(impl: Implementation)
89+
}
90+
export Block.*
91+
92+
case class ValueParam(id: Id, tpe: ValueType)
93+
case class BlockParam(id: Id, tpe: BlockType)
94+
95+
enum Stmt extends Tree {
96+
case Def(id: Id, block: Block, body: Stmt)
97+
case Let(id: Id, tpe: ValueType, binding: Expr, body: Stmt)
98+
case Return(expr: Pure)
99+
case Val(id: Id, tpe: ValueType, binding: Stmt, body: Stmt)
100+
case App(callee: Block, targs: List[ValueType], vargs: List[Pure], bargs: List[Block])
101+
case Invoke(callee: Block, method: Id, targs: List[ValueType], vargs: List[Pure], bargs: List[Block])
102+
case If(cond: Pure, thn: Stmt, els: Stmt)
103+
case Match(scrutinee: Pure, clauses: List[(Id, BlockLit)], default: Option[Stmt])
104+
case Region(body: BlockLit)
105+
case Alloc(id: Id, init: Pure, region: Id, body: Stmt)
106+
case Var(ref: Id, init: Pure, capture: Id, body: Stmt)
107+
case Get(id: Id, ref: Id, body: Stmt)
108+
case Put(ref: Id, value: Pure, body: Stmt)
109+
case Reset(body: Block.BlockLit)
110+
case Shift(prompt: BlockVar, body: BlockLit)
111+
case Resume(k: BlockVar, body: Stmt)
112+
case Hole()
113+
}
114+
export Stmt.*
115+
116+
117+
case class Implementation(interface: BlockType.Interface, operations: List[Operation]) extends Tree
118+
119+
case class Operation(name: Id, tparams: List[Id], cparams: List[Id], vparams: List[ValueParam], bparams: List[BlockParam], body: Stmt)
120+
121+
122+
/**
123+
* Types
124+
*/
125+
sealed trait Type
126+
127+
enum ValueType extends Type {
128+
case Var(name: Id)
129+
case Data(name: Id, targs: List[ValueType])
130+
case Boxed(tpe: BlockType, capt: Captures)
131+
}
132+
133+
enum BlockType extends Type {
134+
case Function(tparams: List[Id], cparams: List[Id], vparams: List[ValueType], bparams: List[BlockType], result: ValueType)
135+
case Interface(name: Id, targs: List[ValueType])
136+
}

0 commit comments

Comments
 (0)