design_muesli.muesli 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # Muesli is a Multi Use Embeddable Scripting Language Indeed.
  2. # It is a scripting language with a TCL or shell like syntax, but somewhat
  3. # OOP and LISP like semantics.
  4. # First it will be an embeddable scripting language implemented in Go.
  5. # In what follows I will describe the design of the language though example.
  6. #
  7. # Why is there a # before these lines? # begins a comment, until the end of the
  8. # line. MUESLI does not execute comments but they are collected
  9. # for documentation purposes. More on that topic later
  10. # Muesli is a line based language.
  11. # A newline preceded by a \ is ignored, even after a comment so \
  12. this is still comment
  13. # Muesli consists of newline separated statements, however, a newline
  14. # after { or the do keyword is ignored and does not count as a separator.
  15. /*
  16. C style comments are also supported, and unlke C, they DO nest, but the
  17. comment indicator and the end of comment indicator must be the fist element
  18. on the line. Anything on the line of the end-of-comment indicator is also
  19. ignored
  20. /*
  21. so this is fine
  22. */ this is ignored too
  23. */
  24. # Empty lines are ignored.
  25. # ...
  26. # Muesli supports integer constants with type Int
  27. 1
  28. 2
  29. 378
  30. +108
  31. -878
  32. # ... Character constants with escapes with type Rune
  33. # This may also be unicode code points if above ascii range.
  34. 'a'
  35. # It also supports multi line string constants with escapes, with type String
  36. "Hello world\"
  37. "
  38. # and multi line strings without escapes
  39. `
  40. "Pop" goes the
  41. weasel's tail.
  42. `
  43. # And booleans constants of type Bool
  44. !true !false
  45. # And simple floating point constants, but no exponential notation, with type Float
  46. +0.5
  47. -7.000005
  48. # Lists can be created between [ ] and may be heterogenous or homogenous.
  49. # The [ ] must be space separated.
  50. # The type is Any[] if heterogenous,
  51. [ foo "bar" ]
  52. # The type is Int@ below
  53. [ 1 2 3 ]
  54. # A sequence of a lower case letter followed by anything that is not whitespace.
  55. # For example: this-IS*a/single+Word._
  56. # The value of a word is a string with the word itself.
  57. # If the word is at the beginning of the line it is invoked as a command.
  58. # Muesli's basic syntax is that of the command. Spaces separate the arguments.
  59. # of the command. The first word is the command, the rest are the arguments.
  60. print "hello" world 7 0.9
  61. # the previous command outputs: "hello world 7 0.9" to standard output
  62. # A command has one or more results that can be captured with a parenthesis
  63. print ( mul 3 ( sum 5 7 ) )
  64. # Commands can be grouped into blocks. The value of the block is that of it's last command.
  65. {
  66. print "hello"
  67. print "world"
  68. }
  69. # Commands can also take blocks as parameters. Don't put a newline between } and {
  70. # to be sure multiple blocks get passed
  71. command {
  72. print "first block"
  73. } and also {
  74. print "second block"
  75. }
  76. # In Muesli all values are typed. Types can be defined by the built type command.
  77. type Door ( object locked Bool keys Item[] )
  78. # Commands can be defined by the built in 'to' command.
  79. # They can have many arguments and many results.
  80. to open [door Door key Item] Bool {
  81. if (contains (member door key) key) {
  82. set
  83. }
  84. }
  85. # Unlike TCL, MUESLI values are typed, and commands
  86. # can be overloaded based on the types the types of their arguments.
  87. # Types are much like words, but they begin with an upper case letter, up to the
  88. # next whitespace.
  89. # A type that end in [] is a list, [][] a list of lists, etc, and a type
  90. # that end in ... represents a variable argument, which must be last.
  91. # Muesli tries to match the types, from narrow to wide for objects in the order
  92. # Outer class, Embedded Class, Object, Any,
  93. # and for primitive types <Bool|Int|String|Float|Word|Type>, Primitive, Any
  94. # You can override commands with more specific ones but not existing ones
  95. # that have the same specificity.
  96. # Variables are nor part of the language but of the built in commands
  97. # Variables are set in the current scope with
  98. set a 10
  99. # And fetched in the current scope with get
  100. print (get a 10)
  101. # To acces a variable in the scope one above current use upset/upget.
  102. upset a 10
  103. # However, there is syntactic sugar:
  104. # =foo bar gets mapped to (set foo bar)
  105. =a 10
  106. # $foo means (get foo)
  107. print $a
  108. # combinations of = followed by $ are allowed for indirect variable assignment
  109. =foo a
  110. =$foo 20
  111. print $a
  112. # Control structures are merely built in functions.
  113. if (less a 10) {
  114. print "Less"
  115. } else {
  116. print "More"
  117. }
  118. # That's all there is to the syntax. Apart from the built in commands,
  119. # the semantics are up to you to implement as
  120. # embedded commands.
  121. /*
  122. BLOCK
  123. PUSHS "More"
  124. CALL print
  125. PUSHBLOCK
  126. PUSHW else
  127. BLOCK
  128. PUSHS "Less"
  129. CALL print
  130. PUSHBLOCK
  131. PUSHI 10
  132. PUSHW a
  133. CALL less
  134. CALL if
  135. */