Changeset 51d4040 in mainline for contrib/arch/hadlbppp.py


Ignore:
Timestamp:
2009-09-16T23:22:20Z (15 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
f1fb1d1
Parents:
cf7b3e0
Message:

add support for 'alternative' statement in BP
implement interface protocol repetition

File:
1 edited

Legend:

Unmodified
Added
Removed
  • contrib/arch/hadlbppp.py

    rcf7b3e0 r51d4040  
    3535
    3636# TODO:
    37 #  - alternative token
    38 #  - iface protocol repetition
    3937#  - interface inheritance
    4038
     
    130128        return True
    131129
    132 def preproc_bp(name, tokens):
     130def tentative_bp(name, tokens):
    133131        "Preprocess tentative statements in Behavior Protocol"
    134132       
     
    153151                                if (level == 0):
    154152                                        result.append("(")
    155                                         result.extend(preproc_bp(name, tokens[start:(i - 1)]))
     153                                        result.extend(tentative_bp(name, tokens[start:(i - 1)]))
    156154                                        result.append(")")
    157155                                        result.append("+")
     
    162160                                        print "%s: Syntax error in tentative statement" % name
    163161                        else:
    164                                 print "%s: Unexpected token in tentative statement" % name
     162                                print "%s: Expected '{' for tentative statement" % name
    165163                else:
    166164                        result.append(tokens[i])
     
    170168        return result
    171169
     170def alternative_bp(name, tokens):
     171        "Preprocess alternative statements in Behavior Protocol"
     172       
     173        result = []
     174        i = 0
     175       
     176        while (i < len(tokens)):
     177                if (tokens[i] == "alternative"):
     178                        if ((i + 1 < len(tokens)) and (tokens[i + 1] == "(")):
     179                                i += 2
     180                                reps = []
     181                               
     182                                while ((i < len(tokens)) and (tokens[i] != ")")):
     183                                        reps.append(tokens[i])
     184                                        if ((i + 1 < len(tokens)) and (tokens[i + 1] == ";")):
     185                                                i += 2
     186                                        else:
     187                                                i += 1
     188                               
     189                                if (len(reps) >= 2):
     190                                        if ((i + 1 < len(tokens)) and (tokens[i + 1] == "{")):
     191                                                i += 2
     192                                               
     193                                                start = i
     194                                                level = 1
     195                                               
     196                                                while ((i < len(tokens)) and (level > 0)):
     197                                                        if (tokens[i] == "{"):
     198                                                                level += 1
     199                                                        elif (tokens[i] == "}"):
     200                                                                level -= 1
     201                                                       
     202                                                        i += 1
     203                                               
     204                                                if (level == 0):
     205                                                        first = True
     206                                                       
     207                                                        for rep in reps[1:]:
     208                                                                retokens = []
     209                                                                for token in tokens[start:(i - 1)]:
     210                                                                        parts = token.split(".")
     211                                                                        if ((len(parts) == 2) and (parts[0] == reps[0])):
     212                                                                                retokens.append("%s.%s" % (rep, parts[1]))
     213                                                                        else:
     214                                                                                retokens.append(token)
     215                                                               
     216                                                                if (first):
     217                                                                        first = False
     218                                                                else:
     219                                                                        result.append("+")
     220                                                               
     221                                                                result.append("(")
     222                                                                result.extend(alternative_bp(name, retokens))
     223                                                                result.append(")")
     224                                                       
     225                                                        if (i < len(tokens)):
     226                                                                result.append(tokens[i])
     227                                                else:
     228                                                        print "%s: Syntax error in alternative statement" % name
     229                                        else:
     230                                                print "%s: Expected '{' for alternative statement body" % name
     231                                else:
     232                                        print "%s: At least one pattern and one replacement required for alternative statement" % name
     233                        else:
     234                                print "%s: Expected '(' for alternative statement head" % name
     235                else:
     236                        result.append(tokens[i])
     237               
     238                i += 1
     239       
     240        return result
     241
    172242def split_bp(protocol):
    173243        "Convert Behavior Protocol to tokens"
     
    176246
    177247def extend_bp(name, tokens, iface):
    178         "Add interface to incoming messages"
    179        
    180         result = []
     248        "Convert interface Behavior Protocol to generic protocol"
     249       
     250        result = ["("]
    181251        i = 0
    182252       
     
    198268                i += 1
    199269       
     270        result.append(")")
     271        result.append("*")
     272       
    200273        return result
    201274
     
    227300        "Parse Behavior Protocol"
    228301       
    229         tokens = preproc_bp(name, tokens)
     302        tokens = tentative_bp(name, tokens)
     303        tokens = alternative_bp(name, tokens)
    230304       
    231305        indent = base_indent
Note: See TracChangeset for help on using the changeset viewer.