Wiki

Ticket #52: dts-as-expr.patch

File dts-as-expr.patch, 4.8 KB (added by hopscc, 15 years ago)
  • Source/Expr.cobra

     
    794794        assert _what.type 
    795795        return _what.type.innerType 
    796796 
     797class StringizeExpr # hops 
     798    is partial 
     799    inherits Expr 
     800    """ 
     801    Turn an expression into something else, String, other expr (?)on code gen. 
     802    """ 
     803    var _template as StringLit 
     804        # metachars [expr.str] = stringized expr,  [expr] = expression, [expr.tts] = toTechString(expr)  
     805        # default is  '[expr.str]=[expr.tts]' 
     806    var _expr as Expr 
    797807 
     808    def init(token as IToken, tmpl as StringLit, exp as Expr ) 
     809        base.init(token) 
     810        _template = tmpl  
     811        _expr = exp 
     812     
     813    def addSubFields 
     814        base.addSubFields 
     815        .addField('template', _template) 
     816        .addField('expr', _expr) 
     817     
     818    def _bindInt is override 
     819        if not _type 
     820            _type = .compiler.stringType 
     821        base._bindInt 
     822 
     823    def _bindImp is override 
     824        if not _type 
     825            _type = .compiler.stringType 
     826        base._bindImp 
     827        _expr.bindImp 
     828             
    798829class IdentifierExpr 
    799830    is partial 
    800831    inherits NameExpr 
  • Source/SharpGenerator.cobra

     
    30833083            sw.dedent 
    30843084            sw.write('})') 
    30853085 
     3086class StringizeExpr #hops 
     3087    is partial 
    30863088 
     3089    def writeSharpDef(sw as SharpWriter) 
     3090        base.writeSharpDef(sw) 
     3091        sep = ', ' 
     3092        sw.write('CobraCore.Tracer.StrExpando(') 
     3093        _template.writeSharpDef(sw, false) 
     3094        sw.write('[sep][Utils.sharpStringLiteralFor(_expr.toCobraSource)][sep]') # stringized expr 
     3095        _expr.writeSharpDef(sw, false) #expr 
     3096        sw.write(')') 
     3097 
    30873098class IdentifierExpr 
    30883099    is partial 
    30893100 
  • Source/CobraLang.cobra

     
    9393                if .willAutoFlush 
    9494                    _dest.flush 
    9595 
     96        def strExpando(template as String, exprStr as String, expr as Object) as String 
     97            exprTTS = CobraImp._techStringMaker.makeString(expr) 
     98            outStr = template.replace(r'[expr.str]', exprStr) 
     99            outStr = outStr.replace(r'[expr]', expr.toString) 
     100            outStr = outStr.replace(r'[expr.tts]', exprTTS) 
     101            return outStr 
     102 
     103 
    96104    class Visitor 
    97105        is abstract 
    98106        """ 
  • Source/CobraParser.cobra

     
    27322732            on 'OPEN_CALL' 
    27332733                assert not callName.endsWith('(') 
    27342734                args = .commaSepExprs(['RPAREN'], true, true) 
     2735                if callName == 'dTs'            # hops 
     2736                    return .stringize(token, args) 
    27352737                if .opStack.count and .opStack.peek == 'DOT' 
    27362738                    return CallExpr(token, callName, args, true) 
    27372739                else 
     
    27562758            else 
    27572759                throw FallThroughException(token)                
    27582760 
     2761    def stringize(token as IToken, args as List<of Expr>) as Expr # hops 
     2762        if args.count == 1 
     2763            expr = args[0] 
     2764            strTok = token.copy('STRING_SINGLE', r'[expr.str]=[expr.tts]') 
     2765            strTok.value = strTok.text 
     2766            tmpl = StringLit(strTok) 
     2767        else if args.count == 2 
     2768            tmpl = args[0] to StringLit 
     2769            expr = args[1] 
     2770        else 
     2771            .throwError('dts pseudo method needs either 1 or 2 args') 
     2772        return StringizeExpr(token, tmpl, expr) 
     2773                     
    27592774    def argument as Expr 
    27602775        """ 
    27612776        In support of .callExpr and others, for when it's legal to write `out x` and such. 
  • Tests/200-misc/999-dts.cobra

     
     1# examples of dts pseudo function  
     2 
     3# dts(strTemplate as String, expr) as String 
     4# OR 
     5# dts(expr) as String 
     6#   which is equivalent to dts(r'[expr.str]=[expr.tts]', expr)  
     7# 
     8#   arg strTemplate is string containing text and placeholders for  
     9#   various representations of the given expression 
     10# Supported placeholders are  
     11# [expr] for the evaluated expression (as a string) 
     12# [expr.str] for the expression 'stringized' - the text of the expr as a string 
     13# [expr.tts] for the expression passed through CobraCore.toTechString 
     14# 
     15#   arg expr is any arbitrary cobra expression 
     16# 
     17class DTS 
     18    def main is shared 
     19        a=99 
     20        b=a+100-47 
     21        r = dTs(b) 
     22        #print r 
     23        assert r == 'b=152' 
     24         
     25        r = dTs("valueIs[b]") 
     26        #print r 
     27        assert r == r'"valueIs[b]"='+"'valueIs152'" 
     28 
     29        r = dTs(a-44+3) 
     30        #print r 
     31        assert r == '((a - 44) + 3)=58' 
     32 
     33        x= {'a':100} 
     34        r = dTs(x) 
     35        #print r 
     36        assert r == "x=Dictionary<of String, int>{'a': 100}" 
     37         
     38        # explicitly specify display template 
     39        r = dTs(r'expr is [expr.str] value=[expr]', b-a) 
     40        #print r 
     41        assert r=='expr is (b - a) value=53'