http://proglit.com/. a first language by sa pigeon (a “fake” language)

Post on 17-Dec-2015

216 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

http://proglit.com/

a firstlanguage

BY

SA

pigeon(a “fake” language)

source code(code as text)

commentthis is code # this is a comment

value(a piece of data)

data typesnumber

stringboolean

number3

-7248.93

-0.88881

string(a piece of text)

“R. Nixon”“T”

“Elementary, my dear Watson.”“%”

boolean(a true/false value)

truefalse

null(a value representing nothing)

null

literal(a value written in code)

null“The owls are not what they seem.”

78false

escape sequence“\”Hi,\” she said.”“Hello,\nworld.”

“c:\\bla\\bla\\bla”

operation(takes input values and

returns an output value)

operator

(an input value)operand

(specifier of the operation to perform)

(+ 3 5)(3 + 5)

(3 + 5)(add 3 5)

3 + 5 + -7 + 11(add 3 5 -7 11)

11 - 2 * 311 - (2 * 3)

(sub 11 (mul 2 3))

variable(a memory location holding a value)

identifier(a name)

NewYorkfoo

asdf43qwerty78

illegal:New Yorkf^$o*o43asdf

(letter case matters)

NewYorknewyorknewYORKnEwYoRkNEWYORK

case sensitive

reserved word(an identifier reserved by the language)

add, sub, mul, div, if, while…

(gives a variable a value)assignment

as dog trueas newt -87.2as bird null

as cat “rubber baby buggy bumper”

as bar 19as foo baras bar 3as fizz (add foo 11)

expression(evaluates into a value)

null(add foo 2)

“hello”bar

statement(the units of syntax that make up the code)

as foo 53(add foo 11)

computer(a cpu, memory, and i/o)

cpu(executes instructions)

memory(holds bits)

i/o(input/output)

(display a value on the screen)print

(print “wakka wakka”)as bar 19(print bar)

(return typed input from user)prompt

as foo (prompt)(print foo)

Hello, world!(print “Hello, world!”)

(equality test)eq

(eq “moo” “moo” “moo”) # true(eq -35 -35) # true(eq 6 2) # false

(reverse the truth value)not

(not true) # false(not false) # true

(maybe do something, or maybe skip over it)

conditional execution

(“run these statements if…”)if

if condition body

if (eq x 3) (print “cat”) (print “dog”) (print “bird”) x equals 3 cat dog bird

x doesn’t equal 3 bird

(do one thing or do the other, but NOT both)

mutual exclusion

if (eq x 3) (print “hi”)if (not (eq x 3)) (print “bye”)

x equals 3 hix doesn’t equal 3 bye

(“…elsewise, run these other statements”)else

if condition body1else body2

if (eq x 3) (print “hi”)else (print “bye”) x equals 3 hi

x doesn’t equal 3 bye

if (eq x 3) (print “hi”)else if (eq x 5) (print “bye”)

x equals 3 hix equals 5 bye

x doesn’t equal 3 or 5

(a convenience for else if)elif

if condition1 body1elif condition2 body2

if (eq x 3) (print “hi”)elif (eq x 5) (print “bye”) x equals 3 hi

x equals 5 byex doesn’t equal 3 or 5

if (eq x 3) (print “hi”)elif (eq x 5) (print “bye”)elif (eq x -7) (print “moo”)elif (eq x 14) (print “woof”)

x equals 3 hix equals 5 bye

x equals -7 moox equals 14 woof

x doesn’t equal any of these values

if (eq x 3) (print “hi”)elif (eq x 5) (print “bye”)elif (eq x -7) (print “moo”)else (print “meow”)

x equals 3 hix equals 5 bye

x equals -7 moox doesn’t equal any of these values meow

(less than)lt

(lt 25 76 8000) # true(lt 35 -2) # false(lt 2 2 7) # false

(less than or equal to)lte

(lte 25 76 8000) # true(lte 35 -2) # false(lte 2 2 7) # true

(greater than)gt

(gt 25 76 8000) # false(gt 35 -2 -10) # true(gt 8 4 4) # false

(greater than or equal to)gte

(gte 25 76 8000) # false(gte 35 -2 -10) # true(gte 8 4 4) # true

(“are all of the operands true?”)and

(and true true) # true(and true false false) # false(and true false true) # false(and false false) # false

(“is at least one of the operands true?”)or

(or true true) # true(or true false false) # true(or true false true) # true(or false false) # false

(modulus, the remainder of division)mod

(mod 15 5) # 0(mod 16 5) # 1(mod 17 5) # 2

if (eq (mod x 2) 0) (print “even”) else (print “odd”)

(a piece of code that repeats)loop

(“repeatedly execute these statements while…”)while

while condition body

as z 6while (gt z 0) (print z) as z (sub z 1)

6 5 4 3 2 1

fizzbuzzprint the integers 1 to 100, except…

a) for any integer evenly divisible by 3, print “Fizz”b) for any integer evenly divisible by 5, print “Buzz”c) for any integer evenly divisible by 3 and 5, print “FizzBuzz”

as x 1while (lte x 100) … as x (add x 1)

as x 1while (lte x 100) as by3 (eq (mod x 3) 0) as by5 (eq (mod x 5) 0) … as x (add x 1)

as x 1while (lte x 100) as by3 (eq (mod x 3) 0) as by5 (eq (mod x 5) 0) if (and by3 by5) (print “FizzBuzz”) elif by3 (print “Fizz”) elif by5 (print “Buzz”) else (print x) as x (add x 1)

if by3 (print “Fizz”)elif (and by3 by5) (print “FizzBuzz”)elif by5 (print “Buzz”)else (print x)

if (and by3 by5) (print “FizzBuzz”)elif by3 (print “Fizz”)elif by5 (print “Buzz”)else (print x)

(a programmer-defined operation)function

routine, sub-routine, procedure, method

(foo true 31)

function name parameters body

function eric (print “hello”)

(eric) # print “hello”

argument

parameter(an input value to a function)

(a variable which receives an input value)

function ryan bat goat (print (sub goat bat))

(ryan 4 -9) # print -13as bar 3(ryan bar 5) # print 2

(return a value from the function)return

return expression

function jerry return 3

(print (jerry)) # print 3

factoriala) for 0, return 1b) for positive n, return product of 1..n

function factorial n as val 1 while (gt n 1) as val (mul n val) as n (sub n 1) return val

(factorial 4) # 24(factorial 5) # 120

(Don’t Repeat Yourself)DRY

do one thing

keep it short

(place and time which in a thing exists)scope

function kate cat dog as bird 4 return (add cat dog bird)

(a “space” in which a set of names exist)

namespace

(the ambiguity of a name having more than one meaning in a single namespace)

namespace collision

function miguel apple orange as coconut “hi” …

function colin sofa coconut …

function harry …

function lisa harry …

(exists everywhere in the program)

global

function alison (print cat)

function jason x as cat x

as cat “rubber”(alison) # print “rubber”(jason “glue”)(alison) # print “glue”

functions are values

function gina (print “hi”)

as dog gina(dog) # print “hi”

function gina (print “hi”)

function harry turtle (turtle)

(harry gina) # print “hi”

function gina (print “hi”)

function harry turtle (turtle)

function alec cow if cow return harry else return gina

(defined in terms of itself)

recursive

(a function which calls itself)

recursive function

function jessica (print “hi”) (jessica)

(jessica) # print “hi hi hi hi hi…”

function kelly (print “a”) (mike)

function mike (print “b”) (kelly)

(kelly) # print “a b a b a b a b…”

factorial (recursive)a) for 0, return 1b) for n, return product of 1..n

function factorial n if (eq n 0) return 1 return (mul n (factorial (sub n 1)))

as name1 “George Washington”as name2 “John Adams”as name3 “Thomas Jefferson”as name4 “James Madison”as name5 “James Monroe”…

(print name1)(print name2)(print name3)(print name4)(print name5)…

(a value made up of multiple values)

collection

(a numerically-indexed collection)list

(list 77 “yo” -6)(list)

(return the value of the nth item in a list)

get

as will (list 77 “yo” -6)

(get will 0) # 77(get will 1) # “yo”(get will 2) # -6

(return the number of items in a list)len

as josh (list 77 “yo” -6)as lisa (list 77)

(len josh) # 3(len lisa) # 1

function printAll lst as i 0 while (lt i (len lst)) (print (get lst i)) as i (add i 1)

(unable to change)immutable

(able to change)mutable

(change the value of the nth item in a list)

set

as kim (list 77 “yo” -6)

(print (get kim 1)) # print “yo”(set kim 1 -53)(print (get kim 1)) # print -53

(add more items to end of a list)append

as hugh (list)(len hugh) # 0(append hugh “hi” “bye”)(len hugh) # 2(append hugh 3 6)(len hugh) # 4

reference variable(holds an address, NOT a value)

as zed 3as zed 5as molly (sub zed 1)

as leo (list)as bruce leo(len leo) # 0(append bruce “salut”)(len leo) # 1

function ian lst (append lst null)

as chris (list 71 false)(len chris) # 2(ian chris)(len chris) # 3

as joe (list “hi” 8)as yves (list joe 4 null)(set yves 2 yves)

(a value in memory is identicalwith only itself)

identity

(a value in memory is equal to itself and other values of same type and content)

equality

(identity test)id

as ed (list 88 8)as thom (list 88 8)(eq ed thom) # true(id ed thom) # false

(a key and its associated value)key-value pair

(a value that acts as an index to another value)

key

(an associative collection)dictionary

(dict)(dict 77 “yo” “avast” true)(dict “avast” true 77 “yo”)

as ted (dict 5 2 “yo” -1)(len ted) # 2(get ted “yo”) # -1(set ted “yo” 8)(len ted) # 2(get ted “yo”) # 8(set ted 21 false)(len ted) # 3

as cow (dict)as goose cow(set cow “name” “Fred”)(get goose “name” “Fred”) # “Fred”

function makePerson name age ss return (dict “name” name “age” age “ss” ss)

as mike (makePerson “Mike Smith” 43 555555555)

what’s wrong with pigeon?

http://proglit.com/

top related