Advertisement
Guest User

Untitled

a guest
Mar 4th, 2010
754
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.04 KB | None | 0 0
  1. # About snow #
  2.     "snow" is a meta language that compiles down to php. It has its own syntax
  3.     inspired by python, ruby and scala that strives to be DRY, clean and easy to
  4.     read as well as write.
  5.    
  6.     snow is called "snow" because it should look "pure as snow", because is has
  7.     significant WHITEspace and because the winter of 2010 in Copenhagen was
  8.     extremely cold and snowy.
  9.    
  10.     Its main features are:
  11.         * The generated PHP code is fast and does not look generated. All
  12.           features of snow are build so they will look nice as regular PHP and
  13.           so you at some point can throw away the snow code if its "just not fun
  14.          anymore".
  15.         * Can compile to PHP using different profiles. During developement one
  16.           can enable extra safetynets and when everything works a lean fast code
  17.           can be compiled. Some safetynets include:
  18.             * Check input and return type for functions and methods.
  19.             * Valididate argument value for function and methods.
  20.             * Custom checks can be created using annotations.
  21.         * Has significant whitespace.
  22.         * Loops can return a value, with the new "yield" type collector called
  23.          "putin" that can collect into all types - not just an array.
  24.         * Has its own scope system that i.e. is being used to join both "->" and
  25.           "::" into ".". The global scope does only work inside one file and
  26.           that is considered a feature.
  27.         * Is parsed using PHP which makes it easy to modify and extend.
  28.         * Has infix operators.
  29.         * Has operator overloading.
  30.         * Has method chaining.
  31.         * Has a _lot_ fewer characters than PHP.
  32.         * Uses Markdown for comments and can generate doc pages in HTML.
  33.         * Has support for inline HTML using HAML-style indention based
  34.           autoclosing tags.
  35.         * Is very well tested with PHPT, including a round trip compiler with
  36.           the existing PHP suite in its enterity.
  37.            
  38.     This file is valid snow code.
  39.  
  40. # Conventions #
  41.     * snow files must be named "[FILENAME].snow".
  42.     * All lines must begin with a number of spaces dividable by 4. Other
  43.       indentation styles are not and will not be supported.
  44.    
  45.     snow has the following recommended naming conventions:
  46.         * CONSTS_ARE_UPPERCASE_SEPERATED_BY_UNDERSCORE (required)
  47.         * ClassesAreUpperCamelCase
  48.         * vars_are_lowercase_separated_by_underscores
  49.         * functions_are_lowercase_separated_by_underscores
  50.         * methods_are_lowercase_separated_by_underscores
  51.         * Static_Methods_Are_Mixed_Case_Seperated_By_Underscores  
  52.  
  53. # Various minor changes from php #
  54.     * "->" is now ".".
  55.     * "::" is now ".".
  56.     * "." is now "^".
  57.     * "^" is now "><".
  58.     * "%" is now "mod".
  59.     * The "new" keyword can be omitted.
  60.     * "function" is now "fn".
  61.     * "foreach" is replaced with a python style "for" loop.
  62.     * The old "for" loop is removed.
  63.     * ";", "{", "}" is removed and white space is used as a determining factor.
  64.     * "array" keyword is replaced with "[]" short form.
  65.     * "=>" in array is replaced with ":".
  66.     * "{}" can be used as a short form of object creation.
  67.     * ":" can be used instead of increased indentation for ie. 1 line functions.
  68.     * "//" and "/* ... */" comments no longer work.
  69.     * "->" is now keyword for method chaining.
  70.     * "<?php ?>" is now "<% %>". "<?php ?>" can be used to inject PHP code (Not
  71.       recommended). "<%" is implied in the beginning of a snow file. "<%=" can
  72.       be used.
  73.     * "pow()" is now "**".
  74.  
  75. # The basics #
  76.  
  77. # Globals are all uppercase with underscores and can hold complex values.
  78. I_AM_GLOBAL = [4,3,2]
  79.  
  80. # $foo and ';' is no more. New string concatenator.
  81. foo = 'This is nuschool' ^ '!'
  82.  
  83. # Method chaining with "->". The () can be omitted.
  84. b = 'string'->ucfirst # b will contain "String".
  85.  
  86. # "%" is used in strings to denote a variable.
  87. # Matches multiple levels without the need of the "{}" notation.
  88. # Functions can be called and their return concatenates to the string.
  89. a_string = "%foo.bar.stuff | %foo['bar']['stuff'] | %foo('bar', 'stuff')"
  90.  
  91. # "%" is also used as a "variable variable"
  92. bar = 'foo'
  93. %bar() # Will call the function "foo".
  94.  
  95. # File level function prefix annotation.
  96. # All functions will be prefixed "bowling_". Inside the file functions can be
  97. # called without the prefix.
  98. @fnprefix bowling
  99.  
  100. # Operators can be used as functions
  101. a = ^ (
  102.     'string'
  103.     myfunc(43)
  104.     'string'
  105. )
  106.  
  107. # Array and objects #
  108.     Both have new short form definitions. The old 'array' definition can no
  109.     longer be used.
  110.    
  111.     The seperator ',' can be omitted.
  112.  
  113. my_array = [1 2 3]
  114. my_array = [1,2,3]
  115. my_array = [
  116.     'x': 32
  117.     'y':
  118.         z: 54
  119.         m: 'foo'
  120. ]
  121. # You can still use "foo = stdClass" (notice that "new" is omitted).
  122. my_object = {
  123.     'x': 32
  124.     'y':
  125.         z: 54
  126.         m: 'foo'
  127. }
  128. # The "->" and "::" is replaced by ".".
  129. echo my_object.y.z
  130. # This will be called as a static method, because there is no a variable called
  131. # SomeClass in scope.
  132. SomeClass.Static_Method()
  133.    
  134. # Functions #
  135.     The argument list of functions is moved above the function definition.
  136.     This makes it possible to add documentation to each argument without
  137.     repeating yourself. This helps to avoid docblock rot as well.
  138.    
  139.     Each argument must have a type and can have optional value checks. Inner
  140.     functions are supported in the same way as in python.
  141.    
  142.     All types are shortened to three letters(int, str, arr, obj, flo, boo) and
  143.     this is also the case for typecasting.
  144.    
  145.     When calling functions:
  146.         * The "," seperator can be omitted:
  147.             myfunc(45 65 somefunc())
  148.         * A trailing "," is allowed:
  149.             myfunc(45,65,somefunc(),)
  150.         * "()" can be replaced by indentation:
  151.             myfunc
  152.                 34
  153.                 42
  154.                 false
  155.        
  156. @ int x
  157.     # This is commentary about x.
  158.     % > 0 && % < 99 && !%->empty # Input validation. "%" replaces variable name.
  159. fn square
  160.     return x ** 2
  161. # Or the function can be a oneliner using the ":" as a seperator.
  162. fn square: return x ** 2
  163.  
  164. # Function with inner function that uses the short form of a function definition.
  165. # Short form is only recommended for very simple inner functions.
  166. @ int x
  167.     # Comment about x.
  168. fn withinner int y
  169.     fn inner int x (% > 0)
  170.         return 2 / x
  171.     return y->inner
  172.  
  173. # Loops #
  174.     "for" keyword is now closer to the good old "foreach".
  175.    
  176.     Understands the "[0-9a-zA-Z] to [0-9a-zA-Z]" infix operator. Implemented the
  177.     same way as python 3s "range".
  178.    
  179.     A new keyword "putin" is introduced that decides what value or variable
  180.     the loop will collect into when returning a value from the loop.
  181. ## Examples ##
  182. a = get_some_random_array()
  183. for x in a
  184. for x:y in a
  185. for [x,y:z,v] in a
  186. for x in 1 to 98 step 2
  187. # "*=", "/=", "+=", "-=" can be used to collected into the value set by "putin"
  188. return for k:v in a putin 1
  189.     *= k + v
  190. a = 32
  191. c = for k:v in array putin a: *= k + v
  192.    
  193. # Classes #
  194.     * @classvar annotation that creates code like "$this->foo = $foo" in the
  195.       constructor.
  196.  
  197. ## Example class with example documentation. ##
  198.     * Markdown is used by default for documentation.
  199.     * Bluh.
  200.     * Images can be used: [image.png].
  201. @classvar int x
  202.     # "x" is stored as a public class var.
  203.     % > 0 && !%->empty
  204. @classvar private static int y
  205.     # "y" is stored in a private static class var.
  206.     % > 0 && !%->empty
  207. @ int z=0
  208.     # Because it does not have public / private keyword, "z" is just passed to the constructor.
  209.     % > 0 && !%->empty
  210. class MyClass
  211.     # Defaults to public.
  212.     int zulu = 0
  213.    
  214.     # Constructor #
  215.         Nothing much to tell.
  216.     fn __construct
  217.         zulu = z * 2 # "this" is implied.
  218.         echo 9->other_method # "this" is implied.
  219.    
  220.     # Talk about this method.
  221.     fn some_method
  222.         return x + y # "this" is implied.
  223.    
  224.     # Methods / functions can have inline args as well.
  225.     fn other_method(int x): return x - 2
  226.    
  227.     # Example of static method.
  228.     @ obj obj (!%->empty)
  229.     static fn Some_Static_Method: return obj.foo.bar
  230.                
  231. foo = MyClass(1,2) # "new" keyword can be ommitted.
  232.  
  233. # Operator overloading #
  234. @ int t
  235. @ int n
  236. class Broek
  237.     fn *(Broek b): return new Broek(t*b.t, n*b.n)
  238.     # Method overloading.
  239.     fn *(int i): return new Broek(t*i, n)
  240.     fn /(Broek b): return new Broek(t*b.n, n*b.t)
  241.     fn +(Broek b):
  242.         c = n * b.n
  243.         return new Broek(c * t + c * b.t, c)
  244.     fn -(Broek b):
  245.         c = n * b.n
  246.         return new Broek(c * t - c * b.t, c)
  247. b = Broek(4,5)
  248. c = Broek(2,3)
  249. d = b * c + c / Broek(1,2)
  250. # The line above will compile to this:
  251. <?php $d = $b->times($d)->plus($c->divide(Broek:factory(1,2))) ?>
  252.  
  253. # Infix operator #
  254. @ int x
  255. @ int y
  256. @ int z
  257. infix x times y plus z: return x * y + z
  258. a = 4 times 9 plus 98
  259.  
  260. # Text formats #
  261.  
  262. ## HAML style inline html ##
  263. $s =
  264.     <div class="blah">
  265.         <p>%obj.foo.bar
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement