Skip to content

Commit 326b359

Browse files
committed
update spec
1 parent 36db982 commit 326b359

File tree

1 file changed

+246
-23
lines changed

1 file changed

+246
-23
lines changed

SPEC.md

Lines changed: 246 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,28 +64,17 @@ Identifiers name program entities such as variables and types. An identifier is
6464
## Keywords
6565

6666
```
67-
bool
6867
break
69-
byte
7068
continue
7169
const
7270
else
7371
for
7472
function
75-
if
76-
import
73+
if
74+
import
7775
return
78-
string
79-
uint8
80-
uint16
81-
uint32
82-
uint64
83-
uint128
84-
uint256
85-
table
86-
union
87-
var
88-
vector
76+
range
77+
var
8978
```
9079

9180
## Operators and punctuation
@@ -245,6 +234,10 @@ function(a, _ uint32, z uint64) bool
245234

246235
# Complex Types
247236

237+
## Array types
238+
239+
TODO
240+
248241
## Vector types
249242
An vector is a numbered sequence of elements of a single type, called the element type. The number of elements is called the length of the vector and is never negative.
250243

@@ -256,6 +249,9 @@ ElementType = Type .
256249

257250
The length is part of the vector's type; it must evaluate to a non-negative constant representable by a value of type int. The length of vector a can be discovered using the built-in function size. The elements can be addressed by integer indices 0 through len(a)-1. Vector types are always one-dimensional but may be composed to form multi-dimensional types.
258251

252+
## Struct
253+
254+
TODO
259255

260256
## Table types
261257

@@ -297,10 +293,30 @@ TODO
297293

298294
## Predeclared identifiers
299295

296+
The following identifiers are implicitly declared in the universe block
297+
298+
```
299+
Types:
300+
bool byte string
301+
uint8 uint16 uint32 uint64 uint128 uint256
302+
303+
Constants:
304+
true false
305+
306+
Zero value:
307+
null
308+
309+
Functions:
310+
append len max min
311+
```
312+
300313
TODO
301314

302315
## Constant declarations
303316

317+
```
318+
const a, b, c = 3, 4, "foo" // a = 3, b = 4, c = "foo", untyped integer and string constants
319+
```
304320
TODO
305321

306322
## Type declarations
@@ -317,7 +333,26 @@ VarSpec = IdentifierList ( Type [ "=" ExpressionList ] | "=" ExpressionList
317333
```
318334

319335
```
320-
var i uin8
336+
var i uint8
337+
var i uint16
338+
var i uint32
339+
var i uint64
340+
var i uint128
341+
var i uint256
342+
343+
var i byte
344+
345+
```
346+
347+
A short variable declaration uses the syntax:
348+
349+
```
350+
ShortVarDecl = IdentifierList ":=" ExpressionList .
351+
```
352+
353+
```
354+
i, j := 0, 10
355+
f := func() int { return 7 }
321356
```
322357

323358
## Function declarations
@@ -332,7 +367,14 @@ FunctionBody = Block .
332367

333368
If the function's signature declares result parameters, the function body's statement list must end in a terminating statement.
334369

335-
370+
```
371+
func min(x uint8, y uint8) uint8{
372+
if x < y {
373+
return x
374+
}
375+
return y
376+
}
377+
```
336378

337379
# Expressions
338380

@@ -363,41 +405,222 @@ A qualified identifier accesses an identifier in a different package, which must
363405
math.Sin // denotes the Sin function in package math
364406
```
365407

408+
## Index expressions
409+
410+
A primary expression of the form
411+
412+
```
413+
a[x]
414+
```
415+
366416
# Statements
367417

368-
if
369-
for
370-
break
371-
continue
372-
return
418+
## Assignment statements
373419

420+
An assignment replaces the current value stored in a variable with a new value specified by an expression. An assignment statement may assign a single value to a single variable, or multiple values to a matching number of variables.
421+
422+
```
423+
Assignment = ExpressionList assign_op ExpressionList .
424+
425+
assign_op = [ add_op | mul_op ] "=" .
426+
```
427+
428+
Each left-hand side operand must be addressable, a map index expression, or (for = assignments only) the blank identifier. Operands may be parenthesized.
429+
430+
```
431+
x = 1
432+
a[i] = 23
433+
```
434+
435+
```
436+
a, b = b, a // exchange a and b
437+
438+
x := []int{1, 2, 3}
439+
i := 0
440+
i, x[i] = 1, 2 // set i = 1, x[0] = 2
441+
442+
i = 0
443+
x[i], i = 2, 1 // set x[0] = 2, i = 1
444+
445+
x[0], x[0] = 1, 2 // set x[0] = 1, then x[0] = 2 (so x[0] == 2 at end)
446+
447+
x[1], x[3] = 4, 5 // set x[1] = 4, then panic setting x[3] = 5.
448+
449+
type Point struct { x, y int }
450+
var p *Point
451+
x[2], p.x = 6, 7 // set x[2] = 6, then panic setting p.x = 7
452+
453+
i = 2
454+
x = []int{3, 5, 7}
455+
for i, x[i] = range x { // set i, x[2] = 0, x[0]
456+
break
457+
}
458+
// after this loop, i == 0 and x is []int{3, 5, 3}
459+
```
460+
## If statements
461+
462+
"If" statements specify the conditional execution of two branches according to the value of a boolean expression. If the expression evaluates to true, the "if" branch is executed, otherwise, if present, the "else" branch is executed.
463+
464+
```
465+
IfStmt = "if" [ SimpleStmt ";" ] Expression Block [ "else" ( IfStmt | Block ) ] .
466+
```
467+
468+
```
469+
if x > max {
470+
x = max
471+
}
472+
```
374473

375-
TODO
474+
475+
## For statements
476+
477+
A "for" statement specifies repeated execution of a block. There are three forms: The iteration may be controlled by a single condition, a "for" clause, or a "range" clause.
478+
479+
```
480+
for a < b {
481+
a *= 2
482+
}
483+
```
484+
485+
```
486+
for i := 0; i < 10; i++ {
487+
f(i)
488+
}
489+
```
490+
491+
```
492+
var a [10]string
493+
for i, s := range a {
494+
// type of i is int
495+
// type of s is string
496+
// s == a[i]
497+
g(i, s)
498+
}
499+
500+
```
501+
502+
## Break statements
503+
504+
A "break" statement terminates execution of the innermost "for" statement within the same function.
505+
506+
```
507+
BreakStmt = "break" [ Label ] .
508+
```
509+
510+
```
511+
for i = 0; i < n; i++ {
512+
for j = 0; j < m; j++ {
513+
if a[i][j] == nil {
514+
state = Error
515+
break OuterLoop
516+
} else if a[i][j] == item {
517+
state = Found
518+
break OuterLoop
519+
}
520+
}
521+
}
522+
```
523+
524+
## Continue statements
525+
526+
A "continue" statement begins the next iteration of the innermost enclosing "for" loop by advancing control to the end of the loop block. The "for" loop must be within the same function.
527+
528+
```
529+
ContinueStmt = "continue" [ Label ] .
530+
```
531+
532+
```
533+
for y, row := range rows {
534+
for x, data := range row {
535+
if data == endOfRow {
536+
continue RowLoop
537+
}
538+
row[x] = data + bias(x, y)
539+
}
540+
}
541+
```
542+
543+
## Return statements
544+
545+
A "return" statement in a function F terminates the execution of F, and optionally provides one or more result values. Any functions deferred by F are executed before F returns to its caller.
546+
547+
```
548+
ReturnStmt = "return" [ ExpressionList ] .
549+
```
550+
551+
In a function without a result type, a "return" statement must not specify any result values.
552+
553+
```
554+
func noResult() {
555+
return
556+
}
557+
```
376558

377559
# Built-in functions
378560

561+
Built-in functions are predeclared. They are called like any other function but some of them accept a type instead of an expression as the first argument.
562+
563+
The built-in functions do not have standard Go types, so they can only appear in call expressions; they cannot be used as function values.
564+
379565
TODO
380566

567+
## Length
568+
569+
The built-in functions len take arguments of various types and return a result of type int. The implementation guarantees that the result always fits into an int.
570+
571+
```
572+
Call Argument type Result
573+
574+
len(s) string type string length in bytes
575+
[n]T, *[n]T array length (== n)
576+
[]T slice length
577+
578+
```
579+
580+
## Min and max
581+
582+
The built-in functions min and max compute the smallest—or largest, respectively—value of a fixed number of arguments of ordered types. There must be at least one argument.
583+
584+
The same type rules as for operators apply: for ordered arguments x and y, min(x, y) is valid if x + y is valid, and the type of min(x, y) is the type of x + y (and similarly for max). If all arguments are constant, the result is constant.
585+
586+
```
587+
var x, y int
588+
m := min(x) // m == x
589+
m := min(x, y) // m is the smaller of x and y
590+
m := max(x, y, 10) // m is the larger of x and y but at least 10
591+
var s []string
592+
_ = min(s...) // invalid: slice arguments are not permitted
593+
t := max("", "foo", "bar") // t == "foo" (string kind)
594+
```
595+
381596
# Packages
382597

598+
TODO
383599

384600
# Program initialization and execution
385601

602+
TODO
386603

387604
# Errors
388605

606+
TODO
389607

390608
# Misc
391609

610+
TODO
392611

393612
## tx
394613

614+
TODO
395615

396616
## debug
397617

398618
Support limited print function. Formatting is not support.
399619

400620
## cell
401621

622+
TODO
402623

403624
# Appendix
625+
626+
TODO

0 commit comments

Comments
 (0)