Repository navigation
Expand file tree
/
Copy path037_language_tour.a7
More file actions
142 lines (119 loc) · 3.45 KB
/
Copy path037_language_tour.a7
File metadata and controls
142 lines (119 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// A7 compact language tour
//
// Read this file top-to-bottom for a compact tour of the current stable
// language surface. Every section is runnable and verified through Zig.
//
// Guard rails: source recursion is rejected, `fall` is allowed in any
// non-final match arm and is an error in the last one, and tagged union tag
// workflows are not implemented.
io :: import "std/io"
BinaryOp :: fn(i32, i32) i32
Point :: struct {
x: i32
y: i32
}
Scale :: enum {
Small
Large
}
Value :: union {
int_val: i32
flag_val: bool
}
add :: fn(a: i32, b: i32) i32 {
ret a + b
}
area :: fn(point: Point) i32 {
ret point.x * point.y
}
bump :: fn(value: ref i32) {
value += 1
}
HeapBox :: struct {
value: i32
}
apply :: fn(op: BinaryOp, a: i32, b: i32) i32 {
ret op(a, b)
}
main :: fn() {
io.println("=== A7 Language Tour ===")
// --- Declarations ---
//
// Declarations use := for mutable inference and : Type = for explicit type.
// Top-level constants and functions use ::.
name: string = "A7"
version := 1
io.println("{} tour v{}", name, version)
// --- Arrays and slices ---
//
// Arrays are fixed-size. Slices are views over arrays or other slices.
numbers: [5]i32 = [3, 5, 8, 13, 21]
middle := numbers[1..4]
total := 0
for value in middle {
total += value
}
io.println("slice total = {}", total)
// Indexed iteration exposes a usize index. Keep indexes as usize unless
// there is a real fixed-width data reason to convert them.
index_total: usize = 0
indexed_value_total := 0
for index, value in numbers {
index_total += index
indexed_value_total += value
}
io.println("indexed totals = {} {}", index_total, indexed_value_total)
// --- Structs ---
//
// Struct literals use named fields.
point := Point{x: 6, y: 7}
io.println("area = {}", area(point))
// --- Enums and match ---
//
// Enums pair naturally with exhaustive match statements.
scale := Scale.Large
match scale {
case Scale.Small: {
io.println("scale = small")
}
case Scale.Large: {
io.println("scale = large")
}
}
// --- Unions ---
//
// Untagged unions currently support one named field at construction time.
payload := Value{int_val: 26}
io.println("payload int = {}", payload.int_val)
// --- References and heap values ---
//
// Ref parameters are passed with ordinary lvalue arguments. Heap
// allocation uses new/del and field access narrows through checked refs.
// A declared local lowers to a mutable `var`, so it can be passed by
// reference directly; the split form below is style, not a requirement.
counter: i32 = 41
bump(counter)
io.println("counter = {}", counter)
heap_value := new HeapBox
if heap_value == nil {
io.println("allocation failed")
ret
}
defer del heap_value
heap_value.value = 99
io.println("heap = {}", heap_value.value)
// --- Function values ---
//
// Function types can be named and passed around.
op: BinaryOp = add
io.println("apply = {}", apply(op, 10, 32))
// --- Loops instead of recursion ---
//
// Source recursion is rejected. Use loops, stacks, queues, or worklists.
countdown := 3
while countdown > 0 {
io.println("countdown {}", countdown)
countdown -= 1
}
io.println("done")
}