Changeset 2009 for proto


Ignore:
Timestamp:
Apr 9, 2012, 12:35:00 PM (7 years ago)
Author:
cameron
Message:

pablo.match() initial implemenation, still needs pablo.lookahead()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • proto/Compiler/pablo.py

    r2004 r2009  
    5050    self.generic_visit(e)
    5151    return ast.Assign([e.target], ast.BinOp(e.target, e.op, e.value))
     52
     53
     54#
     55#  Translating pablo.match(marker, str)
     56#  Incremental character match with lookahead
     57#
     58CharNameMap = {'[' : 'LBrak', ']' : 'RBrak', '{' : 'LBrace', '}' : 'LBrace', '(' : 'LParen', ')' : 'RParen', \
     59              '!' : 'Exclam', '"' : 'DQuote', '#' : 'Hash', '$' : 'Dollar', '%' : 'PerCent', '&': 'RefStart', \
     60              "'" : 'SQuote', '*': 'Star', '+' : 'Plus', ',' : 'Comma', '-' : 'Hyphen', '.' : 'Dot', '/' : 'Slash', \
     61              ':' : 'Colon', ';' : 'Semicolon', '=' : 'Equals', '?' : 'QMark', '@' : 'AtSign', '\\' : 'BackSlash', \
     62              '^' : 'Caret', '_' : 'Underscore', '|' : 'VBar', '~' : 'Tilde', ' ' : 'SP', '\t' : 'HT', '\m' : 'CR', '\n' : 'LF'}
     63
     64def GetCharName(char):
     65        if char >= 'a' and char <= 'z' or char >= 'A' and char <= 'Z': return 'letter_' + char
     66        elif char >= '0' and char <= '9': return 'digit_' + char
     67        else: return CharNameMap[char]
     68
     69def MkCharStream(char):
     70        return mkQname('lex', GetCharName(char))
     71
     72def MkLookAheadExpr(v, i):
     73        return mkCall(mkQname('pablo', 'LookAhead'), [v, ast.Num(i)])
     74
     75def CompileMatch(match_var, string_to_match):
     76        expr = mkCall('simd_and', [match_var, MkCharStream(string_to_match[0])])
     77        for i in range(1, len(string_to_match)):
     78                expr = mkCall('simd_and', [expr, MkLookAheadExpr(MkCharStream(string_to_match[i]), i)])
     79        return expr
     80
     81class StringMatchCompiler(ast.NodeTransformer):
     82  def xfrm(self, t):
     83    return self.generic_visit(t)
     84  def visit_Call(self, callnode):
     85    if is_BuiltIn_Call(callnode,'match', 2):
     86        ast.dump(callnode)
     87        assert isinstance(callnode.args[0], ast.Str)
     88        string_to_match = callnode.args[0].s
     89        match_var = callnode.args[1]
     90        expr = mkCall('simd_and', [match_var, MkCharStream(string_to_match[0])])
     91        for i in range(1, len(string_to_match)):
     92                expr = mkCall('simd_and', [expr, MkLookAheadExpr(MkCharStream(string_to_match[i]), i)])
     93        return expr
     94    else: return callnode
     95
     96
    5297
    5398#
     
    700745                stream_function.initializations = StreamInitializations().xfrm(node)
    701746               
     747                StringMatchCompiler().xfrm(node)
    702748                AugAssignRemoval().xfrm(node)
    703749                Bitwise_to_SIMD().xfrm(node)
     
    771817                       
    772818  def xfrm_block_stmts(self):
     819    StringMatchCompiler().xfrm(self.main_node)
    773820    AugAssignRemoval().xfrm(self.main_node)
    774821    Bitwise_to_SIMD().xfrm(self.main_node)
Note: See TracChangeset for help on using the changeset viewer.