Skip to content

Commit 5a018d1

Browse files
added TeaScript code examples to readme.md
1 parent 7ff6251 commit 5a018d1

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,70 @@ std::cout << "square is " << res.GetAsInteger() << std::endl;
132132
133133
More examples are in the teascript_demo.cpp of this repo.
134134
135+
# Example TeaScript Code
136+
137+
(Better syntax highlighting on the TeaScript home page.)
138+
139+
```cpp
140+
def age := 42 // mutable variable of type i64
141+
const year := 2022 // const variable of type i64
142+
143+
// output: Thomas is 42 years old and born in 1980.
144+
println( "Thomas is %(age) years old and born in %(year - age)." )
145+
146+
147+
// result of a loop can be direct assigned to a variable. (can do this with any code blocks)
148+
// illustrating this by computing the gcd (greatest common divisor) with a loop and store the result directly in a variable:
149+
def x1 := 48
150+
def x2 := 18
151+
def gcd := repeat { // assign the result of the repeat loop
152+
if( x1 == x2 ) {
153+
stop with x1 // the result of the loop
154+
} else if( x1 > x2 ) {
155+
x1 := x1 - x2
156+
} else /* x2 > x1 */ {
157+
x2 := x2 - x1
158+
}
159+
}
160+
161+
// gcd will be 6
162+
163+
164+
// easy loop control - loops are addressable
165+
def c := 10
166+
repeat "this" { // loop is named "this"
167+
repeat "that" { // loop is named "that"
168+
c := c – 1
169+
if( c > 6 ) {
170+
loop "that" // loop to the head (start) of the inner loop (the "that"-loop)
171+
}
172+
stop "this" // stop the outer loop (the "this"-loop) directly from within the inner loop with one statement!
173+
}
174+
// This code will never be reached!
175+
c := 0
176+
}
177+
178+
// c will be 6 here.
179+
180+
181+
// === Lambdas and higher order functions:
182+
183+
// classical way to define a function.
184+
func call( f, arg )
185+
{
186+
f( arg ) // calling f. f must be sth. callable, e.g., a function
187+
// NOTE: return value of f will be implicit returned.
188+
}
189+
190+
def squared := func ( x ) { x * x } // This is the Uniform Definition Syntax for define functions.
191+
192+
call( squared, 3 ) // passing function as parameter. result: 9
193+
194+
call( func (z) { z + z }, 3 ) // passing lambda as parameter. result: 6
195+
```
196+
More impressive highlights on:<br>
197+
https://tea-age.solutions/teascript/overview-and-highlights/
198+
135199

136200
# Supported compiler (tested with)
137201

0 commit comments

Comments
 (0)