misc.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. pour(built_in_functions, {
  2. // OO
  3. get_class: fun (
  4. 'function get_class (i: Instance) -> Class',
  5. i => i.class_
  6. ),
  7. // Input
  8. read: fun (
  9. 'function read (format: String) -> Object',
  10. format => {
  11. try {
  12. require.resolve('scanf')
  13. } catch (e) {
  14. if (e.code == 'MODULE_NOT_FOUND') {
  15. ensure(false, 'scanf_not_found')
  16. }
  17. }
  18. let result = require('scanf')(format)
  19. let normalize = x => {
  20. if (x === null || Number.isNaN(x)) {
  21. return Nil
  22. } else {
  23. return x
  24. }
  25. }
  26. if (is(result, Types.List)) {
  27. return result.map(normalize)
  28. } else {
  29. return normalize(result)
  30. }
  31. }
  32. ),
  33. // Ouput
  34. print: f (
  35. 'print',
  36. 'function print (p: Bool) -> Void',
  37. x => (console.log(x.toString()), Void),
  38. 'function print (x: Number) -> Void',
  39. x => (console.log(x.toString()), Void),
  40. 'function print (s: String) -> Void',
  41. s => (console.log(s), Void)
  42. ),
  43. // Error Handling
  44. custom_error: f (
  45. 'custom_error',
  46. 'function custom_error (msg: String) -> Error',
  47. msg => new CustomError(msg),
  48. 'function custom_error (name: String, msg: String) -> Error',
  49. (name, msg) => new CustomError(msg, name),
  50. 'function custom_error (name: String, msg: String, data: Hash) -> Error'
  51. ,(name, msg, data) => new CustomError(msg, name, data)
  52. ),
  53. })