Dino compiler: language, formatter, language server, docs, editor support
Compiler in Rust (lexer, parser, checker, C codegen), maps, chars, dino fmt, dino lsp (diagnostics, formatting, outline, definition, hover, completion, signature help), docs, examples, VS Code/Vim files. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3e2779f447
commit
eaf9567a36
117 files changed
+19970
No files matched your search
@@ -0,0 +1,93 @@
|
||||
enum Shape {
|
||||
Circle(r: float)
|
||||
Rect(w, h: float)
|
||||
Point
|
||||
|
||||
func area(): float {
|
||||
return match self {
|
||||
Circle(r) => 3.14159 * r * r
|
||||
Rect(w, h) => w * h
|
||||
Point => 0
|
||||
}
|
||||
}
|
||||
|
||||
func name(): string {
|
||||
return match self {
|
||||
Circle(_) => "circle"
|
||||
Rect(_, _) => "rectangle"
|
||||
Point => "point"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum Color {
|
||||
Red
|
||||
Green
|
||||
Blue
|
||||
}
|
||||
|
||||
func describe(c: Color): string {
|
||||
return match c {
|
||||
Color.Red => "warm"
|
||||
Color.Green => "natural"
|
||||
Color.Blue => "cool"
|
||||
}
|
||||
}
|
||||
|
||||
func classify(n: int): string {
|
||||
return match n {
|
||||
0 => "zero"
|
||||
1 => "one"
|
||||
_ => "many"
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
var shapes = [Shape.Circle(1.5), Shape.Rect(2, 3), Shape.Point]
|
||||
for s in shapes {
|
||||
print(s.name(), s.area())
|
||||
}
|
||||
print(shapes)
|
||||
|
||||
var c = Color.Green
|
||||
print(c, describe(c), c == Color.Green, c != Color.Blue)
|
||||
|
||||
for i in 0..4 {
|
||||
print(i, classify(i))
|
||||
}
|
||||
|
||||
// match as a statement can use blocks and loop control
|
||||
var total = 0.0
|
||||
for s in shapes {
|
||||
match s {
|
||||
Circle(r) => {
|
||||
total += r
|
||||
}
|
||||
Rect(w, _) => {
|
||||
if w > 100 {
|
||||
break
|
||||
}
|
||||
total += w
|
||||
}
|
||||
_ => continue
|
||||
}
|
||||
print("so far", total)
|
||||
}
|
||||
|
||||
// bool and string matches
|
||||
var flag = true
|
||||
print(match flag {
|
||||
true => "yes"
|
||||
false => "no"
|
||||
})
|
||||
var cmd = "stop"
|
||||
match cmd {
|
||||
"go" => print("going")
|
||||
"stop" => print("stopping")
|
||||
other => print("unknown:", other)
|
||||
}
|
||||
|
||||
// the zero value of an enum is its first case
|
||||
var z: Shape
|
||||
print(z, Color.Red == (Color.Red))
|
||||
}
|
||||
Reference in new issue
Block a user