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,68 @@
|
||||
// Lists are values: assigning or passing copies them.
|
||||
struct Inventory {
|
||||
items: []string
|
||||
gold: int
|
||||
|
||||
func add(item: string) {
|
||||
items.push(item)
|
||||
}
|
||||
|
||||
func count(): int {
|
||||
return items.len()
|
||||
}
|
||||
}
|
||||
|
||||
func add_one(xs: []int): []int {
|
||||
xs.push(1)
|
||||
return xs
|
||||
}
|
||||
|
||||
func first(xs: []int): int {
|
||||
return xs[0]
|
||||
}
|
||||
|
||||
func main() {
|
||||
var a = [1, 2, 3]
|
||||
var b = a
|
||||
b[0] = 99
|
||||
b.push(4)
|
||||
print(a, b)
|
||||
|
||||
// a function that changes its parameter works on its own copy
|
||||
var c = add_one(a)
|
||||
print(a, c)
|
||||
print(first(a), first(c))
|
||||
|
||||
// structs holding lists copy deeply
|
||||
var inv = Inventory{[], 10}
|
||||
inv.add("sword")
|
||||
var saved = inv
|
||||
inv.add("shield")
|
||||
saved.gold = 0
|
||||
print(inv)
|
||||
print(saved)
|
||||
print(inv.count(), saved.count())
|
||||
|
||||
// nested lists
|
||||
var grid = [[1, 2], [3, 4]]
|
||||
var snapshot = grid
|
||||
grid[0][1] = 20
|
||||
grid[1].push(5)
|
||||
print(grid)
|
||||
print(snapshot)
|
||||
|
||||
// looping over a list while changing it walks the original
|
||||
var xs = [1, 2, 3]
|
||||
for x in xs {
|
||||
xs.push(x * 10)
|
||||
}
|
||||
print(xs)
|
||||
|
||||
// the loop variable is a copy too
|
||||
var ys = [1, 2, 3]
|
||||
for y in ys {
|
||||
y += 100
|
||||
print(y)
|
||||
}
|
||||
print(ys)
|
||||
}
|
||||
Reference in new issue
Block a user