编程语言编译器【免费下载链接】rockstarHome of the Rockstar programming language项目地址https://gitcode.com/gh_mirrors/ro/rockstar点击查看免费下载本篇指南围绕 Rockstar 编程语言的Conversions and Mutations转换与变更机制展开如何在就地in-place与保留源值两种模式下拆分字符串cut/split/shatter、连接数组join/unite/gather、在字符串与数字、Unicode 字符之间互转cast/burn以及用turn up/turn down/turn around完成数值取整、字符串大小写转换与反转。读完本篇你将掌握这组修复者式内置操作的完整语法、参数形态并能对照 Starship 引擎的解析器PEG 文法与运行时RockstarEnvironment理解其底层实现原理。变更操作的四形态语法Rockstar 中大部分内置操作mutation都可以就地修改传入的变量或把结果存入目标变量、保持源值不变。官方文档定义了四种形态形态语义Modify X就地修改XModify X into Y保留X完整把结果存入YModify X with Z就地修改X使用可选参数ZModify X into Y with Z用Z修改X结果存入Y这里的Modify对应三类具体操作split拆分、join连接、cast转换。with子句用于传入分隔符、进制等可选参数。这一语法在引擎的 PEG 文法中定义为mutation规则rockstar.peg它显式支持五种组合op source into target using modifier、op source using modifier into target、op source into target、op source using modifier、op source。注意文法是using与with同义——事实上在 Rockstar 中with本身就是算术plus加法的别名rockstar.peg因此用参数 Z 操作与加上 Z在词法上是同一回事。mutation会构造引擎中的 Mutation 语句节点携带Operator操作符、Expression源表达式、可选的Target目标变量与Modifier可选参数对应的操作符枚举定义在 Operator.csSplit、Join、Cast。PEG 中的mutator规则rockstar.peg负责把关键字映射到枚举值。拆分字符串cut / split / shatter要拆分字符串使用cut变更操作别名split与shatterPEG 规则见 rockstar.pegsplit cut !letter / split !letter / shatter !letter。官方示例split-strings.rock展示了不带分隔符与带分隔符两种拆分行为Split a,b,c into the array Shout the array (prints: [ a, ,, b, ,, c ]) Shout the array 0 (prints: 5) Split a,b,c into the array with , Shout the array (prints: [ a, b, c ]) Shout the array 0 (prints: 3) My life says heartbreak Cut my life into pieces Shout pieces (prints: [ h, e, a, r, t, b, r, e, a, k ]) Shout pieces 0 (prints: 10)三个要点不带with分隔符时字符串被拆成单个字符数组。a,b,c变成[ a, ,, b, ,, c ]长度 5heartbreak变成 10 个单字符元素。这也是把字符串碎成字符的最直接方式。带with ,时按给定分隔符切分a,b,c得到[ a, b, c ]长度 3。Shout the array 0输出数组长度——数组参与算术运算时按长度折算5与3就是两组结果的长度这与文档 09 章数组作为标量的行为一致。运行时实现位于 RockstarEnvironment.cs 的Split方法源值必须是字符串否则抛出Only strings can be split.分隔符取modifier?.ToStrïng() ?? Strïng.Empty——缺省为空字符串。底层字符串拆分逻辑在 Strïng.cs分隔符为空时逐个字符转为Strïng元素否则调用 .NET 的Value.Split(delimiter.Value)按分隔符切分。write-strings.rock示例codewithrockstar.com/examples/10-conversions/write-strings.rock与上面核心演示一致可在测试套件 Starship/Rockstar.Test/programs/examples/10-conversions/ 中找到完全相同的运行用例。连接数组join / unite / gather要连接数组使用join变更操作别名unite或gatherPEG 规则见 rockstar.peg。官方示例join-arrays.rockLet the string be abcde Split the string into the tokens Join the tokens with ; Print the tokens (prints: a;b;c;d;e) The input says hey now hey now now Split the input into the words with Unite the words into the output with ! Print the output with ! (prints: hey! now! hey! now! now!) Gather the words into the output with - Print the output (prints: hey-now-hey-now-now)要点先Split得到数组再Join the tokens with ;就地把tokens从数组变成连接后的字符串a;b;c;d;e。Unite the words into the output with ! 演示into形态源数组words保持不变连接结果写入output。Gather the words into the output with -再次演示别名gather与into组合输出hey-now-hey-now-now。连接字符串可通过任意值充当引擎会调用其字符串表示——测试 ArräyTests.cs 中JoinArrayWorks验证了a.Join(null)得到123、a.Join(Booleän.True)得到1true2true3。运行时实现位于 RockstarEnvironment.cs源值必须是数组否则抛出Cant join something which is not an array.然后委托给 Arräy.cs 的Join方法——用String.Join(joiner?.ToStrïng().Value ?? , ...)把每个元素的字符串形式拼接起来缺省连接符为空字符串。类型转换cast / burncast函数别名burnPEG 规则见 rockstar.peg是 Rockstar 的类型修复者把字符串解析成数字或把数字转成对应码点的 Unicode 字符。官方示例cast.rockLet X be 123.45 Shout X X (prints: 123.45123.45) Cast X with 10 Shout X X (prints: 246.9) Let X be FF Cast X with 16 (cast using base 16) Shout X (prints: 255) Cast 65 into result. Shout result (prints: A) Cast result. Shout result (prints: 65) Cast 1046 into result. Shout result (prints: Ж) Guitar is 1F3B8. Cast it with 16. Cast it. Shout it. (prints: ) The string is 32 Cast the string into the codes Write the codes at 0 (writes: 51) Write the codes at 1 (writes: 50)逐段解读字符串 → 数字Cast X with 10用十进制解析123.45于是X X从字符串拼接123.45123.45变成数值相加246.9。Cast X with 16按十六进制把FF解析为255。with子句在这里就是进制基数。数字 → Unicode 字符Cast 65 into result得到ACast 1046 into result得到西里尔字母ЖU0416Cast it with 16. Cast it.先用十六进制把1F3B8解析成数字0x1F3B8 127928再Cast it不带参数把数字转回字符输出吉他 emoji——这说明cast支持超出基本多文种平面BMP的 Unicode 码点。字符串 → 字符码数组字符串带into目标但不带with进制时cast的行为变为拆成字符码32被转换成数组[51, 50]3的码点是 51、2的码点是 50可通过Write the codes at 0这类下标写入逐一取出。引擎实现RockstarEnvironment.cs的Cast方法按源值类型分派源为字符串时若提供了实现IHaveANumber的进制参数调用Numbër.Parse(s, numberBase)否则调用s.ToCharCodes()。源为数字时new Strïng(Char.ConvertFromUtf32((int) n.Value))用 .NET 的ConvertFromUtf32处理包括增补平面在内的任意码点。Numbër.ParseNumbër.cs在基数为 10 时直接Decimal.Parse其他进制走BaseToDecimalNumbër.cs基于0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ字符表逐位累乘累加且支持小数点后的进制小数位。ToCharCodesStrïng.cs按长度分派空串返回空数组单字符返回单个Numbër多字符返回Numbër数组。另外官方文档还强调了一条通用规则想把任意值转成字符串把它加到空字符串上即可。这正是 Rockstar 的拼接语义——with即加法字符串参与加法时表现为拼接cast.rock中X X输出123.45123.45即为佐证。取整与字符串变换turn up / turn down / turn around数值取整与字符串大小写转换由turn完成PEG 关键字见 rockstar.peg数字Turn up向上取整朝正无穷方向turn down向下取整朝负无穷方向turn round四舍五入到最近整数Bonnie Tyler 爱好者会高兴地发现turn around是turn round的合法别名PEG 规则around around !letter / round !letter见 rockstar.peg。字符串turn up转成UPPERCASEturn down转成lowercase而turn the string around会反转字符串——正如文档所说不然你还能指望turn the string around做什么呢语法上turn variable direction与turn direction variable两种语序都合法。turn 操作就地修改变量并把取整/变换后的值作为语句返回值。官方示例rounding.rockX is 1.2 Turn up X Shout X (prints: 2) X is 1.2 Turn down X Shout X (prints: 1) The radios like a siren... the fire has just begun. Turn up the radio Say the radio (prints: 16) My dad says Motley Crue is lame Turn it up. Shout it (prints: MOTLEY CRUE IS LAME) Turn it down. Shout it (prints: motley crue is lame) Turn it around. Shout it (prints: emal si eurc yeltom)注意The radios like a siren...是诗意数字poetic number字面量The radios like a siren... the fire has just begun.解析为16每个词的长度按诗意数字规则折算Turn up the radio对16取整仍为16。后半段展示了字符串的三种变换Motley Crue is lame→MOTLEY CRUE IS LAME→motley crue is lame→emal si eurc yeltom反转。PEG 文法把turn拆成三条规则rockstar.pegfloordown、ceilup、math_roundaround/round每条都同时接受两种语序统一构造 Rounding 语句节点其方向由 Round.cs 的枚举Up / Down / Nearest表示。运行时RockstarEnvironment.cs按变量当前值类型分派数字Round.Down → Math.Floor、Round.Up → Math.Ceiling、Round.Nearest → Math.Round字符串Round.Down → ToLower()、Round.Up → ToUpperInvariant()、Round.Nearest →将字符序列反转其他类型抛出Cant apply rounding to variable ...运行时错误。最后SetVariable(r.Variable, rounded)写回变量完成就地修改。变更操作与代词pronoun的联动变更操作有个容易被忽略的细节它会把目标变量登记为后续代词的指代对象。示例mutations-update-pronouns.rockX is 65 Y is 66 Cast X shout it (prints: A)Cast X之后直接shout it就能打印A因为cast就地修改了X65 →A并让代词it指向X。引擎中的机制在 RockstarEnvironment.cs执行Mutation时若目标变量存在则SetLocal写入结果并且只要目标不是代词本身就调用UpdatePronounSubject(m.Target)RockstarEnvironment.cs更新代词主体代词读写时通过QualifyPronounRockstarEnvironment.cs解析到最近一次登记的主体变量若从未赋值则抛出You must assign a variable before using a pronoun。这保证了split、join、cast之后都能自然衔接it、them等代词。引擎执行链路与测试验证把四类操作的运行时执行汇总到一条调用链RockstarEnvironment.Execute对语句做模式匹配Mutation m Mutation(m)、Rounding r Rounding(r)见 RockstarEnvironment.cs→Mutation方法对源与修饰符求值后按Operator分派到Join/Split/Cast→ 最终落到Strïng.Split、Strïng.ToCharCodes、Numbër.Parse、Arräy.Join、Char.ConvertFromUtf32等值类型方法Rounding则按Round枚举映射到Math.Floor/Ceiling/Round与字符串的大小写/反转操作。测试验证方面仓库在 Starship/Rockstar.Test/programs/examples/10-conversions/ 下提供了与官方示例一一对应的可运行用例split-strings.rock、join-arrays.rock、cast.rock、rounding.rock、write-strings.rock、mutations-update-pronouns.rock通过RockstarTestBase.RunProgram驱动TestEnvironment执行并断言输出值类型层面另有 ArräyTests.cs 直接验证Join的边界行为无连接符、字符串连接符、布尔值作连接符。文档示例本身也大量采用(prints: ...)注释标明预期输出可直接当作回归测试来阅读。小结Conversions and Mutations 是 Rockstar 的幕后修复者cut/split/shatter拆字符串、join/unite/gather连数组、cast/burn在字符串、数字与 Unicode 字符间互转、turn完成取整与大小写/反转且全部支持into保留源值与with可选参数两种扩展形态。理解这组操作既是编写摇滚风数据处理程序的基础也是深入 Starship 引擎运行时RockstarEnvironment的分派逻辑与 PEG 文法mutation/rounding/mutator规则的绝佳入口。赞分享编程语言编译器【免费下载链接】rockstarHome of the Rockstar programming language项目地址https://gitcode.com/gh_mirrors/ro/rockstar点击查看免费下载相关推荐Rockstar类型转换与字符串操作split、join、cast的终极实用指南Rockstar类型转换与字符串操作split、join、cast的终极实用指南 想要在Rockstar编程语言中轻松处理数据类型转换和字符串操作吗这编程语言编译器Go 类型安全转换实战spf13/cast 库完整指南与源码级解析Go 类型安全转换实战spf13/cast 库完整指南与源码级解析 本指南以 scan4all 仓库中 vendor 目录下引入的 spf13/cast ht网络安全漏洞扫描渗透测试应用安全StarRocks CAST 类型转换函数完全指南语法、ARRAY 转换与加载场景实战StarRocks CAST 类型转换函数完全指南语法、ARRAY 转换与加载场景实战 导读 CAST 是 StarRocks 中最基础也最高频的类型转换函数据库OLAP数据仓库大数据湖仓一体数据分析上一篇终极指南如何在Windows上免费安装ViGEmBus虚拟游戏控制器驱动下一篇为什么这个浏览器插件能让你的微信网页版复活创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
阅读完成 · 觉得有帮助?