☰
Rockstar 语言类型转换与变更操作完全指南:split / join / cast / turn 的源码级实战
2026/10/8 1:56:54 网站建设 项目流程
  • 编程语言
  • 编译器

【免费下载链接】rockstar

Home 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就地修改X
Modify X into Y保留X完整,把结果存入Y
Modify X with Z就地修改X,使用可选参数Z
Modify 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.cs:Split、Join、Cast。PEG 中的mutator规则(rockstar.peg)负责把关键字映射到枚举值。

拆分字符串:cut / split / shatter

要拆分字符串,使用cut变更操作,别名split与shatter(PEG 规则见 rockstar.peg:split = '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)

三个要点:

  1. 不带with分隔符时,字符串被拆成单个字符数组。"a,b,c"变成[ "a", ",", "b", ",", "c" ],长度 5;heartbreak变成 10 个单字符元素。这也是把字符串"碎成字符"的最直接方式。
  2. 带with ","时,按给定分隔符切分,"a,b,c"得到[ "a", "b", "c" ],长度 3。
  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或gather(PEG 规则见 rockstar.peg)。官方示例(join-arrays.rock):

Let 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)

要点:

  1. 先Split得到数组,再Join the tokens with ";"就地把tokens从数组变成连接后的字符串a;b;c;d;e。
  2. Unite the words into the output with "! "演示into形态:源数组words保持不变,连接结果写入output。
  3. Gather the words into the output with "-"再次演示别名gather与into组合,输出hey-now-hey-now-now。
  4. 连接字符串可通过任意值充当,引擎会调用其字符串表示——测试 ArräyTests.cs 中JoinArrayWorks验证了a.Join(null)得到"123"、a.Join(Booleän.True)得到"1true2true3"。

运行时实现位于 RockstarEnvironment.cs:源值必须是数组(否则抛出"Can't join something which is not an array."),然后委托给 Arräy.cs 的Join方法——用String.Join(joiner?.ToStrïng().Value ?? "", ...)把每个元素的字符串形式拼接起来,缺省连接符为空字符串。

类型转换:cast / burn

cast函数(别名burn,PEG 规则见 rockstar.peg)是 Rockstar 的类型"修复者":把字符串解析成数字,或把数字转成对应码点的 Unicode 字符。官方示例(cast.rock):

Let 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得到A;Cast 1046 into result得到西里尔字母Ж(U+0416);Cast 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.Parse(Numbër.cs)在基数为 10 时直接Decimal.Parse,其他进制走BaseToDecimal(Numbër.cs):基于"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"字符表逐位累乘累加,且支持小数点后的进制小数位。ToCharCodes(Strï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转成UPPERCASE,turn down转成lowercase;而turn the string around会反转字符串——正如文档所说,"不然你还能指望turn the string around做什么呢?"

语法上turn <variable> <direction>与turn <direction> <variable>两种语序都合法。turn 操作就地修改变量,并把取整/变换后的值作为语句返回值。官方示例(rounding.rock):

X is 1.2 Turn up X Shout X (prints: 2) X is 1.2 Turn down X Shout X (prints: 1) The radio's 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 radio's like a siren...是诗意数字(poetic number)字面量:The radio's 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.peg):floor(down)、ceil(up)、math_round(around/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 →将字符序列反转;
  • 其他类型抛出"Can't apply rounding to variable ..."运行时错误。

最后SetVariable(r.Variable, rounded)写回变量,完成就地修改。

变更操作与代词(pronoun)的联动

变更操作有个容易被忽略的细节:它会把目标变量登记为后续代词的指代对象。示例(mutations-update-pronouns.rock):

X is 65 Y is 66 Cast X shout it (prints: A)

Cast X之后直接shout it就能打印A,因为cast就地修改了X(65 →'A'),并让代词it指向X。

引擎中的机制在 RockstarEnvironment.cs:执行Mutation时,若目标变量存在则SetLocal写入结果,并且只要目标不是代词本身就调用UpdatePronounSubject(m.Target)(RockstarEnvironment.cs)更新代词主体;代词读写时通过QualifyPronoun(RockstarEnvironment.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规则)的绝佳入口。

  • 编程语言
  • 编译器

【免费下载链接】rockstar

Home of the Rockstar programming language

项目地址:https://gitcode.com/gh_mirrors/ro/rockstar
点击查看免费下载

相关推荐

上一篇:终极指南:如何在Windows上免费安装ViGEmBus虚拟游戏控制器驱动
下一篇:为什么这个浏览器插件能让你的微信网页版"复活"?

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询