Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using stdlib
- namespace program
- //"namespace program" is more than just for organiation
- //you can do "program.gc off", you can do
- //"program.oporder = {div,plus}" to make the div function run
- //before plus if they're infixed operators, you can play god with
- //the compiler, basically. But since all this is isolated to your
- //own namespace, this does not affect other libraries.
- //the ^ line is optional, the compiler can use inference
- //and in fact always does, but will throw an error if the
- //signature doesn't match what the compiler infers
- ^ add -> int -> int -> int
- ? add = \a b -> + a b
- ^ addmany -> list -> int -> int
- ? addmany = \l n ->
- $ l.size //patern match
- | 0 -> n
- | * -> addmany l (add l.pop n)
- //and partial application of course
- ? better-addmany = \l -> addmany 0 l
- ? match = \a b -> (
- $ a;
- | a is b -> "indentation doesn't matter";
- | * -> "inside paren/semicolon blocks";
- )
- ! mylist = list.new
- mylist.add 10
- mylist.add "a list can hold anything"
- mylist.find "and has many methods"
- mylist.add "hi"
- if mylist.pop is "hi" then print(true)
- ^ bike -> class
- ? bike = [
- //wheels is an inline class so it can add a setter and getter
- ? wheels = [
- //~ is private
- ~ num = 0
- ? get = \-> num
- ? set = \n -> num = n
- ]
- ! override-me
- ]
- ^ motorbike -> bike
- ? motorbike = [
- //* is static
- * new = \n -> wheels.set = n; this //return this, creates a new instance
- override-me = \-> "overridden ( because it's a variable with ! instead of a value with ? )"
- ]
- ! mybike = bike.new //classes have an implicit new already defined
- mybike = motorbike.new 2 //class hiarchey, motorbike inherited from bike so it's a bike and fits in the inferred "bike" type mybike variable
- mybike.wheels.set 3
- \-> (print "i can even do this")[] //invoked due to brackets
- \a -> (print (add a a))[10] //getting a little lispy
- add[10 10] //in fact, brackets are how any function is called, they're just optional
- //lets make that above lambda look nicer
- ! <> = \fa fb ->
- fb[fa]
- ! + = \a b -> add a b
- \a -> ((a + a) <> print)[10] //oh, yeah, all functions are also immedately operators
- //^ is actually how we make types in general
- //{} blocks are type literals with symbols in them, like structs
- ^ bool -> { true; false = nil }
- //* is, you guessed it, any type
- ^ string? -> * -> bool
- ? string? = \a ->
- $ a.type
- | string -> true
- | * -> false
- //but wait, we have type literals? could that mean...
- ^ num-to-string -> {int,double} -> string //inline types
- ? num-to-string = \n ->
- $ n.type
- | int -> int-to-string[n]
- | double -> double-to-string[n]
- //and no need for a * case, because all cases are met thanks to the type system.
- //and what good would this be without this
- eval["print[yes]"]
- //but that's not too safe, so lets make it
- ! env = eval.new
- env.sandbox.set = env.sandbox.all
- env.include "stdlib"
- env.eval["nasty_file_operation"] //throws an error
- env.eval["print[\"crash\"]"] //but this also crashes! we don't want that
- env.sandbox.io.console.set true
- //and, why not, lets add a function to the new environment
- env.define "echo" \s -> print s
- //or just let it have all of our functions
- env.define "this" this //we're technically in a class, even in the top-level, so we pass "this"
- env.eval["echo[\"all is good\"]"]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement