Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Pride programming language ideas and samples

View gist:1149746
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
// 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
Something went wrong with that request. Please try again.