Crate valkyrie_ast
source ·Expand description
§The AST Nodes of Valkyrie Language
教你一个编程语言, 高亮的时候使用 scala 的高亮
这个语言是这样定义类的
# 定义一个类 A
class A { }
# 定义一个类 B 继承 A
class B(A) { }
你觉得多继承应该怎么写?
这个语言中这样定义代数效应
class Logging {
level: Integer
message: String
effect debug(s: String): Self / Pure {
new Self { level: 0, message: s }
}
effect print(s: String): Self / Pure {
new Self { level: 1, message: s }
}
effect error(s: String): Self / Pure {
new Self { level: 2, message: s }
}
effect fatal(s: String): Self / Pure {
new Self { level: 3, message: s }
}
}
使用 raise 激发代数效应, 使用 try catch 语句处理代数效应.
try {
raise Logging::debug("hello")
}
.catch {
case Logging(level, message):
std::io::std_err().lock().write(message)
}
这段代码应该怎么编译
if a {
yield 1
print 2
}
yield 2
let yield1 = {
print(2)
if_break()
}
let if_break = {
yield2()
}
let yield2 = {
ret()
}
类型签名部分分为 Type / Effect
两部分, 两者都可以省略, Type 的默认值为 Unit, Effect 的默认值为 Pure.
Modules§
Structs§
#module∷name.variant(args) { ... } modifiers
f(a + b, c: d, ..e) { a + b }
#annotation mut this: null
a + b, c: d, ..e
bind <- [a, b, **]
@[module∷name.function(args), module∷name.function2(args)] <CAPTURE>
module∷name.variant(args) { CAPTURE }
- An expression node with binary operator and two expressions
package∷module∷name
@br if a > 0
, MIRclass Name(Super): Trait {}
a := Named { a: b, c: d }
object.{ lambda(args), ... }
class Name(Super): Trait {}
new stack Type⦓G⦔(args) { body }
object: Trait { ... }
- always equivalent to a statement that returns
( )
, and cannot be used as anrvalue
. - Documentation list
domain { field; method(); domain {} }
a.method.1
[Asynchronous<T>, Logging]
- Helper function to format the body of an if statement
Name = 0x00
- Environment for expression parsing
- The ast node for an expression
extends path::A: Debug {}
field: Type = default
- a number that encodes special semantics
for ref a, mut b in {...}
for ... in ... if ... #label {...}
"string formatter \n\r {expr:args} \u{1234} "
class Name(Super): Trait {}
fun name(): ReturnType / [EffectType]
A⦓T⦔, A⟨T⟩, A::<T>
⁜label.context.1
vow T {} fun f() { return T }
guard a > 0 { ... }
- A node representing a identifier.
#macro mod id
a > 0 then { ... }
if a {1} else if b {2} else if c {3} else {4}
Soma(a) | Success { value :a } := expr
path as alias
import package∷module.*
group.path { item1, item2 }
- A resolved import item
import package::module::path
import "external"
@jmp 1 if a > 0
, MIR※label.context.1
lambda(args) { ... }
$, $1, $x
let mut pattern = expression
.match { when Some(a): a, else: 0}.catch { when IoError: (a), else: 0}
.match { when Some(a): a, else: 0}.catch { when IoError: (a), else: 0}
#attribute modifier Trait::method(): Return / Effect { ... }
public static final synchronized class Main {}
package∷module∷name
namespace std.math
null, nil
, type of null value- A lossless representation of the number literal
- An operator node with kind and range
%1, %%1
, the number of the referencemicro f(t: Type = default)
orclass F⦓T: Trait = Default⦔
- A pattern match branch
case Some(a) | Success { value :a } if a > 0:
type Integer | Decimal:
when a > 0:
- A pattern match statement block
@module∷name(args) { ... }
- The top level elements in script mode.
[0, [], [:], [::]]
{ a; b; c }
- The valid terms in a class body.
handler"text"
, a string literal with a handler.- Pure text of a string literal.
array⁅index0⁆, array[index1]
switch { when a > 0: a, else: 0}
{% end %}
{# any text as comment #}
{% expression %}
t"template string with {slot + 1:X?}"
{% ... %}
trait name: Debug {}
try T? { ... }.catch { ... }
(tuple, ), (named: tuple, expression)
bind := module∷Named(ref a, mut b, ..c)
- Temporary node for use in the parser
- An expression node with unary operator and base expression
union Name(Super): Trait {}
case Some(a) | Success { value: a }:
let mut pattern = expression
Variant { }
while cond {...} otherwise {...}
Enums§
- The key of the argument
- A namepath is a series of identifiers separated by dots.
class A { }, structure V { }
- Valid terms in the class statements
{ 1: x, p: y, [a, b]: c, **list, ***dict }
A valid term in a collector- Valid terms in the class statements
- The control flow keywords
a.method.1
- The base expression type
- The flags kind
- Valid terms in the flags statement
\r, \u{00FF}, {{, }}
micro function(args), macro procedure(args)
- Call with static method
guard <CONDITION> then { ... } else { ... }
- The name of import items
- The resolve result of import
- A valid import term of the import statement
goto ^label
$, $0, $1, $x
- Variants of all logical operators.
.match {}.catch {}
- The kind of namespace
- The kind of the parameter node
T: Type = type_expression
- All valid branches of a pattern match statement
- The literal of array
[index], ⁅start : end : step⁆
- The top level elements in script mode.
{%=-_ ... _-=%}
- The kind of trait
- The valid terms in a trait body.
- The receiver type of the tuple literal
- A valid term in the union declaration.
- All builtin operator in valkyrie language
while true
- The kind of while loop, including
while
anduntil