Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

2025-03_cld_ctx-gen.md 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. Title: CLD: Context generation
  2. Date: 2025-03-11 00:00
  3. Category: News
  4. Slug: cld_ctx-gen
  5. Lang: en
  6. ![splash][splash]
  7. # Context generation
  8. In February I've updated the **C**ross-**l**anguage **d**ialect (CLD) translator to
  9. generate Context out of YML. The generated Contexts have already been
  10. used for the following projects:
  11. * CLD (the CLD translator generated its own Context)
  12. * LHA
  13. * PSKOV
  14. I never really took time to explain what Context is, so here's
  15. a very short explanation: Context is very close to
  16. [Store in Redux][store]. I'm afraid this is not yet the time
  17. to explain Context in detail because I don't yet have a good
  18. argument why you need Context. Once that time comes I do it.
  19. Now let's get back to the Context generation. Here's how LHA's YML for Context looks like: ([entities.yml][entities]):
  20. ```
  21. # Application state context
  22. Context:
  23. type: context
  24. fields:
  25. # Command line arguments
  26. arguments: [String]
  27. consoleOutput: String
  28. defaultDir: String
  29. didLaunch: Bool
  30. dir: String
  31. httpDefaultPort: Int
  32. httpLaunch: Bool
  33. httpPort: Int
  34. httpReply: String
  35. httpRequest: NetRequest
  36. ```
  37. CLD translator converts it to the following code in Kotlin ([entities.kt][entities-result]):
  38. ```
  39. // Application state context
  40. data class Context(
  41. // Command line arguments
  42. var arguments: Array<String> = arrayOf(),
  43. var consoleOutput: String = "",
  44. var defaultDir: String = "",
  45. var didLaunch: Boolean = false,
  46. var dir: String = "",
  47. var httpDefaultPort: Int = 0,
  48. var httpLaunch: Boolean = false,
  49. var httpPort: Int = 0,
  50. var httpReply: String = "",
  51. var httpRequest: NetRequest = NetRequest(),
  52. override var recentField: String = "",
  53. ): CLDContext {
  54. override fun <T> field(name: String): T {
  55. if (name == "arguments") {
  56. return arguments as T
  57. } else if (name == "consoleOutput") {
  58. return consoleOutput as T
  59. } else if (name == "defaultDir") {
  60. return defaultDir as T
  61. } else if (name == "didLaunch") {
  62. return didLaunch as T
  63. } else if (name == "dir") {
  64. return dir as T
  65. } else if (name == "httpDefaultPort") {
  66. return httpDefaultPort as T
  67. } else if (name == "httpLaunch") {
  68. return httpLaunch as T
  69. } else if (name == "httpPort") {
  70. return httpPort as T
  71. } else if (name == "httpReply") {
  72. return httpReply as T
  73. } else if (name == "httpRequest") {
  74. return httpRequest as T
  75. }
  76. return "unknown-field-name" as T
  77. }
  78. override fun selfCopy(): CLDContext {
  79. return this.copy()
  80. }
  81. override fun setField(
  82. name: String,
  83. value: Any?
  84. ) {
  85. if (name == "arguments") {
  86. arguments = value as Array<String>
  87. } else if (name == "consoleOutput") {
  88. consoleOutput = value as String
  89. } else if (name == "defaultDir") {
  90. defaultDir = value as String
  91. } else if (name == "didLaunch") {
  92. didLaunch = value as Boolean
  93. } else if (name == "dir") {
  94. dir = value as String
  95. } else if (name == "httpDefaultPort") {
  96. httpDefaultPort = value as Int
  97. } else if (name == "httpLaunch") {
  98. httpLaunch = value as Boolean
  99. } else if (name == "httpPort") {
  100. httpPort = value as Int
  101. } else if (name == "httpReply") {
  102. httpReply = value as String
  103. } else if (name == "httpRequest") {
  104. httpRequest = value as NetRequest
  105. }
  106. }
  107. }
  108. ```
  109. # March
  110. Kotlin is currently the anchor programming language of CLD. Thus, to use Kotlin,
  111. one would have to install related Java tools (Gradle, Android Studio or JVM itself).
  112. Such a requirement makes it impossible to have a web browser only development
  113. tool. And being able to code in a web browser is something I consider the
  114. key feature for code portability.
  115. That's why in March I plan to create the draft of a tool running in a web browser.
  116. [entities]: https://github.com/OGStudio/local-host-access/blob/main/cld/entities.yml
  117. [entities-result]: https://github.com/OGStudio/local-host-access/blob/main/src/entities.kt#L3
  118. [splash]: ../../images/2025-03_redux-data-flow.jpg
  119. [store]: https://redux.js.org/introduction/getting-started#basic-example