Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions bootstrap/s2c-seed.c

Large diffs are not rendered by default.

50 changes: 19 additions & 31 deletions lib/prelude.srs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ extern def rt_heap_alloc(self: Ptr, size: I64): Ptr
extern def rt_heap_free(self: Ptr): Unit

// Memory region = effect. Open until free; values captured by a region die at free.
protocol Region:
capability Region:
state Open, Freed
start Open
end Freed
op alloc(size: I64): Ptr : Open -> Open
op free(): Unit : Open -> Freed
stop Freed
def alloc <Open> (size: I64): Ptr <Open>
def free <Open> (): Unit <Freed>
drop free

// Allocators = handlers of Region.
Expand Down Expand Up @@ -55,17 +55,14 @@ extern def rt_capture_begin(): Unit
extern def rt_capture_end(): Str

// captureBegin .. captureEnd: the prints between them are one string, not output
protocol Console:
state Live
start Live
end Live
op print(s: Str): Unit : Live -> Live
op printI64(v: I64): Unit : Live -> Live
op printChar(c: I64): Unit : Live -> Live
op eprint(s: Str): Unit : Live -> Live
op eprintI64(v: I64): Unit : Live -> Live
op captureBegin(): Unit : Live -> Live
op captureEnd(): Str : Live -> Live
capability Console:
def print(s: Str): Unit
def printI64(v: I64): Unit
def printChar(c: I64): Unit
def eprint(s: Str): Unit
def eprintI64(v: I64): Unit
def captureBegin(): Unit
def captureEnd(): Str

handler Srs.Console for Console:
def print(s: Str): Unit = rt_print(s)
Expand All @@ -76,31 +73,22 @@ handler Srs.Console for Console:
def captureBegin(): Unit = rt_capture_begin()
def captureEnd(): Str = rt_capture_end()

protocol Args:
state Live
start Live
end Live
op count(): I64 : Live -> Live
op at(i: I64): Str : Live -> Live
capability Args:
def count(): I64
def at(i: I64): Str

handler Srs.Args for Args:
def count(): I64 = rt_argc()
def at(i: I64): Str = rt_arg(i)

protocol Files:
state Live
start Live
end Live
op read(path: Str): Str : Live -> Live
capability Files:
def read(path: Str): Str

handler Srs.Files for Files:
def read(path: Str): Str = rt_read_file(path)

protocol Exit:
state Live
start Live
end Live
op exit(code: I64): Unit : Live -> Live
capability Exit:
def exit(code: I64): Unit

handler Srs.Exit for Exit:
def exit(code: I64): Unit = rt_exit(code)
Expand Down
40 changes: 30 additions & 10 deletions s2/ast.srs
Original file line number Diff line number Diff line change
Expand Up @@ -511,14 +511,27 @@ def Ast.terms(xs: Coll.List[Ast.Term]): Unit = xs match
Console.print(", ")
Ast.terms(t)

// at(x, T) is written x: T (vic 2026-11-09)
def Ast.atom(rel: Sym.Sym, ts: Coll.List[Ast.Term]): Unit =
if Str.eq(Sym.str(rel), "at") && Coll.length(ts) == 2 then ts match
case Coll.Cons(x, rest) =>
Ast.term(x)
Console.print(": ")
Ast.terms(rest)
case Coll.Nil => ()
else Ast.atomCall(rel, ts)

def Ast.atomCall(rel: Sym.Sym, ts: Coll.List[Ast.Term]): Unit =
Ast.sym(rel)
Console.print("(")
Ast.terms(ts)
Console.print(")")

def Ast.atoms(xs: Coll.List[Ast.Atom]): Unit = xs match
case Coll.Nil => ()
case Coll.Cons(a, t) =>
val Ast.Atom(_, rel, ts) = a
Ast.sym(rel)
Console.print("(")
Ast.terms(ts)
Console.print(")")
Ast.atom(rel, ts)
t match
case Coll.Nil => ()
case Coll.Cons(_, _) =>
Expand Down Expand Up @@ -658,16 +671,23 @@ def Ast.trans(xs: Coll.List[Ast.Trans]): Unit = xs match
def Ast.ops(xs: Coll.List[Ast.Op]): Unit = xs match
case Coll.Nil => ()
case Coll.Cons(o, t) =>
// each def moves by a transition named as itself: parsing the text back gives the same transitions
val Ast.Op(_, name, ps, ret, trs) = o
Ast.line(Ast.step())
Console.print("op ")
Console.print("transition ")
Ast.sym(name)
Console.print("(")
Console.print(" ")
Ast.trans(trs)
Ast.line(Ast.step())
Console.print("def ")
Ast.sym(name)
Console.print(" <s ")
Ast.sym(name)
Console.print(" t> (")
Ast.binders(ps)
Console.print("): ")
Ast.ty(ret)
Console.print(" : ")
Ast.trans(trs)
Console.print(" <t>")
Ast.ops(t)

def Ast.rules(xs: Coll.List[Ast.RuleDef]): Unit = xs match
Expand Down Expand Up @@ -720,7 +740,7 @@ def Ast.decl(d: Ast.Decl): Unit = d match
Ast.cases(cs)
case Ast.DProto(p) =>
val Ast.Proto(_, name, sts, start, ends, dropOp, ops, rules) = p
Console.print("protocol ")
Console.print("capability ")
Ast.sym(name)
Console.print(":")
Ast.line(Ast.step())
Expand All @@ -730,7 +750,7 @@ def Ast.decl(d: Ast.Decl): Unit = d match
Console.print("start ")
Ast.sym(start)
Ast.line(Ast.step())
Console.print("end ")
Console.print("stop ")
Ast.syms(ends, ", ")
Ast.ops(ops)
dropOp match
Expand Down
30 changes: 4 additions & 26 deletions s2/parse.srs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def Parse.decl(ts: Coll.List[Lex.Tok]): Parse.R[Ast.Decl] =
case Parse.Err(l, w) => Parse.Err(l, w)
case Parse.Ok(_, t1) => Parse.wrapFn(Parse.fn(t1, Parse.line(ts), Parse.FExtern))
else if Parse.isKw(ts, "enum") then Parse.enumDecl(Parse.next(ts), Parse.line(ts))
else if Parse.isKw(ts, "capability") || Parse.isKw(ts, "protocol") then Parse.proto(Parse.next(ts), Parse.line(ts))
else if Parse.isKw(ts, "capability") then Parse.proto(Parse.next(ts), Parse.line(ts))
else if Parse.isKw(ts, "handler") then Parse.handler(Parse.next(ts), Parse.line(ts))
else if Parse.isKw(ts, "trait") then Parse.traitDecl(Parse.next(ts), Parse.line(ts))
else if Parse.isKw(ts, "given") then Parse.givenDecl(Parse.next(ts), Parse.line(ts))
Expand Down Expand Up @@ -459,7 +459,7 @@ def Parse.cases(ts: Coll.List[Lex.Tok], acc: Coll.List[Ast.Case]): Parse.R[Coll.
case Parse.Ok(fs, t3) => Parse.cases(Parse.endLine(t3), Coll.Cons(Ast.Case(line, name, fs), acc))
else Parse.cases(Parse.endLine(t2), Coll.Cons(Ast.Case(line, name, Coll.Nil), acc))

// ── capability (vic 2026-11-09; protocol is its old name): items until DED (state, start, stop, drop, transition, def,
// ── capability (vic 2026-11-09): items until DED (state, start, stop, drop, transition, def,
// rule def). no state line: one state Live. a def's moves resolve to transitions once every item is read ──
enum Parse.Pr:
case Parse.Pr(states: Coll.List[Sym.Sym], start: Sym.Sym, ends: Coll.List[Sym.Sym], drop: Coll.Option[Sym.Sym], ops: Coll.List[Parse.COp], rules: Coll.List[Ast.RuleDef], rels: Coll.List[Parse.Rel])
Expand All @@ -468,12 +468,11 @@ enum Parse.Pr:
enum Parse.Rel:
case Parse.Rel(name: Sym.Sym, pairs: Coll.List[Ast.Trans])

// how a def moves its subject: <A> .. <B>, <s r t> .. <t>, or no facts (each state stays); op: the old form, resolved
// how a def moves its subject: <A> .. <B>, <s r t> .. <t>, or no facts (each state stays)
enum Parse.Moves:
case Parse.Stay
case Parse.Move(from: Sym.Sym, to: Sym.Sym)
case Parse.ByRel(line: I64, rel: Sym.Sym)
case Parse.Written(trans: Coll.List[Ast.Trans])

enum Parse.COp:
case Parse.COp(line: I64, name: Sym.Sym, params: Coll.List[Ast.Binder], ret: Ast.Ty, moves: Parse.Moves)
Expand Down Expand Up @@ -507,7 +506,6 @@ def Parse.resolveOps(os: Coll.List[Parse.COp], ss: Coll.List[Sym.Sym], rels: Col
val rt: Parse.R[Coll.List[Ast.Trans]] = mv match
case Parse.Stay => Parse.Ok(Parse.stays(ss), Coll.Nil)
case Parse.Move(a, b) => Parse.Ok(Coll.Cons(Ast.Trans(a, b), Coll.Nil), Coll.Nil)
case Parse.Written(trs) => Parse.Ok(trs, Coll.Nil)
case Parse.ByRel(l, r) => Parse.relPairs(rels, r) match
case Coll.Some(trs) => Parse.Ok(trs, Coll.Nil)
case Coll.None => Parse.Err(l, "a transition of this capability")
Expand Down Expand Up @@ -536,7 +534,7 @@ def Parse.items(ts: Coll.List[Lex.Tok], pr: Parse.Pr): Parse.R[Parse.Pr] =
Parse.ident(Parse.next(ts)) match
case Parse.Err(l, w) => Parse.Err(l, w)
case Parse.Ok(x, t1) => Parse.items(Parse.endLine(t1), Parse.Pr(ss, x, es, dr, os, rs, rels))
else if Parse.isKw(ts, "stop") || Parse.isKw(ts, "end") then
else if Parse.isKw(ts, "stop") then
Parse.nameLine(Parse.next(ts), Coll.Nil) match
case Parse.Err(l, w) => Parse.Err(l, w)
case Parse.Ok(xs, t1) => Parse.items(Parse.endLine(t1), Parse.Pr(ss, st, xs, dr, os, rs, rels))
Expand All @@ -554,12 +552,6 @@ def Parse.items(ts: Coll.List[Lex.Tok], pr: Parse.Pr): Parse.R[Parse.Pr] =
Parse.capDef(Parse.next(ts), Parse.line(ts)) match
case Parse.Err(l, w) => Parse.Err(l, w)
case Parse.Ok(o, t1) => Parse.items(Parse.endLine(t1), Parse.Pr(ss, st, es, dr, Coll.Cons(o, os), rs, rels))
else if Parse.isKw(ts, "op") then
Parse.opDecl(Parse.next(ts), Parse.line(ts)) match
case Parse.Err(l, w) => Parse.Err(l, w)
case Parse.Ok(o, t1) =>
val Ast.Op(ol, on, ops, oret, otrs) = o
Parse.items(Parse.endLine(t1), Parse.Pr(ss, st, es, dr, Coll.Cons(Parse.COp(ol, on, ops, oret, Parse.Written(otrs)), os), rs, rels))
else if Parse.isKw(ts, "rule") then
Parse.kw(Parse.next(ts), "def") match
case Parse.Err(l, w) => Parse.Err(l, w)
Expand Down Expand Up @@ -625,20 +617,6 @@ def Parse.moves(line: I64, en: Parse.Entry, ex: Coll.Option[Sym.Sym]): Parse.R[P
case Coll.Some(b) => if Sym.eq(b, t) then Parse.Ok(Parse.ByRel(line, r), Coll.Nil) else Parse.Err(line, "the exit <t> of <s r t>")
case Coll.None => Parse.Err(line, "the exit <t> of <s r t>")

// op name(params): ty : A -> B, ...
def Parse.opDecl(ts: Coll.List[Lex.Tok], line: I64): Parse.R[Ast.Op] = Parse.ident(ts) match
case Parse.Err(l, w) => Parse.Err(l, w)
case Parse.Ok(name, t1) => Parse.params(t1) match
case Parse.Err(l, w) => Parse.Err(l, w)
case Parse.Ok(ps, t2) => Parse.op(t2, ":") match
case Parse.Err(l, w) => Parse.Err(l, w)
case Parse.Ok(_, t3) => Parse.ty(t3) match
case Parse.Err(l, w) => Parse.Err(l, w)
case Parse.Ok(ret, t4) => Parse.op(t4, ":") match
case Parse.Err(l, w) => Parse.Err(l, w)
case Parse.Ok(_, t5) => Parse.trans(t5, Coll.Nil) match
case Parse.Err(l, w) => Parse.Err(l, w)
case Parse.Ok(trs, t6) => Parse.Ok(Ast.Op(line, name, ps, ret, trs), t6)

def Parse.trans(ts: Coll.List[Lex.Tok], acc: Coll.List[Ast.Trans]): Parse.R[Coll.List[Ast.Trans]] = Parse.ident(ts) match
case Parse.Err(l, w) => Parse.Err(l, w)
Expand Down
17 changes: 7 additions & 10 deletions s2/store.srs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,21 @@ extern def rt_out_commit(self: Ptr): Unit
extern def rt_store_read(dir: Str, a: I64, b: I64, c: I64, d: I64): Str

// the store's root and its entries, read (the authority of the store: SRS_STORE, else $HOME/.srs/store)
protocol Store:
state Live
start Live
end Live
op dir(): Str : Live -> Live
op load(dir: Str, a: I64, b: I64, c: I64, d: I64): Str : Live -> Live
capability Store:
def dir(): Str
def load(dir: Str, a: I64, b: I64, c: I64, d: I64): Str

handler Srs.Store for Store:
def dir(): Str = rt_store_dir()
def load(dir: Str, a: I64, b: I64, c: I64, d: I64): Str = rt_store_read(dir, a, b, c, d)

// an entry is open until commit renames it into place
protocol StoreOut:
capability StoreOut:
state Writing, Done
start Writing
end Done
op put(s: Str): Unit : Writing -> Writing
op commit(): Unit : Writing -> Done
stop Done
def put <Writing> (s: Str): Unit <Writing>
def commit <Writing> (): Unit <Done>

handler Srs.StoreFile for StoreOut:
def init(dir: Str, a: I64, b: I64, c: I64, d: I64): Ptr = rt_out_new(dir, a, b, c, d)
Expand Down
14 changes: 8 additions & 6 deletions tests/s2/ast.out
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
protocol Door:
capability Door:
state Shut, Open1, Open2, Gone
start Shut
end Gone
op knock(): I64 : Shut -> Open1, Open1 -> Open2
op leave(): Unit : Open1 -> Gone, Open2 -> Gone
stop Gone
transition knock Shut -> Open1, Open1 -> Open2
def knock <s knock t> (): I64 <t>
transition leave Open1 -> Gone, Open2 -> Gone
def leave <s leave t> (): Unit <t>
rule def twoKnocks(s, s3) = next(s, knock, s2), next(s2, knock, s3)
rule def reach(s, t) = next(s, knock, t)
rule def reach(s, t) = next(s, knock, u), reach(u, t)
Expand All @@ -13,11 +15,11 @@ handler HDoor for Door:
def knock(self: Ptr): I64 = 3
def leave(self: Ptr): Unit = rt_arena_free(self)

def knockTwice {d: Door} <at(d, s), twoKnocks(s, s3)> (): I64 <at(d, s3)> =
def knockTwice {d: Door} <d: s, twoKnocks(s, s3)> (): I64 <d: s3> =
val a = d.knock()
a + d.knock()

def knockOnce {d: Door} <at(d, Shut), reach(Shut, t), next(Shut, knock, t)> (): I64 <at(d, t)> = d.knock()
def knockOnce {d: Door} <d: Shut, reach(Shut, t), next(Shut, knock, t)> (): I64 <d: t> = d.knock()

def main(): I64 =
handle d = HDoor()
Expand Down
28 changes: 28 additions & 0 deletions tests/s2/neg/cap_seq_many.srs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// expect: error[op-not-allowed]
// a b{1,2} a c: a third b is not allowed in S3
capability Seq:
state S0, S1, S2, S3, S4, Done
start S0
stop Done
transition as S0 -> S1, S2 -> S4, S3 -> S4
transition bs S1 -> S2, S2 -> S3
transition cs S4 -> Done
def a <s as t> (): Unit <t>
def b <s bs t> (): Unit <t>
def c <s cs t> (): Unit <t>

handler HSeq for Seq:
def a(): Unit = ()
def b(): Unit = ()
def c(): Unit = ()

def main(): I64 =
handle x = HSeq()
x.a()
x.b()
x.b()
x.b()
x.a()
x.c()
0
// expect: --> tests/s2/neg/cap_seq_many.srs:24
25 changes: 25 additions & 0 deletions tests/s2/neg/cap_seq_none.srs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// expect: error[op-not-allowed]
// a b{1,2} a c: `a a c` has no b, so the second a is not allowed in S1
capability Seq:
state S0, S1, S2, S3, S4, Done
start S0
stop Done
transition as S0 -> S1, S2 -> S4, S3 -> S4
transition bs S1 -> S2, S2 -> S3
transition cs S4 -> Done
def a <s as t> (): Unit <t>
def b <s bs t> (): Unit <t>
def c <s cs t> (): Unit <t>

handler HSeq for Seq:
def a(): Unit = ()
def b(): Unit = ()
def c(): Unit = ()

def main(): I64 =
handle x = HSeq()
x.a()
x.a()
x.c()
0
// expect: --> tests/s2/neg/cap_seq_none.srs:22
25 changes: 25 additions & 0 deletions tests/s2/neg/cap_seq_open.srs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// expect: error[capability-not-ended]
// a b{1,2} a c: `a b a` stops in S4, not Done
capability Seq:
state S0, S1, S2, S3, S4, Done
start S0
stop Done
transition as S0 -> S1, S2 -> S4, S3 -> S4
transition bs S1 -> S2, S2 -> S3
transition cs S4 -> Done
def a <s as t> (): Unit <t>
def b <s bs t> (): Unit <t>
def c <s cs t> (): Unit <t>

handler HSeq for Seq:
def a(): Unit = ()
def b(): Unit = ()
def c(): Unit = ()

def main(): I64 =
handle x = HSeq()
x.a()
x.b()
x.a()
0
// expect: --> tests/s2/neg/cap_seq_open.srs:20
6 changes: 3 additions & 3 deletions tests/s2/neg/eff_flow.srs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// expect: error[op-not-allowed]
// a request is an op on the innermost capability of its protocol: Flow checks its state as for d.shut()
protocol Door:
capability Door:
state Open, Shut
start Open
end Shut
op shut(): Unit : Open -> Shut
stop Shut
def shut <Open> (): Unit <Shut>

handler Hinge for Door:
def shut(): Unit = ()
Expand Down
Loading
Loading