Advertisement
konalisp

Language markup spec 1

Dec 6th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. using stdlib
  2.  
  3. namespace program
  4.  
  5. //"namespace program" is more than just for organiation
  6. //you can do "program.gc off", you can do
  7. //"program.oporder = {div,plus}" to make the div function run
  8. //before plus if they're infixed operators, you can play god with
  9. //the compiler, basically. But since all this is isolated to your
  10. //own namespace, this does not affect other libraries.
  11.  
  12. //the ^ line is optional, the compiler can use inference
  13. //and in fact always does, but will throw an error if the
  14. //signature doesn't match what the compiler infers
  15. ^ add -> int -> int -> int
  16. ? add = \a b -> + a b
  17.  
  18. ^ addmany -> list -> int -> int
  19. ? addmany = \l n ->
  20. $ l.size //patern match
  21. | 0 -> n
  22. | * -> addmany l (add l.pop n)
  23.  
  24. //and partial application of course
  25. ? better-addmany = \l -> addmany 0 l
  26.  
  27. ? match = \a b -> (
  28. $ a;
  29. | a is b -> "indentation doesn't matter";
  30. | * -> "inside paren/semicolon blocks";
  31. )
  32.  
  33. ! mylist = list.new
  34. mylist.add 10
  35. mylist.add "a list can hold anything"
  36. mylist.find "and has many methods"
  37. mylist.add "hi"
  38. if mylist.pop is "hi" then print(true)
  39.  
  40. ^ bike -> class
  41. ? bike = [
  42. //wheels is an inline class so it can add a setter and getter
  43. ? wheels = [
  44. //~ is private
  45. ~ num = 0
  46. ? get = \-> num
  47. ? set = \n -> num = n
  48. ]
  49. ! override-me
  50. ]
  51.  
  52. ^ motorbike -> bike
  53. ? motorbike = [
  54. //* is static
  55. * new = \n -> wheels.set = n; this //return this, creates a new instance
  56. override-me = \-> "overridden ( because it's a variable with ! instead of a value with ? )"
  57. ]
  58.  
  59. ! mybike = bike.new //classes have an implicit new already defined
  60. mybike = motorbike.new 2 //class hiarchey, motorbike inherited from bike so it's a bike and fits in the inferred "bike" type mybike variable
  61. mybike.wheels.set 3
  62.  
  63. \-> (print "i can even do this")[] //invoked due to brackets
  64. \a -> (print (add a a))[10] //getting a little lispy
  65.  
  66. add[10 10] //in fact, brackets are how any function is called, they're just optional
  67.  
  68. //lets make that above lambda look nicer
  69. ! <> = \fa fb ->
  70. fb[fa]
  71.  
  72. ! + = \a b -> add a b
  73.  
  74. \a -> ((a + a) <> print)[10] //oh, yeah, all functions are also immedately operators
  75.  
  76. //^ is actually how we make types in general
  77. //{} blocks are type literals with symbols in them, like structs
  78. ^ bool -> { true; false = nil }
  79. //* is, you guessed it, any type
  80. ^ string? -> * -> bool
  81. ? string? = \a ->
  82. $ a.type
  83. | string -> true
  84. | * -> false
  85.  
  86. //but wait, we have type literals? could that mean...
  87. ^ num-to-string -> {int,double} -> string //inline types
  88. ? num-to-string = \n ->
  89. $ n.type
  90. | int -> int-to-string[n]
  91. | double -> double-to-string[n]
  92. //and no need for a * case, because all cases are met thanks to the type system.
  93.  
  94. //and what good would this be without this
  95. eval["print[yes]"]
  96. //but that's not too safe, so lets make it
  97. ! env = eval.new
  98. env.sandbox.set = env.sandbox.all
  99. env.include "stdlib"
  100. env.eval["nasty_file_operation"] //throws an error
  101. env.eval["print[\"crash\"]"] //but this also crashes! we don't want that
  102. env.sandbox.io.console.set true
  103. //and, why not, lets add a function to the new environment
  104. env.define "echo" \s -> print s
  105. //or just let it have all of our functions
  106. env.define "this" this //we're technically in a class, even in the top-level, so we pass "this"
  107. env.eval["echo[\"all is good\"]"]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement