books-programmingLanguages-programmingLanguagesChSyntax

Difference between revision 1 and current revision

No diff available.

Table of Contents for Programming Languages: a survey

Chapter : syntax

character sets

hard-to-type characters

basic arithmetic

on a traditional american keyboard, many unshifted punctuation characters have obvious arithmetic meanings (+,-*,/,^,=). should a language stick with these, or use those for something more common and use other things for arithmetic?

identifier lexical syntax

matching pairs of symbols

quite useful for nested grouping

see also http://en.wikipedia.org/wiki/Bracket#Computing

/\, <>, [], {}, () any others in ASCII?

symmetric vs asymmetric symbols

significant whitespace

EOL as statement separator

e.g. instead of ';' in C

one-liner support

need a way to replace any function using EOLs. usually semicolons for EOL and braces for grouping blocks.

some languages e.g. Python don't support one-liners. other languages e.g. haskell support both a significant whitespace mode (haskell calls this 'layout') and one-liners

function definition

..and lambdas

need to separate arguments from each other, and all arguments from function body

some languages (e.g. MATLAB) also define return values

multiple return values

tuples

keyword args

default args

fn application

f() or prefix or haskell

infix ops

fixed or custom

infixify a prefix op

section an infix operator to get a prefix function

associativity

(infix) precedence

traditional precedence table

todo; but *,/ > +,0 > comparisons > boolean ops

if too many levels, programmers can't make us of it anyway because they can't remember the precedence and it's easier to use parens than to look it up; but they will still pay a price when they see someone else's code without parens and they must look it up to parse it custom precedence is perhaps an extreme version of this

if too few levels, programmers must use lots of parens extreme version of this: no infix e.g. lisp

promotion

partial fn application

currying

variadic

and variadic keywords

glob expansion

homoiconicity

compile-time conditionals

like C's #ifdef, Nimrod's "when" http://nimrod-code.org/tut1.html#when-statement

destructuring bind

sugar for assignments

e.g. 'x = 3' instead of '(let x 3)

complex l-values or similar

an l-value is the thing on the left of the equals sign in an assignment statements using 'a = b' syntax. e.g. in 'x = 3', the l-value is 'x'.

A complex l-value is when a location must be resolved instead of being immediately given, e.g. "x[2] = 3"

a similar thing is how in Common Lisp you can do (assuming 'behavior' is a property list, that is, a dict/map/hash):

" (setf (get ’dog ’behavior)

  1. ’(lambda () (wag-tail) (bark))) "

here, instead of resolving (get ’dog ’behavior) to a value immediately, setf takes the LOCATION being read by (get ’dog ’behavior), and then assigns to that location.

expression evaluation order

in C, mostly undefined, except for short-circuit operators

e.g. in C code like "a = b() + c()" can call b() and c() in any order. If they have side effects then this might matter, yet no compiler error is given. However, the evaluation order of a() && b() IS specified.

implicit parameter special variable names

e.g. in bash, $0 is the name of the current script, $1 is the first positional parameter that was passed in, $2 is the second positional parameter, etc

in some languages a similar scheme for special variables for positional parameters is at the function level

blocks as first-class functions

e.g. ruby: ruby blocks

e.g. Apple Swift: blocks e.g. "let sortedCities = sort(cities) { $0 < $1 }"

e.g. Objective C: http://arstechnica.com/apple/2009/08/mac-os-x-10-6/10/#blocks

(note: goes well with implicit parameter special variable names)

conditionals and assignments

e.g. in C

http://stackoverflow.com/questions/151850/why-would-you-use-an-assignment-in-a-condition

however this can cause confusion between = and ==:

https://www.securecoding.cert.org/confluence/display/cplusplus/EXP19-CPP.+Do+not+perform+assignments+in+conditional+expressions

e.g. in Apple Swift:

" if let indexOfLondon = find(sortedCities, "London") { println("London is city number \(indexOfLondon + 1) in the list") } "

trailing conditionals

e.g. "print 'big' if x > 3"

explicit type conversion

some choices:

"constructor syntax": e.g. String(2) == "2"

"conversion function syntax": conv(2, Int, String)

"generic conversion-to function syntax": conv(2, String)

no syntax, and conversion functions: intToString(2)

no syntax, and generic conversion-to functions: toString(2)

no syntax, and generic conversion-from functions: fromInt(2) :: String (note: in languages with type inference the destination type may be inferred instead of annotated, making this skirt the line between implicit and explicit)

note: the following is actually implicit, not explicit: "annotation syntax": 2 :: String