📄 slang.txt
字号:
A Guide to the S-Lang Language John E. Davis, davis@space.mit.edu Mar 23, 2003 ____________________________________________________________ Table of Contents Preface 1. A Brief History of S-Lang 2. Acknowledgements 2. Introduction 3. Language Features 4. Data Types and Operators 5. Statements and Functions 6. Error Handling 7. Run-Time Library 8. Input/Output 9. Obtaining S-Lang 9. Overview of the Language 10. Variables and Functions 11. Strings 12. Referencing and Dereferencing 13. Arrays 14. Structures and User-Defined Types 15. Namespaces 15. Data Types and Literal Constants 16. Predefined Data Types 16.1 Integers 16.2 Floating Point Numbers 16.3 Complex Numbers 16.4 Strings 16.5 Null_Type 16.6 Ref_Type 16.7 Array_Type and Struct_Type 16.8 DataType_Type Type 17. Typecasting: Converting from one Type to Another 17. Identifiers 17. Variables 17. Operators 18. Unary Operators 19. Binary Operators 19.1 Arithmetic Operators 19.2 Relational Operators 19.3 Boolean Operators 19.4 Bitwise Operators 19.5 Namespace operator 19.6 Operator Precedence 19.7 Binary Operators and Functions Returning Multiple Values 20. Mixing Integer and Floating Point Arithmetic 21. Short Circuit Boolean Evaluation 21. Statements 22. Variable Declaration Statements 23. Assignment Statements 24. Conditional and Looping Statements 24.1 Conditional Forms 24.1.1 if 24.1.2 if-else 24.1.3 !if 24.1.4 orelse, andelse 24.1.5 switch 24.2 Looping Forms 24.2.1 while 24.2.2 do...while 24.2.3 for 24.2.4 loop 24.2.5 for 24.2.6 forever 24.2.7 foreach 25. break, return, continue 25. Functions 26. Declaring Functions 27. Parameter Passing Mechanism 28. Referencing Variables 29. Functions with a Variable Number of Arguments 30. Returning Values 31. Multiple Assignment Statement 32. Exit-Blocks 32. Name Spaces 32. Arrays 33. Creating Arrays 33.1 Range Arrays 33.2 Creating arrays via the dereference operator 34. Reshaping Arrays 35. Indexing Arrays 36. Arrays and Variables 37. Using Arrays in Computations 37. Associative Arrays 37. Structures and User-Defined Types 38. Defining a Structure 39. Accessing the Fields of a Structure 40. Linked Lists 41. Defining New Types 41. Error Handling 42. Error-Blocks 43. Clearing Errors 43. Loading Files: evalfile and autoload 43. File Input/Output 44. Input/Output via stdio 44.1 Stdio Overview 44.2 Stdio Examples 45. POSIX I/O 46. Advanced I/O techniques 46.1 Example: Reading /var/log/wtmp 46.1 Debugging 46.1 Regular Expressions 47. S-Lang RE Syntax 48. Differences between S-Lang and egrep REs 48. Future Directions 48. Copyright A. The GNU Public License B. The Artistic License ______________________________________________________________________ 1. Preface S-Lang is an interpreted language that was designed from the start to be easily embedded into a program to provide it with a powerful extension language. Examples of programs that use S-Lang as an extension language include the jed text editor, the slrn newsreader, and sldxe (unreleased), a numerical computation program. For this reason, S-Lang does not exist as a separate application and many of the examples in this document are presented in the context of one of the above applications. S-Lang is also a programmer's library that permits a programmer to develop sophisticated platform-independent software. In addition to providing the S-Lang extension language, the library provides facilities for screen management, keymaps, low-level terminal I/O, etc. However, this document is concerned only with the extension language and does not address these other features of the S-Lang library. For information about the other components of the library, the reader is referred to the The S-Lang Library Reference. 1.1. A Brief History of S-Lang I first began working on S-Lang sometime during the fall of 1992. At that time I was writing a text editor (jed), which I wanted to endow with a macro language. It occured to me that an application- independent language that could be embedded into the editor would prove more useful because I could envision embedding it into other programs. As a result, S-Lang was born. S-Lang was originally a stack language that supported a postscript- like syntax. For that reason, I named it S-Lang, where the S was supposed to emphasize its stack-based nature. About a year later, I began to work on a preparser that would allow one to write using a more traditional infix syntax making it easier to use for those unfamiliar with stack based languages. Currently, the syntax of the language resembles C, nevertheless some postscript-like features still remain, e.g., the `%' character is still used as a comment delimiter. 1.2. Acknowledgements Since I first released S-Lang, I have received a lot feedback about the library and the language from many people. This has given me the opportunity and pleasure to interact with several people to make the library portable and easy to use. In particular, I would like to thank the following individuals: Luchesar Ionkov <lionkov@sf.cit.bg> for his comments and criticisms of the syntax of the language. He was the person who made me realize that the low-level byte-code engine should be totally type- independent. He also improved the tokenizer and preparser and impressed upon me that the language needed a grammar. Mark Olesen <olesen@weber.me.queensu.ca> for his many patches to various aspects of the library and his support on AIX. He also contributed a lot to the pre-processing (SLprep) routines. John Burnell <j.burnell@irl.cri.nz> for the OS/2 port of the video and keyboard routines. He also made value suggestions regarding the interpreter interface. Darrel Hankerson <hankedr@mail.auburn.edu> for cleaning up and unifying some of the code and the makefiles. Dominik Wujastyk <ucgadkw@ucl.ac.uk> who was always willing to test new releases of the library. Michael Elkins <me@muddcs.cs.hmc.edu> for his work on the curses emulation. Ulli Horlacher <framstag@belwue.de> and Oezguer Kesim <kesim@math.fu- berlin.de> for the S-Lang newsgroup and mailing list. Hunter Goatley, Andy Harper <Andy.Harper@kcl.ac.uk>, and Martin P.J. Zinser <zinser@decus.decus.de> for their VMS support. Dave Sims <sims@usa.acsys.com> and Chin Huang <cthuang@vex.net> for Windows 95 and Windows NT support. Lloyd Zusman <ljz@asfast.com> and Rich Roth <rich@on-the-net.com> for creating and maintaining www.s-lang.org. I am also grateful to many other people who send in bug-reports and bug-fixes, for without such community involvement, S-Lang would not be as well-tested and stable as it is. Finally, I would like to thank my wife for her support and understanding while I spent long weekend hours developing the library. 2. Introduction S-Lang is a powerful interpreted language that may be embedded into an application to make the application extensible. This enables the application to be used in ways not envisioned by the programmer, thus providing the application with much more flexibility and power. Examples of applications that take advantage of the interpreter in this way include the jed editor and the slrn newsreader. 2.1. Language Features The language features both global and local variables, branching and looping constructs, user-defined functions, structures, datatypes, and arrays. In addition, there is limited support for pointer types. The concise array syntax rivals that of commercial array-based numerical computing environments. 2.2. Data Types and Operators The language provides built-in support for string, integer (signed and unsigned long and short), double precision floating point, and double precision complex numbers. In addition, it supports user defined structure types, multi-dimensional array types, and associative arrays. To facilitate the construction of sophisticated data structures such as linked lists and trees, a `reference' type was added to the language. The reference type provides much of the same flexibility as pointers in other languages. Finally, applications embedding the interpreter may also provide special application specific types, such as the Mark_Type that the jed editor provides. The language provides standard arithmetic operations such as addition, subtraction, multiplication, and division. It also provides support for modulo arithmetic as well as operations at the bit level, e.g., exclusive-or. Any binary or unary operator may be extended to work with any data type. For example, the addition operator (+) has been extended to work between string types to permit string concatenation. The binary and unary operators work transparently with array types. For example, if a and b are arrays, then a + b produces an array whose elements are the result of element by element addition of a and b. This permits one to do vector operations without explicitly looping over the array indices. 2.3. Statements and Functions The S-Lang language supports several types of looping constructs and conditional statements. The looping constructs include while, do...while, for, forever, loop, foreach, and _for. The conditional statements include if, if-then-else, and !if. User defined functions may be defined to return zero, one, or more values. Functions that return zero values are similar to `procedures' in languages such as PASCAL. The local variables of a function are always created on a stack allowing one to create recursive functions. Parameters to a function are always passed by value and never by reference. However, the language supports a reference data type that allows one to simulate pass by reference. Unlike many interpreted languages, S-Lang allows functions to be dynamically loaded (function autoloading). It also provides constructs specifically designed for error handling and recovery as well as debugging aids (e.g., function tracebacks). Functions and variables may be declared as private belonging to a namespace associated with the compilation unit that defines the function or variable. The ideas behind the namespace implementation stems from the C language and should be quite familiar to any one familiar with C. 2.4. Error Handling The S-Lang language defines a construct called an error-block that may be used for error handling and recovery. When a non-fatal run-time error is encountered, any error blocks that have been defined are executed as the run-time stack unwinds. An error block can optionally clear the error and the program will continue running after the statement that triggered the error. This mechanism is somewhat similar to try-catch in C++. 2.5. Run-Time Library Functions that compose the S-Lang run-time library are called intrinsics. Examples of S-Lang intrinsic functions available to every S-Lang application include string manipulation functions such as strcat, strchop, and strcmp. The S-Lang library also provides mathematical functions such as sin, cos, and tan; however, not all applications enable the use of these intrinsics. For example, to conserve memory, the 16 bit version of the jed editor does not provide support for any mathematics other than simple integer arithmetic, whereas other versions of the editor do support these functions. Most applications embedding the languages will also provide a set of application specific intrinsic functions. For example, the jed editor adds over 100 application specific intrinsic functions to the language. Consult your application specific documentation to see what additional intrinsics are supported. 2.6. Input/Output The language supports C-like stdio input/output functions such as fopen, fgets, fputs, and fclose. In addition it provides two functions, message and error, for writing to the standard output device and standard error. Specific applications may provide other I/O mechanisms, e.g., the jed editor supports I/O to files via the editor's buffers. 2.7. Obtaining S-Lang Comprehensive information about the library may be obtained via the World Wide Web from http://www.s-lang.org. S-Lang as well as some programs that embed it are freely available via anonymous ftp in the United States from o ftp://space.mit.edu/pub/davis. It is also available outside the United States from the following mirror sites: o ftp://ftp.uni-stuttgart.de/pub/unix/misc/slang/ o ftp://ftp.fu-berlin.de/pub/unix/news/slrn/ o ftp://ftp.ntua.gr/pub/lang/slang/ The Usenet newsgroup alt.lang.s-lang was created for S-Lang programmers to exchange information and share macros for the various programs the embed the language. The newsgroup comp.editors can be a useful resource for S-Lang macros for the jed editor. Similarly, slrn users will find news.software.readers to be a valuable source of information. Finally, two mailing lists dealing with the S-Lang library have been created: o slang-announce@babayaga.math.fu-berlin.de o slang-workers@babayaga.math.fu-berlin.de The first list is for announcements of new releases of the library, while the second list is intended for those who use the library for their own code development. To subscribe to the announcement list, send an email to slang-announce-subscribe@babayaga.math.fu- berlin.de and include the word subscribe in the body of the message. To subscribe to the developers list, use the address slang-workers-subscribe@babayaga.math.fu-berlin.de. 3. Overview of the Language This purpose of this section is to give the reader a feel for the S- Lang language, its syntax, and its capabilities. The information and examples presented in this section should be sufficient to provide the reader with the necessary background to understand the rest of the document. 3.1. Variables and Functions S-Lang is different from many other interpreted languages in the sense
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -