38 lines
3.0 KiB
Plaintext
38 lines
3.0 KiB
Plaintext
@Balanced[]s // each source must be started by specifying its file name; std extension .Ya could be ommitted and auto added by compiler
|
|
|
|
// all types are prefixed by `
|
|
// definition of anything new is prefixed by \, like \MakeNew_[]s and \len
|
|
// MakeNew_[]s is Ok ident in Ya: _ starts sign part of ident, which must be ended by _ or alphanum
|
|
`Char[^] \MakeNew_[]s(`Int+ \len) // `Char[^] and `Char[=] are arrays that owns their items, like it's usally in other langs;
|
|
// yet in assignment of `Char[^] to `Char[=] the allocated memory is moved from old to new owner, and old string becomes empty
|
|
// there are tabs at starts of many lines; these tabs specify what in C++ is {} blocks, just like in Python
|
|
len & 1 ==0 ! // it's a call to postfix function '!' which is an assert: len must be even
|
|
`Char[=] \r(len) // allocate new string of length len
|
|
// most statements are analogous to C++ but written starting by capital letter: For If Switch Ret
|
|
For `Char[] \eye = r; eye // // `Char[] is a simplest array of chars, which does not hold a memory used by array items; inc part of For loop is missed: it's Ok, and condition and init could also be missed
|
|
*eye++ = '['; *eye++ = '[' // fill r by "[][][]...". The only place with ; as statement delemiter: required because the statement is not started at new line.
|
|
// below is a shuffle of "[][][]..." array
|
|
For `Char[] \eye = r; ++eye // var eye is already defined, but being the same `Char[] it's Ok by using already exisiting var. ++eye is used: it allows use of eye[-1] inside
|
|
`Int+ \at = Random(eye/Length) // `Int+ is C++'s unsigned int. eye/Length: / is used for access to field, like in file path
|
|
eye[-1],eye[at] = eye[at],eye[-1] // swap using tuples; eye[-1] accesses char that is out of current array, yet it's allowed
|
|
Ret r // Ret is C's return
|
|
`Bool \AreBalanced(`Char[] \brackets)
|
|
`Int+ \extra = 0
|
|
For ;brackets ;++brackets
|
|
Switch *brackets
|
|
'[' // it's a C++'s 'case': both 'case' and ':' are skipped being of no value; but the code for a case should be in block, which is here specifyed by tabs at next line start
|
|
++extra
|
|
']'
|
|
If !!extra // '!!' is `Bool not, like all other `Bool ops: && || ^^
|
|
Ret No // No and False are C's false; Yes and True are C's true
|
|
--extra
|
|
// There is no default case, which is written as ':' - so if no case is Ok then it will fail just like if being written as on the next line
|
|
// : { 0! } // C's default: assert(0);
|
|
Ret extra == 0
|
|
// function ala 'main' is not used: all global code from all modules are executed; so below is what typically is in ala 'main'
|
|
For `Int \n=10; n; --n
|
|
// below note that new var 'brackets' is created inside args of func call
|
|
//@Std/StdIO/ is used here to use Print function; else it maybe changed to Use @Std/StdIO at global level before this For loop
|
|
@Std/StdIO/Print(; "%s : %s\n" ;`Char[=] \brackets = MakeNew_[]s(10) /* all bracket strings are of length 10 */; AreBalanced(brackets) ? "Ok" : "bad")
|
|
// note that starting arg of Print is missed by using ';' - default arg value is allowed to use for any arg, even if next args are written
|