Golang : 实验性表情符号或表情图标编程语言

262 阅读1分钟

因此,当我在Facebook上发表了我的实验性Jawi编程语言代码后,有人问我,是否有可能用其他人类语言,如Kristang或emojis/情感图标进行编码?答案是YES。下面是对以前的代码的改编,但字典略有不同。

在字典中,Golang的保留词被映射到一些随机选择的emojis,用于这个演示。在运行下面这段代码之前,你可以把[bikini-icon]、[shit-icon]等替换成合适的表情符号,或者把代码复制到play.golang.org/p/GLlL8v_Xd…

在这里,你可以开始了!


 package main

 import (
  "fmt"
  "strings"
  "text/scanner"
 )

 func main() {

  emojisGolang := map[string]string{}

  dictionary := `var,[bikini-icon]
  Println,[ghost-icon]
  const,[devil-icon]
  package,[briefcase-icon]
  import,[smiley-icon]
  main,[shit-icon]
  func,[candy-icon]`

  lines := strings.Split(dictionary, "\n")

  // build the dictionary
  for _, line := range lines {

 if len(line) > 0 {
 split := strings.Split(line, ",")
 golang := split[0]
 emojis := split[1]

 emojisGolang[emojis] = golang
 }
  }

  //test

  emojisCode := `
  [briefcase-icon] [shit-icon]
  
  [smiley-icon] (
 "fmt"
  )
  
  [bikini-icon] txt = "هاي دنيا ! 世界汝好 ! Hello World ! " 
  
  [candy-icon] [shit-icon]() {
 fmt.[ghost-icon](txt)
  }`

  codeReader := strings.NewReader(emojisCode)

  var scn scanner.Scanner
  scn.Init(codeReader)
  scn.Whitespace ^= 1<<'\t' | 1<<'\n' | 1<<'\r' | 1<<' ' // don't skip tabs and new lines

  for tok := scn.Scan(); tok != scanner.EOF; tok = scn.Scan() {

 switch tok {
 case '\n':
 fmt.Println()
 case '\t':
 fmt.Print(" ")
 default:
 if emojisGolang[scn.TokenText()] != "" {
 fmt.Print(emojisGolang[scn.TokenText()])
 } else {
 fmt.Print(scn.TokenText())
 }
 }

  }
  fmt.Println()
 }

你可以在以下网站上运行该代码:play.golang.org/p/GLlL8v_Xd…

参考资料:

socketloop.com/tutorials/g…