Code Block

Read(690) Label: code block,

In esProc cell types, we learned about the types of cells in esProc, including constant cell, calculation cell, executable cell and comment cell. Similarly, there are types of code blocks into which code is written: calculation block, assignment block, executable block and comment block.

Calculation block

In the following section Long statements, we’ll introduce long statements and sub-statements. Instead of =, the strings in their master cells begin with ==, which tells esProc to handle the code in the block with such a master cell as a single, inseparable statement. The program will ignore other cells in the block after it executes the statement as required. For example:

 

A

B

C

1

==demo.query(

"select NAME as CITY,

 

2

 

STATEID as STATE from CITIES")

 

3

==A1.select(??)

=demo.query(

"select * from STATES where STATEID=?",

4

 

 

STATE)

5

 

if left(B3.ABBR,1)=="N"

>STATE=B3.NAME

6

 

 

=true

7

 

else

=false

In the above code, the first two lines hold a long statement with A1 being the master cell. It selects the desired cities as follows:

The 3~7 lines contain a sub-statement with A3 being the master cell. It selects from CITIES the cities whose home states’ abbreviations begin with "N" and assigns the full names of the states to their STATE fields. Here’s A3’s result:

This block of statement beginning with "==" is also called as a calculation block, which is actually a calculation statement. A single line holding a long statement or a sub-statement can also be treated as a calculation block.

The master cell of a code block is its starting cell. Now let’s look at which cells a code block can cover.

A code block covers the row holding the master cell and the following one or more consecutive rows, if any, where the cells in both the master cell column and the column(s), if any, to the left are blank. This indicates the code block can extend until a row where any cell in the master cell column or a column to the left is non-blank appears. From that row on, the cellset is outside this code block.

 

A

B

C

D

1

 

 

 

 

2

 

>Code begin

 

 

3

 

 

 

 

4

 

 

 

 

5

 

 

 

 

6

Not null

Not null

 

 

In the above cellset, suppose B2 is a code block’s master cell. The cells in the green area are blank, thus rows from the second to the fifth constitute the master cell’s code block. The code block ends before row 6 since neither A6 nor B6 in the red area is blank, no matter it is the code cell, calculation cell, executable cell, constant cell or comment cell. 

A code block can not only hold a single statement, it can also perform a loop procedure or a branch instruction by putting the correponding statement in the master cell. For example:

 

A

B

C

1

=demo.query("select NAME as CITY, STATEID as STATE from CITIES")

[]

 

2

for A1

=demo.query("select * from STATES where STATEID=?",A2.STATE)

 

3

 

if left(B2.ABBR,1)=="N"

>A2.STATE=B2.NAME

4

 

 

>B1=B1|A2

The above code blocks don’t contain long statements or sub-statements. The code block with A2 being the master cell executes a loop and the one with B3 being the master cell executes an instruction of judgment. B1 also gets the cities whose home states’ abbreviations begin with "N".

However, the code blocks under discussion are not the cases where a statement is used to introduce various instructions, but those cases where a statement is written in multiple cells according to sequential cells rule and thus should be executed as a whole. The rule allows the a statement written in a calculation cell or an executable cell to automatically move on to the next cell when certain characters, including ",", ";" and "(", are entered until no such characters appear or the statement reaches the end.

Assignment block and executable block

The double equal sign == at the head of a calculation block has the similar function to that in a calculation cell. If a code block contains an expression returning no results, the string in its master cell will begin with the double greater-than symbol >> and the expression written according to the sequential cells rule will be executed as a single statement. For example:

 

A

B

C

1

==demo.query("select NAME as CITY,

POPULATION from CITIES")

 

2

>>C1=A1.select(

left(CITY,1):"C",

 

3

 

POPULATION>500000:true)

 

The long statement in A2 doesn’t return a result. It selects cities whose names begin with "C" and whose population is greater than 500,000 and assigns the result to C1. Here’s C1’s result:

The code block for value assignment is called as an assignment block.

Some other long statements are used to execute certain instructions. For example:

 

A

B

1

=connect@e("demo")

=A1.query("select * from EMPLOYEE")

2

>A1.execute("drop table EMPLOYEE1")

 

3

>>A1.execute("create table EMPLOYEE1(

EID int,FULLNAME varchar(30),

4

 

GENDER varchar(10))")

5

>>A1.execute(B1,

"insert into EMPLOYEE1(EID,

6

 

FULLNAME,GENDER) values(?,?,?)",

7

 

EID,NAME+" "+SURNAME, GENDER)

8

=A1.query("select * from EMPLOYEE1")

>A1.close()

In this cellset, A3’s code block creates a table – EMPLOYEE1- in the database. A5’s code block inserts records into EMPLOYEE1 using B1’s data. After that A8’s query result is as follows:

The code blocks defined by A3 and A5 are called as execution blocks. Both the execution block and assignment block are classified according to their functions.

Comment block

If the string in a cell starts with //, the code block with this cell being the master cell holds comments and it is thus called as a comment block. esProc will skip the whole comment block when encountering it. For example:

 

A

B

1

//comment

1.note...

2

 

2.note...

3

 

3.note...

4

 

=1+1

5

=1+1

 

Within the code block whose master cell is A1, all cells, including B4 of course, are naturally treated as comment cells, without the need of being marked with a slash / in the front as in a comment cell. The non-blank A5 marks the end of the code block, and the expression in A5 will be parsed and computed normally.

Different from a calculation block, an assignment block and an executable block, a comment block contains no expression for execution, so it doesn’t need the sequential cells rule.