I am going through the Go source code of ast.go at here,and there are 3 types of interfaces that are Expression,Statement and Declaration. But only with the source code I couldn't figure out the difference between them.What I could figure out is that expression results in a object that could be assigned or compared or used as parameter,while statements are some flow control like if-else or for loop. But I found some definitions like
// An IncDecStmt node represents an increment or decrement statement.
IncDecStmt struct {
X Expr
TokPos token.Pos // position of Tok
Tok token.Token // INC or DEC
}
shouldn't it be a expression?I feel confused how to distinguish expressions and statements,are there any rules?
In programming language terminology, an “expression” is a combination of values and functions that are combined and interpreted by the compiler to create a new value, as opposed to a “statement” which is just a standalone unit of execution and doesn't return anything.
An expression is something that returns a value, whereas a statement does not. The Big Deal between the two is that you can chain expressions together, whereas statements cannot be chained. Sure statements can be chained.
Variable declarations and assignments, such as those in the previous section, are statements, as are the basic language structures like conditionals and loops. Expressions describe values; an expression is evaluated to produce a result, to be used as part of another expression or in a statement.
Statements represent an action or command e.g print statements, assignment statements. Expression is a combination of variables, operations and values that yields a result value. Save this answer.
The Golang spec uses those terms:
The IncDecStmt
is specified as
IncDecStmt = Expression ( "++" | "--" ) .
The "++" and "--" statements increment or decrement their operands by the untyped constant 1.
It uses an expression, but remains a statement (don't produce a new value).
Note: an untyped constant is when you declare a constant without explicitly mentioning its type:
i := 0 # int8? uint8? int16? ...
An untyped constant has a default type which is the type to which the constant is implicitly converted in contexts where a typed value is required, for instance, in a short variable declaration such as where there is no explicit type.
The default type of an untyped constant is
bool
,rune
,int
,float64
,complex128
orstring
respectively, depending on whether it is a boolean, rune, integer, floating-point, complex, or string constant.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With