📄 m2_syntax.hlp
字号:
{smcl}
{* 25mar2005}{...}
{cmd:help m2 syntax}
{hline}
{* index syntax}{...}
{* index grammar}{...}
{title:Title}
{p 4 4 2}
{bf:[M-2] syntax -- Mata language grammar and syntax}
{title:Syntax}
{p 4 4 2}
The basic language syntax is
{it:istmt}
{p 4 4 2} where
{col 9}{it:istmt} :={col 25}{it:stmt}
{col 25}{cmd:function} {it:name}{cmd:(}{it:farglist}{cmd:)} {it:fstmt}
{col 25}{it:ftype} {it:name}{cmd:(}{it:farglist}{cmd:)} {it:fstmt}
{col 25}{it:ftype} {cmd:function} {it:name}{cmd:(}{it:farglist}{cmd:)} {it:fstmt}
{col 9}{it:stmt} :={col 25}{it:nothing}
{col 25}{cmd:;}{col 55}(meaning {it:nothing})
{col 25}{cmd:version} {it:number}
{col 25}{cmd:{c -(}} {it:stmt} ... {cmd:{c )-}}
{col 25}{it:exp}
{col 25}{cmd:pragma} {it:pstmt}
{col 25}{cmd:if (}{it:exp}{cmd:)} {it:stmt}
{col 25}{cmd:if (}{it:exp}{cmd:)} {it:stmt} {cmd:else} {it:stmt}
{col 25}{cmd:for (}{it:exp}{cmd:;}{it:exp}{cmd:;}{it:exp}{cmd:)} {it:stmt}
{col 25}{cmd:while (}{it:exp}{cmd:)} {it:stmt}
{col 25}{cmd:do} {it:stmt} {cmd:while (}{it:exp}{cmd:)}
{col 25}{cmd:break}
{col 25}{cmd:continue}
{col 25}{it:label}{cmd::}
{col 25}{cmd:goto} {it:label}
{col 25}{cmd:return}
{col 25}{cmd:return(}{it:exp}{cmd:)}
{col 9}{it:fstmt} :={col 25}{it:stmt}
{col 25}{it:type} {it:arglist}
{col 25}{cmd:external} {it:type} {it:arglist}
{col 9}{it:arglist} :={col 25}{it:name}
{col 25}{it:name}{cmd:()}
{col 25}{it:name}{cmd:,} {it:arglist}
{col 25}{it:name}{cmd:(),} {it:arglist}
{col 9}{it:farglist} :={col 25}{it:nothing}
{col 25}{it:efarglist}
{col 9}{it:efarglist} :={col 25}{it:felement}
{col 25}{it:felement}{cmd:,} {it:efarglist}
{col 25}{cmd:|} {it:felement}
{col 25}{cmd:|} {it:felement}{cmd:,} {it:efarglist}
{col 9}{it:felement} :={col 25}{it:name}
{col 25}{it:type} {it:name}
{col 25}{it:name}{cmd:()}
{col 25}{it:type} {it:name}{cmd:()}
{col 9}{it:ftype} :={col 25}{it:type}
{col 25}{cmd:void}
{col 9}{it:type} :={col 25}{it:eltype}
{col 25}{it:orgtype}
{col 25}{it:eltype orgtype}
{col 9}{it:eltype} :={col 25}{cmd:transmorphic}
{col 25}{cmd:string}
{col 25}{cmd:numeric}
{col 25}{cmd:real}
{col 25}{cmd:complex}
{col 25}{cmd:pointer}
{col 25}{cmd:pointer(}{it:ptrtype}{cmd:)}
{col 9}{it:orgtype} :={col 25}{cmd:matrix}
{col 25}{cmd:vector}
{col 25}{cmd:rowvector}
{col 25}{cmd:colvector}
{col 25}{cmd:scalar}
{col 9}{it:ptrtype} :={col 25}{it:nothing}
{col 25}{it:type}
{col 25}{it:type} {cmd:function}
{col 25}{cmd:function}
{col 9}{it:pstmt} :={col 25}{cmd:unset} {it:name}
{col 25}{cmd:unused} {it:name}
{col 9}{it:name} :={col 25}identifier up to 32 characters long
{col 9}{it:label} :={col 25}identifier up to 8 characters long
{col 9}{it:exp} :={col 25}expression as defined in {bf:{help m2_exp:[M-2] exp}}
{title:Description}
{p 4 4 2}
Mata is a C-like compiled-into-pseudocode language with matrix extensions
and run-time linking.
{title:Remarks}
{p 4 4 2}
Remarks are presented under the headings
{bf:Treatment of semicolons}
{bf:Types and declarations}
{bf:Void matrices}
{bf:Void functions}
{bf:Operators}
{bf:Subscripts}
{bf:Implied input tokens}
{bf:Function argument-passing convention}
{bf:Passing functions to functions}
{bf:Optional arguments}
{title:Treatment of semicolons}
{p 4 4 2}
Semicolon ({cmd:;}) is treated as a line separator. It is not required, but
it may be used to place two statements on the same physical line:
{col 9}{cmd:x = 1 ; y = 2 ;}
{p 4 4 2}
The last semicolon in the above example is unnecessary, but allowed.
{p 4 4 2}
Single statements may continue onto more than one line if the continuation is
obvious. Take obvious to mean that there is a hanging open parenthesis or
a hanging dyadic operator; e.g.,
{cmd:x = (}
{cmd:3)}
{cmd:x = x +}
{cmd: 2}
{p 4 4 2}
See {bf:{help m2_semicolons:[M-2] semicolons}} for more information.
{title:Types and declarations}
{p 4 4 2}
The {it:type} of a variable or function is described by
{it:eltype} {it:orgtype}
{p 4 4 2}
where {it:eltype} and {it:orgtype} are each one of
{it:eltype orgtype}
{hline 12} {hline 10}{cmd}
transmorphic matrix
numeric vector
real rowvector
complex colvector
string scalar
pointer{txt} {hline 10}
{hline 12}
{p 4 4 2}
For example, a variable might be {cmd:real} {cmd:scalar}, or
{cmd:complex} {cmd:matrix}, or {cmd:string} {cmd:vector}.
{p 4 4 2}
Declarations are optional. When the {it:type} of a variable or function is
not declared, it is assumed to be a {cmd:transmorphic matrix}. In particular:
{p 8 12 2}
1. {it:eltype} specifies the type of the elements.
When {it:eltype} is not specified, {cmd:transmorphic} is assumed.
{p 8 12 2}
2. {it:orgtype} specifies the organization of the elements.
When {it:orgtype} is not specified, {cmd:matrix} is assumed.
{p 4 4 2}
All {it:types} are special cases of {cmd:transmorphic} {cmd:matrix}.
{p 4 4 2}
The nesting of {it:eltypes} is
{cmd:transmorphic}
{c |}
{c TLC}{hline 15}{c +}{hline 13}{c TRC}
{c |} {c |} {c |}
{cmd:numeric string pointer}
{c |}
{c TLC}{hline 13}{c TRC}
{c |} {c |}
{cmd:real complex}
{p 4 4 2}
{it:orgtypes} amount to nothing more than a constraint on the number of
rows and columns of a matrix:
{it:orgtype} constraint
{hline 49}
{cmd:matrix} {it:r}>=0 & {it:c}>=0
{cmd:vector} {it:r}==1 & {it:c}>=0 -or- {it:r}>=0 & {it:c}==1
{cmd:rowvector} {it:r}==1 & {it:c}>=0
{cmd:colvector} {it:r}>=0 & {it:c}==1
{cmd:scalar} {it:r}==1 & {it:c}==1
{hline 49}
{p 4 4 2}
See {bf:{help m2_declarations:[M-2] declarations}}.
{title:Void matrices}
{p 4 4 2}
A matrix (vector, rowvector, or colvector) that is 0 {it:x} 0,
{it:r} {it:x} 0, or 0 {it:x} {it:c} is said to be void;
see {bf:{help m2_void:[M-2] void}}.
{p 4 4 2}
The function {cmd:J(}{it:r}{cmd:,} {it:c}{cmd:,} {it:val}{cmd:)}
returns an {it:r} {it:x} {it:c} matrix with each element containing
{it:val}; see {bf:{help mf_j:[M-5] J()}}.
{p 4 4 2}
{cmd:J()} can be used to create void matrices.
{p 4 4 2}
See {bf:{help m2_void:[M-2] void}}.
{title:Void functions}
{p 4 4 2}
Rather than {it:eltype} {it:orgtype}, a function can be declared to return
nothing by being declared to return {cmd:void}:
{cmd}void function example(matrix A)
{
real scalar i
for (i=1; i<=rows(A); i++) A[i,i] = 1
}{txt}
{p 4 4 2}
A function that returns nothing (does not include a
{cmd:return(}{it:exp}{cmd:)} statement), in fact returns {cmd:J(0, 0, .)}, and
the above function could equally well be coded
{cmd}void function example(matrix A)
{
real scalar i
for (i=1; i<=rows(A); i++) A[i,i] = 1
return(J(0, 0, .))
}{txt}
{p 4 4 2}
or
{cmd}void function example(matrix A)
{
real scalar i
for (i=1; i<=rows(A); i++) A[i,i] = 1
return(J(0,0,.))
}{txt}
{p 4 4 2}
Thus {cmd:void} too is a special case of {cmd:transmorphic} {cmd:matrix}
(it is in fact a 0 {it:x} 0 {cmd:real} {cmd:matrix}). Since declarations
are optional (but recommended both for reasons of style and for reasons
of efficiency), the above function could also be coded
{cmd}function example(A)
{
for (i=1; i<=rows(A); i++) A[i,i] = 1
}{txt}
{p 4 4 2}
See {bf:{help m2_declarations:[M-2] declarations}}.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -