Created
August 16, 2011 18:07
-
-
Save alexnask/1149746 to your computer and use it in GitHub Desktop.
Pride programming language ideas and samples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // NO TYPES SEEN, NO HARM DONE <- todo :P | |
| // One-liner comment | |
| /* | |
| Multiligne | |
| comment | |
| */ | |
| var :: Int // Declaration | |
| var = 42 // Assignement | |
| blah :: Int = 42 // Wow! :D | |
| foo = 42 // Decl-assign (type infered) | |
| (a :: Int, b :: Int) -> a+b // Function (lambda) literal = [declaration tuple] -> [Instruction Block] | |
| f = (a :: Int, b :: Float) -> a+b | |
| f (1,41) // Function call = [Function object][argument tuple] | |
| {a = 1, b :: Float, c = "lol"} // Object literal | |
| obj :: {a :: Int, b :: Float, c :: String} // Type is similar to the object itself | |
| obj = {a = 1, b :: Float, c = "lol"} | |
| obj b = 41.0 // Object access | |
| doStuff() | |
| returnSomething() // Instruction block -- doStuff will be executed and the block will be evaluated to the return of returnSomething() | |
| obj a = 41 | |
| b = 1.0 // Chain calling | |
| /* | |
| This is not exactly chain calling. | |
| It is actually an instruction block inside obj's scope. | |
| */ | |
| type Foo = {a :: Int, b :: Float, c :: String} // Type alias | |
| foo :: Foo | |
| foo a = 1 | |
| foo b = 41.0 | |
| foo c = "bar" | |
| // a :: B form | |
| // a :: B is a definition when it is a statement in an instruction block | |
| // Else, it is a cast. | |
| // Let's see a special case | |
| func = -> a = 1 | |
| a :: Float | |
| /* | |
| According to the above rules, you would probably think that a :: Float is a definition and that you would have a redifinition error. | |
| But this is not the case. | |
| The a :: Float statement is read as return a :: Float by the compiler, as it is the last expression in an instruction block. | |
| Thus, the second rule applies and a is casted to a Float and then returned. | |
| */ | |
| [1,2,3,4,5,6,7,8,9] // Array literal | |
| [1..10] // Ranges -> [1,2,3,4,5,6,7,8,9] | |
| // Flow control | |
| // if statement | |
| if true then doTrue() else doFalse() | |
| // Else can be ignored | |
| if true then doTrue() | |
| /* else if is actually else (if ... ) | |
| In general, | |
| if [condition] then [instrBlock1] {else [instrBlock2]} | |
| while statement | |
| */ | |
| while true do stuff() | |
| // or | |
| do stuff() while true | |
| /* | |
| Go read a C manual if you need an explanation for the difference between the two. | |
| In general, | |
| while [condition] do [instrBlock] | |
| or | |
| do [instrBlock] while [condition] | |
| for statement | |
| */ | |
| for i in [1..10] do stuff(i) | |
| // or | |
| for i :: Float in [1..10] do stuffFloat(i) | |
| /* | |
| In general, | |
| for [varName] in [array] do [instrBlock] | |
| or | |
| for [declaration] in [array] do [instrBlock] | |
| */ | |
| (1,41.0,"lol") // Tuple | |
| // :: (Int,Float,String) | |
| // Here is a nice example with tuples | |
| f = -> (1,41.0,"lol") | |
| (_,_,x) = f() // Yay! Kinda tuple retrospecting | |
| // A kind of pattern matching | |
| // Here is the pattern matching literal | |
| (Int,Int) => | |
| (0,_) -> 0 | |
| (_,0) -> 0 | |
| (a,b) -> a+b | |
| // With objects | |
| type Foo = { a :: Int, b :: Float } | |
| (Int,Foo) => | |
| (0,_) -> 0 | |
| (_,{ a = 0, b = _ }) -> 0 | |
| (a,b) -> a+(b a)+(b b) | |
| // Or | |
| (Int,Foo) => | |
| (0,_) -> 0 | |
| (_,{ a = 0 }) -> 0 | |
| (a,b) -> a+(b a)+(b b) | |
| // When we only ask for some members of the object to be the same, the rest are not taken into account | |
| // What is the type of a function? | |
| f :: () -> // A function that takes no argument and returns nothing | |
| g :: () -> Int // A function that takes no argument and returns an Int | |
| h :: (Int,Float) -> Float // A function that takes an Int and a Float argument and returns a Float | |
| /* | |
| A little history? Why () -> ? | |
| If the arrow was omitted, the type would be () (the empty tuple) wich is equivalent to void. | |
| Also, I did not want to have a prefix like Func (see ooc ;) ) | |
| That are the deep reasons of doom ^.^ | |
| */ | |
| use something // will search in path for something.pri and include it | |
| link something // will cause the C compiler to link against -lsomething | |
| // Here is a sample from the true SDK to show the C FFI | |
| type Int = ctype int with { | |
| sin :: extern(sin) ()->Int | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment