KSM-Scheme accepts C-like syntax to represent Scheme expressions. When
an input begins with (, it is parsed as a Scheme expression as
usual. Otherwise, it is parsed as a C-like syntax. A line starting with
; is regarded as comment in both syntax.
Character constants are written as in the standard Scheme syntax See section Characters. This syntax is different from the standard C syntax.
#\a -- 'a'
#\A -- 'A'
#\( -- '('
#\Space -- ' '
#\newline -- '\n'
#\U{0} -- '\0'
#\U{D} -- '\r'
#\U{3042} -- Unicode 3042(hex)
Strings constants are written as in the Scheme syntax See section Strings.
"Hello, world"
Following escaping sequences are supported.
"\"" ==> " (a string composed of a doublequote character)
"\\" ==> \
"\n" ==> newline
"\r" ==> carriage return
"\t" ==> tab
"\U{XXXX} ==> character corresponding to Unicode
code value XXXX (hexadecimal)
When a backslash is followed by a character that is not listed above, backslash looses its special meaning and represents a backslash character by itself. This feature is in contrast to the standard C syntax.
"\b" ==> a string composed of two characters ('\' and 'b').
Numerical constants are represented as in the standard C syntax.
12345 -- decimal 01234 -- octal 0x1234aB -- hexadecimal 1.23 -- floating point 1.23E-3 -- floating point
In addition, rational numbers are represented as in the Scheme syntax.
1/2 -- one half 12/345
Imaginary numbers are represented by appending i to a real
number.
1i ( 1i * 1i = -1 ) 1.23i
All variables must be declared before use, as in C. The syntax of
variable declaration is the same with C except that type specification
part is replaced by the keyword var.
var lower, upper, step; var c; var vec[10];
The last declaration declares a variable which is a vector with 10 elements.
A variable may be initialized in its declaration, as in C.
var esc = #\\; var i = 0; var eps = 1.0e-5;
If the declaration is in the toplevel (not within {}), the
scope of the variable is the whole program as variables declared by
define in Scheme.
If the declaration is within {} block, the scope is within the
block, as automatic variables in C.
All operators except ?: are supported. Operator precedence is as
in the standard C.
The binary arithmetic operators are +, -, *, /, and the modulus operator %. Integer division yields a rational number.
1+2 ==> 3 1+2*3 ==> 7 (1+2)*3 ==> 9 2 / 3 ==> 2/3
The relational operators are >, >=, <, <=, ==, !=, &&, ||.
1<2 ==> #t 2>=2 ==> #t 2!=2 ==>#f
Prefix ++ and -- operators and postfix ++ and -- operators are provided.
var x = 12; x++ ==> 12, x becomes 13 x = 100; ++x ==> 101, x becomes 101
Bitwise operators are &, |, ^, <<, >>, and ~.
Syntax and semantics of assignment expression is the same as in C.
var x; x = 12 ==> 12
All the assignment operators (+=, -=, *=, /=, %=, <<=, >>=, &=, ^=, and |=) are supported.
The conditional expression, written with the ternary operator ?:
is not supported. ? and : are used for other
purposes in C-like syntax in KSM-Scheme.
Precedence and associativity of all operators are the same with C.
| OPERATORS | ASSOCIATIVITY |
| () [] -> . | left to right |
| ! ~ ++ -- + - * & sizeof | right to left |
| * / % | left to right |
| + - | left to right |
| << >> | left to right |
| < <= > >= | left to right |
| == != | left to right |
| & | left to right |
| ^ | left to right |
| | | left to right |
| && | left to right |
| || | left to right |
| = += -= *= /= %= &= ^= |= <<= >>= | right to left |
| , | left to right |
Unary +, -, and * have higher precedence than the binary forms.
Syntax of statements are the same as in the standard C except that there
are no switch, continue, and goto statements.
In KSM-Scheme, each statement has its value. How this value is determined is described below.
An expression followed by a semicolon is a statement.
x = 0;
i++;
printf("Hello, world!");
The value of this kind of statement is the value of the expression.
Braces {} are used to group declarations and statements
together into a compound statement, or block, so that they are
syntactically equivalent to a single statement.
{
var x = 1;
x+2;
}
The value of a block statement is the value of the last statement in the block. Therefore, the value of the above block statement is x+2 (that is, 3).
The syntax of if-else statement is the same as C.
Formally, the syntax is
if ( expression ) statement1 else statement2
where else part is optional.
The value of an if-else statement is the value of statement1 if expression evaluates to true, or the value of statement2 if expression evaluates to false. If else part is omitted, and expression evaluates to false, the value of if-else statement is unspecified.
In KSM-Scheme, a value represents false if it is #f, 0 (integer zero), 0.0 (floating point zero), or NULL (zero pointer). True is represented by any value that is not false.
The syntax of while statement is
while ( expression ) statement
The value of a while statement is unspecified.
Interpretation of true or false in the evaluation of expression is the same as in if-else statement.
continue statement is not supported in KSM-Scheme.
The syntax of for statement is
for ( expr1 ; expr2 ; expr3 ) statement
Interpretation of true or false in the evaluation of expr2 is the same as in if-else statement.
The value of a for statement is unspecified.
continue statement is not supported in KSM-Scheme.
the syntax of do-while statement is
do statement while ( expression );
Interpretation of true or false in the evaluation of expression is the same as in if-else statement.
The value of do-while statement is unspecified.
continue statement is not supported in KSM-Scheme.
The syntax of break statement is
break expression;
Break statement provides an early exit from for, while,
and do.
The value of a break statement is the value of the expression.
The syntax of return statement is
return expression;
The value of a break statement is the value of the expression.
The syntax of cond statement is
cond {
expr1 : stmt
expr2 : stmt
...
else : stmt
}
where else clause if optional.
expr1, expr2, ... are evaluated sequentially. When an expression evaluates true (in the sense described in if-else statement), the following statement is evaluated and the control exits the cond block. If else clause is encountered, the following statement is evaluated and the control exits the cond block.
var x = 1;
cond {
x%2==0 : 1;
x-1 : 2;
abs(x) : 3;
else : 4;
} ==> 3
The value of a cond statement is the value of stmt evaluated. If all expressions evaluates to false, the value of cond statement is unspecified.
The syntax of case statement is
case ( expression ) {
expr [, expr]... : stmt
...
else : stmt
}
in which else part is optional. First, expression is evaluated and its
value is compared (in the sense of eqv?) with the values of expr's
listed on the left side of :. If one of them is equivalent to the
value of expression, the value of stmt on the right of : becomes
the value of the case statement. If else is encountered instead
of expr, the value of the following stmt becomes the value of case
statement unconditionally.
var x = 12;
case(x){
1,3,5: #f;
2,4,6: #t;
} ==> #t
var y = #\a
case(y){
#\a,#\i,#\u,#\e,#\o: 'vowel;
#\w,#\y: 'semiconsonant;
else: ''consonant;
} ==> vowel
The syntax of function definition similar to the C syntax except that
type specifications are omitted and that the definition is preceded by
the keyword fun.
fun identifier ( para_list ) block_stmt
The value of function call becomes the value of block_stmt.
fun add2(x)
{
x+2;
}
add2(3) ==> 5
var x = 12;
add2(x) ==> 14
fun sum(x,y){ x+y; }
sum(1,2) ==> 3
return statement is supported.
fun my_sign(x)
{
if( x > 0 )
return 1;
if( x == 0 ) 0; else -1;
}
my_sign(0) ==> 0
Syntax of referencing C variables and functions becomes very natural using the C-like syntax.
By usual C declarations for extern variables and functions (preceded by
the keyword extern), C variables and functions become accessible
from KSM-Scheme.
extern int errno;
errno ==> 0
extern int strlen(char *str);
strlen("Hello"); ==> 5
Go to the first, previous, next, last section, table of contents.