Home / Highlights / FAQ / Examples / Quick Start / Roadmap / Download / About Us
main()
{
	SimpleLoops()
	Ifs(123)
	LoopsWithExits()
}

SimpleLoops()
{
	first_array = [9, 8, 7]
	
	test1 := [] //:= [] is same as := $int[]
	for v in first_array {
		test1.Push(v)
	}
	assert(test1 == first_array)

	for v in first_array index i {
		assert(v == 9-i)
	}

	test2 := []
	//no need to init to 0
	for i to 3 {
		test2.Push(i)
	}
	assert(test2 == [0, 1, 2])

	test3 := []
	for i to 10 inc 3 {
		test3.Push(i)
	}
	assert(test3 == [0, 3, 6, 9])

	//for to may be mutable, but not yet for in
	sum := 0
	for i mut to 10
	{
		i .= 9
		sum += i;
	}
	assert(sum == 9)
}
Ifs(int param_val)
{
	result := 0
	//if statement must be bool
	if param_val != 0 { result .= 1 }
	//However literal 1 and 0 is ok
	if 1 { result |= 2 }
	if 0 { result |= 4 }
	assert(result == 3)

	if 0 {
		//Not enforced but it's best to write NOOP in intentually empty blocks
	}
	else if 1
		result |= 8 //Because there's an else statement we don't need curly braces
	else
		return //Must have curly braces with the exception of return/break/continue

	//Considering different syntax
	assert(if(param_val == 123, 987, 456) == 987)

	//If we don't change syntax we'll add
	//result = if(cond1, result1, cond2, result2, cond3, result3, failResult)

	//|| and && will short circuit
	result2 := 0
	test1 := 3; if 1 || ChangeValue(0, test1 mut) { result2 |= 0x01 } else { result2 |= 0x02 }
	/* 1||0 is true with test1 unchanged */ assert( result2 == 0x01); assert(test1 == 3)
	test2 := 3; if 0 || ChangeValue(0, test2 mut) { result2 |= 0x04 } else { result2 |= 0x08 }
	/* 0||0 is false with test2 modified */ assert( result2 == 0x09); assert(test2 == 0)
	test3 := 3; if 1 && ChangeValue(1, test3 mut) { result2 |= 0x10 } else { result2 |= 0x20 }
	/* 1&&1 is true with test3 modified  */ assert( result2 == 0x19); assert(test3 == 1)
	test4 := 3; if 0 && ChangeValue(0, test4 mut) { result2 |= 0x40 } else { result2 |= 0x80 }
	/* 0&&1 is false with test4 unchanged*/ assert( result2 == 0x99); assert(test4 == 3)
}

ChangeValue(bool b, int v mut) bool { v .= 0 + b; return b } //bool can not convert to int, but can add/subtract

LoopsWithExits()
{
	result := 0

	//_On* must follow a loop like an else follows an if

	for i to 5 { }
	_OnBreak { result |= 1 } //not executed
	_OnComplete { result |= 2 }
	assert(result == 2)

	index := 0
	//doesn't matter if loop is on last element, break causes _OnBreak
	for v in [1, 2, 3] index i { if v == 3 break }
	_OnBreak { result |= 4; index .= i } //index is availiable
	_OnComplete { result |= 8 } 
	assert(index == 2)
	assert(result == 6)

	//while's also support _On*
	whileCounter := 2
	while whileCounter-- > 0 { }
	_OnBreak { result |= 0x10 }
	_OnComplete { result |= 0x20 }
	assert(result == 0x26)

	for v1 in [3, 4, 8] {
		for v2 in [5, 9, 10] {
			if v1 * v2 == 20 {
				break //no need for goto's, _OnBreak helps
			}
		} _OnBreak {
			break
		}
	}
	_OnBreak { result |= 0x1000 }
	assert(result == 0x1026)

	//There's also _OnEmpty
	for v in [] {
		break
	}
	_OnEmpty    { result |= 0x20000 } //NOTE: _OnComplete will run after this
	_OnBreak    { result |= 0x40000 }
	_OnComplete { result |= 0x80000 } //NOTE: Empty will run first regardless of order
	assert(result == 0xA1026)
}