Ticket #52: dts-as-expr.patch
File dts-as-expr.patch, 4.8 KB (added by hopscc, 16 years ago) |
---|
-
Source/Expr.cobra
794 794 assert _what.type 795 795 return _what.type.innerType 796 796 797 class 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 797 807 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 798 829 class IdentifierExpr 799 830 is partial 800 831 inherits NameExpr -
Source/SharpGenerator.cobra
3083 3083 sw.dedent 3084 3084 sw.write('})') 3085 3085 3086 class StringizeExpr #hops 3087 is partial 3086 3088 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 3087 3098 class IdentifierExpr 3088 3099 is partial 3089 3100 -
Source/CobraLang.cobra
93 93 if .willAutoFlush 94 94 _dest.flush 95 95 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 96 104 class Visitor 97 105 is abstract 98 106 """ -
Source/CobraParser.cobra
2732 2732 on 'OPEN_CALL' 2733 2733 assert not callName.endsWith('(') 2734 2734 args = .commaSepExprs(['RPAREN'], true, true) 2735 if callName == 'dTs' # hops 2736 return .stringize(token, args) 2735 2737 if .opStack.count and .opStack.peek == 'DOT' 2736 2738 return CallExpr(token, callName, args, true) 2737 2739 else … … 2756 2758 else 2757 2759 throw FallThroughException(token) 2758 2760 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 2759 2774 def argument as Expr 2760 2775 """ 2761 2776 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 # 17 class 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'