[1] module ; /* cpp on */ /* whitespace on */ /* comment on */ /* -- ANNOTATION - Prompt #1 merely annotates the current settings of the switches subsidiary to the 'module' statement. */ [2] module "hello.c" ; /* -- ANNOTATION - Prompt #2 reads the C source file "hello.c" and interprets it. No output is produced. */ [3] _2 ; /* -- ANNOTATION - Prompt #3 evaluates the symbolic variable '_2', which was produced by step #2. Below, the internal representation is displayed. */ [CPP (INK, [WHT (" "), PUN ("<"), VAL (IDT, "stdio"), PUN ("."), VAL (IDT, "h"), PUN (">")]), WHT ("\n\n"), VAL (IDT, "main"), BLK (BAL, nil), WHT ("\n"), BLK (BRC, [WHT ("\n"), WHT (" "), VAL (IDT, "printf"), BLK (BAL, [VAL (STX, "\"hello, world.\\n\"")]), PUN (";"), WHT ("\n")]), WHT ("\n")] : list (Clex) 60 ms. [4] Csrc ( _2 ) ; /* -- ANNOTATION - Prompt #4 invokes the 'Csrc' function (supplied as part of the 'capini.wrk' script. The argument to 'Csrc' is the '_2' variable. The result of this evaluation is the original source code. This is IMPORTANT! because it demonstrates that by evaluating an expression, one can produce source code. True, in this simple example, the code being produced already existed, but this is not always the case. */ #include main() { printf("hello, world.\n"); } 110 ms. [5] echo on ; /* -- ANNOTATION - Prompt #5 switches on a switch that displays files as loaded. */ [6] load range ; /* * Functions that return a range of numbers or characters */ /* * m..n = the list of integers between m and n inclusive. * from n = the infinite list of integers starting at n. * a --- b = the ordered sequence of characters, from a to b. */ rightassoc dyadic .. : 19; rightassoc dyadic --- : 20; dcl .. : num # num --> list num; say n .. m <== when n > m then [] else n :: (succ n .. m); dcl from : num --> list num; say from n <== n :: from(succ n); dcl --- : char # char --> str ; say a --- b <== when a == b then [a] else a :: ((chr(asc(a)+1)) --- b); [7] 'a' --- 'z' ; /* -- ANNOTATION - Prompt #7 makes use of the operator '---'. This is not a primitive operator, but an augmented operator. The '---' operator definition was defined in the file 'range.wrk'. */ "abcdefghijklmnopqrstuvwxyz" : list (char) [8] dyadic rightassoc ! : 5 ; /* -- ANNOTATION - This establishes a new operator, the '!'. It is described as dyadic and right associative with a parsing precedence of 5. */ [9] dcl ! : str # str --> str ; /* -- ANNOTATION - Prompt #9 describes the '!' operator as having two operands, both strings. The result of this operator is also a string. */ [10] say a ! b <== a <> " and " <> b ; [11] "cat" ! "dog" ; /* -- ANNOTATION - The '!' operator is applied. It works! */ >> "cat and dog" : list (char) 16 ms. [12] say "" ! b <== b ; /* -- ANNOTATION - Another semantic rule which augments the 'program' associated with this operator. */ [13] say a ! "" <== a ; /* -- ANNOTATION - And yet another semantic rule which augments the 'program' associated with this operator. */ [14] "" ! "" ; /* -- ANNOTATION - Test of the last two rules. */ >> "" : list (char) [15] exit ;