|
|
Label: ♦english
♦news
fu
created at Sunday, 2010-04-25, 05:40:10
fu
modified at Sunday, 2010-04-25, 05:51:59
0 Replies, 1740 Hits
A new feature has just been added to the Dao macro system,
this new feature allows defining python style
indentation sensitive syntax. It is pretty simple to do this now, an example
with explanation is available in the svn on google code:
demo/macro/scoping_by_indentation.dao
The key point is to use $IBL prefixed macro variable instead of $BL prefixed variable, to match Indented code BLock, which means the matching is stopped once a line with indentation narrower than the indentation of the first matched line. Another key point is to use indentation alignment @X marker to force all tokens followed by the same marker to have the same indentation. This is need to properly handle if-elif-else constructs. Just for convenience, that scoping_by_indentation.dao is listed here,
#{
Indentation mark: @x: where x=1,2,...,9, tokens followed with the same indentation mark must be aligned with the same indentation; Indented code block: $IBL prefixed variable matches a block of indented codes; String literal from tokens: Tokens expanded for patterns between a pair of \" or \', are concatenated into a string literal enclosed in the corresponding quotation mark. A space is inserted between to consecutive identifer tokens. #} syntax{ # if : elif : else : if @1 $EXP_1 : \[ $IBL_1 \] \{ elif @1 $EXP_2 : \[ $IBL_2 \] \} \[ else @1 : \[ $IBL_3 \] \] }as{ # if(){}elif(){}else{} if( 1 ){ # extra nesting to avoid the inside messing up with the outside if( $EXP_1 ){ \[ $IBL_1 \] } \{ elif( $EXP_2 ){ \[ $IBL_2 \] } \} \[ else{ \[ $IBL_3 \] } \] } } a = 1 b = 0 if a == 0 : io.writeln( 'python "if" works!' ) if b == 1 : io.writeln( 'b =', b ) elif a == 1 : io.writeln( 'python "if-elif" works!' ) if b == 0 : io.writeln( 'b =', b ) list1 = { 1, 2, 3 } list2 = { 'a', 'b', 'c' } for x in list1: io.writeln( x ) for y in list2: io.writeln( y ) if y == 'b': io.writeln( y ) for x in list1 , y in list2 : io.writeln( x, y ) Compiling options can be set in Dao source codes by changing member values of the global object daoConfig : daoConfig.tabspace is the number of spaces counted for a tab; daoConfig.chindent indicates if indentation should be checked. When the indentation is check, the proper indentation width is chosen as the most frequent indentation width in the source codes, and a warning is printed if the indentation width of a line is not a multiplication of the chosen indentation width. A few additional features are also implemented to enhance the macro. One is to allow creating string literals from tokens: tokens expanded for patterns between a pair of \" or \' , are concatenated into a string literal, enclosed in the corresponding quotation mark. A space is inserted between to consecutive identifer tokens. The other is to allow multiple uses of a source pattern in target syntax. This makes macro more flexible and more expressive. For example:
syntax{
var $ID_1 = $ID_2 = $EXP_1 }as{ var $ID_1 = $EXP_1; var $ID_2 = $ID_1 io.writeln( \" $ID_1 and $ID_2 are defined as the result of $EXP_1 \" ); } var a = b = math.sqrt( 2 ); io.writeln( a, b ); Executing this code will print out:
a and b are defined as the result of math.sqrt(2) 1.414214 1.414214 Source URL:
http://www.daovm.net/space/dao/thread/224
Comments
|
fu: ... I forgot to say something about the plan for the whole new year in my previous reply. Well, besides w ... (Jan.19,01:40) fu: ... Happy new dragon year (which will start from this sunday)! Actually, it was a busy month (I wish th ... (Jan.18,22:46) ybabel: What's the plan for the new year ? Hello 'vry budy :- ) happy new year (when is the new year for you Fu ?) I saw you come back and comm ... (Jan.18,18:59) |