/* syntax */ decl ::= class_decl | val_decl | meth_decl suite ::= NEWLINE INDENT stmt+ DEDENT class_decl ::= CLASS class [(superclass)] ':' decls decls ::= NEWLINE INDENT decl+ DEDENT decl ::= var_decl | meth_decl var_decl ::= VAR init { ',' init } init ::= id [ = expr ] meth_decl ::= DEF id ( [ param { ',' param } ] ) ':' suite param ::= [ optional | keyword | rest | dictionary | required ] id [ '=' constant ] stmt ::= if_stmt | assign_stmt | stmt | expr | while_stmt | print_stmt | load_stmt if_stmt ::= IF expr ':' suite { ELIF expr ':' suite } [ ELSE ':' suite ] assign_stmt ::= lval_expr = expr while_stmt ::= WHILE expr ':' stmt print_stmt ::= PRINT [ expr { ',' expr } ] for_stmt ::= FOR id IN expr ':' suite | FOR id FROM expr [ TO expr ] [ BY expr ] : return stmt ::= RETURN expr load_stmt ::= LOAD expr expr ::= or_expr [ IF expr ELSE or_expr ]* or_expr ::= and_expr [ OR and_expr ]* and_expr ::= rel_expr AND and_expr | rel_expr rel_expr ::= sum_expr [ < | <= | = | != | > | >= | IS | IS NOT | IN | NOT IN ] sum_expr | NOT rel_expr sum_expr ::= prod_expr [ + | - | '|' | '^' | << | >> ] expr | prod_expr prod_expr ::= factor_expr [ * | / | % | & ] prod_expr | factor_expr factor_expr = unary_op factor_expr | inner_expr unary_op = + | - | ~ | not exp_expr ::= inner_expr | exp_expr [ '**' inner_expr ] inner_expr ::= ( expr ) | inner_expr . meth ( [expr { ',' expr}] ) | meth ( [expr { ',' expr}] ) | lval_expr | inner_expr '**' inner_expr long | double | '[' [ expr [ ':' expr ] ']' lval_expr ::= inner_expr . id | inner_expr '[' expr ']' | id Primitive functions are as follows: array(n) -- create array of length n dict(n) -- create dict of size n abs(x) -- absolute value int(x) -- conversion to 64-bit integer float(x) -- conversion to 64-bit double pow(x, y) -- x to the power y x ** y -- x to the power y x | y -- bitwise or of x and y x ^ y -- bitwise exclusive or of x and y x & y -- bitwise and of x and y x << n -- x shifted n bits left x >> n -- x shifted n bits right ~x -- the bits of x inverted len(s) -- length of s min(s) -- minimum value in s max(s) -- maximum value in s cos(x) -- cosine sin(x) -- sine tan(x) -- tangent exp(x) -- natural exponent sqrt(x) -- square root rem(n, m) -- remainder of n divided by m random(n) -- random float from 0 to 1 s.append(x) -- extend s by element or sequence x s.unappend() -- remove and return last element of s s.count(x) -- count x's in s s.index(x) -- index of first x in s s.insert(i, x) -- insert x as new ith element s.remove(x) -- find and remove first x s.reverse() -- reverse order of sequence s s.sort() -- sort elements of s a.clear() -- remove all items from a a.copy() -- shallow copy of a a.has_key(k) -- t if k is key in a a.keys() -- keys of a a.values() -- values of a a.get(k [, f]) -- gets item in a with key k (f is returned if no item found, None is returned if no f is given) f.close() -- close a file f.flush() -- flush a file f.fileno() -- file number f.read([size]) -- read bytes from f f.readline() -- read string from f f.readlines([sizehint]) -- read lines and return in list (if sizehint is given, then read about that many bytes rather than reading to eof) f.seek(offset, whence) -- position in file (whence = 0 means absolute, 1 means relative, 2 means relative to the end) f.tell() -- return current position in file f.write(str) -- write string to file f.writelines(list) -- write strings to file f.closed() -- boolean status f.mode() -- string that file was opened with f.name() -- string that file was opened with chr(i) -- convert int to ascii string hex(i) -- convert int to hex string oct(i) -- convert int to octal string hash(object) -- return a hash value id(object) -- address of the object intern(string) -- convert string to atom isinstance(object, class) -- direct or indirect issubclass(class1, class2) -- direct or indirect open(filename, mode) -- file open ord(c) -- inverse of chr() repr(object) -- readable string representation of object round(x, n) -- round x to n digits str(object) -- string representation of object (not necessarily machine readable) type(object) -- return type of object (an atom) flatten(array) -- convert array of strings to one string strcat(a, b) -- concatenate two strings subseq(s, start, end) -- subsequence of string or array isupper(s) -- is character in A:Z? islower(s) -- is character in a:z? isalpha(s) -- is character in A:Z,a:z? isdigit(s) -- is character in 0:9? isalnum(s) -- isdigit(s) || isalpha(s)? toupper(s) -- convert letters to upper case in s tolower(s) -- convert letters to lower case in s find(string, pattern [, start [, end]) -- search string for pattern contained within start:end, no match returns -1 exit() -- stop execution frame_previous() -- return previous stack frame frame_variables() -- return dictionary of variables frame_pc() -- program counter of frame frame_method() -- method name of frame frame_class() -- class of frame runtime_exception_nesting() -- nesting level of exceptions frame_get() -- get current frame When a built-in operation encounters the wrong types and the first argument is an object, then the call should be converted into a method lookup. It is illegal to define a global function with the name of a built-in function. The String_parse class: String_parse(string) -- create instance and initialize get_alpha() -- scan next field of alphabetic chars get_alnum() -- scan next field of alphanumerics get_csym() -- scan next field with C symbol syntax get_nonspace() -- scan next field of non-space chars skip_space() -- skip over spaces skip(s) -- skip over s, returning true, else false peek() -- return next character in string remainder() -- return all remaining characters Function arguments: Serpent does not have first-class functions. Instead, functions are represented by atoms (call the corresponding global function) or by objects (call the call method of the object).