|
1 | | -# StaticScript |
| 1 | +<h1 align="center">StaticScript</h1> |
2 | 2 |
|
3 | | -> StaticScript is a statically typed and strongly typed programming language, syntactically like TypeScript. |
| 3 | +<div align="center"> |
4 | 4 |
|
5 | | -## Status |
| 5 | +StaticScript is a statically typed and strongly typed programming language, syntactically like TypeScript. |
6 | 6 |
|
7 | | - |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +English | [简体中文](./README-zh_CN.md) |
| 21 | + |
| 22 | +</div> |
| 23 | + |
| 24 | + |
| 25 | +## Language Features Summary |
| 26 | + |
| 27 | +### Variable and Constant Declaration |
| 28 | + |
| 29 | +Here are some variable declarations. |
| 30 | + |
| 31 | +```typescript |
| 32 | +let flag: boolean = true; |
| 33 | +let count: number = 20; |
| 34 | +let content: string = "Hello World"; |
| 35 | +``` |
| 36 | + |
| 37 | +Thanks to the type inference feature of StaticScript, we can write the above variable declaration as follows. They are exactly equivalent. |
| 38 | + |
| 39 | +```typescript |
| 40 | +let flag = true; |
| 41 | +let count = 20; |
| 42 | +let content = "Hello World"; |
| 43 | +``` |
| 44 | + |
| 45 | +The compiler of StaticScript cleverly deduced the type of the variable from the initial value. |
| 46 | + |
| 47 | +In addition to using `let` to declare variables, you can also use `const` to declare constants. |
| 48 | + |
| 49 | +```typescript |
| 50 | +const name = "StaticScript"; |
| 51 | +const age = 1; |
| 52 | +const developing = true; |
| 53 | +``` |
| 54 | + |
| 55 | +The difference between `let` and `const` is that constants declared with `const` cannot be modified. |
| 56 | + |
| 57 | +### Variable Evaluation |
| 58 | + |
| 59 | +You can use a wealth of operators to perform operations on variables, including arithmetic operations, bitwise operations, logical operations, relational operations, assignments, and string concatenation. |
| 60 | + |
| 61 | +```typescript |
| 62 | +let a = 1; |
| 63 | +let b = 2; |
| 64 | + |
| 65 | +// add, subtract, multiply and divide |
| 66 | +let sum = a + b; |
| 67 | +let diff = a - b; |
| 68 | +let product = a * b; |
| 69 | +let quotient = a / b; |
| 70 | + |
| 71 | +a = a << 1; // equivalent to `a <<= 1` |
| 72 | +b = b >> 1; // equivalent to `b >>= 1` |
| 73 | + |
| 74 | +let year = "2020", month = "08", day = "06"; |
| 75 | +let birthday = year + "/" + month + "/" + day; |
| 76 | +``` |
| 77 | + |
| 78 | +### Control Flow |
| 79 | + |
| 80 | +```typescript |
| 81 | +let a = 1; |
| 82 | +let b = 100; |
| 83 | +if (a < b) { |
| 84 | + ss_println_string("b is bigger"); |
| 85 | +} else { |
| 86 | + ss_println_string("b is not bigger"); |
| 87 | +} |
| 88 | + |
| 89 | + |
| 90 | +let max = a; |
| 91 | +if (a < b) { |
| 92 | + max = b; |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +### Loops |
| 97 | +StaticScript supports using `while` statement and `for` statement to do some repetitive things. |
| 98 | + |
| 99 | +```typescript |
| 100 | +// Calculate the sum of all even numbers between [1, 100] |
| 101 | +let sum1 = 0; |
| 102 | +let i = 1; |
| 103 | +while(i <= 100) { |
| 104 | + if (i % 2 == 0) { |
| 105 | + sum1 += i; |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | + |
| 110 | +// Calculate the sum of all integers between [1, 100] |
| 111 | +let sum2 = 0; |
| 112 | +for(let i = 1; i <= 100; i++) { |
| 113 | + sum2 += i; |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +### Function |
| 118 | + |
| 119 | +StaticScript supports defining functions in the top level scope and using function in any scope. |
| 120 | + |
| 121 | +```typescript |
| 122 | +function add(a: number, b: number): number { |
| 123 | + return a + b; |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +The above function can omit the return type because StaticScript can deduce the return type through the expression of the return statement. |
| 128 | + |
| 129 | +It is important to note that the parameter types of functions must be explicitly declared. |
0 commit comments