📄 vtc.doc
字号:
VTC Language DescriptionDescription-----------This file is intended mainly as a description of the VTC extensionlanguage for users not familiar with C. The file vt.doc contains afairly complete description of VTC for those who already know C.Learning VTC might help to learn some of the basics of C, but readersshould be aware that VTC is a very simplified and less restrictivelanguage.This file assumes that the user knows how to enter commands to the VTCinterpreter. Read vt.doc for instructions on basic operations in theclient. vt.doc also contains more in-depth descriptions of some VTCfeatures.VTC SYNTAXConstants---------One of the basic building blocks of VTC is constants. VTC has threetypes of constants: integer constants, character constants, stringconstants, and named constants.Integer constants can be specified in three ways: in decimal, inhexadecimal, or in octal. A decimal integer constant is just a seriesof decimal digits not beginning with 0, e.g. 12, 273, or 65. An octalinteger constant is a series of octal digits beginning with 0. Forinstance, 0100 is the same as the decimal integer constant 64. Ahexadecimal constant is a series of hexadecimal digits beginning with0x or 0X. For instance, 0xA00 is the same as the decimal constant2560. The alphabetical hexadecimal digits can be either uppercase orlowercase, so the previous example could have been written 0xa00. Thefollowing are all valid integer constants: 26 040 0x7b4 14Most integer constants are entered in decimal. Once the compiler hasread an integer constant, it does not remember what form it is in; theinteger constants 0100 and 64 act exactly the same.Character constants are integer constants written in ASCII form.Character constants are written as single quotes surrounding an ASCIIcharacter. For instance, 'a' and 'd' specify the ASCII codes for aand d. The backslash (\) is treated specially. The followingbackslash codes can be used as character constants: '\n' Newline (line feed) '\t' Tab '\b' Backspace '\v' Vertical tab '\r' Carriage return '\f' Form feed '\\' Literal backslash (\) '\'' ' '\"' "The ' and " characters do not need to be escaped with a backslash incharacter constants, although the " character needs to be escapedinside a string constant (q.v.). A sequence like '\(' which is notdefined above is not translated, and is thus illegal as a characterconstant because it is two separate characters.You can specify an ASCII value directly by using \ followed by up tothree octal digits. For instance, '\33' codes for the ASCII escapecharacter.String constants are a sequence of characters inside double quotes("). For instance, "foo", "\33[H", or "Done.\n". A sequence like"\(" in a string constant is legal, and is left as a backslashfollowed by a left parenthesis.Named constants are integer constants with names attached to them.They are built into the compiler, and are in all capital letters. Forinstance, K_BPSC is a named constant that translates to 8, a numberwhich has a special meaning to some primitives like bind(). The namedconstants are listed in vt.doc.Identifiers-----------Names of variables, functions, and some other objects are allexpressed by identifiers. An identifier is an alphabetical letter orunderline followed by a sequence of alphabetical letters, underlines,or digits. The following are valid identifiers: abcde _foo_bar The2ndThingOnTheListThe following are invalid identifiers: 2ndThingOnTheList Charles'sName My-FootIdentifiers are case-sensitive, so "Foobar" and "foobar" aredifferent.Expressions-----------A major part of the VTC syntax are constructs called expressions.Expressions are defined recursively, so expressions can contain otherexpressions. Every expression has a data type and a value. Constantsare the simplest type of expression. An integer constant has a datatype of "integer" and a value corresponding to the value of theinteger constant. A string constant has a data type of stringpointer; its value is a pointer to a copy of the string.The evaluation of an expression can involve performing an action. Forinstance, the expression "++a" increases the value of a by one, andreturns the new value. This is called a side-effect. Side-effectsare important considerations when considering whether certain controlstructures will evaluate certain expressions. One 'side-effect' weusually try to avoid is a run-time error, and so we try to avoidevaluating expressions which will result in them.VTC has the following data types: Integer Primitive pointer Remote pointer Window pointer Key binding pointer File pointer String pointer Array (or variable) pointer Function pointer Regexp pointer Association type Property list pointer Null (no value)The simplest type of directive to the VTC parser is anexpression-statement. This consists of an expression followed by a';'. The result of an expression-statement is to evaluate theexpression and discard the value. The following parser directiveshave no effect because they are expression-statements whoseexpressions have no side-effects: 4; 10; "foobar";Function calls and built-in variables-------------------------------------A function call is a type of expression with the form: function-name(argument, argument, ...)The function name can be the name of a primitive function (a functionpredefined by VT) or of a uesr-defined function. Each argument is anexpression. The following is a syntactically correct expression: foo(1, bar(3, 4, baz(), "a"), "b")A function may perform an action. This is a side-effect of theexpression which calls the function. A function also returns a value,which can be used as part of another expression. In the aboveexample, the return value of the function call to baz() is used as anargument to the function call to bar(), which has four arguments. Thereturn value of bar() is used in turn as an argument in the functioncall to foo(), which has three arguments.Closely related to function calls are built-in variables. These havereturn values which can vary from invocation to invocation, but nevertake any arguments. They have the simple form: builtin-nameAs an example, consider the statement: split(active, 10);"split" is the name of a primitive function that splits an outputwindow. "active" is the name of a built-in variable that returns apointer to the active window. The effect of the statement is to splitthe active window at line 10.Variables---------VTC has several types of variables: global variables, parametervariables, and local variables. Parameter variables and localvariables are similar to global variables, and will be discussed inthe section about functions.Variables are named constructs that contain a data value. They can beassigned a value using the assignment operator: variable-name = expressionThe above pattern is an expression involving the binary operator '='.The '=' operator assigns the value of the expression on the right handside to the variable on the left hand side, and returns that value.Global variables do not need to be declared. They are createdautomatically as their names are used and initialized to NULL, thedata value of null type.A variable's value can be used in other expressions by referring to itby its name. A variable name is an expression whose value is thecontents of the variable. For example, using "foo" as a variablename: foo = "Hello, world.\n"; echo(foo);This would write the line "Hello, world." to the active window.Binary operators----------------Function calls are written in prefix order, so that compiler reads thename of a function and then a list of arguments to operate on. Binaryoperators are called in infix order. They operate on two arguments,with the operator placed between them.A commonly-used group of binary operators are the arithmeticoperators: * / % + - (multiplication, division, modulus, addition andsubtraction). They can be used to write familiar-looking mathematicalformulae. For instance, (3 + 4) is an integer expression with thevalue 7, and (9 % 4) is an integer expression with the value 1.Infix notation is ambiguous; for instance, the expression (3 - 4 - 5)could be evaluated as (3 - (4 - 5)) or as ((3 - 4) - 5). Ambiguitiesin the binary operator notation are resolved by two properties ofoperators, precedence and association. Parentheses can also be usedto group expressions to resolve conflicts.Operators are grouped into precedence levels. The multiplicativearithmetic operators *, / and % are all on the same level ofprecedence. They have higher precedence than the additive operators +and -. So the expression (3 + 4 * 5) is evaluated as (3 + 20) and notas (7 * 5). One could write "((3 + 4) * 5)" to force the compiler toevaluate the addition first.Within precedence groups, operators can associate left-to-right orright-to-left. All binary operators except for the assignmentoperators associate left-to-right. (3 - 4 - 5) is the same as ((3 -4) - 5), and evaluates to -6.VTC has the following binary operators, in order of precedence, fromhighest to lowest: Multiplication: * / % Addition: + - Bitwise shift: << >> Relational: < <= > >= Equality: == != Bitwise and: & Bitwise xor: ^ Bitwise or: | Logical and: && Logical or: || Lookup: -> Assignment: = += -= *= /= %= &= != <<= >>= ?:=The relational and equality operators return a result of 1 if thecondition they test for is true, and 0 if not. So, the expression (-1== 3) has the value 0, while the expression (3 < 6) has the value 1.The logical and and logical or operators (&& and ||) behave specially.If the left-hand argument is sufficient to determine the value of theexpression--that is, if the expression to the left of a && is false,or the expression to the left of || is true--then the right hand sideis not evaluated. Errors and side-effects that would have ocurred inevaluating the right-hand expression will not occur in this case. Allthree operators return 1 if the logical relationship is true and 0 ifthe relationship is false.The assignment operators are different from other binary operators inthree ways. First, they associate right-to-left. "a = b = 3;"assigns 3 to both a and b and is the same as "a = (b = 3);". Second,the argument on the left hand side of an assignment operator is not anexpression but an lvalues. An lvalue is usually a variable name. "b= 3;" is a valid VTC statement, while "b + 1 = 3;" is not. Third, theassignment operators have side-effects as their primary purpose. Theother operators can produce run-time errors, and the -> operator canhave a side-effect relating to order of name association in propertylists, but these effects incidental to the functions of thenon-assignment operators.The assignment operators other than = are abbreviations. "a -= 3;" isequivalent to "a = a - (3);" and so forth. "a ?:= 3;" is equivalentto "a = a ? : (3);", described below.The comma (,) is a binary operator with lower precedence than theassignment operators. Inside an argument list, it separatesarguments. Outside of an argument list, a list of expressionsseparated by commas evaluates to the value of the last expression,after all the other expressions are evaluated and their valuesdiscarded. For example, (3, 4, x + 2, itoa(7), 6) evaluates to 6.Unary operators---------------VTC also has unary operators that apply to only one argument. Theyare appended to the left side of an expression. They have higherprecedence than binary operators, and are evaluated right-to-left.They are: ++ -- increment and decrement (lvalues only) ! Negation (true --> 0, false --> 1) ~ Bitwise one's complement + Nothing (no operation) - Additive inverse (3 --> -3) * Dereference (see pointers, below) & Address (lvalues only; see pointers)The ++ and -- operators can also be appended to the right side of anlvalue. They are called the postdecrement or postincrement operatorswhen appended to the right side of an lvalue, and the predecrement orpreincrement operators when appended to the left side. If a is avariable, then "++a" increments the contents of a by one (aside-effect) and has the value of a after the increment operation."a++" increments the contents of a by one and has the value of abefore the increment operation. The same applies to decrement. Sothe following code produces the output "3": a = 3; echo(itoa(a++));While this code produces the output "4": a = 3; echo(itoa(++a));In both cases, a has the value 4 after the code is executed.The conditional operator and expression truth---------------------------------------------An expression is false if it is NULL or the integer 0. Otherwise, itis true.The conditional operator takes three arguments in the form "a ? b :c", where <a>, <b>, and <c> are expressions. If <a> is true, then <b>is evaluated and its value is returned. Otherwise, <c> is evaluatedand its value is returned. The conditional operator associatesright-to-left and has lower priority than all of the binary operatorsexcept the assignment operators and the comma operator.The second argument to the conditional can be omitted. The expression a ? : chas the value of <a> if <a> is true, and the value of <c> otherwise.The interpreter only evaluates <c> if <a> is false.Order of evaluation-------------------Arguments to binary operators and the conditional operator areevaluated left-to-right. Any side-effects in the left argument occurbefore side-effects in the right argument. Arguments to functions arealso evaluated left-to-right.Statements----------Statements are directives to the VTC interpreter. Like expressions,they are defined recursively, so statements can contain otherstatements.The expression-statement has already been described. The followingare examples of useful expression-statements: a = 3; echo(itoa(a++));Another form of statement is the compound statement. It is a list ofany number of statements surrounded by { and }. { a = 3; echo(itoa(a++)); }is a valid compound statement.There are two kinds of conditional statements: if (expression) statementcauses statement to be executed if expression is true. if (expression) statement1 else statement2causes statement1 to be executed if expression is true, and statement2to be executed otherwise. Any of these statements could be compoundstatements. For example: if (new = split(win, row)) { set_obj(new, new_pager(new)); set_termread(win, .std_termread); }A common pitfall with if-else statements is: if (a) if (b) c; else d;The indentation would suggest that the "else" is supposed to associatewith the first if statement. It will instead associate with thesecond if statement. There are two ways to resolve this: if (a) { if (b) c; } else d;or: if (!(a)) d; else if (b) c;The second method uses a common construction, the "else if". This isused often enough that the indentation scheme is usually changed toaccount for it. According to common indentation system, according towhich a line is indented by a tab for each level of nestedflow-control depth, you might write the following code: if (a) b; else if (c) d; else if (e) f; else g;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -