📄 ctalk.txt
字号:
C-Talk is interpreted script language with C-like syntax and dynamic type checking.
Variables in C-Talk have no type. So there is no static type checking in C-Talk,
all checking is performed at runtime. C-Talk has no explicit memory deallocation and
provides garbage collection instead. C-Talk provides build-in string type
and multithreading support.
1. Syntax
The syntax of C-Talk language is similar with C/Java.
program: import-list {stmt}
import-list: "import" module {, module}
module: indentifier
func-def: "function" func-name "(" params-list ")" block-stmt
func-name: identifier
params-list: empty | IDENT { "," IDENT }
stmt: ";" | expr ";" | block-stmt
| while-stmt | do-while-stmt | for-stmt | if-stmt | break-stmt | continue-stmt
| return-stmt | throw-stmt | try-catch-stmt
// while, do-while, for, if, break and continue statements are similar with C
throw-stmt: "throw" string
return-stmt: "return" (expr)
try-catch-stmt: "try" stmt "catch" "(" exception-var ")" stmt
exception-var: identifier
block-stmt: "{" { stmt } "}"
expr: literal | func-call | start-thread | binary-expr | unary-expr | "(" expr ")" |
| variable | index-expr | env-var
literal: string | integer | array-construcor
variable: identifier
index-expr: expr "[" expr "]" | expr "." identifier
env-var: "$" identifier
array-constructor: "[" empty | array-elem { "," array-elem } "]"
array-elem: expr [ ":" expr ]
func-call: expr "(" expr-list ")"
start-thread: "par" func-call
expr-list: empty | expr { "," expr }
binary-expr: expr bin-op expr
unary-expr: unary-op expr
bin-op: "+" | "-" | "/" | "*" | ">>" | "<<" | ">>>" | "|" | "&" | "^" |
| "+=" | "-=" | "/=" | "*=" | ">>=" | "<<=" | ">>>=" | "|=" | "&=" | "^=" | "="
| "==" | ">" | ">=" | "<" | "<=" | "!="
unary-op: "~" | "!" | "-" | "+"
// Syntax of identifiers, comments, string and integer literals is the same as in C.
The table below summurize differences between C and C-Talk syntax
- C-Talk has no preprocessor
- C-Talk has "function" keyword
- C-Talk treats " and ' equally, so "ok" and 'ok' means the same - string character constant
- C-Talk program consist of modules loaded by "import" construction
- C-Talk expression a.x is syntax sugar for a["x"] and is treated as asspciative
array access operation
- C-Talk variable has no types - so no function return type, formal paramter type or variable type should not (and can not) be specified.
- C-Talk has no goto construction
- C-Talk has "par" construction for starting thread
2. Types
C-Talk provides the following types
- Integer (32 bit integer type with operations similar to C)
- String (+ operator)
- Associative array (index of the array can be object of any type, for example a["foo"])
- Raw pointer (used only by primitives, C-Talk can only store such value to variables
and pass them to the functions, example is C FILE* type)
- threadt type (with associated FIFO queue)
- function type (function is first class value and can assigned to variable and passed
as parameter to other function)
String operations:
relation operations
s + "xxx" - concatenation of strings
indexOf(s, "xxx") - index of first occurance of substring
lastIndexOf(s, "xxx") - index of first occurance of substring
substring(s, start-pos, end-pos) - extract substring
toLower(s) - convert to lower case
toUpper(s) - convert to upper case
trim(s) - remove trailing spaces
startsWith(s, prefix)
endsWith(s, suffix)
string(x) - convert argument to string
length(x) - length of string
Arrays operations:
a[x] = y - store element
x = a[y] - fetch element
x = ["red", "green", "blue"] - array constructor with implicitly defined keys (0,1,2)
x += ['one':1, 'two':2] - concatenate arrays
a -= x - remove element(s) from the array, x can be array or key of element
length(a) - length of array
cloneArray(a) - create a copy of the array
deleteArray(a) - deallocate array
clearArray(a) - truncate array to size 0
Mutex operations:
m = createMutex() - create mutex
lockMutex(m) - lock mutex
unlockMutex(m) - unlock mutex
deleteMutex(m) - delete mutex
Thread operations:
m = getMessage(t) - get message from thread queue
putMessage(t, m) - put message in thread queue
t = currentThread() - get reference to current thread
IO operations:
f = createFile("test.txt", "r") - create file with specified name and acess mode
deleteFile(f) - close file
flushFile(f) - flush buffers
printFile(f, "x=", x) - print varying number of arguments to the file
printlnFile(f, "x=", x) - the same as above but append '\n'
print("x=", x) - print varying number of arguments to the console
println("x=", x) - the same as above but append '\n'
rc = execute("cp f.dbs f.sav") - execute shell command and wait for result
Miscellaneous:
t = time() - get current system time (seconds)
assert(i >= 0) - check the assert condition, throw exception if predicate is false
loadDLL("test.dll") - load dynamic linking library
loadModule("test.ctk") - load C-Talk module
chdir(path) - change current directory
dir = pwd() - get name of working directory
sleep(seconds) - sleep specified ammount of seconds
printExceptionStackTrace() - print stack trace for the frame where exception was thrown,
till the frame where it is catched
3. Program execution
C-Talk is command line tool which accepts list of files with C-Talk programs as parameters:
$ ctalk file-1 file-2 ...
The loaded module is first compiled to byte code and the all global-level statments
are executed. For example, the result of following program execution:
---------------------------
hello = "Hello World";
foo(hello);
foo(s) {
println(s);
}
---------------------------
will be prining of "Hello World" line at the screen
4. C-Talk primitives.
Most of C-Talk functions will be implementaed in C/C++. C-Talk provides special library
for mapping C-Talk type to C++ types. C-Talk rpmitives can accept varying number of arguments.
To declare C-Talk promitive, C programmer should define the function:
CtkObject foo_func(int nArgs, CtkObject args[]) { ... }
REGISTER(foo_func, "foo");
The code above defines the function "foo_func" and registers it as C-Talk priitive "foo".
Now C-Talk program can call "foo()" function in the same way as it was declared in C-Talk.
The C-Talk C-interface library provides methods for creating objects of C-Talk types,
converting CtkObject with type checking to primitive C types, operations with
C-Talk types (such as strings, arrays and mutexes),
throwing C-Talk exceptions.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -