Created
February 10, 2020 16:11
-
-
Save wooosh/5f381870d028894e3b6f65853efb7f34 to your computer and use it in GitHub Desktop.
blang example code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef ScarfDesign struct { | |
Pattern []byte | |
Length int | |
} | |
// If main throws an error, the return code will be set accordingly | |
fn main() { | |
// Try will use the default error message if catch is not used | |
{pattern, length} := try ReadDesign() | |
// Print scarf design | |
// Generator function | |
while range(length) { | |
try Print(pattern) | |
} | |
} | |
fn ReadDesign() ScarfDesign { | |
// standard library calls are not decided yet | |
try { | |
Print("Scarf Pattern: ") | |
// TODO: consider adding a way to automatically control copy buffer/new buffer as caller | |
// Read has multiple function signatures | |
pattern := Read() | |
Print("Scarf Length: ") | |
var buf []byte | |
read(&buf) | |
length := try int(buf) | |
return ScarfDesign{pattern, length} | |
} catch e { | |
case e.Type { | |
CastError { | |
error("Cannot parse intger:", err) | |
} | |
// IOError matches all errors related to IO | |
IOError { | |
error("Cannot access stdin/stdout", err) | |
} | |
default { | |
panic(e) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment